Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

With Java, using Netbeans please Construct the following code. The figures from the book are included. Thank you! Modify the payroll system of Figs. 10.4-10.9

With Java, using Netbeans please Construct the following code. The figures from the book are included. Thank you! image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Modify the payroll system of Figs. 10.4-10.9 to include an additional Employee subclass TemporaryPieceWorker that represents an employee whose pay is based on the number of pieces of merchandise produced. Class TemporaryPieceWorker should contain private instance variables wage (to store wages per piece) and pieces (to store number of pieces produced). Provide a concrete implementation of method earnings in class TemporaryPieceWorker that calculates the employees earnings (wages number of pieces). Create an array of Employee variables to store references to objects of each concrete class in the new Employee hierarchy. Display each Employees details (String representations and earnings) I // Fig. 10.4: Employee.java 2 // Employee abstract superclass 4 public abstract class Employee t 5 private final String firstName; 6 private final String lastName; 7 private final String socialSecurityNumber; 8 9 // constructor 10 public Employee(String firstName, String lastName I1 12 13 14 15 16 17 String socialSecurityNumber) ( this.firstName firstName; this.lastName 1astName; this.socialSecurityNumber socialSecurityNumber; //return first name public String getFirstNameO freturn firstName; // return last name 19 20 21 public String getLastNameO treturn lastName;3 // return social security number 5public String getSocialsecurityNumber) return socialSecurityNumber; 26 / return String representation of Employee object 27 C0verride 28 public String toStringO String.format ("%s getFirstName ), getlastName O, getScialSeutNumerO); 9 return %s%nsocial 9 security number: %s", 5 33 / abstract method must be overridden by concrete subclasses 34 public abstract double earnings O; // no implementation here 35 1 1 9 I 11 Fig. 10.5: SalariedEmployee.java 2 I SalariedEmp1oyee concrete clas rity elas extends abstract class Emp loyee. 4 public class SalariedEmployee extends Employee f 5 private double weeklySalary // constructor public SalariedEmployee(String firstName, String 1astName, 6 Strin super(firstName, lastName, socialSecurityNumber); if (weeklySalary 0.0) f g socialSecurityNumber, double weeklySalary) f 10 12 throw new In1ega1ArgumentException "Weekly salary must be ># 0.0"); 13 14 15 16 this.weeklySalary weeklySalary 18 20 21 public void setweeklySalary(double weeklySalary) // set salary if (weeklySalary- 0.0"); this.weeklySalary - weeklySalary; // return salary // calculate earnings; override abstract nethod earnings in Emp oye 31 public double getweeklySalaryO (return weeklySalary;J 32 34 @Override 35 public double earningsO treturn getweeklySalaryO; 36 37 return String representation of SalariedEmployee object 38 @Override 39 public String toStringO f 41 42 43 return String . format ("salaried employee : %s%n%s: S%,"2f", super. toStringO, "weekly salary", getweeklySalaryO): 10.6: HourlyEmployee.java HourlyEmployee class extends Employee. I // Fig. lic class HourlyEmployee extends Employee t private double wage; // wage per hour private double hours; // hours worked for week 4 pu 8 /constructor 9 public HourlyEmployee (String firstName, String lastName, 10 String socialSecurityNumber, double wage, double hours) t super(firstName, lastName, socialSecurityNumber); 12 13 14 15 16 17 18 19 20 if (wage =0.0"); if (Chours = 0.0 and 168.0)) // validate hours throw new IllegalArgumentException( "Hours worked must be- 0.0 and 168.0) 45 this.hours hours; 46h 48 return hours worked 49 public double getHoursO (return hours;) 50 51 52 0verride 53 public double earnings O f 54 // calculate earnings; override abstract method earnings in Employee if (getHoursO = 1.0) { // validate throw new Il1egalArgumentException "Commission rate must be 0.0 and - 0.0"); this.grosssales grossSales; 6//return gross sales amount ossSales;J public double getGrossSalesO freturn gr // set commission rate public void setCommissionRate(double commissionRate) f if (commissio nRate . 1.0) { // validate 0 g. 10.7 1 ssionEmployee class extends Employee. (Part I of 2.) throw new IllegalArgumentException( 42 43 "Commission rate must be 0.0 and -0.0"); 24 new must be 2 throw 27 this.baseSalary baseSalary; 28 30 I/return base salary 31 public double getBasesalaryO return baseSalary; 13 I/ calculate earnings; override method earnings in CommissionEmployee 34 @Override 36 38 Override 40 S public double earningsO (return getBasesalaryO super.earningsO: return String representation of BasePlusCommissionEmployee object 9 public String toStringO f ss, . 2f", String. format("%s %s ; "base-salaried", super.toStringO, "base salary", getBaseSalaryO) %s: return Fig. 10.8 | o I BasePlusCommissionEmployee class extends CommissionEmployee Comm //Fig. 10.9: Payro11SystemTest.java 21/ Employee hierarchy test program 4 public class Payro11SystemTest f public static void main(String[] args) f SalariedEmployee salariedEmployee - // create subclass objects new SalariedEmployee("John", "Smith", "111-11-1111", 800.00)- HourlyEmployee hourlyEmployee- 75, 40); i0 new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75 ommissionEmployee commissionEmployee - 12 new CommissionEmployee "Sue", "Jones", "333-33-3333", 10000, .06); BasePlusCommissionEmployee basePlusCommissionEmployee - 14 15 16 new BasePlusCommissionEmployee( "Bob", "Lewis" System.out.println("Employees processed individually: "; System . out . printf("%nXsZnXs : S%, .21%n%n", , "444-44-4444", 5000, .04, 300): 19 20 salariedEmployee, "earned", salariedEmployee.earnings O); System.out .printf("%s%nXs : S%,.2f%n%n", hourlyEmployee, "earned", hourlyEmployee.earnings O); System . out . printf("%s%n%s : S%, .21%n%n", 23 24 25 26 27 28 29 30 3 1 32 commissionEmployee, "earned", commissionEmployee.earnings O) System . out .printf("%s%n%s: SX, .2f%n%n", basePlusCommissionEmployee, "earned", basePlusCommissionEmployee.earningsO); // create four-element Employee array Employee [] employees new Employee[4]; 34 35 36 37 38 39 40 // initialize array with Employees employees [0] salariedEmployee; employees [1] hourlyEmployee; employees [2] commissionEmployee; employees [3] basePlusCommissionEmployee; System.out.printf("Employees processed polymorphically:%nXn"); // generically process each element in array employees for (Employee currentEmployee: employees) f 42 43 System.out.println(currentEmployee);// invokes toString Fig. 10.9 Employee hierarchy test program. (Part I of 3.) / determine whether element is a BasePlusCommissionEmployee if (currentEmployee instanceof BasePlusCommissionEmployee) t // downcast Employee reference to // BasePlusCommissionEmployee reference BasePlusCommissionEmployee employee - (BasePlusCommissionEmployee) currentEmployee; employee.setBaseSalary(1.10 employee.getBaseSalaryO); System.out.printf "new base salary with 103% increase is: 5%, . 2f%n", employee.getBaseSalaryO); 54 57 59 61 System.out.printf "earned $%, .21%n%n", currentEmployee . earnings()); // get type name of each object in employees array for (int j-0; j

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions