Question
In Java Add a static method which computes the area of several common shapes: triangle, rectangle and parallelogram. The class should have a single static
In Java
Add a static method which computes the area of several common shapes: triangle, rectangle and parallelogram. The class should have a single static method called printArea that takes in three parameters: an enum for the shape type, a double for the base and a double for the height.
Your program will first ask the user to type in a shape (square, parallelogram, triangle) , base and height via the console. Then, use an IF-ELSE statement to call the printArea() method with the appropriate parameters.
As a reminder, the equations for area for the above shapes is: areasquare = bh
areaparallelogram = bh areatriangle = bh*(1/2)
Your printArea method will be static, so you should be able to call it within your main() method. For example, you may call your method using the following code: printArea(RECTANGLE, 2.5, 3.2);, which may yield the following output: The rectangle with base of 2.5 and height of 3.2 has an area of 8.0.
Sample Output
Please enter a shape (e.g. Square, Parallelogram, Triangle): Parallelogram Please enter the base value: 2.5 Please enter the height value: 4.6 The parallelogram with base of 2.5 and height of 4.6 has an area of 11.5
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started