I need to write a program to prompt the user to choose from a menu of 5 differen
ID: 3653171 • Letter: I
Question
I need to write a program to prompt the user to choose from a menu of 5 different shapes then write the code to calculate and display the area of that shape. This is what I have so far. I just have to shapes in the switch and I can not get it to compile. import java.util.Scanner; public class shapes { public static void main(String[] args) { Scanner stdIn=new Scanner(System.in); String shapes= "1. Square " + "2. Rectangle " + "3. Triangle " + "4. Trapezoid " + "5. Circle "; String shapeNum; int shapeIndex; int eolIndex; String shape; System.out.println("Pick a number for the shape you want the area of " + shapes); shapeNum = stdIn.nextLine(); shapeIndex = shapes.indexOf(shapeNum); eolIndex = shapes.indexOf(" ", shapeIndex); shape = shapes.substring(shapeIndex, eolIndex); System.out.println(shape); switch (shape.charAt(0)) { case '1': int length; int area; System.out.println("What is the length of the Square = "); length = stdIn.nextInt(); area = length*4; System.out.println("The area of the Square is = " + area); break; case '2': int length1; int width; int area2; System.out.println("What is the length of the Rectangle = "); lenght1 = stdIn.nextInt(); System.out.println("What is the width of the Rectangle = "); width = stdIn.nextInt(); area2 = length1*width; System.out.println("The area of the Rectangle is = "); break; } } }Explanation / Answer
Please rate...
program Shapes.java
====================================================
import java.util.Scanner;
public class Shapes
{
public static void main(String[] args)
{
Scanner stdIn=new Scanner(System.in);
String shapes= "1. Square " + "2. Rectangle " +
"3. Triangle " + "4. Trapezoid " + "5. Circle ";
String shapeNum;
int shapeIndex;
int eolIndex;
String shape;
System.out.println("Pick a number for the shape you " +
"want the area of: " + shapes);
shapeNum = stdIn.nextLine();
shapeIndex = shapes.indexOf(shapeNum);
eolIndex = shapes.indexOf(" ", shapeIndex);
shape = shapes.substring(shapeIndex, eolIndex);
System.out.println(shape);
switch (shape.charAt(0))
{
case '1':
int length;
int area;
System.out.print("What is the length of the Square = ");
length = stdIn.nextInt();
area = length*length;
System.out.println("The area of the Square is = " + area);
break;
case '2':
int length1;
int width;
int area2;
System.out.print("What is the length of the Rectangle = ");
length1 = stdIn.nextInt();
System.out.print("What is the width of the Rectangle = ");
width = stdIn.nextInt();
area2 = length1*width;
System.out.println("The area of the Rectangle is = "+area2);
break;
case '3':
int base;
int height;
System.out.print("What is the base of the triangle = ");
base = stdIn.nextInt();
System.out.print("What is the height of the triangle = ");
height = stdIn.nextInt();
double area1 = (base*height)/2;
System.out.println("The area of the Square is = " + area1);
break;
case '4':
int base1,base2;
int height1;
System.out.print("What is the base 1 of the trapezoid = ");
base1 = stdIn.nextInt();
System.out.print("What is the base 2 of the trapezoid = ");
base2 = stdIn.nextInt();
System.out.print("What is the height of the trapezoid = ");
height1 = stdIn.nextInt();
double area4 = ((base1+base2)*height1)/2;
System.out.println("The area of the Square is = " + area4);
break;
case '5':
int radius;
double area5;
System.out.print("What is the radius of the circle = ");
radius = stdIn.nextInt();
area5 = Math.PI*radius*radius;
System.out.println("The area of the Square is = " + area5);
break;
default:System.out.println("Wrong entry");
}
}
}
=========================================================
Sampe output: