I would like to separate the program below into two classes. When I tried to separate them, it didn't work. Please separate into two classes and give an explanation of the answer. Please paste screenshot of successful program run.
Program:
import java.util.Scanner; //for scanner class
public class Geometry{
//method to calculate the area of a circle and return the area
public static double circleArea(double r){
double area = Math.PI * r * r; //calculate area
return area; //return area
}
//method to calculate the area of a rectangle and return the area
public static double rectangleArea(double length, double width){
double area = length * width; //calculate area
return area; //return area
}
//method to calculate the area of a triangle and return the area
public static double triangleArea(double base, double height){
double area = base * height * 0.5; //calculate area
return area; //return area
}
//main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //Scanner object for input
double area; //for area
int choice; //for reading choice of user
//loop to print menu and loop will run until user want to exit
do {
//menu
System.out.println("\nGeometry Calculator\n");
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. Quit");
System.out.print("\nEnter your choice (1-4): ");
choice = sc.nextInt(); //reading choice of user
System.out.println(); //new line
switch (choice) {
case 1: //if user choose option 1
System.out.print("Enter the Radius of a Circle = ");
double radius = sc.nextDouble(); //reading radius of a circle
area = circleArea(radius); //calling method to calculate the area
System.out.println("The Area of a Circle = " + area); //print the area
break;
case 2: //if user choose option 2
System.out.print("Enter the Length of a Rectangle = ");
double length = sc.nextDouble(); //reading length of a rectangle
System.out.print("Enter the Width of a Rectangle = ");
double width = sc.nextDouble(); //reading width of a rectangle
area = rectangleArea(length, width); //calling method to calculate the area
System.out.println("The Area of a Rectangle = " + area); //print the area
break;
case 3: //if user choose option 3
System.out.print("Enter the Base of a Triangle = ");
double base = sc.nextDouble(); //reading base of a triangle
System.out.print("Enter the Height of a Triangle = ");
double height = sc.nextDouble(); //reading height of a triangle
area = triangleArea(base, height); //calling method to calculate the area
System.out.println("The Area of a Triangle = " + area); //print the area
break;
case 4: //if user choose option 4
break;
default: //if user choose wrong option
System.out.println("Error! Enter valid choice...");
}
} while (choice != 4);
}
}