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...
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 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)...
(JAVA) Write an application that reads three nonzero values entered by the user and determines and...
(JAVA) Write an application that reads three nonzero values entered by the user and determines and prints whether they could represent the sides of a triangle. Enter three sizes, separated by spaces(decimals values are acceptable): 4.5·5.5·3.5 A triangle could measure 4.50, 5.50, by 3.50.
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate,...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also...
1. Write a class Compare3 that provides a static method largest. Method largest should take three...
1. Write a class Compare3 that provides a static method largest. Method largest should take three Comparable parametersand return the largest of the three (so its return type will also be Comparable). Recall that method compareTo is part ofthe Comparable interface, so largest can use the compareTo method of its parameters to compare them. 2. Write a class Comparisons whose main method tests your largest method above. •   First prompt the user for and read in three strings, use your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT