1. Member
Join Date
Oct 2010
Posts
25
Rep Power
0

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

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. 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. 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. 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

5. Member
Join Date
Oct 2010
Posts
25
Rep Power
0
Originally Posted by Zack
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. Member
Join Date
Aug 2010
Posts
16
Rep Power
0
Sounds like a jerk teacher.

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

7. 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

#### Posting Permissions

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