Question

In: Computer Science

Java Question: Write three static functions that each take two double values which represent the two...

Java Question:

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. (Formulas for triangles are easily found on the internet, but I can provide them for you if you can’t find them.)

If you can put comments on lines to explain codes, it would be much appreciated! Thanks!

Solutions

Expert Solution

import java.util.*;
import java.lang.Math;
public class Main
{
public static double getArea(double a,double b)
{
//right triangle area formula is 0.5*base*height;
return 0.5*a*b;
}
public static double getHypotenuse(double a,double b)
{
//hypotenuse formula is Square root(a*a+b*b)
return Math.sqrt(a*a+b*b);
}
public static double getPerimeter(double a,double b)
{
//perimeter is a+b+c
return a+b+getHypotenuse(a,b);
}
   public static void main(String[] args)
   {
   Scanner s = new Scanner(System.in);
   double a,b,c,area,perimeter;
   String ch;
   //we use a do while loop and continue until user wants to exit
       do
       {
       System.out.print("Enter the first side: ");
       a=s.nextDouble();
       System.out.print("Enter the second side: ");
       b=s.nextDouble();
       area=getArea(a,b);
       c=getHypotenuse(a,b);
       perimeter=getPerimeter(a,b);
       System.out.println("First Side: "+a);
       System.out.println("Second Side: "+b);
       System.out.println("Hypotenuse: "+c);
       System.out.println("Area: "+area);
       System.out.println("Perimeter: "+perimeter);
       System.out.print("Do you want to continuue?(y/n): ");
       ch=s.next();
       }while(ch.equals("y"));
   }
}



Related Solutions

(In Java) Write three static functions that each take two double values which represent the two...
(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.)...
Java please. Write a static method sqrt()that takes a double argument and returns the square root...
Java please. Write a static method sqrt()that takes a double argument and returns the square root of that number using newton's method to compute result.
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on input two sets represented as hash sets, returns their Jaccard similarity. The following are a few sample runs: Input :    A=1, 2, 3, 4,   B=2, 4, 6, 8 Return:   0.3333333333333333 Input :    A=Larry, Michael, Shaq, Kobe, LeBron                    B=Steve, Kobe, Shaq, LeBron, Steph, Jeremy, Michael Return:   0.5 Your method must have time complexity On and space complexity O1, where n is the size of...
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds) *Please do it in eclipse and this is java language*
In this homework assignment, you will be writing three `void` functions. These functions each take a...
In this homework assignment, you will be writing three `void` functions. These functions each take a single integer parameter. The functions accomplish the following tasks. * The first function prints out the sum of the numbers from 1 up to and including the number passed to it as an argument. * The second function prints out the sum of the even numbers from 2 up to and including the number passed to it as an argument. * The third function...
Write a Reverse Polish Calculator, RPN in JAVA Each time an operand is encountered, two values...
Write a Reverse Polish Calculator, RPN in JAVA Each time an operand is encountered, two values are popped from this stack, the given operation is performed, and the result is pushed back onto the stack. Utilize Java generic Stack Class to implement this algorithm with four arithmetic operations: +, *, -, /. Implement a class RPN with a single static method evaluate. This method should have the following signature:             public static String evaluate(String expression) It should split the passed...
JAVA Write a for loop which will display this set of values 1,3,5,7,9.
JAVA Write a for loop which will display this set of values 1,3,5,7,9.
Write a program in Java that will take as input two sets A and B, and...
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
Recursion practice Write a Java program with functions for each of the following problems. Each problem...
Recursion practice Write a Java program with functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. All of your solutions should be in a single .java file. The main function of the file should demonstrate each of your solutions, by running several tests and producing the corresponding outputs. Write a recursive method to 1. calculate power of a give number 2. multiply...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described in the specifications below. Utilizing the if and else statements, write a program which will calculate a commission based on a two-tiered commission plan of 3% and 7% paid on amounts of $15,000 or less, or over $15,000 in monthly sales by a sales force paid on a commission basis. Use the output specifications below. ? Specifications There will be two classes (separate files)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT