1,105,644 Community Members

calculates the length of the hypotenuse of a right triangle

Member Avatar
jsefraijeen
Newbie Poster
14 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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


please help me on this one

Member Avatar
Akill10
Posting Pro
585 posts since Sep 2010
Reputation Points: 96 [?]
Q&As Helped to Solve: 81 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Post your attempt please.

Member Avatar
jsefraijeen
Newbie Poster
14 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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

Member Avatar
bibiki
Posting Whiz
393 posts since Nov 2009
Reputation Points: 18 [?]
Q&As Helped to Solve: 31 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
jsefraijeen
Newbie Poster
14 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thank you for the corrections sir..

Member Avatar
Akill10
Posting Pro
585 posts since Sep 2010
Reputation Points: 96 [?]
Q&As Helped to Solve: 81 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
bibiki
Posting Whiz
393 posts since Nov 2009
Reputation Points: 18 [?]
Q&As Helped to Solve: 31 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Question Answered as of 3 Years Ago by bibiki and Akill10
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article