Write a program that presents the user a menu of four choices, “Circle”, “Square
ID: 3594511 • Letter: W
Question
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 in Java.
Explanation / Answer
import java.util.Scanner;
public class Area {
public static void main(String[] args){
int select;
Scanner sc=new Scanner(System.in);
System.out.println("Area Calculator");
System.out.println("Select your choice");
System.out.println("1.Square");
System.out.println("2.Rectangle");
System.out.println("3.circle");
select=sc.nextInt();
Area choice=new Area();
switch(select){
case 1:
choice.square();
//Calls Square() function
break;
case 2:
choice.Rectangle();
//calls Rectangle()Function
break;
case 3:
choice.circle();
//calls Circle()Function
break;
default:
System.out.println("Enter a valid command");
}
}
// Program to calculate the area of Square
private void square(){
int area,s;
System.out.println("Enter the length of side");
//Get the length of the side
Scanner sc=new Scanner(System.in);
s=sc.nextInt();
area=s*s;
System.out.println("area of square ="+area);
//prints the Area of square
}
// Program to calculate the area of Recatngle
private void Rectangle(){
int area,l,b;
System.out.println("Enter the length of side");
//Get the Length
System.out.println("Enter the breadth of side");
//get the Breadth
Scanner sc=new Scanner(System.in);
l=sc.nextInt();
b=sc.nextInt();
area=l*b;
System.out.println("area of square ="+area);
//prints the are of Rectangle
}
// Program to calculate the area of Circle
private void circle(){
int area,r;
System.out.println("Enter the length of side");
//Get the length of the circle
Scanner sc=new Scanner(System.in);
r=sc.nextInt();
area=22/7*r*r;
System.out.println("area of circle ="+area);
//Prints the area of Circle
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.