Question
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
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 types 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, named FreeIntern.
--Write a class named CommissionIntern with the following features:
1. It extends the Volunteer class.
It will have an additional instance variable (in addition to those inherited) named commission_Rate for the employee (the commission rate will represent the percent (in decimal form) that the employee earns on sales (so .25 would mean the employee earns 25% commission on sales)).
You may want to create an additional instance variable to keep track of the total sales earned.
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 value 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:
1. Increase the size of the array to 10.
Add two interns to the staffListmake 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.
//******************************************************************** // Employee.java Java Foundations // // Represents a general paid employee. //******************************************************************** public class Employee extends StaffMember { protected String socialSecurityNumber; protected double payRate; //----------------------------------------------------------------- // Constructor: Sets up this employee with the specified // information. //----------------------------------------------------------------- public Employee (String eName, String eAddress, String ePhone, String socSecNumber, double rate) { super (eName, eAddress, ePhone); socialSecurityNumber = socSecNumber; payRate = rate; } //----------------------------------------------------------------- // Returns information about an employee as a string. //----------------------------------------------------------------- public String toString() { String result = super.toString(); result += " Social Security Number: " + socialSecurityNumber; return result; } //----------------------------------------------------------------- // Returns the pay rate for this employee. //----------------------------------------------------------------- public double pay() { return payRate; } }
//******************************************************************** // Executive.java Java Foundations // // Represents an executive staff member, who can earn a bonus. //******************************************************************** public class Executive extends Employee { private double bonus; //----------------------------------------------------------------- // Constructor: Sets up this executive with the specified // information. //----------------------------------------------------------------- public Executive (String eName, String eAddress, String ePhone, String socSecNumber, double rate) { super (eName, eAddress, ePhone, socSecNumber, rate); bonus = 0; // bonus has yet to be awarded } //----------------------------------------------------------------- // Awards the specified bonus to this executive. //----------------------------------------------------------------- public void awardBonus (double execBonus) { bonus = execBonus; } //----------------------------------------------------------------- // Computes and returns the pay for an executive, which is the // regular employee payment plus a one-time bonus. //----------------------------------------------------------------- public double pay() { double payment = super.pay() + bonus; bonus = 0; return payment; } }
//******************************************************************** // Firm.java Java Foundations // // Demonstrates polymorphism via inheritance. //******************************************************************** public class Firm { //----------------------------------------------------------------- // Creates a staff of employees for a firm and pays them. //----------------------------------------------------------------- public static void main (String[] args) { Staff personnel = new Staff(); personnel.payday(); } }
//******************************************************************** // Hourly.java Java Foundations // // Represents an employee that gets paid by the hour. //******************************************************************** public class Hourly extends Employee { private int hoursWorked; //----------------------------------------------------------------- // Constructor: Sets up this hourly employee using the specified // information. //----------------------------------------------------------------- public Hourly (String eName, String eAddress, String ePhone, String socSecNumber, double rate) { super (eName, eAddress, ePhone, socSecNumber, rate); hoursWorked = 0; } //----------------------------------------------------------------- // Adds the specified number of hours to this employee's // accumulated hours. //----------------------------------------------------------------- public void addHours (int moreHours) { hoursWorked += moreHours; } //----------------------------------------------------------------- // Computes and returns the pay for this hourly employee. //----------------------------------------------------------------- public double pay() { double payment = payRate * hoursWorked; hoursWorked = 0; return payment; } //----------------------------------------------------------------- // Returns information about this hourly employee as a string. //----------------------------------------------------------------- public String toString() { String result = super.toString(); result += " Current hours: " + hoursWorked; return result; } }
//******************************************************************** // Staff.java Java Foundations // // Represents the personnel staff of a particular business. //******************************************************************** public class Staff { private StaffMember[] staffList; //----------------------------------------------------------------- // Constructor: Sets up the list of staff members. //----------------------------------------------------------------- public Staff () { staffList = new StaffMember[6]; staffList[0] = new Executive ("Tony", "123 Main Line", "555-0469", "123-45-6789", 2423.07); staffList[1] = new Employee ("Paulie", "456 Off Line", "555-0101", "987-65-4321", 1246.15); staffList[2] = new Employee ("Vito", "789 Off Rocker", "555-0000", "010-20-3040", 1169.23); staffList[3] = new Hourly ("Michael", "678 Fifth Ave.", "555-0690", "958-47-3625", 10.55); staffList[4] = new Volunteer ("Adrianna", "987 Babe Blvd.", "555-8374"); staffList[5] = new Volunteer ("Benny", "321 Dud Lane", "555-7282"); ((Executive)staffList[0]).awardBonus (500.00); ((Hourly)staffList[3]).addHours (40); } //----------------------------------------------------------------- // Pays all staff members. //----------------------------------------------------------------- public void payday () { double amount; for (int count=0; count < staffList.length; count++) { System.out.println (staffList[count]); amount = staffList[count].pay(); // polymorphic if (amount == 0.0) System.out.println ("Thanks!"); else System.out.println ("Paid: " + amount); System.out.println ("-----------------------------------"); } } }
//******************************************************************** // StaffMember.java Java Foundations // // Represents a generic staff member. //******************************************************************** abstract public class StaffMember { protected String name; protected String address; protected String phone; //----------------------------------------------------------------- // Constructor: Sets up this staff member using the specified // information. //----------------------------------------------------------------- public StaffMember (String eName, String eAddress, String ePhone) { name = eName; address = eAddress; phone = ePhone; } //----------------------------------------------------------------- // Returns a string including the basic employee information. //----------------------------------------------------------------- public String toString() { String result = "Name: " + name + " "; result += "Address: " + address + " "; result += "Phone: " + phone; return result; } //----------------------------------------------------------------- // Derived classes must define the pay method for each type of // employee. //----------------------------------------------------------------- public abstract double pay(); }
//******************************************************************** // Volunteer.java Java Foundations // // Represents a staff member that works as a volunteer. //******************************************************************** public class Volunteer extends StaffMember { //----------------------------------------------------------------- // Constructor: Sets up this volunteer using the specified // information. //----------------------------------------------------------------- public Volunteer (String eName, String eAddress, String ePhone) { super (eName, eAddress, ePhone); } //----------------------------------------------------------------- // Returns a zero pay value for this volunteer. //----------------------------------------------------------------- public double pay() { return 0.0; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started