Geometry Calculator : ( Please use a switch statement in this program). Write a
ID: 3595586 • Letter: G
Question
Geometry Calculator : (Please use a switch statement in this program).
Write a program that displays the following menu:
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):
If the user enters 1, the program should ask for the radius of the circle and then display its area. Use the following formula:
area r2
Use 3.14159 for and the radius of the circle for r. If the user enters 2, the program should ask for the length and width of the rectangle and then display the rectangle’s area. Use the following formula:
area = length * width
If the user enters 3 the program should ask for the length of the triangle’s base and its height, and then display its area. Use the following formula:
area = base * height * .5
If the user enters 4, the program should end.
Input Validation: Display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu. Do not accept negative values for the circle’s radius, the rectangle’s length or width, or the triangle’s base or height.
(Please use a switch statement in this program).
Explanation / Answer
Diaz_Driver.java
import java.util.Scanner;
public class Diaz_Driver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Geometry Calculator");
System.out.println("1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle 3. Calculate the Area of a Triangle 4. Quit");
System.out.println("Enter your choice (1-4): ");
int choice = scan.nextInt();
while(choice != 4){
switch(choice) {
case 1: System.out.println("Please enter the radius of Circle");
double radius = scan.nextDouble();
double area = getCircleArea(radius);
System.out.println("The area of the Circle is: "+area);
break;
case 2:
System.out.println("Please enter the length of Rectangle");
double length = scan.nextDouble();
System.out.println("Please enter the width of Rectangle");
double width = scan.nextDouble();
area = getRectangleArea(length, width);
System.out.println("The area of the Rectangle is: "+area);
break;
case 3:
System.out.println("Please enter the base of Triangle");
double base = scan.nextDouble();
System.out.println("Please enter the height of Triangle");
double height = scan.nextDouble();
area = getTriangleArea(base, height);
System.out.println("The area of the Rectangle is: "+area);
case 4:
System.out.println("Exit");
break;
default:
System.out.println("Error: The range should be between 1 and 4");
}
System.out.println("1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle 3. Calculate the Area of a Triangle 4. Quit");
System.out.println("Enter your choice (1-4): ");
choice = scan.nextInt();
}
}
public static double getCircleArea(double radius){
return 2 * Math.PI * radius;
}
public static double getRectangleArea(double length, double width){
return length * width;
}
public static double getTriangleArea(double base, double height){
return base * 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
Please enter the radius of Circle
2
The area of the Circle is: 12.566370614359172
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):
3
Please enter the base of Triangle
3
Please enter the height of Triangle
4
The area of the Rectangle is: 6.0
Exit
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):
3
Please enter the base of Triangle
4
Please enter the height of Triangle
5
The area of the Rectangle is: 10.0
Exit
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):
4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.