In: Computer Science
Write a Java program such that it consists of 2 classes:
1. a class that serves as the driver (contains main())
2. a class that contains multiple private methods that compute and display
a. area of a triangle (need base and height)
b area of a circle (use named constant for PI) (need radius)
c. area of rectangle (width and length)
d. area of a square (side)
e. surface area of a solid cylinder (height and radius of base)
N.B. You will also need an accessor public method (interface) that is called from the driver. This method should:
a. Display a menu of options to the user. (Suggest use switch statement)
b. Accept input from the user (keyboard) representing parameter values needed.
c. Invoke other [private] methods in the class to perform the necessary area computations
Add comments throughout your code as you deem appropriate.
import java.util.Scanner;
class Geometry {
static Scanner in = new
Scanner(System.in);
static double PI = 3.14;
// public interface method which asks user for choice and calls
private method
public void performUserOperation() {
// Show menu
System.out.println("1.
Compute Area of triangle");
System.out.println("2.
Compute Area of Circle");
System.out.println("3.
Compute Area of Rectangle");
System.out.println("4.
Compute Area of Square");
System.out.println("5.
Compute Surface Area of Cylinder");
System.out.println("Enter
choice: ");
int choice =
in.nextInt();
switch (choice)
{
case 1:
System.out.print("Enter
triangle base: ");
double
base = in.nextDouble();
System.out.print("Enter
triangle height: ");
double
height = in.nextDouble();
System.out.println("Area:
" + triangleArea(base, height));
break;
case 2:
System.out.print("Enter
circle radius: ");
double
radius = in.nextDouble();
System.out.println("Area:
" + circleArea(radius));
break;
case 3:
System.out.print("Enter
Rectangle width: ");
double
width = in.nextDouble();
System.out.print("Enter
Rectangle height: ");
height
= in.nextDouble();
System.out.println("Area:
" + rectangleArea(width, height));
break;
case 4:
System.out.print("Enter
Square side: ");
double
side = in.nextDouble();
System.out.println("Area:
" + squareArea(side));
break;
case 5:
System.out.print("Enter
cylinder base radius: ");
radius
= in.nextDouble();
System.out.print("Enter
cylinder height: ");
height
= in.nextDouble();
System.out.println("Area:
" + cylindersurfaceArea(radius, height));
break;
default:
break;
}
}
private double cylindersurfaceArea(double
radius, double height) {
return 2 * PI *
radius * (radius + height);
}
private double squareArea(double side)
{
return side *
side;
}
private double rectangleArea(double width,
double height) {
return width *
height;
}
private double circleArea(double radius)
{
return PI * radius
* radius;
}
private double triangleArea(double base,
double height) {
return 0.5 * base *
height;
}
}
// driver class
public class GeometryHelper {
public static void main(String[] args)
{
Geometry geometry =
new Geometry();
geometry.performUserOperation();
}
}
**************************************************
Thanks for your question. We try our best to help you with detailed
answers, But in any case, if you need any modification or have a
query/issue with respect to above answer, Please ask that in the
comment section. We will surely try to address your query ASAP and
resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.