Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Question ? Create a Geometry class with the following methods: A static met

ID: 3674368 • Letter: J

Question



Java Question ?


Create a Geometry class with the following methods:

A static method called circle that accepts the radius of a circle and returns the area of the circle. (Use Math.PI for and the radius of the circle for r) Use the following formula:

Area = r2

A static method called rectangle that accepts the length and width of a rectangle and returns the area of the rectangle. Use the following formula:

Area = Length * Width

A static method called triangle that accepts the length of a triangle’s base and the triangle’s height. The method should return the area of the triangle. Use the following formula:

Area = 0.5 * Base * Height

The methods should display an error message if zeros, or negative values are used for the circle’s radius, the rectangle’s length or width, or the triangle’s base or height

Next, write the GeometryDemo program to test the class, which displays the following menu and responds to the user’s selection:

**********Geometry Calculator********* Enter 1 to calculate the area of a circle Enter 2 to calculate the area of a rectangle Enter 3 to calculate the area of a triangle Enter 4 to quit

Please enter your choice: (1 - 4)

Display an error message if the user enters a number outside of the range of 1 through 4 when selecting an item from the menu.

Explanation / Answer

GeometryCalculator.java

import java.util.Scanner;

public class GeometryCalculator {

   /**
   * @param args
   */
   public static void main(String[] args) {
      
       Scanner keyboard = new Scanner(System.in);
       int choice;
       double area = 0;
      
       do {
           System.out.println("Geometry Calculator 1. Calculate the Area"
                   + " of a Circle 2. Calculate the Area of a Rectangle 3. "
                   + "Calculate the Area of a Triangle 4. Quit Enter your choice (1-4):");
          
           choice = keyboard.nextInt();
          
           if (choice < 1 || choice > 4) {
               System.out.println("You didn't choose a calculation. Try again.");
           }
          
       } while (choice < 1 || choice > 4);
      
       if (choice == 1) {
          
           double radius;
           System.out.println("You chose to Calculate the Area of a Circle.");
           System.out.println("Please enter the radius of a circle.");
           radius = keyboard.nextDouble();
           area = Geometry.getAreaOfCircle(radius);
          
       } else if (choice == 2) {
          
           double length;
           double width;
           System.out.println("You chose to Calculate the Area of a Rectangle.");
           System.out.println("Enter the length of a rectangle");
           length = keyboard.nextDouble();
           System.out.println("Enter the width of a rectangle");
           width = keyboard.nextDouble();
           area = Geometry.getAreaOfRectangle(length, width);
          
       } else if (choice == 3) {
          
           double baseLength;
           double height;
           System.out.println("You chose to Calculate the Area of a Triangle.");
           System.out.println("Enter the length of the triangle's base.");
           baseLength = keyboard.nextDouble();
           System.out.println("Enter the height of the triangle.");
            height = keyboard.nextDouble();
           area = Geometry.getAreaOfTriangle(baseLength, height);
          
       }else if (choice == 4) {
           System.out.println("You chose to quit.");
           System.exit(0);
       }
      
       System.out.println("Area: " + area);
      
       keyboard.close();
      
   }

}


Geometry.java

public class Geometry {
  
   public static double getAreaOfCircle(double radius) {
       return Math.PI * (radius * radius);
   }
  
   public static double getAreaOfRectangle(double length, double width) {
       return length * width;
   }

   public static double getAreaOfTriangle(double baseLength, double height) {
       return baseLength * (height * 0.5);
   }
  
}


output


Geometry Calculator                                                                                                                                         
1. Calculate the Area of a Circle                                                                                                                          
2. Calculate the Area of a Rectangle                                                                                                                       
3. Calculate the Area of a Triangle                                                                                                                        
4. Quit                                                                                                                                                    
Enter your choice (1-4):                                                                                                                                    
1                                                                                                                                                           
You chose to Calculate the Area of a Circle.                                                                                                                
Please enter the radius of a circle.                                                                                                                        
3                                                                                                                                                           
Area: 28.274333882308138                                                                                                                                    

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote