Question

1. Employees and overriding a class method The Java program (check below) utilizes a superclass named...

1. Employees and overriding a class method

The Java program (check below) utilizes a superclass named EmployeePerson (check below) and two derived classes, EmployeeManager (check below) and EmployeeStaff (check below), each of which extends the EmployeePerson class. The main program creates objects of type EmployeeManager and EmployeeStaff and prints those objects.

  1. Run the program, which prints manager data only using the EmployeePerson class' printInfo method.
  2. Modify the EmployeeStaff class to override the EmployeePerson class' printInfo method and print all the fields from the EmployeeStaff class. Run the program again and verify the output includes the manager and staff information.
  3. Modify the EmployeeManager class to override the EmployeePerson class' printInfo method and print all the fields from the EmployeeManager class. Run the program again and verify the manager and staff information is the same.

Sample Input and Output:

Name: Michele, Department: Sales, Birthday: 03-03-1975, Salary: 70000, Staff managed: 25
Name: Bob, Department: Sales, Birthday: 02-02-1980, Salary: 50000, Manager: Michele

-----------------------------------------------------------

Java program:

package Labs.Lab09.Sample;

public class EmployeeMain {

public static void main(String [] args) {

// Create the objects

EmployeeManager manager = new EmployeeManager(25);

EmployeeStaff staff1 = new EmployeeStaff("Michele");

// Load data into the objects using the Person class' method

manager.setData("Michele", "Sales", "03-03-1975", 70000);

staff1.setData ("Bob", "Sales", "02-02-1980", 50000);

// Print the objects

manager.printInfo();

staff1.printInfo();

}

}

EmployeePerson:

package Labs.Lab09.Sample;

public class EmployeePerson {

protected String fullName; // Format: last name, first name

protected String departmentCode;

protected String birthday;

protected int annualSalary;

//

***********************************************************************

// Default constructor. Set protected variables to the empty string or

0

public EmployeePerson() {

fullName = "";

departmentCode = "";

birthday = "";

annualSalary = 0;

}

//

***********************************************************************

// Constructor with parameters to set the private variables

public EmployeePerson(String empFullName, String empDepartmentCode,

String empBirthday, int empAnnualSalary) {

setData(empFullName, empDepartmentCode, empBirthday,

empAnnualSalary);

}

//

***********************************************************************

public void setData(String empFullName, String empDepartmentCode,

String empBirthday, int empAnnualSalary) {

fullName = empFullName;

departmentCode = empDepartmentCode;

birthday = empBirthday;

annualSalary = empAnnualSalary;

}

//

***********************************************************************

public void printInfo() {

System.out.print("Name: " + fullName + ", Department: " +

departmentCode + ", Birthday: " + birthday +

", Salary: " + annualSalary);

}

}

EmployeeManager:

package Labs.Lab09.Sample;

public class EmployeeManager extends EmployeePerson {

private int numManaged; // Number of staff managed

//

***********************************************************************

// Default constructor

public EmployeeManager() {

numManaged = 0;

}

//

***********************************************************************

// Constructor with parameters

public EmployeeManager(int nManaged) {

numManaged = nManaged;

}

//

***********************************************************************

// Get the number of people managed

public int getNumManaged() {

return numManaged;

}

//

***********************************************************************

// FIXME: Override the EmployeePerson class' printInfo method with a

// FIXME: printInfo method to print all manager fields here.

}

EmployeeStaff:

package Labs.Lab09.Sample;

public class EmployeeStaff extends EmployeePerson {

private String managerName;

//

***********************************************************************

// Default constructor

public EmployeeStaff() {

managerName = "";

}

//

***********************************************************************

// Constructor with parameters

public EmployeeStaff(String reportsTo) {

managerName = reportsTo;

}

//

***********************************************************************

// Get the name of the manager

public String getManagerName() {

return managerName;

}

//

***********************************************************************

// FIXME: Override the EmployeePerson class' printInfo method with a

// FIXME: printInfo method to print all staff fields.

@Override

public void printInfo() {

}

}

Thank you.

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

//########################## EmployeeManager.java ####################

package Labs.Lab09.Sample;

public class EmployeeManager extends EmployeePerson {

   private int numManaged; // Number of staff managed
   //***********************************************************************
   // Default constructor
   public EmployeeManager() {
       numManaged = 0;
   }

   //***********************************************************************
   // Constructor with parameters
   public EmployeeManager(int nManaged) {
       numManaged = nManaged;
   }
   //***********************************************************************
   // Get the number of people managed
   public int getNumManaged() {
       return numManaged;
   }

   //***********************************************************************
   // FIXME: Override the EmployeePerson class' printInfo method with a
   // FIXME: printInfo method to print all manager fields here.
   public void printInfo() {
       super.printInfo();
       System.out.println(", Staff managed: "+numManaged);
   }
}

//############################# EmployeeStaff.java ############################

package Labs.Lab09.Sample;

public class EmployeeStaff extends EmployeePerson {

   private String managerName;
   //***********************************************************************
   // Default constructor

   public EmployeeStaff() {
       managerName = "";
   }

