# calculating the hypotenuse

• 10-11-2010, 07:34 PM
Latanyar
calculating the hypotenuse
I made a 70 on the follwing assignment:

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);     } }```

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.
• 10-11-2010, 07:56 PM
JosAH
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
• 10-11-2010, 07:57 PM
Zack
Not sure why he took off 30% for this, but:
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:
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:
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. :(
• 10-11-2010, 08:04 PM
JosAH
Quote:

Originally Posted by Zack
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
• 10-11-2010, 08:20 PM
Latanyar
Quote:

Originally Posted by Zack
Not sure why he took off 30% for this, but:
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:
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:
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.
• 10-12-2010, 08:51 AM
Rectal Exambot
Sounds like a jerk teacher.

But just going Math.sqrt(x*x + y*y) is alot easier though
• 10-12-2010, 09:20 AM
JosAH
Quote:

Originally Posted by Rectal Exambot
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