Description
The following workshop lets you practice basic java coding techniques, creating classes, methods, using arrays, inheritance, polymorphism, Exceptional Handling. Employee and Payable Case Study: JAC – 444 Semester 2021 The Payable Interface An interface declares one or more methods. The Invoice and Employee Classes Both the Employee class and Invoice class implements Payable interface. This allows Employee objects and Invoice objects to be processed polymorphically as Payable objects. The Invoice class is a basic class with • instance variables • A constructor • getters and setters • A toString method – returns String representation of Invoice Object. • An overridden getPaymentAmount method – returns the cost of the invoice Note: toString and getPaymentAmount use the get methods rather than directly accessing the variables. The Employee class • Should be an abstract class. You cannot create an object whose class is Employee. For example: Employee employee007 = new Employee(“James”, “Bond”, “007-00-7007”); would cause a runtime error. • instance variables • getters and setters • A constructor that initialize the variables of the class • A toString method – returns String representation of Employee Object. Subclasses of Employee Each of the subclasses have the following features: • It extends the Employee class or a subclass of Employee. • The parameters of its constructor include values to initialize all the instance variables of the class and its superclasses. • The first line of code in the constructor calls the constructor of the immediate superclass. • The toString method combines the result of the toString method of the superclass with additional information. • The constructor, getPaymentAmount and toString methods use the getters and setters to get and set the instance variables. JAC – 444 Semester 2021 • The setters should also include code to validate (or at least partially validate) the new values. CommissionEmployee Class Commission employees are paid a percentage of their sales • instance variabes o grossSales – (throw an exception when <= 0.0) o commissionRate – (throw an exception when rate is not between 0.0 to 1.0) • getters and setters • A constructor • A toString method – Overrides toString method in class Employee and returns String representation of CommissionEmployee Object. HourlyEmployee Class Hourly employees are paid by the hour and receive overtime pay (i.e., 1.5 times their hourly salary rate) for all hours worked in excess of 40 hours. • instance variabes o wage – (throw an exception when <= 0.0) o hours – (throw an exception when hours is not between 0.0 to 168.0) • getters and setters • A constructor • A toString method – Overrides toString method in class Employee and returns String representation of HourlyEmployee Object. SalariedEmployee Class Salaried employees are paid a fixed weekly salary regardless of the number of hours worked • instance variabes o weeklysalary – (throw an exception when <= 0.0) • getters and setters • A constructor • A toString method – Overrides toString method in class Employee and returns String representation of SalariedEmployee Object. BasePlusCommissionEmployee Class Base-salaried commission employees receive a base salary plus a percentage of their sales. For the current pay period, the company has decided to reward salaried-commission employees by adding 10% to their base salaries JAC – 444 Semester 2021 • instance variabes o baseSalary – (throw an exception when <= 0.0) • getters and setters • A constructor • A toString method – Overrides toString method in class Employee and returns String representation of BasePlusCommissionEmployee Object. Testing (main method): Payroll System Test • Polymorphically process o Two Inovices o One SalariedEmployee o One HourlyEmployee o One CommissionEmployee o One BasePlusCommissionEmployee First output a String representation of each Payable object. Next, if an object is a BasePlusCommissionEmployee, increase its base salary by 10%. Finally output the payment amount for each Payable object. Continue to next page… JAC – 444 Semester 2021 Workshop Header /********************************************** Workshop # Course: – Semester Last Name: First Name: ID: Section: