LAB EXERCISE 17: Area Calculator

Goal: In this exercise, you will learn how to:
1. Insert the Main Method (Main Entry Point to the Java Program)
2   Import a Java Class Library
3.  Create a Superclass
4.  Create Subclasses
5.  Declare Public Variables that can be access by Subclasses
6.  Create Public Void Method that can be access by Subclasses
7.  Use a Try-Catch block statement for error handling and exceptions
8.  Use the This keyword to reference a method in a Superclass
9.  Create the Object from the Class
10.  Call the Object’s Method


Program Specifications:
Create an application that prompts the user to input the height, width and color of a shape.  The program will then calculate and display the area of the shape.

A. Pre-requisites:
1. Create a folder on your desktop Exercise-17

2. Launch Java EE- Eclipse
Note:  You will need to use the Java Perspective Workbench for this exercise

3. Setup your Eclipse Workspace to point to the Exercise-17 folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-17 folder as your Workspace.



B. Requirements:

1.  Create a Java Project and name it as AreaCalculator

2.  Create the first Class that will have the Main Method

a.       Name the Class as MainApp

b.       Choose the main method to insert into the class

         

3.  Create the second Class that will be the Superclass

a.      Name the Class as Shape

b.      Do not select the main method stub

   

4.  Create the first Subclass that will inherit the methods and variables in the Shape class.
Name the Class as Rectangle

5.  Create the second Subclass that will inherit the methods and variables in the Shape class. 
Name the Class as Triangle

 

6.  Create the third Subclass that will inherit the methods and variables in the Shape class. 
Name the Class as Square

 

C. Requirements for the Shape Class:

1.  Insert the import java.util.Scanner class which will allow data to be inputted from the console. 
Insert the line @SuppressWarnings("resource") 

 

2.  Declare the Public variables height and weight using the double data type.
Declare the Public variable color using the String data type
NOTE:  These variables will be accessed by the subclasses

3.  Create the Public Void Method called getData () that that will prompt the user to input the data
Note: This method does not return a value. It just executes code

a.  Use the Try-Catch block statement to perform error handling.

b.  Below is the code to prompt the user to enter the shape’s height, width and color

Scanner inputHeight = new Scanner(System.in);
System.out.print("Enter height of the area (inches): ");
height = inputHeight.nextDouble();

Scanner inputWidth= new Scanner(System.in);
System.out.print("Enter width of the area (inches): ");
width = inputWidth.nextDouble();

Scanner inputColor= new Scanner(System.in);
System.out.print("Enter color of the shape: ");
color = inputColor.nextLine();

 

c.    If the user inputs an invalid data, the code in the Catch block will be executed:

System.out.println("Invalid data entered.")

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


D. Requirements for the Rectangle Class:

1.  The Rectangle class will be a subclass of the Shape parent class (superclass)
NOTE: A subclass will inherit the methods and variables from the superclass

Add the keyword "extends Shape" after the name of class

 

2.  Create the Void Method called getDimensions()

 

3.  Insert the report header

System.out.println("Rectangle Shape Dimensions");

 

4.  Call the this.getData() Method from the parent class- Shape
NOTE:  Use the “this” keyword to call the method

5.  Calculate the area of the shape- double area = height * width

 

6.  Display the results on the screen

// display the results on the screen              
System.out.print("Area of the " + color + " Rectangle shape (inches): ");
System.out.format("%.02f", area);
System.out.println();
System.out.println();

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


E. Requirements for the Triangle Class:

1.  The Triangle class will be a subclass of the Shape parent class (superclass)
NOTE: A subclass will inherit the methods and variables from the superclass

Add the keyword "extends Shape" after the name of class

 

2.  Create the Void Method called getDimensions()

 

3.  Insert the report header

System.out.println("Triangle Shape Dimensions");

 

4.  Call the this.getData() Method from the parent class- Shape
NOTE:  Use the “this” keyword to call the method

5.  Calculate the area of the shape- double area = (height * width)/2;

 

6.  Display the results on the screen

// display the results on the screen              
System.out.print("Area of the " + color + " Triangle shape (inches): ");
System.out.format("%.02f", area);
System.out.println();
System.out.println();

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

F. Requirements for the Square Class:

1.  The Square class will be a subclass of the Shape parent class (superclass)
NOTE: A subclass will inherit the methods and variables from the superclass

Add the keyword "extends Shape" after the name of class

 

2.  Create the Void Method called getDimensions()

 

3.  Insert the report header

System.out.println("Square Shape Dimensions");

 

4.  Call the this.getData() Method from the parent class- Shape
NOTE:  Use the “this” keyword to call the method

5.  Calculate the area of the shape- double area = (height * width);

 

6.  Display the results on the screen

// display the results on the screen  
System.out.print("Area of the " + color + " Square shape (inches): ");
System.out.format("%.02f", area);
System.out.println();
System.out.println();

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


G. Requirements for the MainApp Class:

1.  Add comments (documentation)– Program Description, Author and Date

2.  Create the instance of object called areaRec from the Rectanagle class
Call the object’s method - areaRec.getDimensions()

 

3.  Create the instance of object called areaTri from the Triangle class
Call the object’s method - areaTri.getDimensions()

4.  Create the instance of object called areaSqr from the Square class
Call the object’s method areaSqr.getDimensions()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


H. Test:  

1.   Save your Java code

2.   Compile and run your Java program.

3.   Verify there is no syntax, logical or run-time errors.

4.   Use the following set of test data to determine if the application is running properly

5.   

 

 

 

 

 

 

 

 

 


I. Submit your exercise in the Canvas Lab Exercise #17 Drop Box.

1. Submit the screen shot of the Eclipse Workbench window showing the Console output screen.
You can use Paint (save as JPG) or Word to paste the screenshot.

2. Zip up and submit the compressed AreaCalculator subfolder that is in the Exercise-17 folder.
NOTE: Right click on the
subfolder and select Send to “Compress Folder”.  The file will have a file extension of .zip.