Results 1 to 9 of 9

Thread: Methods Help

  1. #1
    uppercool is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default 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. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    zzebie is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    You are calculating average as "average = n1 + n2 + n3 / 3;"
    It should be "average = "(n1 + n2 + n3) / 3;"

  4. #4
    zzebie is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    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. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Nice attempt at a spoon feed...
    Don't you think the OP would learn better figuring this out themselves, with some guidance?

  6. #6
    zzebie is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Sorry about that. Will take note of it going foward

  7. #7
    uppercool is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    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. #8
    uppercool is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Um, how do I state this as solved?

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Help with get and set methods
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 09:04 PM
  3. Help with Methods Please :-)
    By jkhamler in forum New To Java
    Replies: 22
    Last Post: 11-10-2009, 09:54 PM
  4. methods
    By lilac87 in forum New To Java
    Replies: 7
    Last Post: 07-22-2009, 07:37 PM
  5. Replies: 4
    Last Post: 04-03-2009, 07:36 AM

Posting Permissions

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