Lab 2: Another Employee Type The files Firm.java, Staff.java, StaffMember.java,
ID: 3669355 • Letter: L
Question
Lab 2: Another Employee Type
The files Firm.java, Staff.java, StaffMember.java, Volunteer.java, Employee.java, Executive.java, and Hourly.java are from Listings 9.1 – 9.7 in the text. The program illustrates inheritance and polymorphism. In this exercise you will add two more employee type to the class hierarchy (see Figure 9.1 in the text). One of the new employee types will be an intern, but earns a commission on sales. Hence the class, which we'll name CommissionIntern, will be derived from the Volunteer class. The second employee type will also be an intern but is not paid.
--Write a class named Commission with the following features:
It extends the Volunteer class.
It has one instance variable (in addition to those inherited - commission rate for the employee (the commission rate will be type double and will represent the percent (in decimal form) commission the employee earns on sales (so .2 would mean the employee earns 20% commission on sales)).
The constructor takes 4 parameters: the first 3 are the same as for Volunteer (name, address, phone number) and the 4th is the commission rate for the employee. The constructor should call the constructor of the parent class with the first 3 parameters then use the 4th to set the commission rate.
One additional method is needed: public void addSales (double totalSales) that adds the parameter to the instance variable representing total sales.
The pay method must call the pay method of the parent class to compute the pay from commission on sales. (See the pay method in the Executive class.) The total sales should be set back to 0.
The toString method needs to call the toString method of the parent class then add the total sales to that.
--Write a second class named FreeIntern using the same ideas as above, but be sure that it fits the scenario.
To test your classes, update Staff.java as follows:
Increase the size of the array to 10.
Add two interns to the staffList—make up your own names, addresses, and phone numbers. Have one of the employees earn a 10% commission and the other one earn a15% commission.
For the first additional employee you added, put the total sales $150; for the second, put the sales at $95.
Add two non-paid interns to the staffList
Be sure that you test your program, and that you have adequate output that shows that your code is working.
Explanation / Answer
//Firm.java
public class Firm
{
public static void main( String arguments[] )
{
Staff workforce =new Staff () ;
workforce.paymentday () ;
}
}
//Staff.java
public class Staff
{
StaffMember[] staffList ;
public Staff()
{
staffList =new StaffMember[6] ;
staffList[0] =new Executive( " Hem ", " 092 Gajuwaka ", "888-5111", "012-125-5544", 545.455 ) ;
staffList[1] =new Employee( "Raju", "125 Old Gajuwka", "888-1110", "741-682-5545", 156.545 ) ;
staffList[2] =new Employee( "Niru", "458 New Gajuwaka", "888-0164", "741-682-5546", 056.654 ) ;
staffList[3] =new Hourly( "Dinakar", "012 BC Road", "888-4542", "741-682-5547", 105.54 ) ;
staffList[4] =new Volunteer( "Venkat", "652 Nagar", "888-5400" ) ;
staffList[5] =new Volunteer("Ram", "653 Laptopy", "888-5404" ) ;
( ( Executive ) staffList[0] ).giftedRewarded( 500.00 ) ;
( ( Hourly )staffList[3] ).overTimeHourly( 40 ) ;
}
public void paymentday ()
{
double money ;
for( int total = 0 ; total < staffList.length ; total++ )
{
System.out.println( staffList[total] ) ;
money =staffList[total].pay () ;
if( money == 0.0 )
System.out.println( " Thanks... " ) ;
else
System.out.println( " Paid : " + money ) ;
System.out.println( "*******************************" ) ;
}
}
}
//StaffMember.java
abstract public class StaffMember
{
protected String name ;
protected String address ;
protected String phone ;
public StaffMember( String empName, String empAddress, String empPhone )
{
name =empName ;
address =empAddress ;
phone =empPhone ;
}
public String toString ()
{
String outcome =" Name : " + name + " " ;
outcome +=" Address : " + address + " " ;
outcome +=" Phone : " + phone ;
return outcome ;
}
public abstract double pay () ;
}
//Volunteer.java
public class Volunteer extends StaffMember
{
public Volunteer( String empName, String empAddress, String empPhone )
{
super( empName, empAddress, empPhone ) ;
}
public double pay ()
{
return 0.0 ;
}
}
//Employee.java
public class Employee extends StaffMember
{
protected String socialSecretNumber ;
protected double assignedRateSal ;
public Employee( String empName, String empAddress, String empPhone, String socialSecNumber, double rate )
{
super( empName, empAddress, empPhone ) ;
socialSecretNumber =socialSecNumber ;
assignedRateSal =rate ;
}
public String toString ()
{
String outcome =super.toString () ;
outcome +=" Social Security Number : " + socialSecretNumber ;
return outcome ;
}
public double pay ()
{
return assignedRateSal ;
}
}
//Executive.java
public class Executive extends Employee
{
private double reward ;
public Executive( String empName, String empAddress, String empPhone, String socialSecNumber, double rate )
{
super( empName, empAddress, empPhone, socialSecNumber, rate ) ;
reward =0 ; // reward has yet to be awarded
}
public void giftedRewarded( double execReward )
{
reward =execReward ;
}
public double pay ()
{
double salary =super.pay () + reward ;
reward =0 ;
return salary ;
}
}
//Hourly.java
public class Hourly extends Employee
{
private int wordedHourly ;
public Hourly( String empName, String empAddress, String empPhone, String socialSecNumber, double rate )
{
super( empName, empAddress, empPhone, socialSecNumber, rate ) ;
wordedHourly =0 ;
}
public void overTimeHourly( int overTimeHour )
{
wordedHourly +=overTimeHour ;
}
public double pay ()
{
double salary =assignedRateSal * wordedHourly ;
wordedHourly =0 ;
return salary ;
}
public String toString ()
{
String outcome =super.toString () ;
outcome +=" Current hours : " + wordedHourly ;
return outcome ;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.