COMP 14

advertisement
14-23.1
COMP 14, Spring 2006
Program 7 : Payroll
Due:
April 28, 2006 at 4:30 pm. No late programs will be accepted.
Objectives
Create an abstract class.
Create several classes that inherit from that class
Create multiple objects of different classes
Use random numbers
Experience polymorphism
This program will do a payroll for a company with three kinds of employees. First, salaried
employees who have a fixed annual salary. Each week they receive exactly 1/52 of their annual
salary. Second, hourly employees whose weekly pay is the number of hours they work times their
hourly rate, with time and a half for any hours over 40. And third, commissions employees whose
weekly salary is a fixed base plus a percentage of their sales that week. So we will want three
classes, each with its own set of data and each with its own way of computing salary.
Salary employee
Data: annual salary
Salary computation: annual salary/52
Hourly employee
Data: hours worked, hourly rate
Salary computation: hours*rate for the first 40 hours and
1.5*hours*rate for hours over 40.
Commission employee
Data: weekly base pay, sales, commission rate
Salary computation: base + sales*rate
All employees will have an employee id number (integer), a name (String), and a height (double).
You should create your class so that height can be read or written in either inches or in centimeters,
but make sure that these two values are consistent.
2/12/2016
14-23.2
Program
Step 1. Create an array capable of holding 20 employees. Then fill in the array with randomly
generated employees
id number: same as array index (0…19).
name: randomly chosen from the name array (below).
height: random number of inches chosen in the range 60-84.
employee type: randomly chosen among salary, hourly, and commission
For salary employees:
annual salary: random integer in the range 25000-150000
For hourly employees:
hours: random integer in the range 30-60
hourly rate: random number with 2 decimal places in the range 5.00-15.00
For commission employees:
commission base: random integer 500-1500
sales: random integer in the range 0-10000
commission rate: random number in the range 0.0-0.1
Step 2. Go through the array you created above and generate the payroll information consisting of
the information about each employee (from toString()), and the salary (from getSalary()).
If you set things up properly, the body of the loop to generate the payroll should have only one line
of code!
Your program should require no user interaction.
Using the Random class
First, you have to import java.util.* to make the random number generator available. Then
you create a random number generator by
Random r = new Random();
After that, you can generate (pseudo) random integers with
r.nextInt() or r.nextInt(n) where n is an integer.
Note that the first form will generate integers in the full integer range -231…+231, so you would
have to take the absolute value and then use mod to get the desired range. The second form
generates random integers in the range 0…n-1. So, for example, if you needed random integers in
the range 100-200, you could use
r.nextInt(101)+100
2/12/2016
14-23.3
The method r.nextDouble() returns a random double number in the range from 0.0 up to but
not including 1.0. To round to a number to 2 decimal places, multiply by 100, round (using
Math.round), and then divide by 100.
Classes
First create an abstract parent Employee class that has the data common to all employee types,
reader and writer methods for each of these, a toString method, and an abstract getSalary
method. Then create the three child classes that inherit this parent class, implement the
getSalary method, and override the parent's toString method. You should then be able to
go through the employee array and calculate the salary for each with only one line of code and
without having to know what type of employee your were dealing with (polymorphism in action!).
Specification for Employee class
Constructor
3 parameter: id, name, height in inches
Readers
getName()
getHtInch()
getHtCm()
toString() (returns a string with the id and name)
getSalary() (abstract method that returns a double).
Writers
setName(String s)
setHtInch(double h)
setHtCm(double h)
Specification for the children classes
Constructor
each will have a multi parameter constructor capable of setting all the
data for that particular type of employee. Remember, you can call the parent's
constructor using super, but this has to be the first statement of the child's
constructor.
Readers
getSalary() (Returns the appropriately calculated salary.)
toString() (returns a string with the id, name, and all the other information
required to calculate the salary, but not the salary itself, all
appropriately labeled. You can call the parent's toString method by
super.toString().
Writers
a writer method for each data element appropriate for that class.
2/12/2016
14-23.4
Name array (just copy and paste into your program).
String[] nameList={"Andrew","Erin","Mariatu","Brooke","Hugh",
"Carmen","Alexandria","Effiong","Benjamin","Sean-Michael",
"Margaret","Rafeal","Nicholas","Stephanie","Diana","Justin",
"Catherine","Jaffrey","Aras","Ji","Gretchen","Keith","John",
"Matthew","Emma","Wesley","Sarah","Christopher","Ryan","Phuong",
"Sharath","David","Kerin","Paul","Kevin","Charles","Robert","Jon",
"Tom","Steve","Craig","Justin","Larry","Curly","Moe"};
Hint
To give you some idea of the lengths of the various class definitions, the table below shows the
number of lines of code in my solution to this problem. Comment lines are not included. You
need not try to match these numbers in your program, but this should give you some idea of what's
expected.
Class
name
Employee
HourlyEmp
SalEmp
CommEmp
Total number of
lines
28
25
22
27
Lines containing
only brackets { or }
12
10
10
10
Optional Employee generator (small extra credit)
Create an Employee generator class that has a method getRandomEmployee() that returns a
randomly chosen specific employee object with the various data fields filled in appropriately. If
you do this, then the main method for this program can be less than ten lines long.
Do not attempt this unless you have the basic program running perfectly.
2/12/2016
Download