Question
Wrie an application program in Java caleed ShapesApp to develop an application to determine the area of a shape according to the following: a-
Wrie an application program in Java caleed " ShapesApp" to develop an application to determine the area of a shape according to the following:
a- Define enumerated type to define shapes as follows:
[Initial ShapesApp image] 1 package shapesapp; 2 3 import java.util.Scanner; 4 5 enum Shapes { CIRCLE, SQUARE, RECTANGLE, TRIANGLE, UNKNOWN }; 6 7 public class ShapesApp 8 { 9 public static void main( String[] args ) 10 { 11 } 12 }
b- define a static variable called PI
this we add the following line: static final double PI = 3.14159; within the ShapesApp class, but outside the main method. (at line 9).
c-Define Instance Variables of parameters that will be used the application as follows:
. One variable of type Scanner for reading user input from the keyboard One variable of type Shapes for storing the shape type One String for storing the desired user shape Three variables of type int for assigning the Shape Length, Width and Radius One variable of type double for storing the shape calculate area
d- Prompt the user to enter the type of shape you would like to calculate the area for. This user entry should be stored in the String variable you defined in Task C
e-Define method for determining the proper shape and returning the value of the enumerated type which matches. This method should be defined as a static variable that accepts one parameter of type String and returns a value of type Shapes.
static Shapes getShape( String input ) The String variable that is passed into the method is an object that contains methods. Once such method is called equalsIgnoreCase(), which can be used for determining if a specific string was entered, such as: if ( input.equalsIgnoreCase( "Circle" )) { return Shapes.CIRCLE; // Return the enumerated type CIRCLE } [Add else if statements for determining if the user entered either SQUARE or RECTANGLE if they did not enter CIRCLE. If none of these three shapes were not entered, the method should return UNKNOWN.
f-To use our new method, we need to call it after we stored the shape entered by the user, in Task 5E. To call a method we need invoke it by entering the method name, and passing the user string into it. getShape(
g- Define a switch statement using the
h- Within the switch statement, you need to develop a case entry for each of the three shapes in the enumerated types. Under each of these case entries, the application needs to prompt for the information they need to determine the area of the specific shape. For Circle, the user should prompt for the radius. For Rectangle, the user should prompt for length and width. For Square, the user should prompt for length.
i- We now need to define another method which determines the area of a circle. This method will be called area, which will accept one input parameter which contains the radius and returns a double which corresponds to the area of a circle. static double area( int radius )
j-We have already defined a method called area for determining the area of a circle. We now need to define a second method called area. Java allows us to define methods of duplicate names as long as the parameter lists are different. The list of parameters of the method is called the method signature. static int area( int length, int width ) Defining a method of a duplicate names but with different parameters is called overloading.
k-The case block for circle needs to call the method area passing in the entered radius and storing the returned double result into the area variable. The application should now display the area of the circle. Enter Shape ( Circle, Square, Rectangle ): Circle Enter Radius: 5 Area of Circle is: 78.53975 [Note #1] When a variable is used, the storage type is used to determine what precision should be used. In the case of displaying the area of the circle, the storage area is a double so the output statement will display digits right of the decimal point. System.out.println( "Area of Circle is: " + shapeArea );
l-case block for rectangle and square needs to call the method area passing in the entered lengths of the appropriate shape. Enter Shape ( Circle, Square, Rectangle ): rectangle Enter Width: 4
.
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