Reputation: 63
I am stumped trying to send a value through some methods as part of a question which reads
Write the following method: computeDiameter: This method accepts the radius (r) of a circle, and returns it's diameter (2*r)
It's under the "Passing values through different methods", so instead of asking me for a simple variable/formula like
double r;
double diameter = r*2; //along with Scanner and some System out prints
It's asking me to send the value of r through another method, then return the method and print out the new value for r. This is what I have so far
import java.util.*;
public class UserSquares
{
public static void main(String[] args)
{
int r = 2;
int result=0; //I set result=0 hoping to initialize it properly.
computeDiameter(r);
System.out.println(result);
}
public static int computeDiameter(int r)
{
int result = r * 2;
return result;
}
}
This question seemed very simple which is why i tried to tackle it. I know the basics of sending values through a method, but i don't know how to return them. I don't really have money for actual text books (so i settle for quizlet questions and stuff like that), and youtube videos don't help at all so I like coming to the forums to get help when I am really stumped but want to move on with other topics.
(this question is part of a java self test, just thought I should share that as I've asked a number of questions on this site and each time someone responds with something negatively as if I am trying to get you guys to complete my homework for me. I am not taking any Java classes, these questions are just self testing my skills so that when I do major in java programming after i finish highschool, I can have a pretty nice understanding of it. Again, this is not for a test, I am welcome to all types of explanations, as long as it is in the realm of comprehension for a beginner Java student. Please and thank you in advance)
EDIT: Thanks Guys. Immediately after I submitted this I worked on it some more and found a solution so i'll share it
new code
import java.util.*;
public class UserSquares
{
public static void main(String[] args)
{
int total, r = 2;
total = computeDiameter(r);
System.out.println(total);
}
public static int computeDiameter(int value1)
{
int result;
result = value1 * 2;
return result;
}
}
EDIT #2: Holy crap I didn't expect to receive so many solutions so quick. Was about to answer it myself so that it would appear to be done. Sorry for the waste of your time guys, I will read every one of them to see the different solutions you guys offered and learn them so that your time isn't totally wasted. Thanks so much.
EDIT #3: Had a redundant line remaining from my first code that I forgot to take out that went through the compilation and didn't affect the outcome in anyway, but still decided to take it out. Thanks CodeMatrix!
Upvotes: 1
Views: 193
Reputation: 2154
As I said in the comment section.
Go ahead and change this
int result = 0;
computeDiameter(r);
System.out.println(result);
to this
int result = 0; //or you use int result = computeDiameter(r);
result = computeDiameter(r);
System.out.println(result);
Upvotes: 2
Reputation: 7720
You have to assign the value which is being returned from computeDiameter(r) method to the result variable in order to print that.
result = computeDiameter(r);
System.out.println(result);
Upvotes: 1
Reputation: 329
its very simple:
int result = computeDiameter(r);
or just
System.out.println(coputeDiameter(r));
Upvotes: 1