In: Computer Science
Java instructions:
1. Modify abstract superclass (Employee10A) so it comment out the abstract payPrint method and uses a toString method to print out it’s instance variables. Make sure toString method cannot be overridden. Source code below:
public abstract class Employee10A {
private String firstName, lastName;
static int counter = 0;
public Employee10A(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return ("The employee's full name is " + firstName + " " + lastName
+ ".");
}
abstract void payPrint();
}
2. Create an interface named PartTimeEmployee10A that contains the weekly pay for the Hourly, Salaried, and Commission part time employees as constants: hourly earns $300, salary earns $200, and commission earns $175. Meaning there's no need to calculate weekly pay for hourly, salaried, and commission part time employees, instead use these flat amounts as weekly pay.
3. Modify all three subclasses (HourlyEmployee10A, SalariedEmployee10A, CommissionEmployee10A) so they implement the interface created. Add code that correctly calculates and sets the weekly pay according to whether the employee is part time or full time. Change the payPrint method in each subclass so that it uses the toString method when printing out the objects. Source code below:
HourlyEmployee10A source code:
public class HourlyEmployee10A extends Employee10A {
Scanner sc = new Scanner(System.in);
private int hrsWorked, otHrs;
private double hrlyRate, regularPay, otPay, wklyPaycheck;
public HourlyEmployee10A(String firstName, String lastName, int
hrsWorked,
int otHrs, double hrlyRate, double regularPay, double otPay)
{
super (firstName, lastName);
this.hrsWorked = hrsWorked;
this.otHrs = otHrs;
if (hrsWorked > 40) {
System.out.println("Enter the number of overtime hours worked
"
+ "by the employee:");
otHrs = sc.nextInt();
otPay = ((hrlyRate * 1.5) * otHrs);
regularPay = hrlyRate * 40;
}
else {
otPay = 0;
regularPay = hrlyRate * hrsWorked;
}
this.hrlyRate = hrlyRate;
this.regularPay = regularPay;
this.otPay = otPay;
counter++;
}
//method that calculates the weekly paycheck
public void WklyPaycheck() {
wklyPaycheck = regularPay + otPay;
}
//getter(method) that retrieves the "wklyPaycheck"
public double getWklyPaycheck() {
return wklyPaycheck;
}
@Override
public String toString() {
return String.format("%s The employee is an Hourly Employee and its
"
+ "weekly paycheck amount is $%.2f.", super.toString(),
wklyPaycheck);
}
@Override
public void payPrint() {
WklyPaycheck();
}
}
SalariedEmployee10A source code:
public class SalariedEmployee10A extends Employee10A {
private double yrlySalary, bonusPercent, wklyPaycheck;
private static final double weeks = 52;
private static final double toDecimal = 100;
public SalariedEmployee10A(String firstName, String lastName,
double yrlySalary,
double bonusPercent) {
super(firstName, lastName);
this.yrlySalary = yrlySalary;
this.bonusPercent = bonusPercent;
counter++;
}
public void WklyPaycheck() {
wklyPaycheck = (yrlySalary * (1.0 + bonusPercent/toDecimal)) /
weeks;
}
@Override
public String toString() {
return String.format("%s The employee is a Salaried Employee and
its "
+ "weekly paycheck amount is $%.2f.", super.toString(),
wklyPaycheck);
}
@Override
public void payPrint() {
WklyPaycheck();
}
}
CommissionEmployee10A source code:
public class CommissionEmployee10A extends Employee10A {
private int soldItems;
private double itemCost, wklyPaycheck;
private static final double baseComm = 200;
private static final double employeePercent = .1;
public CommissionEmployee10A(String firstName, String lastName, int
soldItems,
double itemCost) {
super(firstName, lastName);
this.soldItems = soldItems;
this.itemCost = itemCost;
counter++;
}
public void WklyPaycheck() {
wklyPaycheck = baseComm + ((employeePercent) * (double)soldItems *
itemCost);
}
@Override
public String toString() {
return String.format("%s The employee is a Commission Employee and
its "
+ "weekly paycheck amount is $%.2f.", super.toString(),
wklyPaycheck);
}
@Override
public void payPrint() {
WklyPaycheck();
}
}
4. Modify driver class (EmployeeTest10A) so it adds code to create a part time employee of each type and a fulltime employee of each type (6 employees/objects). Print all 6 objects using payPrint. Add all 6 objects to the superclass array. Use the instanceof and downcasting to search for and print out the object of your choice. Source code below:
public class EmployeeTest10A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String firstName, lastName;
int otHrs = 0;
int answer, hrsWorked, soldItems;
double otPay = 0;
double regularPay = 0;
double hrlyRate, wklyPaycheck, bonus, itemCost;
//create an array of type Employee10A to store objects of its
subclasses
Employee10A[] employees = new Employee10A[3];
System.out.println("Enter any number to continue or enter a -1 to
exit "
+ "the program.");
answer = sc.nextInt();
while(answer != -1) {
System.out.println("Please provide information for Hourly
Employee.");
System.out.println("Enter the employee's first name:");
firstName = sc.next();
System.out.println("Enter the employee's last name:");
lastName = sc.next();
System.out.println("Enter the employee's hourly rate of
pay:");
hrlyRate = sc.nextDouble();
System.out.println("Enter the number of hours worked by the
employee:");
hrsWorked = sc.nextInt();
//creating object of "HourlyEmployee10A" and storing it in
Employee10A type array
employees[0] = new HourlyEmployee10A(firstName, lastName,
hrsWorked, otHrs, hrlyRate, regularPay, otPay);
// calling method from "HourlyEmployee10A" to calculate weekly
paycheck
employees[0].payPrint();
// printing results
System.out.println(employees[0]);
System.out.println("\nPlease provide information for Salaried
Employee.");
System.out.println("Enter the employee's first name:");
firstName = sc.next();
System.out.println("Enter the employee's last name:");
lastName = sc.next();
System.out.println("Enter the employee's yearly salary:");
regularPay = sc.nextDouble();
System.out.println("Enter the employee's bonus percent:");
bonus = sc.nextDouble();
//creating object of "SalariedEmployee10A" and storing it in
Employee10A type array
employees[1] = new SalariedEmployee10A(firstName, lastName,
regularPay, bonus);
//calling method from "SalariedEmployee10A" to calculate weekly
paycheck
employees[1].payPrint();
// printing results
System.out.println(employees[1]);
System.out.println("\nPlease provide information for Commission
Employee.");
System.out.println("Enter the employee's first name:");
firstName = sc.next();
System.out.println("Enter the employee's last name:");
lastName = sc.next();
System.out.println("Enter the number of items sold by the
employee:");
soldItems = sc.nextInt();
System.out.println("Enter the price of each item:");
itemCost = sc.nextDouble();
//creating object of "CommissionEmployee10A" and storing it in
Employee10A type array
employees[2] = new CommissionEmployee10A(firstName, lastName,
soldItems, itemCost);
//calling method from "CommissionEmployee10A" to calculate weekly
paycheck
employees[2].payPrint();
//printing results
System.out.println(employees[2]);
System.out.println("\nEnter any number to continue or enter a -1 to
"
+ "exit the program and calculate the total number of
paychecks.");
answer = sc.nextInt();
}
System.out.println("\nTotal number of paychecks calculated: " +
Employee10A.counter);
}
}
Interface:
Code:
package assignment;
import java.util.Scanner;
class Employee10A {
private String firstName,
lastName;
static int counter = 0;
public Employee10A(String
firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String toString() {
return ("The employee's full name
is " + firstName + " " + lastName + ".");
}
public void payPrint() {
// TODO
Auto-generated method stub
}
//abstract void
payPrint();
}
class HourlyEmployee10A extends
Employee10A implements PartTimeEmployee10A{
Scanner sc = new
Scanner(System.in);
private int hrsWorked, otHrs;
private double regularPay, otPay,
wklyPaycheck;
public HourlyEmployee10A(String
firstName, String lastName, int hrsWorked,
int otHrs, double regularPay,
double otPay) {
super (firstName, lastName);
this.hrsWorked = hrsWorked;
this.otHrs = otHrs;
if (hrsWorked > 40) {
System.out.println("Enter the
number of overtime hours worked "
+ "by the employee:");
otHrs = sc.nextInt();
otPay = ((hrsearns * 1.5) *
otHrs);
regularPay = hrsearns * 40;
}
else {
otPay = 0;
regularPay = hrsearns *
hrsWorked;
}
//this.hrsearns = hrsearns;
this.regularPay = regularPay;
this.otPay = otPay;
counter++;
}
//method that calculates the weekly
paycheck
public void WklyPaycheck() {
wklyPaycheck = regularPay +
otPay;
}
//getter(method) that retrieves the
"wklyPaycheck"
public double getWklyPaycheck()
{
return wklyPaycheck;
}
public String toString() {
return String.format("%s The
employee is an Hourly Employee and its "
+ "weekly paycheck amount is
$%.2f.", super.toString(), wklyPaycheck);
}
public void payPrint() {
WklyPaycheck();
}
}
//SalariedEmployee10A source
code:
class SalariedEmployee10A
extends Employee10A implements PartTimeEmployee10A {
private double yrlySalary,
bonusPercent, wklyPaycheck;
private static final double weeks =
52;
private static final double
toDecimal = 100;
public SalariedEmployee10A(String
firstName, String lastName,double bonusPercent) {
super(firstName, lastName);
//this.salaryearns =
salaryearns;
this.bonusPercent =
bonusPercent;
counter++;
}
public void WklyPaycheck() {
wklyPaycheck = (salaryearns * (1.0
+ bonusPercent/toDecimal)) / weeks;
}
public String toString() {
return String.format("%s The
employee is a Salaried Employee and its "
+ "weekly paycheck amount is
$%.2f.", super.toString(), wklyPaycheck);
}
public void payPrint() {
WklyPaycheck();
}
}
//CommissionEmployee10A source code:
class CommissionEmployee10A extends Employee10A implements PartTimeEmployee10A {
private int soldItems;
private double itemCost,
wklyPaycheck;
//private static final double
baseComm = 200;
private static final double
employeePercent = .1;
public CommissionEmployee10A(String
firstName, String lastName, int soldItems,
double itemCost) {
super(firstName, lastName);
this.soldItems = soldItems;
this.itemCost = itemCost;
counter++;
}
public void WklyPaycheck() {
wklyPaycheck = commision +
((employeePercent) * (double)soldItems * itemCost);
}
public String toString() {
return String.format("%s The
employee is a Commission Employee and its "
+ "weekly paycheck amount is
$%.2f.", super.toString(), wklyPaycheck);
}
public void payPrint() {
WklyPaycheck();
}
}
public class EmployeeTest10A
implements PartTimeEmployee10A {
public static void main(String[]
args) {
Scanner sc = new
Scanner(System.in);
String firstName, lastName;
int otHrs = 0;
int answer, hrsWorked,
soldItems;
double otPay = 0;
double regularPay = 0;
double hrlyRate, wklyPaycheck,
bonus, itemCost;
//create an array of type
Employee10A to store objects of its subclasses
//Employee10A[] employees = new
Employee10A[3];
HourlyEmployee10A hour;
SalariedEmployee10A salary;
CommissionEmployee10A comm;
System.out.println("Enter any
number to continue or enter a -1 to exit "
+ "the program.");
answer = sc.nextInt();
while(answer != -1) {
System.out.println("Please provide
information for Hourly Employee.");
System.out.println("Enter the
employee's first name:");
firstName = sc.next();
System.out.println("Enter the
employee's last name:");
lastName = sc.next();
System.out.println("Enter the
number of hours worked by the employee:");
hrsWorked = sc.nextInt();
//creating object of
"HourlyEmployee10A" and storing it in Employee10A type array
System.out.println("weekly pay for
the hourly: "+PartTimeEmployee10A.hrsearns);
hour = new
HourlyEmployee10A(firstName, lastName, hrsWorked, otHrs,
PartTimeEmployee10A.hrsearns, regularPay);
// calling method from
"HourlyEmployee10A" to calculate weekly paycheck
hour.payPrint();
// printing results
System.out.println(hour);
System.out.println(hour instanceof
Employee10A );//instaceof
System.out.println("\nPlease
provide information for Salaried Employee.");
System.out.println("Enter the
employee's first name:");
firstName = sc.next();
System.out.println("Enter the
employee's last name:");
lastName = sc.next();
System.out.println("Enter the
employee's bonus percent:");
bonus = sc.nextDouble();
System.out.println("Salary earns:
"+PartTimeEmployee10A.salaryearns);
//creating object of
"SalariedEmployee10A" and storing it in Employee10A type
array
salary = new
SalariedEmployee10A(firstName, lastName,
PartTimeEmployee10A.salaryearns);
//calling method from
"SalariedEmployee10A" to calculate weekly paycheck
salary.payPrint();
// printing results
System.out.println(salary);
System.out.println(salary
instanceof SalariedEmployee10A);//instaceof
System.out.println("\nPlease
provide information for Commission Employee.");
System.out.println("Enter the
employee's first name:");
firstName = sc.next();
System.out.println("Enter the
employee's last name:");
lastName = sc.next();
System.out.println("Enter the
number of items sold by the employee:");
soldItems = sc.nextInt();
System.out.println("Enter the
price of each item:");
itemCost = sc.nextDouble();
System.out.println("commision for
parttime employess"+PartTimeEmployee10A.commision);
//creating object of
"CommissionEmployee10A" and storing it in Employee10A type
array
comm = new
CommissionEmployee10A(firstName, lastName, soldItems,
itemCost);
//calling method from
"CommissionEmployee10A" to calculate weekly paycheck
comm.payPrint();
//printing results
System.out.println(comm);
System.out.println(comm instanceof
CommissionEmployee10A );//instanceof
System.out.println("\nEnter any
number to continue or enter a -1 to "
+ "exit the program and calculate
the total number of paychecks.");
answer = sc.nextInt();
}
System.out.println("\nTotal number
of paychecks calculated: " + Employee10A.counter);
}}
Inteface:
package assignment;
public interface PartTimeEmployee10A {
public final int hrsearns= 300;
public final int salaryearns = 200;
public final int commision = 175;
}
Note:***I hope i met your requirements*****If its right please give me upvote*****Thank you.....