The following program is designed to find the area of a rectangle, the area of a circle, or the volume of a cylinder. However, (a) the statements are in the incorrect order; (b) the method calls are incorrect; (c) the logical expression in the while loop is incorrect; and (d) method definitions are incorrect. Rewrite the java program so that it works correctly. Your program must be properly indented. (Note that the program is menu driven and allows the user to run the program as long as the user wishes.) import java.util.Scanner; public class Assigment3_Q1 { final double PI = 3.1419; public static void main(String args []) { Scanner scan = new Scanner(System.in); double radius; double height; System.out.println("This program can calculate the area of a rectangle,\n" + "the area of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of a rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the area of a cylinder."); System.out.println("-1: To terminate the program."); int choice = scan.nextInt(); while(choice != 1) { case 1: System.out.print("Enter the radius of the base and the height of the cylinder: "); radius = scan.nextDouble(); height = scan.nextDouble(); System.out.println("Area: " + circle(length, height)); break; case 3: double length, width; System.out.print("Enter the radius of the circle: "); radius = scan.nextDouble(); System.out.println("Area: " + rectangle(radius)); break; case 2: System.out.print("Enter the length and width of the rectangle: "); length = scan.nextDouble(); width = scan.nextDouble(); System.out.println("Volume: " + cylinder(radius, height)); break; default: System.out.println("Invalid choice!"); } switch (choice) System.out.println("To run the program enter: "); System.out.println("1: To find the area of a rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the area of a cylinder."); System.out.println("-1: To terminate the program."); choice = scan.nextInt(); } double rectangle(double i, double w) { return l*r; } double circle(double r) { return PI*r*w; } double cylinder(double bR, double h) { return PI*bR*bR*l; } }
The following
import java.util.Scanner;
public class Assigment3_Q1 {
final double PI = 3.1419;
public static void main(String args []) {
Scanner scan = new Scanner(System.in);
double radius;
double height;
System.out.println("This program can calculate the area of a rectangle,\n" +
"the area of a circle, or volume of a cylinder.");
System.out.println("To run the program enter: ");
System.out.println("1: To find the area of a rectangle.");
System.out.println("2: To find the area of a circle.");
System.out.println("3: To find the area of a cylinder.");
System.out.println("-1: To terminate the program.");
int choice = scan.nextInt();
while(choice != 1) {
case 1:
System.out.print("Enter the radius of the base and the height of the cylinder: ");
radius = scan.nextDouble();
height = scan.nextDouble();
System.out.println("Area: " + circle(length, height));
break;
case 3:
double length, width;
System.out.print("Enter the radius of the circle: ");
radius = scan.nextDouble();
System.out.println("Area: " + rectangle(radius));
break;
case 2:
System.out.print("Enter the length and width of the rectangle: ");
length = scan.nextDouble();
width = scan.nextDouble();
System.out.println("Volume: " + cylinder(radius, height));
break;
default:
System.out.println("Invalid choice!");
} switch (choice)
System.out.println("To run the program enter: ");
System.out.println("1: To find the area of a rectangle.");
System.out.println("2: To find the area of a circle.");
System.out.println("3: To find the area of a cylinder.");
System.out.println("-1: To terminate the program.");
choice = scan.nextInt();
}
double rectangle(double i, double w) {
return l*r;
}
double circle(double r) {
return PI*r*w;
}
double cylinder(double bR, double h) {
return PI*bR*bR*l;
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps