COMP 14

advertisement
110-29.1
COMP 110, Spring 2009
Java Program 2: Payroll 2
Due:
April 22, 2009 at 4:30 pm.
Objectives
Create an abstract class.
Create several classes that inherit from that class
Create multiple objects of different classes
Use random numbers
Experience polymorphism
Cut and paste1
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.
Employee class
Data: ID, name, and height
Readers and writers for all three
Abstract method for getting the 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
So, for example, if Joe's base is $500, his sales are $2000 and his
commission rate is 0.05, then his salary this week is
500+(2000*0.05) == $600. My version of this class is 32 lines long, of which
8 lines are just brackets; 6 lines are comments, and 4 lines are blank.
That leaves only 12 lines with real code.
1
Before getting too uptight about this assignment, read on. Almost all of the work has been done for you.
All you'll need to do is to copy code from this document and paste it into jGrasp windows and then write
one class yourself.
110-29.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 the array index (0…19).
name: randomly chosen from the name array from the last program
height: random number of inches chosen in the range 60-85.
employee type: randomly chosen among salary, hourly, and commission
For salary employees:
annual salary: random integer in the range 25000-125000
For hourly employees:
hours: random integer in the range 20-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()).
Your program should require no user interaction.
Classes
Most of the work has been done for you. 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 appropriate getSalary method, and override the parent's
toString method. The HourlyEmployee and SalaryEmployee classes can just be
copied and pasted. You will need to write the CommittionEmployee class. The
EmployeeGenerator class has a method that returns a randomly generated employee.
Once you have copied all the classes (each to its own file) and written the
CommissionEmployee class, you should be able to run the Main class. Notice that you can 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!).
What's important in this assignment is that you understand what's going on!
110-29.3
public abstract class Employee
{
private int idNumber;
private String name;
private double htInch;
// Constructor
Employee (int id, String n, double ht)
{
idNumber=id;
name=n;
htInch=ht;
}
// Readers
public int getId(){return idNumber;}
public String getName()
{return name;}
public double getHtInch() {return htInch;}
public double getHtCm()
{return htInch*2.54;}
public String toString()
{
return idNumber +" "+ name;
}
// Writers
public void setName(String n) {name=n;}
public void setHtInch(double h) {htInch=h;}
public void setHtCm(double h) {htInch=h/2.54;}
// Abstract salary reader
public abstract double getSalary();
}
110-29.4
public class
{
private
private
private
HourlyEmployee extends Employee
int hours;
// Hours worked
double wage;
// Hourly wage
final int STD_WEEK=40; // Standard work week.
// Constructor
HourlyEmployee(int id, String n, double h,
int hrs, double w)
{
// Use parent constructor to set the first three
// variables.
super(id,n,h);
// Set the remaining two variables.
hours=hrs;
wage=w;
}
// Salary conputation
public double getSalary()
{
return Math.min(hours,STD_WEEK)*wage +
Math.max(0.0, hours-STD_WEEK)*1.5*wage;
}
// Override toString
public String toString()
{
return "Hourly "+super.toString();
}
}
110-29.5
public class SalaryEmployee extends Employee
{
private double salary;
// Annual salary
// Constructor
SalaryEmployee(int id, String n, double h,
double s)
{
// Use parent constructor to set the first three
// variables.
super(id,n,h);
// Set the remaining variable.
salary=s;
}
// Salary computation
public double getSalary()
{
return salary/52;
}
// Override toString
public String toString()
{
return "Salary "+super.toString();
}
}
110-29.6
// Randomly generate Employee objects.
import java.util.*;
public class EmployeeGenerator
{
Random r; // Reference to random number generator.
String[] nameList=
{"Kyle","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", "Tyler", "Ty", "Wayne"};
// Constructor: get a random number generator.
EmployeeGenerator()
{
r = new Random();
}
// Return a randomly generated employee with the
// specified id number.
public Employee getNextEmployee(int id)
{
int x=r.nextInt(3); // Generate 0, 1, or 2.
// Get random name and height.
String name=nameList[r.nextInt(nameList.length)];
double ht= r.nextDouble()*25.0+60.0;
// Get appropriate attributes for each employee type.
switch(x)
{
case 0:
// Hourly
return new HourlyEmployee(id, name, ht,
r.nextInt(40)+20,
r.nextDouble()*10.0+5.0);
case 1:
// Salary
return new SalaryEmployee(id, name, ht,
r.nextDouble()*100000.0+25000.0);
default: // Commission
return new CommissionEmployee(id, name, ht,
r.nextInt(1000)+500, // Base salary
r.nextInt(10000),
// Sales
r.nextDouble()/10.0); // Comm rate
}
} // End of switch.
// End of getNextEmployee
110-29.7
}
// End of class
110-29.8
import java.text.*;
public class Main
{
public static void main(String[] args)
{
// Set up number format.
NumberFormat f=NumberFormat.getInstance();
f.setMaximumFractionDigits(2);
f.setMinimumFractionDigits(2);
// Create an array of Employee references.
// Note that we cannot create an
// Employee object. Why is that? <-Good final exam question
Employee[] eList=new Employee[20];
// Create Employee generator.
EmployeeGenerator eGen=new EmployeeGenerator();
// Populate Employee array randomly.
for (int i=0;i<eList.length;i++)
eList[i]=eGen.getNextEmployee(i);
// Display payroll information. Note polymorphism!
for (int i=0;i<eList.length;i++)
System.out.println(eList[i]+" "+
f.format(eList[i].getSalary()));
} // End of main
}
Sample output
Hourly 0 Mariatu 562.68
Hourly 1 Kyle 314.19
Hourly 2 Justin 427.45
Commission 3 Ryan 727.79
Hourly 4 Sharath 485.08
Salary 5 Benjamin 1,700.43
Salary 6 Hugh 2,160.00
Salary 7 Diana 857.23
Salary 8 Matthew 2,064.18
Salary 9 Robert 1,990.03
Commission 10 Jaffrey 1,461.82
Salary 11 Nicholas 1,633.35
Salary 12 Phuong 1,287.06
Salary 13 Larry 1,017.08
Hourly 14 Rafeal 221.94
Salary 15 Kevin 1,387.75
Hourly 16 Steve 360.93
Hourly 17 Larry 351.47
Hourly 18 Phuong 531.20
Commission 19 David 1,269.86
Download