Need Help,! Using only import java.util.Scanner; The solution you developed for
ID: 3729112 • Letter: N
Question
Need Help,!
Using only
import java.util.Scanner;
The solution you developed for PLP02 found the area and perimeter for the four shapes we have investigated so far, but what if the user wanted to find those properties for a single shape rather than all four? The solution developed in PLP02 can be visually confusing (not to mention annoying if the user was indeed interested in only a single shape). Modify your solution for PLP02 to allow the user to choose the shape to compute and report. Changing the "look and feel" of the program enhance the user experience by allowing her/him to focus on the information needed. There are several selection structure implementations (if-else, switch) that can be used in the solution. The user should be provided with a "menu of choices" that will allow them to identify the shape they wish to work with. 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
Thank you
Procedure:
Locate your PLP02.java source file in your files, make a copy and rename it xxPLP04.java
Move it into Eclipse and begin the necessary modifications.
Add the comment // modified by: ... and supply your name and the current date.
Save the program with the file name xxPLP04.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 PLP02.java for this assignment. The original should be stored on your flashdrive.
Use the same "prompt and respond" dialog from your version of PLP02.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 (preferably of the type double) 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
import java.util.Scanner;
public class DAPLP02 {
public static void main(String[] args) {
double side1 = 0.0;
double side2 = 0.0;
double radius = 0.0;
double base = 0.0;
double height = 0.0;
final double PI = 3.14159;
System.out.print("Enter the first side ");
Scanner inData; // A class that reads input
inData = new Scanner(System.in);
System.out.print(" Enter the length of a side: ");
side1 = inData.nextDouble();
System.out.print(" Enter the length of second side ");
side2 = inData.nextDouble();
System.out.println(" The area is " + side1 * side2 + " ");
System.out.println("The perimeter is " + 2 * (side1 + side2));
// Student fill in the rest to compute the area and circumference of
// a circle and the area and perimeter of a right angled triangle
System.out.print(" Enter the radius (decimal): ");
radius = inData.nextDouble();
System.out.println("The area is:" + (PI*radius*radius));
System.out.println("The circumference is: " + (2*PI*radius));
System.out.print(" Enter the height of the triangle (decimal): ");
height = inData.nextDouble();
System.out.print("Enter the base of the triangle (decimal): ");
base = inData.nextDouble();
System.out.println("The area is: " + (0.5*base*height));
System.out.println("The perimeter is " + (base+height+Math.sqrt(base*base+height*height)));
}
}
Explanation / Answer
Following is the full java code as per the Question:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.