Create a class named Employee with private name and a salary data fields, a toSt
ID: 3828464 • Letter: C
Question
Create a class named Employee with private name and a salary data fields, a toString method to print the data, and get and set methods for the two private data items. Create another class named Manager with a field named department that inherits from the Employee class. Include a toString() method that prints the manager’s name, department, and salary. Create a third class named Director that inherits from the Manager class and has a field named stipendAmount. Supply the toString() method for Director that prints all of its instance variables. Also, write a main program named myOutput that instantiates separate objects using each of the classes and invokes the toString() method to print out each of its instance variables. (15 points) 4. Add overloaded constructors to the Employee and Manager classes where you to pass the name and salary using the Employee constructor and pass the name, salary, and department to the Manager constructor. Write a test class to instantiate a manager object and display its contents. (15 points)
Explanation / Answer
Employee.java
public class Employee {
//Declaring variables
private String name;
private double salary;
//Parameterized constructor
public Employee(String name) {
super();
this.name = name;
}
//Parameterized constructor
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
//getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return " Name=" + name + ", Salary=" + salary ;
}
}
__________________
Manager.java
public class Manager extends Employee {
// Declaring instance variables
private String department;
// Parameterized constructor
public Manager(String name, double salary, String department) {
super(name, salary);
this.department = department;
}
// Parameterized constructor
public Manager(String name, String department) {
super(name);
this.department = department;
}
// getters and setters
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString() + " Department=" + department;
}
}
____________________
Director.java
public class Director extends Manager {
// Declaring instance variables
private double stipendAmount;
// Parameterized constructor
public Director(String name, String department, double stipendAmount) {
super(name, department);
this.stipendAmount = stipendAmount;
}
// getters and setters
public double getStipendAmount() {
return stipendAmount;
}
public void setStipendAmount(double stipendAmount) {
this.stipendAmount = stipendAmount;
}
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString() + " StipendAmount=" + stipendAmount;
}
}
__________________
MyOutput.java
import javax.security.auth.login.AccountException;
public class MyOutput {
public static void main(String[] args) {
//Creating an Object Of Employee class object
Employee acc=new Employee("Kane Williams",50000);
//Displaying the Employee object info
System.out.println("Employee Info :"+acc.toString());
//Creating an Object Of Manager class object
Manager m=new Manager("Ricky Pointing", 45000, "Accounts");
//Displaying the Employee object info
System.out.println("Manager Info :"+m.toString());
//Creating an Object Of Director class object
Director d=new Director("Pat Simcox", "Managing Dept", 30000);
//Displaying the Employee object info
System.out.println("Director Info :"+d.toString());
}
}
___________________
Output:
Employee Info : Name=Kane Williams, Salary=50000.0
Manager Info : Name=Ricky Pointing, Salary=45000.0 Department=Accounts
Director Info : Name=Pat Simcox, Salary=0.0 Department=Managing Dept StipendAmount=30000.0
__________________
Test.java
public class Test {
public static void main(String[] args) {
Manager man=new Manager("Ken Williams",66000,"Services");
System.out.println("Manager Info :"+man.toString());
}
}
__________________________
Output:
Manager Info : Name=Ken Williams, Salary=66000.0 Department=Services
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.