Define a method hypotenuse that calculates the length of the hypotenuse of a right triangle when the lengths of the other two sides are given. (Use the sample data in the table below) The method should take two arguments of type double and return the hypotenuse as a double. Incorporate this method into an application that reads values for side1 and side2 and performs the calculation with the hypotenuse method.

Triangle Side 1 Side 2
1 3.0 4.0
2 5.0 12.0
3 8.0 15.0

3
Contributors
6
Replies
8
Views
6 Years
Discussion Span
Last Post by bibiki

Ok, so you need a method that takes 2 double as paramaters, and returns a double.
Then just calculate the hypotenuse using the formula you should have learned in primary school if you weren't one of those students who got other people to do their homework...oh wait.

``````public class Hypotenuse
{

public double Hypo(double s1,double s2)
{

double hypotenuse = Math.sqrt(s1 * s2);
return hypotenuse;
}

}``````
``````import java.util.Scanner;

public class HypotenuseTest
{
public static void main(String[]args)
{
Hypotenuse h = new Hypotenuse();

System.out.println("The hypotenuse of Triangle 1 is :"+h.Hypo(3.0,4.0));

Hypotenuse h2 = new Hypotenuse();

System.out.println("The hypotenuse of Triangle 2 is :"+h.Hypo(5.0,12.0));

Hypotenuse h3 = new Hypotenuse();

System.out.println("The hypotenuse of Triangle 3 is :"+h.Hypo(8.0,15.0));

}
}``````

here's my code sir

well, you are basically all done. except that your formula for calculating hypotenuse is wrong. and, inside your main method you do not need to declare multiple instances of Hypotenuse objects. one would suffice.

p.s. what you are basically doing is finding the square root of 3*4 in the first case, which of course returns a number near 3.5

Edited by bibiki: n/a

thank you for the corrections sir..

well, you are basically all done. except that your formula for calculating hypotenuse is wrong. and, inside your main method you do not need to declare multiple instances of Hypotenuse objects. one would suffice.

p.s. what you are basically doing is finding the square root of 3*4 in the first case, which of course returns a number near 3.5

He only uses 1 anyway(By accident I assume he forgot to change them once he copy pasted).

But yes, the only problem now is your formula for finding length of the hypotenuse of a right angled triangle.
What you have written basically means:
The hypotenuse is equal to the square root of the opposite side multiplied by the adjacent side.

It should be:
The hypotenuse is equal to the square root, of the sum of, the square of each side

you are welcome jsefraijeen,
if your problem is solved, mark this thread solved then lest someone reacts to this post four years from now like i see happening in other posts.