Question

In: Computer Science

Modify the supplied payroll system to include a private instance variable called joinDate in class Employee...

Modify the supplied payroll system to include a private instance variable called joinDate in class Employee to represent when they joined the company. Use the Joda-Time library class LocalDate as the type for this variable. See http://www.joda.org/joda-time/index.html for details end examples of how to use this library. You may also have to download the Joda-Time library (JAR File) and include this in your CLASSPATH or IDE Project Libraries.

Use a static variable in the Employee class to help automatically assign each new employee a unique (incremental) id number.

Assume the payroll is processed once per week. Create an array of Employee variables to store references to the various employee objects. In a loop, calculate the payroll for each Employee, and add a €50.00 bonus to the person’s payroll if they joined the company more than five years before the current date.

Change the Earnings() method in Employee and all sub-classes to throw a user defined Exception if the total earnings are less than the equivalent of €10 per hour, define this amount as a minimum wage. The exception should have a message with the employee's name, the minimum wage that should be paid, and a short description of the problem.

Modify the Test class to be able to handle exceptions. When an exception is encountered calculating an employee's earnings, the Test class should print out the error message and continue as normal with the next employees. Test this by changing the Test class so that two of the employees will have earnings that are below the minimum wage.

Comment in each Java source file, and explain your code with comments.

Boss.java

// Boss class derived from Employee.

public final class Boss extends Employee {

private double weeklySalary;

// constructor for class Boss
public Boss(String first, String last, double salary) {
super(first, last); // call superclass constructor
setWeeklySalary(salary);
}

// set Boss's salary
public void setWeeklySalary(double salary) {
weeklySalary = (salary > 0 ? salary : 0);
}

// get Boss's pay
public double earnings() {
return weeklySalary;
}

// get String representation of Boss's name
public String toString() {
return "Boss: " + super.toString();
}
} // end class Boss

Commission worker.java


public final class CommissionWorker extends Employee {

private double salary; // base salary per week
private double commission; // amount per item sold
private int quantity; // total items sold for week

// constructor for class CommissionWorker
public CommissionWorker(String first, String last,
double salary, double commission, int quantity) {
super(first, last); // call superclass constructor
setSalary(salary);
setCommission(commission);
setQuantity(quantity);
}

// set CommissionWorker's weekly base salary
public void setSalary(double weeklySalary) {
salary = (weeklySalary > 0 ? weeklySalary : 0);
}

// set CommissionWorker's commission
public void setCommission(double itemCommission) {
commission = (itemCommission > 0 ? itemCommission : 0);
}

// set CommissionWorker's quantity sold
public void setQuantity(int totalSold) {
quantity = (totalSold > 0 ? totalSold : 0);
}

// determine CommissionWorker's earnings
public double earnings() {
return salary + commission * quantity;
}

// get String representation of CommissionWorker's name
public String toString() {
return "Commission worker: " + super.toString();
}
} // end class CommissionWorker

Employee.java

public abstract class Employee {

private String firstName;
private String lastName;

// constructor
public Employee(String first, String last) {
firstName = first;
lastName = last;
}

// get first name
public String getFirstName() {
return firstName;
}

// get last name
public String getLastName() {
return lastName;
}

public String toString() {
return firstName + ' ' + lastName;
}

public abstract double earnings();
}

HourlyWorker.java

public final class HourlyWorker extends Employee {

private double wage; // wage per hour
private double hours; // hours worked for week

// constructor for class HourlyWorker
public HourlyWorker(String first, String last,
double wagePerHour, double hoursWorked) {
super(first, last); // call superclass constructor
setWage(wagePerHour);
setHours(hoursWorked);
}

// Set the wage
public void setWage(double wagePerHour) {
wage = (wagePerHour > 0 ? wagePerHour : 0);
}

// Set the hours worked
public void setHours(double hoursWorked) {
hours = (hoursWorked >= 0 && hoursWorked < 168
? hoursWorked : 0);
}

// Get the HourlyWorker's pay
public double earnings() {
return wage * hours;
}

public String toString() {
return "Hourly worker: " + super.toString();
}
}

PieceWorker.java

public final class PieceWorker extends Employee {

private double wagePerPiece; // wage per piece output
private int quantity; // output for week

// constructor for class PieceWorker
public PieceWorker(String first, String last,
double wage, int numberOfItems) {
super(first, last); // call superclass constructor
setWage(wage);
setQuantity(numberOfItems);
}

// set PieceWorker's wage
public void setWage(double wage) {
wagePerPiece = (wage > 0 ? wage : 0);
}

// set number of items output
public void setQuantity(int numberOfItems) {
quantity = (numberOfItems > 0 ? numberOfItems : 0);
}

// determine PieceWorker's earnings
public double earnings() {
return quantity * wagePerPiece;
}

public String toString() {
return "Piece worker: " + super.toString();
}
}

