Results 1 to 7 of 7
  1. #1
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default calculating the hypotenuse

    I made a 70 on the follwing assignment:


    Java Code:
    public class CalculatingTheHypotenuse
    {
        public static void main(String[] args)
        {
            //Declaring the variables
           Scanner input = new Scanner(System.in);
           
           //requesting user input
           System.out.println("Please enter both sides of the right triangle and I will calculate the hypotenuse.");
           double x = input.nextDouble();
           double y = input.nextDouble();
           
           //Call for the hypot method
           double z = hypot(x,y);
           
           //Calculate the hypot and display it to screen
           double w = Math.sqrt(z);
           System.out.println("The hypotenuse is " + w);
           
        }
        //Method to multiple the sqaure of two number
        //Parameters are the 2 values of the sides of the triangle to be squared then multiplied
        public static double hypot(double side1, double side2)
        {
            return (side1 * side1) + (side2 * side2);
        }
    }

    Here were my teacher's comments:

    Your hypot method does NOT calculate the length of the hypotnuse. It needs to get the square root of the value it does compute (something you erroneously put in the main program). -30 points

    Could someone explain this in detail for me with examples? I am not quite sure on what my instructor is saying.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Your hypot method calculates the square of the hypothenuse; as you see from your own code your main method needs to take the square root of the number returned from that method. Put the Math.sqrt( ... ) method in your hypot method as well.

    kind regards,

    Jos

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Not sure why he took off 30% for this, but:
    Java Code:
           //Call for the hypot method
           double z = hypot(x,y);
           
           //Calculate the hypot and display it to screen
           double w = Math.sqrt(z);
    That code should JUST be:
    Java Code:
    double z = hypot(x,y);
    ...and the hypot() function should square root the number in that method instead of in the main method.

    So the pseudocode would be like this:
    Java Code:
    Take double x as input
    Take double y as input
    Put x,y into hypot function:
        return square root of (x*x+y*y)
    Print out value of result


    Edit: Dang it, Jos... that'll teach me to type too much. :(

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Zack View Post
    Edit: Dang it, Jos... that'll teach me to type too much. :(
    I had more espressos than usual today so I feel extremely speedy ;-)

    kind regards,

    Jos

  5. #5
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    Not sure why he took off 30% for this, but:
    Java Code:
           //Call for the hypot method
           double z = hypot(x,y);
           
           //Calculate the hypot and display it to screen
           double w = Math.sqrt(z);
    That code should JUST be:
    Java Code:
    double z = hypot(x,y);
    ...and the hypot() function should square root the number in that method instead of in the main method.

    So the pseudocode would be like this:
    Java Code:
    Take double x as input
    Take double y as input
    Put x,y into hypot function:
        return square root of (x*x+y*y)
    Print out value of result


    Edit: Dang it, Jos... that'll teach me to type too much. :(
    Thank you for that explicit break down. Now I understand what I did wrong.

  6. #6
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Sounds like a jerk teacher.

    But just going Math.sqrt(x*x + y*y) is alot easier though

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Rectal Exambot View Post
    Sounds like a jerk teacher.
    Nope, if the OP is supposed to write a method that calculates the hypothenuse the OP should'nt write one that calculates the square of the hypothenuse. That teacher is a fine person.

    kind regards,

    Jos

Similar Threads

  1. need help with calculating something
    By mikec420 in forum New To Java
    Replies: 13
    Last Post: 09-29-2011, 10:14 PM
  2. Help in calculating time different!
    By rocky86 in forum New To Java
    Replies: 6
    Last Post: 10-11-2010, 05:18 PM
  3. Calculating average
    By clocksaysits9 in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 06:03 AM
  4. calculating exponents
    By GPB in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 12:44 PM
  5. calculating with dates
    By hannes in forum New To Java
    Replies: 1
    Last Post: 01-14-2010, 09:22 PM

Posting Permissions

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