# Thread: Methods Help

1. Member
Join Date
Apr 2011
Posts
3
Rep Power
0

## Methods Help

Hi everyone. A couple days ago, I was told by my teacher to write a method that was to output the sum, average, and largest number of three numbers to-be input by the user. I'm having trouble with the output, though. I can't seem to get the right average and largest, and I was wondering if you guys could help me. Your help is very much appreciated, thanks! :)

Java Code:
```import java.io.*;
public class Method6Part2 {
public static void main (String args []) throws IOException {
double sum, n1, n2, n3, largest=0, average;
Method6Part2 m6P2 = new Method6Part2 ();
System.out.println ("Please enter number 1.");
n1 = m6P2.getInput ();
System.out.println ("Please enter number 2.");
n2 = m6P2.getInput ();
System.out.println ("Please enter number 3.");
n3 = m6P2.getInput ();
sum = m6P2.getSum (n1, n2, n3);
average = m6P2.getAverage(n1, n2, n3);
largest = m6P2.getLargest(n1, n2, n3, largest);
m6P2.output (sum, average, largest);
}
public double getInput () throws IOException {
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
return Double.parseDouble(br.readLine());
}
public double getSum (double n1, double n2, double n3) {
double sum;
sum = n1 + n2 + n3;
return sum;
}
public double getAverage (double n1, double n2, double n3) {
double average;
average = n1 + n2 + n3 / 3;
return average;
}
public double getLargest (double n1, double n2, double n3, double largest) {
double newlargest;
newlargest = largest;
if (n1>n2){//use the if statement to sort from smallest to largest
newlargest=n1;//Assign largest to number 1
}
else if (n1>n3){//use the if statement to sort from smallest to largest
newlargest=n1;//Assign largest to number 1
}
else if (n2>n1){//use the if statement to sort from smallest to largest
newlargest=n2;//Assign largest to number 2}
}
else if (n2>n3){//use the if statement to sort from smallest to largest
newlargest=n2;//Assign largest to number 2
}
else if (n3>n1){//use the if statement to sort from smallest to largest
newlargest=n3;//Assign largest to number 3
}
else if (n3>n2){//use the if statement to sort from smallest to largest
newlargest=n3; //Assign largest to number 3
}
return largest;
}
public void output (double sum, double average, double largest) {
System.out.println ("The sum of these three numbers is " + sum +".");
System.out.println ("The average of these three numbers is " + average + ".");
System.out.println ("The largest value of these three numbers is " + largest + ".");
}

}```

2. For the average, check the order of operation. Division happens before addition.

I'm not really sure what your largest function is doing. Can't you just use Math.max()? Hint: Only one if statement will be entered, the way you have them.

3. Member
Join Date
Feb 2011
Posts
3
Rep Power
0
You are calculating average as "average = n1 + n2 + n3 / 3;"
It should be "average = "(n1 + n2 + n3) / 3;"

4. Member
Join Date
Feb 2011
Posts
3
Rep Power
0
Uppercool, I have rewritten getlargest as below.Seems, it works, but do your own testing.(Please note, i am Java newbie myself.there might be a more correct or efficient way to do this)

public double getLargest (double n1, double n2, double n3, double largest) {
double newlargest;
newlargest = largest;
if (n1>n2)
{
newlargest=n1;
}
else if (n3>n2)
{
newlargest=n3;
return newlargest;
}
else if (n2>n3)
{
newlargest=n2;
return newlargest;
}
if (n1>n3)
{
newlargest=n1;
return newlargest;
}
else
{
newlargest=n3;
return newlargest;
}
}

5. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
25
Nice attempt at a spoon feed...
Don't you think the OP would learn better figuring this out themselves, with some guidance?

6. Member
Join Date
Feb 2011
Posts
3
Rep Power
0
Sorry about that. Will take note of it going foward

7. Member
Join Date
Apr 2011
Posts
3
Rep Power
0
Thanks zzebie for the spoon-feed. :3

It'd be nice if I got it myself, though. XD

Thanks for the tips guys, and it works, too. :3

8. Member
Join Date
Apr 2011
Posts
3
Rep Power
0
Um, how do I state this as solved?

9. Up at the top, thread tools> Mark this thread as solved.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•