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

Dani AI

Generated

Good start, . As and hinted, you want the Pythagorean theorem: square each leg, add them, then take the square root. In Java, prefer Math.hypot(a, b) over Math.sqrt(a*a + b*b) because it handles very large/small values more safely. Also follow Java naming conventions and make the utility method static so you do not need multiple instances.

Here is a compact, self-contained example that validates input and computes the three sample rows in one pass:

public final class RightTriangleApp {

  public static double hypotenuse(double a, double b) {
    if (a < 0 || b < 0) throw new IllegalArgumentException("Sides must be non-negative");
    return Math.hypot(a, b); // numerically stable
  }

  public static void main(String[] args) {
    double[][] data = { {3.0, 4.0}, {5.0, 12.0}, {8.0, 15.0} };
    for (int i = 0; i < data.length; i++) {
      double h = hypotenuse(data[i][0], data[i][1]);
      System.out.printf("Triangle %d: hypotenuse = %.1f%n", i + 1, h);
    }
  }
}

Expected output:

  • Triangle 1: 5.0
  • Triangle 2: 13.0
  • Triangle 3: 17.0

Quick tips:

  • Avoid Math.pow(x, 2) for squaring; x * x (or Math.hypot) is faster and clearer.
  • If you later read user input, wrap a Scanner in a loop that rejects negatives and non-numbers, and close it (or use try-with-resources).
  • For tests, assert that hypotenuse(3,4) equals 5.0 within a small delta (e.g., 1e-12) to account for floating-point rounding.

This keeps the code simple, idiomatic, and accurate while matching the assignment requirements.

Recommended Answers

All 6 Replies

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.