LAB
EXERCISE 20: Paint Job Estimator
Goal:
In this exercise, you will learn how to:
1. Create Packages containing
Classes
2. Insert the Main Method (Main Entry
Point to the Java Program)
3 Import a Java Class Package and Java
Library
4. Create a Superclass
5. Create Subclasses
6. Declare Public Variables
that can be accessed by a Subclass
7. Declare Constant Variables
8. Create a Constructors
9. Use for the For Loop and
running total variables
10. Use a Try-Catch block statement for error
handling and exceptions
11. Create the Object from the Class
12. Call the Object’s Variables
Program Specifications:
Create
an application that allows the user to enter the number of rooms to be painted
and the price of the paint per gallon.
Assume for every 115 square feet of wall space that one gallon of paint and
eight hours of labor are required. The
hourly wage rate is $18per hour.
The application should display the following information:
1) Number of gallons of painted requires
2) Hours of labor required
3) Cost of the paint
4) Labor charges
5) Total cost of the paint job.
A.
Pre-requisites:
1. Create a folder on your desktop Exercise-20
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-20
folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-20 folder as your Workspace.
B. Requirements:
1. Create
a Java Project and name it as PaintJobEstimator
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 1st Package called residentialPaintJobs and create two Classes in the package:
a.
Create a class called RoomsPainted. This will be the superclass (parent) of the
Cost class
b. Create a class called Cost. This subclass will inherit the methods and
variables of the RoomsPained superclass
4. Create
the 2nd Package called buildingPaintJobs and create two Classes in the package:
a.
Create a class called RoomsPainted. This will be the superclass (parent) of the
Cost class
b. Create a class called Cost. This subclass will inherit the methods and
variables of the RoomsPained superclass
5. Below is the
folder structure of the Project:
C. Requirements
for the RoomsPainted Class in the residentialPaintJobs Package:
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 numRooms and pricePerGallon using the
double data type.
NOTE: These variables will be accessed
by the subclass
3. Create
the Constructor called RoomsPainted() that that will prompt the
user to input the data
a. Use a try-catch
block to perform error handling if invalid data is inputted.
//
declare the scanner object that will be used to input the number of rooms to
be painted |
b. In the catch block,
display “Invalid data entered” message
System.out.println("Invalid Data
Entered") |
D. Requirements
for the Cost Class in the residentialPaintJobs Package:
1. Insert
the import java.util.Scanner class
which will allow data to be inputted from the console.
2. The Cost class will be a subclass of the RoomsPainted parent class (superclass)
NOTE: A subclass will inherit the methods and variables from the superclass
Add the
keyword "extends RoomsPainted" after the name of class
3. Declare
the local variables squareFeet and totalSquareFeet
using the double data type
4. Declare
the public variables as double that will be called from the object in the MainApp
totalGallonsReq, totalHoursLaborReq, costOfPaint, laborCost and totalCost
5. Declare
the local static constant variables as double and initialize
HOURLYRATE = 18,
WALLSPACE = 115, LABORHOURS = 8
6. Create
the Constructor called Cost () and declare as public. This special method will prompt the user to
input the data
a. Declare the scanner object used to
input the square feet for each room
Scanner
input1= new Scanner(System.in) |
b. Use the For Loop to enter the square feet for each room and
accumulate a running total of the square feet of rooms to paint
System.out.print("Enter the
square feet of room " + (i+1) + " :"); //
running total of the square feet of room(s) to paint |
c.
Close the scanner object: input1.close()
d. Calculate the total paint gallons required:
totalGallonsReq = totalSquareFeet/ WALLSPACE
e. Calculate the labor hours required to paint
all rooms:
totalHoursLaborReq = (totalSquareFeet/WALLSPACE) *
LABORHOURS
f.
Calculate the paint and labor cost and total
cost:
costOfPaint = totalGallonsReq * pricePerGallon
laborCost = totalHoursLaborReq * HOURLYRATE
totalCost = costOfPaint + laborCost
E. Requirements for the MainApp Class in the default
package:
1. Add
comments (documentation)– Program Description, Author and Date
2. Import
the Residential package containing the classes: import
residentialPaintJobs.*
3. Create the instance of the object - paintjob from the
Cost class: Cost paintJob =
new Cost()
4. Display the required results on the console
System.out.println(); |
F. 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
G. Submit your exercise in the Canvas Lab
Exercise #20 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 PaintJobEstimator subfolder that is in the Exercise-20
folder.
NOTE: Right click on the subfolder and select
Send to “Compress Folder”. The file will
have a file extension of .zip.