Forums Register Login

Question about methods

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
Hello,

Of course it can be done. Please have a look at this: arguments .
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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!
1
+Pie Number of slices to send: Send
You're welcome

And sorry for not noticing earlier: welcome to the Ranch.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
I was born with webbed fish toes. This tiny ad is my only friend:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 643 times.
Similar Threads
GUI and Strings
exception in thread "main"
variables not found with OOP
Need help fixing error
JOptionPane - showInputDialog
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:06:58.