In: Computer Science
(In Java) Write three static functions that each take two double values which represent the two smaller sides of a right triangle. The first will calculate and return the area of the triangle, the second will calculate and return the length of the hypotenuse and the third will calculate and return the perimeter of the triangle. (Note: to calculate the perimeter, you need the value of the hypotenuse, so you must be calling that method from inside the other method.) You will also create a main method to ask the user to give the two inputs for a triangle. Then report the results of calls to the three methods. Important: None of these methods should output the results to the screen, all input and output should be done in the main. Additionally the user should be allowed to input multiple sets of data until they decide that they are finished.
if something does not work, just drop a comment;
the required methods are given as follows:
//-------------------
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
double a;
double b;
//loops infinitely
while(true){
System.out.println("enter the length of the first side");
a=scanner.nextDouble();
System.out.println("enter the length of the second side");
b= scanner.nextDouble();
System.out.println("Area: "+getArea(a, b));
System.out.println("Hypoteneuse length: "+getHypot(a, b));
System.out.println("perimeterer: "+getPerimeter(a, b));
System.out.println("");
}
}
static double getHypot(double a, double b){
//using the pythagorus theorem
return Math.sqrt((a*a) +(b*b));
}
static double getPerimeter(double a, double b){
//the sum of all sides
return (a+b+getHypot(a, b));
}
static double getArea(double a, double b){
//using the area formula
return 0.5*(a*b);
}