Need to revise working code below A good design means the ap
Need to revise working code below,
A good design means the application should scale easily -- that is, the application should be designed so that additional processing requirements can be added without making having to rewrite the application each time. Revise code below to add the following design features:
Move the code to display the menu to it\'s own method.
Create a method to handle capturing the shape dimensions. One approach may be to pass to the method the dimension to enter (\"base\", \"height\", \"side\" or \"radius\") as a String.
Processing for each of the shapes matched in the switch statement should also be moved to their own methods.
Notes -
The program should \"look and feel\" like the code below.
The program should display the menu and prompt the user for a shape (or an exit).
If the user enters a value that corresponds to a shape, program control (via the switch statement) should transfer control to a method specific to the shape that will pass control to prompt for the appropriate dimensions to another method, then calculate the area and perimeter (circumference) as directed in PLP06, and display the result.
The program should repeat from step 1 above.
import java.util.Scanner;
public class Pickashape {
//Declaring constant
public static final double PI = 3.14159;
public static void main(String[] args) {
//Declaring variable
int choice;
//Scanner class Obejct is used to read the inputs entered by the user
Scanner sc = new Scanner(System.in);
//This loop continues to execute until user enters choice \'5\'
do {
//Displaying the menu
System.out.println(\"\ \ Choose the shape\");
System.out.println(\"1. Square\");
System.out.println(\"2. Rectangle\");
System.out.println(\"3. Circle\");
System.out.println(\"4. Triangle\");
System.out.println(\"5. Quit\");
System.out.print(\"Enter your choice:\");
choice = sc.nextInt();
//Based on the User selection the corresponding case will be executed
switch (choice) {
//This case will calculate the area and perimeter of the square
case 1: {
//Declaring variables
double side, area, perimeter;
//Getting the side of the square entered by the user
System.out.print(\"Enter the side (as a decimal):\");
side = sc.nextDouble();
//calculating the area of the square
area = side * side;
//calculating the perimeter of the square
perimeter = 4 * side;
//Displaying the area of the square
System.out.println(\"The area is \" + area);
//Displaying the perimeter of the square;
System.out.println(\"The perimeter is \" + perimeter);
break;
}
case 2: {
//Declaring variables
double firstside, secondside, area, perimeter;
//Getting the first side of the rectangle
System.out.print(\"Enter the first side (as a decimal):\");
firstside = sc.nextDouble();
//Getting the second side of the rectangle
System.out.print(\"Enter the second side (as a decimal):\");
secondside = sc.nextDouble();
//Calculating the area of the rectangle
area = firstside * secondside;
//Calculating the perimeter of the rectangle
perimeter = 2 * firstside + 2 * secondside;
//Displaying the area of the rectangle
System.out.println(\"The area is \" + area);
//Displaying the perimeter of the rectangle
System.out.println(\"The perimeter is: \" + perimeter);
break;
}
case 3: {
//Declaring variables
double radius, area, circumference;
//Getting the radius of the circle
System.out.print(\"Enter the radius (decimal):\");
radius = sc.nextDouble();
//calculating the area of the circle
area = PI * radius * radius;
//calculating the circumference of the circle
circumference = 2 * PI * radius;
//displaying the area of the circle
System.out.printf(\"The area is %.2f \", area);
//displaying the circumference of the circle
System.out.printf(\"\ The circumference is %.2f \", circumference);
break;
}
case 4: {
//Declaring variables
double height, base, area, perimeter, hypotenuse;
//Getting the height of the triangle
System.out.print(\"Enter the height of the triangle (decimal):\");
height = sc.nextDouble();
//Getting the base of the triangle
System.out.print(\"Enter the base of the triangle (decimal):\");
base = sc.nextDouble();
//calculating the area of the triangle
area = 0.5 * base * height;
//calculating the hypotenuse of the triangle
hypotenuse = Math.sqrt(Math.pow(base, 2) + Math.pow(height, 2));
//Calculating the perimeter of the triangle
perimeter = base + height + hypotenuse;
//Displaying the area of the triangle
System.out.printf(\"The area is %.2f\", area);
//Displaying the perimeter of the triangle
System.out.printf(\"\ The perimeter is %.2f\", perimeter);
break;
}
case 5: {
break;
}
}
} while (choice != 5);
}
}
Solution
import java.util.Scanner;
public class Pickashape {
//Declaring constant
public static final double PI = 3.14159;
public static void main(String[] args) {
//Declaring variable
int choice;
//Scanner class Obejct is used to read the inputs entered by the user
Scanner sc = new Scanner(System.in);
//This loop continues to execute until user enters choice \'5\'
do {
//Displaying the menu
System.out.println(\"\ \ Choose the shape\");
System.out.println(\"1. Square\");
System.out.println(\"2. Rectangle\");
System.out.println(\"3. Circle\");
System.out.println(\"4. Triangle\");
System.out.println(\"5. Quit\");
System.out.print(\"Enter your choice:\");
choice = sc.nextInt();
//Based on the User selection the corresponding case will be executed
switch (choice) {
//This case will calculate the area and perimeter of the square
case 1:
//Declaring variables
double side;
//Getting the side of the square entered by the user
System.out.print(\"Enter the side (as a decimal):\");
side = sc.nextDouble();
square(side);// calling square method
break;
case 2:
//Declaring variables
double firstside, secondside;
//Getting the first side of the rectangle
System.out.print(\"Enter the first side (as a decimal):\");
firstside = sc.nextDouble();
//Getting the second side of the rectangle
System.out.print(\"Enter the second side (as a decimal):\");
secondside = sc.nextDouble();
Rectangle(firstside,secondside);
break;
case 3:
//Declaring variables
double radius;
//Getting the radius of the circle
System.out.print(\"Enter the radius (decimal):\");
radius = sc.nextDouble();
Circle(radius);// calling circle method
break;
case 4:
//Declaring variables
double height, base;
//Getting the height of the triangle
System.out.print(\"Enter the height of the triangle (decimal):\");
height = sc.nextDouble();
//Getting the base of the triangle
System.out.print(\"Enter the base of the triangle (decimal):\");
base = sc.nextDouble();
Triangle(height,base); //calling triangle method
break;
case 5:
break;
}
}
} while (choice != 5);
}
public static void square(double side){
double area, perimeter;
//calculating the area of the square
area = side * side;
//calculating the perimeter of the square
perimeter = 4 * side;
//Displaying the area of the square
System.out.println(\"The area is \" + area);
//Displaying the perimeter of the square;
System.out.println(\"The perimeter is \" + perimeter);
}
public static void Rectangle(double firstside, double secondside){
//Declaring variables
double area, perimeter;
//Calculating the area of the rectangle
area = firstside * secondside;
//Calculating the perimeter of the rectangle
perimeter = 2 * firstside + 2 * secondside;
//Displaying the area of the rectangle
System.out.println(\"The area is \" + area);
//Displaying the perimeter of the rectangle
System.out.println(\"The perimeter is: \" + perimeter);
}
public static void Circle(double radius){
///Declaring variables
double area, circumference;
//calculating the area of the circle
area = PI * radius * radius;
//calculating the circumference of the circle
circumference = 2 * PI * radius;
//displaying the area of the circle
System.out.printf(\"The area is %.2f \", area);
//displaying the circumference of the circle
System.out.printf(\"\ The circumference is %.2f \", circumference);
}
public static void Triangle(double height, double base){
//Declaring variables
double area, perimeter, hypotenuse;
//calculating the area of the triangle
area = 0.5 * base * height;
//calculating the hypotenuse of the triangle
hypotenuse = Math.sqrt(Math.pow(base, 2) + Math.pow(height, 2));
//Calculating the perimeter of the triangle
perimeter = base + height + hypotenuse;
//Displaying the area of the triangle
System.out.printf(\"The area is %.2f\", area);
//Displaying the perimeter of the triangle
System.out.printf(\"\ The perimeter is %.2f\", perimeter);
}
}





