In: Computer Science
Correct the code:
import java.util.Scanner;
public class Ch7_PrExercise5 {
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
double radius;
double height;
System.out.println("This program can calculate "+ "the area of a
rectangle, 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 rectangle.");
System.out.println("2: To find the area of a circle.");
System.out.println("3: To find the volume of a cylinder.");
System.out.println("-1: To terminate the program.");
choice = console.nextInt();
System.out.println();
int choice;
switch (choice == -1){
case1:
System.out.print("Enter the radius of the base and "+ "the height
of the cylinder: ");
radius = console.nextDouble();
height = console.nextDouble();
System.out.println();
System.out.printf("Area = %.2f%n",circle(length, height));
break;
case3:
doublelength, width;
System.out.print("Enter the radius of the circle: ");
radius = console.nextDouble();
System.out.println();
System.out.printf("Area = %.2f%n", rectangle(radius));
break;
case2:
System.out.print("Enter the length and the width "+ "of the
rectangle: ");
length = console.nextDouble();
width = console.nextDouble();
System.out.println();
System.out.printf("Volume = %.2f%n",cylinder(radius,
height));
break;
default:
System.out.println("Invalid choice!");
}
switch(choice){
System.out.println("To run the program enter: ");
System.out.println("2: To find the area of a circle.");
System.out.println("1: To find the area of rectangle.");
System.out.println("3: To find the volume of a cylinder.");
System.out.println("-1: To terminate the program.");
choice = console.nextInt();
System.out.println();
}
public static double rectangle(doublel,doublew)
{
returnl * r;}
public static double circle(doubler)
{
returnMath.PI * r * w;
}
public static double cylinder(doublebR,doubleh)
{
returnMath.PI * bR * bR * l;
}
import java.util.Scanner;
public class Ch7_PrExercise5 {
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
double radius;
double height,width,length;
int choice;
System.out.println("This program can calculate "+ "the area of a
rectangle, 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 rectangle.");
System.out.println("2: To find the area of a circle.");
System.out.println("3: To find the volume of a cylinder.");
System.out.println("-1: To terminate the program.");
choice = console.nextInt();
System.out.println();
switch (choice)
{
case 1:
System.out.print("Enter the length and the width "+ "of the
rectangle: ");
length = console.nextDouble();
width = console.nextDouble();
System.out.println();
System.out.printf("Area = %.2f%n",rectangle(length, width));
break;
case 2:
System.out.print("Enter the radius of the circle: ");
radius = console.nextDouble();
System.out.println();
System.out.printf("Volume = %.2f%n",circle(radius));
break;
case 3:
System.out.print("Enter the radius of the base and "+ "the height
of the cylinder: ");
radius = console.nextDouble();
height = console.nextDouble();
System.out.println();
System.out.printf("Area = %.2f%n", cylinder(radius, height));
break;
default:
System.out.println("Invalid choice!");
}
}
public static double rectangle(double l,double w)
{
return l * w;
}
public static double circle(double r)
{
return Math.PI * r * r;
}
public static double cylinder(double bR,double h)
{
return Math.PI * bR * bR * h;
}
}