Need Help using only import java.util.Scanner; Problem Statement The solution yo
ID: 3735457 • Letter: N
Question
Need Help using only import java.util.Scanner;
Problem Statement
The solution you developed for PLP04 found the area and perimeter for any of the four shapes we have developed so far, the problem however is that we can only do one shape per program execution. We have to "restart" the program if we want to do another shape (or enter different dimensions for the same shape). Modify your solution for PLP04 to allow the user to do find properties of more than one shape without having to restart the program. As with PLP04, the user should be provided with a "menu of choices" that will allow them to identify the shape they wish to work with. This solution should however, provide the user with the menu after each solution is found. The user can exit the program by choosing option 5 (the Quit option). A typical session might look like this:
Choose the shape
1. Square
2. Rectangle
3. Circle
4. Triangle
5. Quit
Enter your choice: 1
Enter the side (as a decimal): 10.5
The area is 110.25
The perimeter is: 42.0
Choose the shape
1. Square
2. Rectangle
3. Circle
4. Triangle
5. Quit
Enter your choice: 1
Enter the side (as a decimal): 5.5
The area is 30.25
The perimeter is: 22.0
Choose the shape
1. Square
2. Rectangle
3. Circle
4. Triangle
5. Quit
Enter your choice: 5
Thank you
Procedure:
Locate your PLP04.java source file in your files and edit with Eclipse.
Add the comment // modified by: ... and supply your name and the current date.
Save the program with the file name xxPLP05.java where xx are your initials. Be sure to change the class name to this as well (to avoid the run-time error "class not found" message).
Modify the program as indicated in the Problem Statement above.
Compile the program and correct any syntax errors detected by the compiler.
When the syntax errors have been corrected, test the program in Eclipe. Before testing, on paper, create test data and perform the computations manually. These will serve as your control for testing.
Compare the results produced by your program with your hand derived results.
Using the commenting facility, put your test data at the end of the program file.
Submit the source program file through this Canvas dropbox for evaluation, feedback and credit.
Notes:
Modify your copy of PLP04.java for this assignment. The original should be stored on your flashdrive.
Use the same "prompt and respond" dialog from your version of PLP04.java to collect the data and report the results for each of the other three shapes. A refresher:
Should the user choose the rectangle (2):
Enter the first side (as a decimal): 10.5
Enter the second side (as a decimal): 15.5
The area is 162.75
The perimeter is: 52.0
Should the user choose the circle (3)
Enter the radius (decimal): 30.2
The area is:2,865.2557
The circumference is: 189.752
Should the user choose the triangle:
Enter the height of the triangle (decimal): 3.0
Enter the base of the triangle (decimal): 4.0
The area is: 6.0
The perimeter is 12.0
When deciding on the data type to use for your sides (rectangle), radius (circle), base and height (triangle) consider how you will use them and the numeric types that will be used in the computations.
Declare PI as a named constant and assign it the value of 3.14159 at the time of declaration.
Notice that the example above strongly suggests that your program will require a series of interactions with the user in order to gather data for each set of shape calculations.
Assume you will be working with a right triangle (a triangle of 90 degrees).
Some calculations you may find useful:
for the perimeter of a rectangle:
perimeter = 2 * (side1 + side2)
for the circumference of a circle:
circumference = 2 * PI * radius
for the perimeter of a triangle:
perimeter = base + height + hypotenuse
to find the hypotenuse of the right triangle:
hyptenuse2 = base2 + height2
Deliverables:
The source file for problem, xxPLP05.java (where xx are your initials), with the test data given as comments at the end of the file.
Explanation / Answer
import java.util.Scanner;
public class CHPLP05 {
public static void main(String args[]) {
final double PI = 3.14159;
Scanner in = new Scanner(System.in);
int choice;
do {
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 = in.nextInt();
switch (choice) {
case 1: {
double side;
System.out.print("Enter the side (as a decimal): ");
side = in.nextDouble();
System.out.println("The area is: " + side * side);
System.out.println("The perimerter is: " + 4 * side);
}
break;
case 2: {
double length, width;
System.out.print("Enter the first side(as a decimal): ");
length = in.nextDouble();
System.out.print("Enter the second side(as a decimal): ");
width = in.nextDouble();
System.out.println("The area is: " + length * width);
System.out.println("The perimerter is: " + 2 * (length + width));
}
break;
case 3: {
double radius;
System.out.print("Enter the radius (decimal): ");
radius = in.nextDouble();
System.out.println("The area is: " + PI * radius * radius);
System.out.println("The circumference is: " + 2 * PI * radius);
}
break;
case 4: {
double height, base, hyp;
System.out.print("Enter the height of the triangle (decimal): ");
height = in.nextDouble();
System.out.print("Enter the base of the triangle (decimal): ");
base = in.nextDouble();
hyp = base + height;
System.out.println("The area is: " + (height + base) / 2);
System.out.println("The perimeter is: " + height + base + hyp);
}
break;
case 5:
System.out.println("Thank you!");
System.exit(1);
break;
default:
System.out.println("Invalid choice...");
}
} while (choice != 5);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.