Results 1 to 7 of 7
Thread: calculating the hypotenuse
 10112010, 07:34 PM #1Member
 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); } }
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.
 10112010, 07:56 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,526
 Blog Entries
 7
 Rep Power
 20
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
 10112010, 07:57 PM #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);
Java Code:double z = hypot(x,y);
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. :(
 10112010, 08:04 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,526
 Blog Entries
 7
 Rep Power
 20
 10112010, 08:20 PM #5Member
 Join Date
 Oct 2010
 Posts
 25
 Rep Power
 0
 10122010, 08:51 AM #6Member
 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
 10122010, 09:20 AM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,526
 Blog Entries
 7
 Rep Power
 20
Similar Threads

need help with calculating something
By mikec420 in forum New To JavaReplies: 13Last Post: 09292011, 09:14 PM 
Help in calculating time different!
By rocky86 in forum New To JavaReplies: 6Last Post: 10112010, 04:18 PM 
Calculating average
By clocksaysits9 in forum New To JavaReplies: 4Last Post: 04062010, 05:03 AM 
calculating exponents
By GPB in forum New To JavaReplies: 2Last Post: 03212010, 11:44 AM 
calculating with dates
By hannes in forum New To JavaReplies: 1Last Post: 01142010, 08:22 PM
Bookmarks