Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java programming assignment: By using the Employee classes indicated create an I

ID: 664182 • Letter: J

Question

Java programming assignment:

By using the Employee classes indicated create an InvalidNameException that is thrown from anywhere a name can be set if the name string is empty.

Test the new versions, catching any InvalidNameExceptions thrown.

public abstract class Employee {
private String name;
private double payRate;

public Employee(String name, double payRate) {
  this.name = name;
  this.payRate = payRate;
}

public Employee(String name) {
  this.name = name;
  this.payRate = 0;
}

public String getName() {return name;}
public void setName(String s) {name = s;}
public double getRate() {return payRate;}
public void setRate(double rate) {payRate = rate;}

public abstract double calculatePay();

public boolean equals(Object o) {
  if (o == null) return false;
  if(o.getClass() != this.getClass()) {return false;}
  //Class objects will be compared to see if this and o are exactly the same class.
  Employee temp = (Employee)o;
  return (payRate == temp.payRate);
}

}

class HourlyEmployee extends Employee {
private double hours;

public HourlyEmployee(String name, double rate) {
  super(name, rate);
  if (rate < 15) setRate(15);
  
  //super(name, rate < 15 ? 15 : rate);
  
  //super(name);
  //setRate(r); //once you have setRate overridden in HourlyEmployee
}

public HourlyEmployee(String name) {
  super(name, 15);
}

public void addHours(double d) {
  hours += d;
}

public double getHours() {return hours;}

public double calculatePay() {
  double pay = hours * getRate();
  hours = 0;
  return pay;
}

public void setRate(double r) {
  if (r < 15) super.setRate(15);
  else super.setRate(r);
}

public boolean equals(Object o) {
  if (o == null) return false;
  if(o.getClass() != this.getClass()) {return false;}
  HourlyEmployee temp = (HourlyEmployee)o;
  //These three lines could be ommitted as super.equals will return false if the cast
  //would fail. Therefore, the cast can be made inline in the second half of the and.
  return super.equals(temp) && this.hours == temp.hours;

}

}

class SalariedEmployee extends Employee {
public SalariedEmployee(String name, double rate) {
  super(name, rate < 0 ? 0 : rate);
  //or any other way as in Hourly
}

public SalariedEmployee(String name) {
  super(name);
}

public double calculatePay(){
  return getRate() / 12;
}
}

class EmployeeTest {
public static void main(String args[]) {
  Employee [] ems = new Employee[6];
  
  ems[0] = new HourlyEmployee("Bill", 12);
  ems[1] = new HourlyEmployee("Mary", 23);
  ems[2] = new HourlyEmployee("Greg", 19);
  
  ems[3] = new SalariedEmployee("Sara", 43000);
  ems[4] = new SalariedEmployee("Pat", 62000);
  ems[5] = new SalariedEmployee("Paul", -2);
  
  
  for(int i = 0; i < 6; i ++) {
   if(ems[i] instanceof HourlyEmployee) {
   //instanceof is an operator that returns true if the object referred to by the
   //reference on the left "is a" the class on the right -- of that class or
   //any descendant of it.
    ((HourlyEmployee)ems[i]).addHours((int)(Math.random() * 20 +30));
    //or
    //HourlyEmployee temp = (HourlyEmployee)ems[i];
    //temp.addHours((int)(Math.random() * 20 +30)));
   }
  }
  
  for (int i = 0; i < 6; i ++) {
   System.out.println("Name: " + ems[i].getName() + ", pay rate: "
       + ems[i].getRate() + ", Pay: " + ems[i].calculatePay());
  }

}
}

Explanation / Answer

public class checkexception extends Exception
{

    private static final long serialVersionUID = 1997753363232807009L;

       public checkexception()
       {
       }

       public checkexception(String message)
       {
           super(message);
       }

       public checkexception(Throwable cause)
       {
           super(cause);
       }

       public checkexception(String message, Throwable cause)
       {
           super(message, cause);
       }

       public checkexception(String message, Throwable cause,
                                           boolean enableSuppression, boolean writableStackTrace)
       {
           super(message, cause, enableSuppression, writableStackTrace);
       }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote