Lab 2: Mangle the Triangle

Due by the end of class

Your mission adds input and some simple calculations to the lab from last week. You must implement a simple program that asks the user for a width and a height, storing them as double values. Afterwards, you compute area and perimeter as if the shape were a rectangle. Output the results. Then, you compute the area and perimeter as if the shape were a right triangle and you have been given the base and height. Output the results. Make sure that the output is formatted nicely.

Don't be afraid to ask for help. We are still just trying to get you comfortable with basic features of Java and IntelliJ.

Specification

Create a project called Lab2. Add a class called Shapes. Complete the description of the program given above so that your output looks as close to the following sample output as possible. In this sample, the user entered 3 and 5, respectively. User input is shown in green, like in IntelliJ.

           
Area and Perimeter Calculator

Enter width: 3
Enter height: 5

If your shape is a rectangle, its area is 15.0
Its perimeter is 16.0

If your shape is a triangle, its area is 7.5
If it is a right triangle, its perimeter is 8.0 + square root of 34.0

Here is another sample, in which the user entered 6.2 and 9.7, respectively.


Area and Perimeter Calculator

Enter width: 6.2
Enter height: 9.7

If your shape is a rectangle, its area is 60.14
Its perimeter is 31.799999999999997

If your shape is a triangle, its area is 30.07
If it is a right triangle, its perimeter is 15.899999999999999 + square root of 132.53

Use the Scanner as we discussed in class to read the two double values width and height.

Formulas

A key programming skill is learning how to convert from a mathematical equation to its equivalent in Java. Below I give the formulas, not the equivalent lines of Java. Remember that you must use the * operator to do multiplication. Simply putting two values next to each other is insufficient. There is also no "raise to the power of" operation. You only need to square numbers, and that is easily done with multiplication. Finally, multiplying by 0.5 is not always the same as dividing by 2. Because they are both integers, the quantity 1/2 is actually equal to 0 in Java.

The formula for the area of a rectangle is A = wh. The formula for the perimeter of a rectangle is P = 2w + 2h.

Recall that the formula for the area of a triangle is A = 1/2 bh. Treat width as the base of the triangle. As implied above, it's better to multiply by 0.5 to find the area of the triangle instead of dividing by 2. If you store the area of a triangle, store it into a double value.

The formula for the perimeter of a triangle is P = a + b + c, where a, b, and c are the three sides of the triangle. With a right triangle, you can find the hypotenuse (third side c ) using the following equation: c 2 = a 2 + b 2. Next week, we will learn to compute the square root using Java. For now, just follow the example above and print out square root of with the appropriate squared value afterwards.

Turn In

Turn in your code by uploading Shapes.java from the Lab2\src folder wherever you created your project to Blackboard. Do not upload the entire project. I only want the Shapes.java file.

All work must be done individually. Never look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.