Question

In: Computer Science

Lesson on Inheritance in Java: Using the payroll weekly paycheck calculator scenario as a model, your...

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

Solutions

Expert Solution

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.


Related Solutions

This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface called EmployeeInfo with the following constant variables: FACULTY_MONTHLY_SALARY = 6000.00 STAFF_MONTHLY_HOURS_WORKED = 160 2. Implement an abstract class Employee with the following requirements: Attributes last name (String) first name (String) ID number (String) Sex - M or F Birth date - Use the Calendar Java class to create a date object Default argument constructor and argument constructors. Public methods toString - returning a string...
Lesson is on Inheritance and overriding the ToString ( ) method in Java: The program should...
Lesson is on Inheritance and overriding the ToString ( ) method in Java: The program should consist of 3 classes and will calculate an hourly paycheck.    - The superclass will 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.    - The subclass will calculate a paycheck for an hourly employee and will inherit from the superclass.   ...
Using Java, The program you will be writing displays a weekly payroll report. A loop in...
Using Java, The program you will be writing displays a weekly payroll report. A loop in the program should ask the user for the employee’s number, employee’s last name, number of hours worked, hourly pay rate, state tax, and federal tax rate. After the data is entered and the user hits the enter key, the program will calculate gross and net pay then displays employee’s payroll information as follows and asks for the next employees’ information. if the user works...
Bus ticket reservation project in java using class ,inheritance,interface
Bus ticket reservation project in java using class ,inheritance,interface
Java GPA calculator. Apply GUI, methods, exception handling, classes, objects, inheritance etc. The program should prompt...
Java GPA calculator. Apply GUI, methods, exception handling, classes, objects, inheritance etc. The program should prompt user for input. When done it should give an option to compute another GPA or exit.
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create an application with 5 classes: 1.) Vehicle 2.) Car 3.) Bus 4.) Truck 5.) Tester Following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats, maxWeightLoad, numberAxels **Class Vehicle: should have a constructor that initializes all of it's data **Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data...
create scientific calculator using java language with OOP rule and interfaces.
create scientific calculator using java language with OOP rule and interfaces.
What are the ‘Lessons’ of the Specific Factor Model? Demonstrate this lesson using formal modeling (either...
What are the ‘Lessons’ of the Specific Factor Model? Demonstrate this lesson using formal modeling (either graphs or equations).
Please solve using graphing calculator. Include what is typed into calculator in your explaination. For example:...
Please solve using graphing calculator. Include what is typed into calculator in your explaination. For example: First, type the data under L1, then go to STAT --> TESTS -->TInverval. Inpt: Data (highlight the word Data) List: L1 Freq: 1 C-level .98 Calculate--> (3.54, 4.29) Show as much work as you can to support answer. In a Pew Research Center poll 745 randomly selected adults, 589 said that it is morally wrong not to report all income on tax returns. Assume...
***USING JAVA Scenario: You will be writing a program that will allow a user to find...
***USING JAVA Scenario: You will be writing a program that will allow a user to find and replace misspelled words anywhere in the phrase, for as many words as the user wishes. Once done (see example runs below), the program will print the final phrase and will show the Word Count, Character Count, the Longest Word and the Shortest Word. Requirements: Do not use Arrays for this assignment. Do not use any String class methods (.phrase(), replace methods, regex methods)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT