Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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).

Test Run 1 Shape Menu [C]ircle [R]ectangle [Triangle Tralplezoid shape (C, R, T, or P)? c Radius? 7.8 The area of your shape is 191.134 square units. Test Run 4: Shape Menu [C]ircle [R]ectangle [Triangle Tralplezoid Shape (C, R, T, or P)? P Base 17.2 Top? 10.5 Height? 5.72 Test Run 2 Shape Menu (Circle [R]ectangle [Triangle Tralplezoid Shape (C, R, T, or P)? R Width? 12.2 Height? 7.8 The area of your shape is 95.16 square units Test Run 5 Shape Menu (Circle [R]ectangle [Triangle Tralplezoid Shape (C, R, T, or P)? 1 1 is an invalid menu option, program terminated Test Run 3 Shape Menu [C]ircle [R]ectangle [Triangle Tralplezoid Shape (C, R, T, or P)? T Base? 7.2 Height? 4.44 The area of your shape is 15.984 square units.

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;
       }
   }
}