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

Design a Geometry class with the following methods: * A static method that accep

ID: 3555211 • Letter: D

Question

Design a Geometry class with the following methods:

* A static method that accepts the radius of a circle and returns the area of the circle.

Using the following formula:

Area=?r^2

Use Math.PI for ? and the radius of a circle for r.

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

Area = Length x Width

* A static method that accepts the length of a triangle's base and the triangle's hight. The method should return the area of the triangle. Use the following formula:

Area = Base x Height x 0.5

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

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

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):

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

Explanation / Answer

import java.util.Scanner;
public class GeometryCalculatorDriver {

public static void main(String[] args) {
int selection;
double area;
GeometryCalculator Calc = new GeometryCalculator();
Scanner console = new Scanner(System.in);
System.out.println("Geometry Calculator");
System.out.println("1. Calculate the Area of a Circle");
System.out.println("2. Calculate the Area of a Rectangle");
System.out.println("3. Calculate the Area of a Triangle");
System.out.println("4. Exit");

while (true) {
selection = console.nextInt();
if (selection == 1) {
System.out.println("Enter the radius: ");
double radius = console.nextDouble();
area = Calc.circleArea(radius);
}
else if (selection == 2) {
System.out.print("Enter the length: ");
double length = console.nextDouble();
System.out.println("Enter the width: ");
double width = console.nextDouble();
area = Calc.rectangleArea(length, width);
}
else if (selection == 3) {
System.out.print("Enter the base: ");
double base = console.nextDouble();
System.out.println("Enter the height: ");
double height = console.nextDouble();
area = Calc.triangleArea(base, height);
}
else if (selection == 4) {
break;
}
else {
System.out.println(selection+" is not a valid menu seleciton, please try again.");
}
}
System.out.println("The area is : "+area);
}
} Code public class GeometryCalculator {
private double area;

public double circleArea(double r) {
area = r * Math.PI * 2;
return area;
}

public double rectangleArea(double length, double width) {
area = length * width;
return area;
}

public double triangleArea(double base, double height) {
area = base * height * 0.5;
return area;
}
}