In: Computer Science
Lesson on Inheritance in Java:
Using the payroll weekly paycheck calculator scenario as a model, your code will consist of a superclass (Employee), three subclasses (SalaryEmployee, HourlyEmployee, CommEmployee) and a test class (PayrollTest).
- The superclass should contain fields for common user data for an employee (first name and last name) and a toString method that returns the formatted output of the names to the calling method.
- Three subclasses should be created that all inherit from the superclass: one for a salaried employee, one for an hourly employee and one for a commission employee.
- Subclasses should call the superclass constructor within their constructor to set their instance variables (names).
- Each subclass should have an additional instance variable for the weekly pay that they calculate plus the user input.
- Each subclass should have a toString method that prints the calculated weekly paycheck from the class plus calls the toString method of the superclass to obtain the formatted first and last name.
- The driver class should supply input from the user into the program so that you create one object of each payroll type.
- The superclass should contain a static int variable that keeps a running total of the number of paychecks calculated.
- The final output should print the results PLUS the total number of paychecks calculated. The results must be correct.
The grading rubric is as follows:
Create a superclass that sets the first name and last name as private instance variables and has a toString method that returns a formatted string of the employee’s first and last name
Create 3 subclasses that calculate either a salaried, hourly or a commission paycheck. All subclasses inherit the superclass and contain a toString method that calls the superclass toString method plus adds the weekly paycheck amount to the formatted output supplied by the superclass
Each subclass contains a constructor that calls the super class constructor
Each subclass overrides the toString method in the superclass and adds the weekly paycheck amount to the string it returns
Each subclass should calculate a paycheck (salary, hourly, or commission)
The test class code should create objects of each subclass, call methods to calculate the weekly paychecks and print the results of each object plus the total number of paychecks
Super contains a static int variable for total paychecks
import java.util.Scanner;
class Employee
{
private String firstName,lastName;
public static int numPayChecks = 0;
public Employee(String firstName,String lastName) //
constructor
{
this.firstName = firstName;
this.lastName = lastName;
numPayChecks++;
}
public String toString()
{
return firstName +" "+lastName;
}
public double getPayment()
{
return 0.0;
}
}
class SalaryEmployee extends Employee
{
private double salary;
public SalaryEmployee(String firstName,String lastName,double
salary)
{
super(firstName,lastName); // call to base class constructor
this.salary = salary;
}
public String toString()
{
return super.toString() + String.format(" Salary =
$%.02f",salary);
}
public double getPayment()
{
return salary/12/2;
}
}
class CommissionEmployee extends Employee
{
private double commissionRate;
private double totalSales;
public CommissionEmployee(String firstName,String lastName,double
commissionRate,double totalSales)
{
super(firstName,lastName);
this.commissionRate = commissionRate;
this.totalSales = totalSales;
}
public String toString()
{
return super.toString() + String.format(" Commission = %.02f%% @
$%.02f",commissionRate,totalSales);
}
public double getPayment()
{
return commissionRate*totalSales;
}
}
class HourlyEmployee extends Employee
{
private double rate;
private double hours;
public HourlyEmployee(String firstName,String lastName,double
rate,double hours)
{
super(firstName,lastName);
this.rate = rate;
this.hours = hours;
}
public String toString()
{
return super.toString() + String.format(" Hourly = $%.02f @ $%.02f
hours",rate,hours);
}
public double getPayment()
{
return rate * hours;
}
}
class PayrollTest
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
Employee[] e = new Employee[3];
System.out.println("Enter the firstname and lastname of salary
employee : ");
String firstName = input.next();
String lastName = input.next();
System.out.println("Enter his salary : ");
double salary = input.nextDouble();
e[0] = new SalaryEmployee(firstName,lastName,salary);
System.out.println("Enter the name of commission employee :
");
firstName = input.next();
lastName = input.next();
System.out.println("Enter his commission rate : ");
double commRate = input.nextDouble();
System.out.println("Enter total sales : ");
double totalSales = input.nextDouble();
e[1] = new CommissionEmployee(firstName,lastName,commRate,totalSales);
System.out.println("Enter the name of hourly employee :
");
firstName = input.next();
lastName = input.next();
System.out.println("Enter his hourly rate : ");
double hourlyRate = input.nextDouble();
System.out.println("Enter hours : ");
double hours = input.nextDouble();
e[2] = new
HourlyEmployee(firstName,lastName,hourlyRate,hours);
for(Employee worker : e)
{
System.out.println(worker);
System.out.printf("Paycheck : $%.02f%n",worker.getPayment());
}
System.out.println("Number of Pay Checks :
"+Employee.numPayChecks);
}
}
Output:
Enter the firstname and lastname of salary employee : Meg Manager Enter his salary : 50000.00 Enter the name of commission employee : Sal Salesman Enter his commission rate : 0.15 Enter total sales : 3400 Enter the name of hourly employee : Timmy Temp Enter his hourly rate : 10.5 Enter hours : 25 Meg Manager Salary = $50000.00 Paycheck : $2083.33 Sal Salesman Commission = 0.15% @ $3400.00 Paycheck : $510.00 Timmy Temp Hourly = $10.50 @ $25.00 hours Paycheck : $262.50 Number of Pay Checks : 3
Do ask if any doubt. Please upvote.