Question

public class Employee { public int getHours() { return 40; // works 40 hours / week...

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 "pink". (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 "red".) 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

##############

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

*/

Add a comment
Know the answer?
Add Answer to:
public class Employee { public int getHours() { return 40; // works 40 hours / week...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the missing keyword in the program below? public class Batman { public static int...

    What is the missing keyword in the program below? public class Batman { public static int minFunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1; return min; //This is the value that will be returned from the minFunction method. //The value after the keyword return must match the return type from the method header. Ours is OK because they are both int. } // end the public method named minFunction }...

  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • public class Animal {    private String name; //line 1    private int weight; //line 2...

    public class Animal {    private String name; //line 1    private int weight; //line 2    private String getName(){       return name;    } //line 3    public int fetchWeight(){       return weight; } //line 4 } public class Dog extends Animal {    private String food; //line 5    public void mystery(){       //System.out.println("Name = " + name); //line 6            System.out.println("Food = " + food); //line 7    } } I want to know the super...

  • departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE =...

    departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee;    } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • //*Manager.java*// public interface Manager { public void handleCrisis(); } _________________________________________________________ /**Employee.java**/ Public abstract class Employee {...

    //*Manager.java*// public interface Manager { public void handleCrisis(); } _________________________________________________________ /**Employee.java**/ Public abstract class Employee { protected final String name; protected final int id; public Employee(String empName, int empId) { name = empName; id = empId; } public Employee() { name = generatedNames[lastGeneratedId]; id = lastGeneratedId++; } public String getName() { return name; } public int getId() { return id; } //returns true if work was successful. otherwise returns false (crisis) public abstract boolean work(); public String toString() { return...

  • Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int...

    Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Here is the code for the Infant class: public class Infant{ private String name; private int...

    Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...

  • Given the following code fragment public class Point { public int x; // Because these are...

    Given the following code fragment public class Point { public int x; // Because these are public, you can access them public int y; // directly without getters and setters }; public class Rectangle { private Point ll; // the lower left corner of the rectangle private Point ur; // the upper right corner of the rectangle public Point getLLPoint() {return ll;} public Point getURPoint() {return ur;} } (a) Write a method equals for the Rectangle class that takes a...

  • Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private...

    Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT