• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about methods  RSS feed

 
P Kay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've been stuck on this problem for awhile now, is there a way for me to use a value that I found in one method, into another? Specifically, I'm trying to use the data that I got in getDogsWeight, and use that data in my getDogsHumanAge method.



I wanna do it so that I don't have to ask the user again to input the code, but rather use it from the main that is running it.

 
Popa Mihai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Of course it can be done. Please have a look at this: arguments .
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One reason you are having problems is that you don't have a Dog class.
The weight of a dog should be a property of the dog and should be recorded as a field in a dog object.
 
P Kay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, I'm still having trouble trying to understand how to reference the 'answer' from one method, and use that in another method. I tried looking at the arguments link, and got even more lost. I'm trying to do this:
MAIN CLASS
method1( )
method2( )

method 1
int X
whatever code
return X

method 2
int Z
int Y = ***method1 data??***

whatever code

return Z

etc.

When I try to recall it, I get an error asking to put in a class




my full program code is this: (the assignment was to split everything into different methods, and then have the user input the data we need and spit it back out to them.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's difficult to do what you want to do because your methods do too much. They have multiple responsibilities. The "smell" to recognize is "I just want to do a little bit of what that method does." The solution is to break up the method that has the little bit of functionality that you want so that the "little bit" is in a method by itself. Then it's easy to call it from wherever you want to use it without worry about other "side-effects".

Standard convention in Java is to have a method named "getSomething()" just return the value of that "something". It could be a value stored someplace, like an instance variable, or it could be a calculated value. You would not, however, put logic that prompts the user and accepts user input for a value of something in the getSomething() method. That job would be given to another method, say "inputSomething()" for example.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, those switch statements to get the dogs age are horrific. you should find a way to simplify the calculation and make the code shorter. Brute forcing it like that is just not a good way to go.
 
P Kay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:BTW, those switch statements to get the dogs age are horrific. You should find a way to simplify the calculation and make the code shorter. Brute forcing it like that is just not a good way to go.


Thank you very much for your help! (I agree, but the assignment was to get the different values from a website and do it accordingly) I ended up using parameters to store the values I got from one method to use into another.

I appreciate everyone's help/feedback!
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

And sorry for not noticing earlier: welcome to the Ranch.
 
P Kay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You're welcome

And sorry for not noticing earlier: welcome to the Ranch.


no worries! thank you very much! I'm glad I joined, such friendly and helpful people
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read this out loud, this code doesn't make a lot of sense.

How can a dog's human age be its weight? If I look at the xxxDogWeight() methods, they all return the value of dogsHumanAge. There seems to be some conceptual disconnect there.

Also, what's the point of the do-while loop? The loop condition is (dogsWeightIsInvalid) but this will always be false, so you only ever execute the loop body, and consequently the if statement, exactly one time. In fact, your indentation in the last else actually misleads the reader into thinking that the last dogsWeightIsInvalid = false is only executed as part of the last else clause. This is not the case. It is a separate statement and will be executed independently of the if-else statement. If you want the execution to be as the indentation suggests, you need to put {} around the two statements of the last else clause.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!