write a program that outputs a menu and reads a shape choice (circle, rectangle,
ID: 3787556 • Letter: W
Question
write a program that outputs a menu and reads a shape choice (circle, rectangle, or triangle). The program should then prompt for the appropriate dimensions and calculate the area of the shape chosen. Assume =3.141592. See the test runs below for the details on what data to ask for in order to calculate the required areas.
Your program must behave EXACTLY like the program that produced the test data.
(Use a switch statement in this program; you may NOT use if statements).
Explanation / Answer
package chegg;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Area {
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.###");
String option;
float radius,width,height,base,top,invalid;
System.out.println("Shape Menu");
System.out.println("[C]ircle");
System.out.println("[R]ectangle");
System.out.println("[T]riangle");
System.out.println("Tra[P]ezoid");
option=sc.nextLine();
double area;
switch (option) {
case "C":
System.out.println("Radius?");
radius=sc.nextFloat();
area=(Math.PI)*radius*radius;
System.out.println("The area of your shape is "+df.format(area)+" Square Units");
break;
case "R":
System.out.println("Width?");
width=sc.nextFloat();
System.out.println("Height?");
height=sc.nextFloat();
area=width*height;
System.out.println("The area of your shape is "+df.format(area)+" Square Units");
break;
case "T":
System.out.println("Base?");
base=sc.nextFloat();
System.out.println("Height?");
height=sc.nextFloat();
area=base*height*0.5;
System.out.println("The area of your shape is "+df.format(area)+" Square Units");
break;
case "P":
System.out.println("Base?");
base=sc.nextFloat();
System.out.println("Top?");
top=sc.nextFloat();
System.out.println("Height?");
height=sc.nextFloat();
area=(base+top)*height*2;
System.out.println("The area of your shape is "+df.format(area)+" Square Units");
break;
default:
System.out.println(option+"is invalid option");
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.