Test.java

// Driver for Employee hierarchy

// Java core packages
import java.text.DecimalFormat;

// Java extension packages
import javax.swing.JOptionPane;

public class Test {

// test Employee hierarchy
public static void main(String args[]) {
Employee employee; // superclass reference
String output = "";

Boss boss = new Boss("John", "Smith", 800.0);

CommissionWorker commissionWorker =
new CommissionWorker(
"Sue", "Jones", 400.0, 3.0, 150);

PieceWorker pieceWorker =
new PieceWorker("Bob", "Lewis", 2.5, 200);

HourlyWorker hourlyWorker =
new HourlyWorker("Karen", "Price", 13.75, 40);

DecimalFormat precision2 = new DecimalFormat("0.00");

// Employee reference to a Boss
employee = boss;

output += employee.toString() + " earned $"
+ precision2.format(employee.earnings()) + "\n"
+ boss.toString() + " earned $"
+ precision2.format(boss.earnings()) + "\n";

// Employee reference to a CommissionWorker
employee = commissionWorker;

output += employee.toString() + " earned $"
+ precision2.format(employee.earnings()) + "\n"
+ commissionWorker.toString() + " earned $"
+ precision2.format(
commissionWorker.earnings()) + "\n";

// Employee reference to a PieceWorker
employee = pieceWorker;

output += employee.toString() + " earned $"
+ precision2.format(employee.earnings()) + "\n"
+ pieceWorker.toString() + " earned $"
+ precision2.format(pieceWorker.earnings()) + "\n";

// Employee reference to an HourlyWorker
employee = hourlyWorker;

output += employee.toString() + " earned $"
+ precision2.format(employee.earnings()) + "\n"
+ hourlyWorker.toString() + " earned $"
+ precision2.format(hourlyWorker.earnings()) + "\n";

JOptionPane.showMessageDialog(null, output,
"Demonstrating Polymorphism",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
} // end class Test

Solutions

Expert Solution

In above code

  1. import java.util.Scanner;  
  2. public class Employes {  
  3.     String name;  
  4.         int bonus;  
  5.         long salary;  
  6.         long totalsalary;  
  7.         void accept()  
  8.         {  
  9.          Scanner s= new Scanner(System.in);  
  10.          System.out.println("Enter Name of Employes\n");  
  11.          name=s.nextLine();  
  12.          System.out.println("Enter the Bonus of Employe\n");  
  13.          bonus=s.nextInt();  
  14.          System.out.println("Enter the Salary\n");  
  15.          salary=s.nextInt();  
  16.         }  
  17.         void calctotal()  
  18.         {  
  19.             totalsalary=salary+bonus;  
  20.         }  
  21.         void print()  
  22.         {  
  23.             System.out.println("Name of Employe\n"+name);  
  24.             System.out.println("Salary of Employe\n"+salary);  
  25.             System.out.println("Bonus of Employe\n"+bonus);  
  26.             System.out.println("Totalsalary of Employee\n"+totalsalary);  
  27.         }  
  28.     /**
  29.      * @param args the command line arguments
  30.      */  
  31.     public static void main(String[] args) {  
  32.           
  33.         Employes emp = new Employes(); //object of empolye
  34.         emp.accept();  
  35.         emp.calctotal();  
  36.         emp.print();  
  37.         // TODO code application logic here  
  38.     }  
  39.       
  40. }  

Related Solutions

in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Create a class called employee which has the following instance variables: Employee ID number Salary Years...
Create a class called employee which has the following instance variables: Employee ID number Salary Years at the company Your class should have the following methods: New employee which reads in an employee’s ID number, salary and years of service Anniversary which will up the years of service by 1 You got a raise which will read in how much the raise was (a percent) and then calculate the new salary You get a bonus which gives a yearly bonus...
Write a class called Pen that contains the following information: Private instance variables for the price...
Write a class called Pen that contains the following information: Private instance variables for the price of the pen (float) and color of the pen (String). A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
Question 1 price is an instance variable of a class. Inside that class there is a...
Question 1 price is an instance variable of a class. Inside that class there is a method public void change(int price). From inside the method " change" how do we refer to the instance variable price and not the parameter price? Answer: Question 2 A method of a class can access which of the following: 1 global variables in the same scope 2. local variables defined within the method 3. the instance variables of its class 4. its parameters Question...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class Create another class called CourseSection Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of the methods required for a standard user defined...
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT