Employee class public class Employee { public int getHours() { return 40; // wor
ID: 3788748 • Letter: E
Question
Employee class
public class Employee {
public int getHours() {
return 40; // works 40 hours / week
}
public double getSalary() {
return 40000.0; // $40,000.00 / year
}
public int getVacationDays() {
return 10; // 2 weeks' paid vacation
}
public String getVacationForm() {
return "yellow"; // use the yellow form
}
}
public class Lawyer extends Employee {
public int getVacationDays() {
return super.getVacationDays() + 5;
}
public String getVacationForm() {
return "pink";
}
public void sue() {
System.out.println("I'll see you in court!");
}
}
Given the Employee class and the Lawyer class above
a. Write an employee class Janitor to accompany the other employees. Janitors
work twice as many hours (80 hours/week), they make $30,000 ($10,000 less than
others), they get half as much vacation (only 5 days), and they have an additional
method named clean that prints "Cleaning!." Use the super keyword to interact
with the Employee superclass as appropriate.
b. Write an employee class HarvardLawyer to accompany the other employees.
Harvard lawyers are like normal lawyers, but they make 20% more money than a
normal lawyer, they get 3 days more vacation, and they have to fill out four of the
lawyer's forms to go on vacation. That is, the getVacationForm method should
return "pinkpinkpinkpink". (If the normal Lawyer's vacation form ever
changed, the HarvardLawyer's should as well. For example, if Lawyer's vacation
form changed to "red", the HarvardLawyer's should return "redredredred".)
Use the super keyword to interact with the Employee superclass as appropriate.
c. Write a client class called EmployeeMain that creates objects of Lawyer,
Janitor and HardvardLawyer class in the main method. Write a method called
printEmployee() that takes an object of Employee as a parameter and prints out
salary, hour, vacation days and vacation form for the employee. Also call
clean() method if you are printing a Janitor object. Call printEmployee()
method from main method.
Example output for Lawyer object
Lawyer:
Salary: $40000
Hours: 40
Vacation days: 15
Vacation form: pink
Explanation / Answer
Hi, Please find my implementation.
##############
public class Janitor extends Employee {
public int getHours() {
return super.getHours()*2; // twice
}
public double getSalary() {
return super.getSalary()-10000.0; // less by 10000
}
public int getVacationDays() {
return super.getVacationDays() - 5;
}
public void clean() {
System.out.println("Cleaning!.");
}
}
#################
public class HarvardLawyer extends Lawyer {
public double getSalary() {
return super.getSalary() + super.getSalary()*0.2; // 20% more
}
public int getVacationDays() {
return super.getVacationDays() + 3; // 3 more vacation
}
public String getVacationForm() {
return super.getVacationForm()+super.getVacationForm()+
super.getVacationForm()+super.getVacationForm(); // 4 vacation form
}
}
##################
public class EmployeeMain {
public static void printEmployee(Employee employee){
if(employee instanceof Lawyer){
Lawyer lw = (Lawyer)employee; // type casting
System.out.println("Lawyer:");
System.out.println("Salary: $"+lw.getSalary());
System.out.println("Hours: "+lw.getSalary());
System.out.println("Vacation days: "+lw.getVacationDays());
System.out.println("Vacation form: "+lw.getVacationForm());
}else if(employee instanceof Janitor){
Janitor janti = (Janitor)employee; // type casting
System.out.println("Janitor:");
System.out.println("Salary: $"+janti.getSalary());
System.out.println("Hours: "+janti.getSalary());
System.out.println("Vacation days: "+janti.getVacationDays());
System.out.println("Vacation form: "+janti.getVacationForm());
janti.clean();
}else if(employee instanceof HarvardLawyer){
HarvardLawyer hw = (HarvardLawyer)employee; // type casting
System.out.println("Lawyer:");
System.out.println("Salary: $"+hw.getSalary());
System.out.println("Hours: "+hw.getSalary());
System.out.println("Vacation days: "+hw.getVacationDays());
System.out.println("Vacation form: "+hw.getVacationForm());
}
}
public static void main(String[] args) {
Lawyer lawyer = new Lawyer();
Janitor janitor = new Janitor();
HarvardLawyer harva = new HarvardLawyer();
printEmployee(lawyer);
System.out.println();
printEmployee(janitor);
System.out.println();
printEmployee(harva);
}
}
/*
Sample run:
Lawyer:
Salary: $40000.0
Hours: 40000.0
Vacation days: 15
Vacation form: pink
Janitor:
Salary: $30000.0
Hours: 30000.0
Vacation days: 5
Vacation form: yellow
Cleaning!.
Lawyer:
Salary: $48000.0
Hours: 48000.0
Vacation days: 18
Vacation form: pinkpinkpinkpink
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.