(Java is the language we must use) NO ARRAYS please help Write a program that pr
ID: 3594748 • Letter: #
Question
(Java is the language we must use) NO ARRAYS please help
Write a program that presents the user a menu of four choices, “Circle”, “Square”, “Rectangle”, and “Exit” to calculate the area of one of these geometric objects. Once a choice is made, the program would ask the user for the data required to do the calculation. For example, if the user input were “Circle”, the program would prompt the user to enter the value of the radius of the circle. The program will let the user run area calculations until “Exit” is entered. Use a WHILE or a DO-WHILE loop.
Explanation / Answer
AreaMenu.java
import java.util.Scanner;
public class AreaMenu {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Program to calculate areas of objects: ");
System.out.println("1 -- Square 2 -- Circle 3 -- Rectangle 4 -- Exit ");
int choice = scan.nextInt();
double area;
while(choice != 4) {
if(choice == 1) {
System.out.print("Radius of the circle: ");
double radius = scan.nextDouble();
area = radius * radius;
System.out.println("Area = "+area);
} else if(choice == 2){
System.out.print("Radius of the circle: ");
double radius = scan.nextDouble();
area = Math.PI * radius * radius;
System.out.println("Area = "+area);
} else if(choice == 3){
System.out.print("Enter the length: ");
double l = scan.nextDouble();
System.out.print("Enter the width: ");
double w = scan.nextDouble();
area = (l * w);
System.out.println("Area = "+area);
}else {
System.out.println("an Invalid choice.");
}
System.out.println("1 -- Square 2 -- Circle 3 -- Rectangle 4 -- Exit ");
choice = scan.nextInt();
}
}
}
Output:
Program to calculate areas of objects:
1 -- Square
2 -- Circle
3 -- Rectangle
4 -- Exit
1
Radius of the circle: 2
Area = 4.0
1 -- Square
2 -- Circle
3 -- Rectangle
4 -- Exit
2
Radius of the circle: 3
Area = 28.274333882308138
1 -- Square
2 -- Circle
3 -- Rectangle
4 -- Exit
3
Enter the length: 2
Enter the width: 4
Area = 8.0
1 -- Square
2 -- Circle
3 -- Rectangle
4 -- Exit
4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.