Question
The statements in the following program are in the incorrect order. Rearrange the statements so that it prompts the user to input the shape type
The statements in the following program are in the incorrect order. Rearrange the statements so that it prompts the user to input the shape type (rectangle, circle or cylinder), and the appropriate dimension of the shape. The program then outputs the following information about the shape: For a rectangle, it outputs the are and perimeter; for a circle, it outputs the area and circumference; and for a cylinder, it outputs the volume and surface area. After rearranging the statements, your program should be properly indented.
Here is my code so far :
import java.util.*;
package Exercise4;
public class Exercise4 {
static Scanner console = new Scanner(System.in); static final double PI = 3.1416; public static void main(String[] args) { String shape; System.out.print("Enter the shape type: (rectangle," + " circle,cylinder): "); shape = console.next();
System.out.println();
double length; double width; double radius; double height;
if(shape.compareTo("rectangle")== 0) { System.out.print("Enter the length of the rectangle: "); length = console.nextDouble(); System.out.println(); System.out.print("Enter the width of the rectangle: "); width = console.nextDouble(); System.out.println(); //calculate and print System.out.printf("Area of the rectangle=%.2f%n",length*width);
System.out.printf("Perimenter of the rectangle=%.2f%n",(2*(length+width))); } else if(shape.compareTo("circle")==0) { //prompt user to ender radius System.out.print("Enter the radius of the circle: "); radius = console.nextDouble(); System.out.println(); //calculate area System.out.printf("Area of the circle = %.2f%n",(PI*Math.pow(radius,2.0))); //calculate circumference System.out.printf("Circumference of the circle = %.2f%n",(2*PI*radius));
} //if shape entered is cylinder else if(shape.compareTo("cylinder")==0 ) { System.out.print("Enter the radius of the base of the cylinder: "); radius = console.nextDouble(); System.out.println(); //prompt to enter height System.out.print("Enter the height of the cylinder: "); height = console.nextDouble(); System.out.println(); //calculate and print volume System.out.printf("Volume of the cylinder" + " = %.2f%n", (PI*Math.pow(radius,2.0)*height)); //calculate and print area System.out.printf("Surface area of the cylinder =%.2f%n",(2* PI * radius* height +2*PI*Math.pow(radius,2.0))); } else System.out.println("The program does not handle "+ shape);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started