# Methods Help

• 04-11-2011, 01:57 PM
uppercool
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! :)

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 ();
n1 = m6P2.getInput ();
n2 = m6P2.getInput ();
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 {
}
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 + ".");
}

}

• 04-11-2011, 02:03 PM
KevinWorkman
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.
• 04-11-2011, 03:33 PM
zzebie
You are calculating average as "average = n1 + n2 + n3 / 3;"
It should be "average = "(n1 + n2 + n3) / 3;"
• 04-11-2011, 04:24 PM
zzebie
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;
}
}
• 04-11-2011, 04:34 PM
Tolls
Nice attempt at a spoon feed...
Don't you think the OP would learn better figuring this out themselves, with some guidance?
• 04-11-2011, 06:27 PM
zzebie
Sorry about that. Will take note of it going foward
• 04-12-2011, 01:33 PM
uppercool
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
• 04-12-2011, 01:34 PM
uppercool
Um, how do I state this as solved?
• 04-12-2011, 01:46 PM
Dark