In: Computer Science
Using Java, write the the following code. Only the bold needs to be answered
Here is the driver:
package assign
public class A3Driver {
public static void main(String[] args) {
//Test GSUStudent object
GSUStudentWorker gsw = new GSUStudentWorker("Bunny","Bugs", 2000, 9, 8, 9001234);
gsw.setGPA(3.7);
gsw.setHourlyRate(10.00);
gsw.setTotalHours(30);
System.out.println(gsw);
GSUStudentWorker gsw2 = new GSUStudentWorker("Bunny","Betty", 1999, 9, 8, 9002413);
gsw2.setGPA(4.0);
gsw2.setHourlyRate(10.00);
gsw2.setTotalHours(45);
System.out.println(gsw2);
//Test GSUStoreSupervisor
GSUStoreSupervisor gss = new GSUStoreSupervisor("Duck","Daffy", 1980, 9, 8);
gss.setMonthlyPay(4000);
System.out.println(gss);
//test GSUSuperStudent
GSUSuperStudent gsuper = new GSUSuperStudent("Mouse","Minny", 1990, 9, 8, 9004321);
gsuper.setGPA(3.9);
gsuper.setHourlyRate(10.00);
gsuper.setTotalHours(30);
gsuper.setMonthlyPay(4000);
System.out.println(gsuper);
}
}
1. The Person class
a. Person is an abstract class.
b. All class variables of the Person class must be private.
c.is a class with a single constructor, Person(String lName, String fName, int birthYear, int birthMonth, int birthDay). All arguments of the constructor should be stored as class variables. There should only be getter methods for first and last name variables. There should be no getters or setters forbirthYear, birthMonth, birthDay.
The method computeAge() takes no arguments and returns the person’s computed age (as of today's date) as a string of the form "X years, Y months and Z days". Hint: Use the LocalDate and Period classes.
i. e.g. "21 years, 2 months and 3 days". ii. e.g. "21 years and 3 days".
iii. e.g. "21 years and 2 months". iv. e.g. "21 years".
v. e.g. "2 months and 3 days".
The toString method returns a string comprised of the results of
getFname, getLName and computeAge. E.g.
i. “Bunny Bugs is 19 years and 1 day old”
The equals method returns true if the name and date of birth of this person and the other person are the same, otherwise return false.
2. The Employee interface
a.has a single method, getPay(), which returns a double
representing the pay earned for the period in dollars and cents.
3. The MonthlyEmployee interface
a. This interface extends the Employee interface.
b. has a single void method, setMonthlyPay(double perMonth),
which
takes a double as an argument. The argument is the amount earned for the month in dollars and cents.
4. The HourlyEmployee interface
a. This interface extends the Employee interface.
b. has a void method, setHourlyRate(double perHour), which takes
a
double as an argument. The argument is the amount earned for
each hour that the employee works.
c. has a void method, setTotalHours(double hoursWorked), which
takes a double as an argument. The argument is the total hours worked for the week.
Has a final integer variable MAX_WEEKLY_HOURS, which is assigned
a value of 40. This variable is used to assess if the total
hours worked exceeds the mandated hours.
Has a final double variable OVERTIME_RATE, which is assigned a
value of 1.75. This variable is used to compute the pay amount for hours worked that exceeds the mandated hours.
5. The Student class
a. The Student class is abstract.
The Student class extends the Person class.
All (if any) class variables of the Student class must be private.
has an abstract void method, setGPA(double gpa), which takes a double as an argument. The argument is the grade point average of the student using a 4.0 scale.
has an abstract method, getGPA(), which returns a double. The returned value is the grade point average of the student using a 4.0 scale.
6. The GSUStudentWorker class
a. The GSUStudentWorker class extends the Student class
and
inherits from the HourlyEmployee interface.
is a class with a single constructor, GSUStudentWorker (String lName, String fName, int birthYear, int birthMonth, int birthday, int eid). There should be a getter and setter for eid, which is the Eagle ID of the student.
All class variables of the Student class must be private.
has a void method, setGPA(double gpa), which takes a double as an argument. The argument is the grade point average of the student using a 4.0 scale.
has a method, getGPA(), which returns a double. The returned value is the grade point average of the student using a 4.0 scale.
f. Has a toString() method that returns a string in the form shown below.
i. Bugs Bunny is 19 years and 15 days old and is a Great Sauce student with a 3.7 GPA who earned $300.00 this week.
7. The GSUStoreSupervisor class
a. The GSUStoreSupervisor class extends the Person class
and
inherits from the MonthlyEmployee interface.
is a class with a single constructor, GSUStoreSupervisor(String lName, String fName, int birthYear, int birthMonth, int birthDay).
All class variables of the Student class must be private.
Has a toString() method that returns a string in the form shown below.
i. Daffy Duck is 39 years and 15 days old and is a Great Sauce monthly employee who earned $4000.00 this period.
8. The GSUSuperStudent class
a. GSUSuperStudent has found a way to get paid as a
monthly
employee and as a student employee.
The GSUSuperStudent class extends the GSUStudentWorker class and inherits from the MonthlyEmployee interface.
All class variables of the Student class must be private.
Has a toString() method that returns a string in the form shown below.
i. Minny Mouse is 29 years and 15 days old and is a Great Sauce student with a 3.9 GPA who earned $4300.00 this period.
Example output:
__________Example from A3Driver shown below
Bugs Bunny is 19 years and 15 days old and is a Great Sauce student with a 3.7 GPA who earned $300.00 this week.
Betty Bunny is 20 years and 15 days old and is a Great Sauce student with a 4.0 GPA who earned $487.50 this week.
Daffy Duck is 39 years and 15 days old and is a Great Sauce monthly employee who earned $4000.00 this period.
Minny Mouse is 29 years and 15 days old and is a Great Sauce student with a 3.9 GPA who earned $4300.00 this period.
//Java code
package assign; public class A3Driver { public static void main(String[] args) { //Test GSUStudent object GSUStudentWorker gsw = new GSUStudentWorker("Bunny","Bugs", 2000, 9, 8, 9001234); gsw.setGPA(3.7); gsw.setHourlyRate(10.00); gsw.setTotalHours(30); System.out.println(gsw); GSUStudentWorker gsw2 = new GSUStudentWorker("Bunny","Betty", 1999, 9, 8, 9002413); gsw2.setGPA(4.0); gsw2.setHourlyRate(10.00); gsw2.setTotalHours(45); System.out.println(gsw2); //Test GSUStoreSupervisor GSUStoreSupervisor gss = new GSUStoreSupervisor("Duck","Daffy", 1980, 9, 8); gss.setMonthlyPay(4000); System.out.println(gss); //test GSUSuperStudent GSUSuperStudent gsuper = new GSUSuperStudent("Mouse","Minny", 1990, 9, 8, 9004321); gsuper.setGPA(3.9); gsuper.setHourlyRate(10.00); gsuper.setTotalHours(30); gsuper.setMonthlyPay(4000); System.out.println(gsuper); } }
//====================================
package assign; public interface Employee { public double getPay(); }
//================================
package assign; public class GSUStoreSupervisor extends Person implements MonthlyEmployee { private double salary; public GSUStoreSupervisor(String lastName, String firstName, int birthYear, int birthMonth, int birthDay) { super(lastName, firstName, birthYear, birthMonth, birthDay); salary =0; } @Override public void setMonthlyPay(double perMonth) { salary = perMonth; } @Override public double getPay() { return salary; } @Override public String toString() { return super.toString()+" and is a Great Sauce monthly employee who earned $"+String.format("%.2f",getPay())+" this period."; } }
//======================================
package assign; public class GSUStudentWorker extends Student implements HourlyEmployee { private int eid; private double gpa; private double rate; private double hours; public GSUStudentWorker(String lastName, String firstName, int birthYear, int birthMonth, int birthDay, int eid) { super(lastName, firstName, birthYear, birthMonth, birthDay); this.eid = eid; gpa=0; rate=0; hours=0; } //getters and setters public int getEid() { return eid; } public void setEid(int eid) { this.eid = eid; } @Override public void setHourlyRate(double perHour) { rate = perHour; } @Override public void setTotalHours(double hoursWorked) { hours = hoursWorked; } @Override public double getPay() { if(hours<MAX_WEEKLY_HOURS) { return hours*rate; } else { return MAX_WEEKLY_HOURS*rate+( hours-MAX_WEEKLY_HOURS)*rate*OVERTIME_RATE; } } @Override public void setGPA(double gpa) { this.gpa = gpa; } @Override public double getGPA() { return gpa; } @Override public String toString() { return super.toString()+" and is a Great Sauce student with a "+gpa+" GPA who earned $"+String.format("%.2f",getPay())+" this week."; } }
//======================================
package assign; public class GSUSuperStudent extends GSUStudentWorker implements MonthlyEmployee { private double salary; public GSUSuperStudent(String lastName, String firstName, int birthYear, int birthMonth, int birthDay, int eid) { super(lastName, firstName, birthYear, birthMonth, birthDay, eid); salary = 0; } @Override public void setMonthlyPay(double perMonth) { salary=perMonth+super.getPay(); } @Override public String toString() { return firstName+" "+lastName+ " is "+computeAge()+" and is a Great Sauce student with a "+getGPA()+" GPA who earned $"+String.format("%.2f",salary)+" this period."; } }
//========================================
package assign; public interface HourlyEmployee extends Employee { public final int MAX_WEEKLY_HOURS = 40; public final double OVERTIME_RATE = 1.75; public void setHourlyRate(double perHour); public void setTotalHours(double hoursWorked); }
//==============================
package assign; public interface MonthlyEmployee extends Employee { public void setMonthlyPay(double perMonth); }
//===================================
package assign; import java.time.LocalDate; import java.time.Period; public abstract class Person { protected String lastName; protected String firstName; protected int birthYear; protected int birthMonth; protected int birthDay; //Constructor public Person(String lastName, String firstName, int birthYear, int birthMonth, int birthDay) { this.lastName = lastName; this.firstName = firstName; this.birthYear = birthYear; this.birthMonth = birthMonth; this.birthDay = birthDay; } //getters public String getLastName() { return lastName; } public String getFirstName() { return firstName; } public String computeAge() { LocalDate localDate = LocalDate.of(birthYear,birthMonth,birthDay); LocalDate localDateNow = LocalDate.now(); Period diff = Period.between(localDate,localDateNow); if(diff.getYears()==0) return diff.getMonths()+" months and "+diff.getDays()+" days."; else if(diff.getDays()==0) return diff.getYears()+" years and "+diff.getMonths()+" months."; else if(diff.getMonths()==0 && diff.getDays()==0) return diff.getYears()+" years."; else if(diff.getMonths()==0) return diff.getYears()+" years and "+diff.getDays()+" days."; else return diff.getYears()+" years, "+diff.getMonths()+" months, and "+diff.getDays()+" days."; } @Override public String toString() { return firstName+" "+lastName+ " is "+computeAge(); } @Override public boolean equals(Object obj) { Person person = ((Person)obj); return (this.firstName.equalsIgnoreCase(person.firstName) && this.computeAge().equalsIgnoreCase(person.computeAge())); } }
//==================================
package assign; public abstract class Student extends Person{ public Student(String lastName, String firstName, int birthYear, int birthMonth, int birthDay) { super(lastName, firstName, birthYear, birthMonth, birthDay); } public abstract void setGPA(double gpa); public abstract double getGPA(); }
//Output
//If you need any help regarding this solution ......... please leave a comment ........ thanks