In: Computer Science
Write class Hypotenuse that calculates the length of the
hypotenuse of a right triangle and return to HypotenuseTest class.
The lengths of the other two sides are given by the user by using
HypotenuseTest class and send into Hypotenuse class. Hypotenuse
class should
take two arguments of type double and return the hypotenuse as a
double into HypotenuseTest’s Main method. Finally, the
HypotenuseTest class displays the hypotenuse side of a
triangle.
import java.lang.Math;
public class Hypotenuse {
double length;
double breadth;
Hypotenuse(double length,double breadth)
{
this.length=length;
this.breadth=breadth;
}
public double getHypo()
{
return(Math.pow(length*length+breadth*breadth, 0.5));
}
}
import java.util.Scanner;
class HypotenuseTest{
public static void main(String[] args) {
double length,breadth;
int choice;
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("1.Get hypotenuse");
System.out.println("2.Exit");
System.out.print("Enter your choice:");
choice=sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter length of a right triangle:");
length=sc.nextDouble();
System.out.print("Enter breadth of a right triangle:");
breadth=sc.nextDouble();
Hypotenuse obj=new Hypotenuse(length, breadth);
System.out.printf("Hypotenuse of a rigth triangle is %f\n", obj.getHypo());
break;
case 2:
System.exit(0);
default:
break;
}
}
}
}