Madhav Vadalia
Madhav Vadalia

Reputation: 9

Find Hypotenuse of right triangle

Assignment:

Write a method named Hypo, which calculates the hypotenuse of a right triangle. This method accepts two double values representing the sides of the triangle. The method Hypo calculates and displays on the screen the value of the 3rd side of the right triangle. The method main( ) should read in two double values from the user, using an input box, and then call Hypo, sending those two values as parameters to it. Hypo( ) then prints the result in an output box onto the screen. Both main( ) and Hypo( ) reside in the same class, named A2.


Here is my code. There is no error but it doesn't give me any output.can you help me out?

import java.util.Scanner;
import javax.swing.JOptionPane;

public class A2 
{

    public static void main(String[] args)
    {

        double height=Double.parseDouble(JOptionPane.showInputDialog("Enter       1st side of triangle: "));

        double base=Double.parseDouble(JOptionPane.showInputDialog("Enter 2nd     side of triangle: "));

        RightTriangle newTriangle = new RightTriangle(height, base);



        newTriangle.getHypotenuse();
        double hypotenuse = newTriangle.getHypotenuse();


        JOptionPane.showMessageDialog(null,hypotenuse);

    }
    public double height;
    public double base;
    public final double hypotenuse = Math.sqrt(Math.pow(height, 2) + Math.pow(base, 2));


    public A2(double triHeight, double triBase)
    {
        height = triHeight;
        base = triBase;
    }

    public double getHypotenuse()
    {
        return hypotenuse;
    }


}

Upvotes: 1

Views: 11755

Answers (4)

Kandarp
Kandarp

Reputation: 965

 \\Sawh Class CCM
 import javax.swing.JOptionPane; 
 import javax.swing.text.*;
 import java.lang.Math;
 import java.text.DecimalFormat;

public class A2 
{

    public static void main(String[] args) 
    {
        double base;
        double height;
        base = Double.parseDouble(JOptionPane.showInputDialog("Enter base: "));
        height = Double.parseDouble(JOptionPane.showInputDialog("Enter height: "));
        Hypo(base, height);
    }

    public static void Hypo(double baseInp, double heightInp)
    {
        double hypo;
        DecimalFormat round = new DecimalFormat("0.00");
        hypo = Math.sqrt((Math.pow(baseInp, 2)) + (Math.pow(heightInp, 2)));
        JOptionPane.showMessageDialog(null, "Hypotenuse is: " + round.format(hypo));
    }

}

Upvotes: 0

Sotti
Sotti

Reputation: 14409

You can just use Math.hypo().

Upvotes: 5

Null7
Null7

Reputation: 25

import java.util.Scanner;
import javax.swing.JOptionPane;

public class A2 
{
    public static double height;
    public static double base;
    public static double hypotenuse;

    public static void main(String[] args)
    {
        height = Double.parseDouble(JOptionPane.showInputDialog("Enter 1st side of triangle: "));
        base = Double.parseDouble(JOptionPane.showInputDialog("Enter 2nd side of triangle: "));

        hypotenuse = Hypo(height, base);

        JOptionPane.showMessageDialog(null,hypotenuse);
    }

    public static double Hypo(double height, double base)
    {
        return Math.sqrt(Math.pow(height, 2) + Math.pow(base, 2));
    }
}

That may work...

Couple of things: It looked like you had your class (A2) setup so that you could create an instance of it to represent the triangle that you were trying to find the hypotenuse of, but then you went and created a RightTriangle. Really, both are probably not needed, as a method (called Hypo as per your instructions) within the class A2 is probably sufficient. Additionally, you had some issues with the order of your code (ex: you were calculating the hypotenuse at the wrong point), and you had some redundant/unused code.

Upvotes: 0

TMR
TMR

Reputation: 96

You are calculating the hypotenuse before you're actually acceptin height and width as parameters. You have two options. One is to change your constructor to initialize hypotenuse there. the other (and my preference) is this:

public double getHypotenuse()
{
      return Math.sqrt(Math.pow(height, 2) + Math.pow(base, 2));
}

That way, you won't even need to store the hypotenuse.

Upvotes: 5

Related Questions