   //***********************************************************************
   // Constructor with parameters
   public EmployeeStaff(String reportsTo) {
       managerName = reportsTo;
   }
   //***********************************************************************
   // Get the name of the manager
   public String getManagerName() {
       return managerName;
   }
   //***********************************************************************
   // FIXME: Override the EmployeePerson class' printInfo method with a
   // FIXME: printInfo method to print all staff fields.

   @Override
   public void printInfo() {
       super.printInfo();
       System.out.println(", Manager: "+managerName);
   }

}

//################################### EmployeePerson.java ########

package Labs.Lab09.Sample;

public class EmployeePerson {

   protected String fullName; // Format: last name, first name
   protected String departmentCode;
   protected String birthday;
   protected int annualSalary;

   //***********************************************************************
   // Default constructor. Set protected variables to the empty string or 0
   public EmployeePerson() {
       fullName = "";
       departmentCode = "";
       birthday = "";
       annualSalary = 0;
   }

   //***********************************************************************
   // Constructor with parameters to set the private variables
   public EmployeePerson(String empFullName, String empDepartmentCode,
           String empBirthday, int empAnnualSalary) {
       setData(empFullName, empDepartmentCode, empBirthday,empAnnualSalary);
   }

   //***********************************************************************
   public void setData(String empFullName, String empDepartmentCode,String empBirthday, int empAnnualSalary) {
       fullName = empFullName;
       departmentCode = empDepartmentCode;
       birthday = empBirthday;
       annualSalary = empAnnualSalary;
   }

   public void printInfo() {
       System.out.print("Name: " + fullName + ", Department: " +departmentCode + ", Birthday: " + birthday +", Salary: " + annualSalary);
   }
}

//############################# EmployeeMain.java ########################

package Labs.Lab09.Sample;

public class EmployeeMain {

   public static void main(String [] args) {
       // Create the objects
       EmployeeManager manager = new EmployeeManager(25);
       EmployeeStaff staff1 = new EmployeeStaff("Michele");

       // Load data into the objects using the Person class' method
       manager.setData("Michele", "Sales", "03-03-1975", 70000);
       staff1.setData ("Bob", "Sales", "02-02-1980", 50000);

       // Print the objects
       manager.printInfo();
       staff1.printInfo();
   }

}

//################################## PGM END ###########################

OUTPUT
########

Add a comment
Know the answer?
Add Answer to:
1. Employees and overriding a class method The Java program (check below) utilizes a superclass named...
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
  • Write Java program( see IncomeTaxClassTemplate) uses a class( TaxTableToolTemplate), which has a tax table built in....

    Write Java program( see IncomeTaxClassTemplate) uses a class( TaxTableToolTemplate), which has a tax table built in. The main method prompts for a salary, then uses a TaxTableTools method to get the tax rate. The program then calculates the tax to pay and displays the results to the user. Run the program with annual salaries of 10000, 50000, 50001, 100001 and -1 (to end the program) and note the output tax rate and tax to pay. a. Modify the TaxTableTools class...

  • Write Java program ( see IncomeTaxOverloadClassTemplate) calculates a tax rate and tax to pay given an...

    Write Java program ( see IncomeTaxOverloadClassTemplate) calculates a tax rate and tax to pay given an annual salary. The program uses a class,(see TaxTableToolsOverloadTemplate), which has the tax table built in. Run the program with annual salaries of 10000, 50000, 50001, 100001 and -1 (to end the program) and note the output tax rate and tax to pay. Overload the constructor. Add to the TaxTableTools class an overloaded constructor that accepts the base salary table and corresponding tax rate table...

  • Task 3: Main Program Create a main program class that: Creates three or more Nurse instances...

    Task 3: Main Program Create a main program class that: Creates three or more Nurse instances assigned to different shifts. Creates three or more Doctor instances. Creates three or more Patient instances with pre-determined names and manually assigned physicians chosen from the pool of Doctor instances previously created. Generates another 20 Patient instances using randomly generated names and randomly assigns them physicians chosen from the pool of Doctor instances previously created. Prints the toString() values for all employees. Prints the...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • For Java please.Artwork. javaimport java.util.Scanner;public class ArtworkLabel {public static void main(String[] args)...

     Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...

  • in java Define a class named ComparableNames thats extends Person and implements comparable Define a class...

    in java Define a class named ComparableNames thats extends Person and implements comparable Define a class named ComparableNames that extends Person (defined below) and implements Comparable. Override the compare To method to compare the person on the basis of name. Write a test class to find the order of two ComparableNames. class Person String name; Person00 Person(String n) name=n: Hint: in class ComparableName, you only need a constructor with arg to initialize the data filed defined in the super class...

  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • CODING CHALLENGE 01 Write a Java program that tests the following Distance class. In [5]: class...

    CODING CHALLENGE 01 Write a Java program that tests the following Distance class. In [5]: class Distance private int feet, inches; public Distance() { this.feet = 0; this.inches = 0; public Distance(int f, int n) { this. feet = f; this. inches = n; public String toString() { return String.format("%d' $d\"", this. feet, this.inches); In the main function of this program, create two distance objects (one per each constructor). Print both objects to the console.

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