LAB
EXERCISE 12: Payroll
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. Declare Instance and Static Variables
4. Create a Constructor (Special Method)
6. Create a Void Method
7. Create an Array to
store data of the same data type in memory
8. Use the For Loop to
allow the iteration and the repetitive execution of code
9. Create an Object from the Class
10. Call an Object’s Method
Program
Specifications:
Description:
LMC House of Pizza has 5 employees, each paid $14 per hour. Create an
application that will prompt the user to input the hours worked and calculate
gross pay of the 5 employees using the formula gross pay= hours worked * hourly
rate.
Use the For Loop to input the the data and calculate the
gross pay. The employee’s gross pay is then stored in an array and displayed on
the console.
A.
Pre-requisites:
1.
Create a folder on your desktop Exercise-12
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-12
folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-12 folder as your Workspace.
B. Requirements:
1. Create
a Java Project and name it as Payroll
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 have the Constructor and Methods
a.
Name the Class as GrossPay
b.
Do not select the main method stub
C. Requirements
for the GrossPay Class:
1. Insert
the import java.util.Scanner class
which will allow data to be inputted from the console.
2. a.
Declare the variable- hoursWorked
using the double data type
b. Declare the final static variable HOURL_PAY_RATE=14 using
the double data type
c. Declare the final static variable MAX_EMPLOYEES= 5 using
the integer data type
3. Declare
the array employeePayArray[] using the double data type
4. Create
the Constructor (special method) called GrossPay() that
will prompt the user to input the data
a. Declare
the scanner input class objects - Scanner inputEmpHours = new Scanner(System.in)
b. Used
the For Loop to perform the following iteration:
1. Prompt the user to input the hours
worked for each 5 employees
2. Calculate their gross pay
3. Store the employee’s gross pay in the
array.
c. Close the scanner input class object - inputEmpHours.close()
5. Create
the Void Method called displayPayroll() that will
display the gross pay of each
employee.
a. Use the For Loop to perform the iteration of displaying the
results on the console
b. Below is the code you can copy and
paste in the For Loop to display the output
System.out.print("Employee
" + (i+1) + " earned: "); |
D. Requirements for the MainApp Class:
1. Add
comments (documentation)– Program Description, Author and Date
2. Create
the instance of object called employee from the GrossPay class
3. Call
the object’s method to display the employee’s gross pay.
E.
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 calculating properly:
Array Position Employee Hours
Worked Gross Pay
0 1 10 $140
1 2 25 $350
2 3 20 $280
3 4 15 $210
4 5 30 $420
F. Submit your exercise in the Canvas Lab
Exercise #12 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 Payroll subfolder that is in the Exercise-12
folder.
NOTE: Right click on the subfolder and select
Send to “Compress Folder”. The file will
have a file extension of .zip.