Create a class named Employee with private name and a salary data fields, a toSt
ID: 3828463 • 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.
Explanation / Answer
JAVA :
import java.util.*;
import java.io.*;
class Employee{
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
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;
}
@Override
public String toString() {
return "Employee{" + "name=" + name + ", salary=" + salary + '}';
}
}
class Manager extends Employee{
private String department;
public Manager(String department, String name, double salary) {
super(name, salary);
this.department = department;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return "Manager{" + "name=" + getName() + ", salary=" + getSalary() + "depattment=" + department + '}';
}
}
class Director extends Manager{
private double stipend;
public Director(double stipend, String department, String name, double salary) {
super(department, name, salary);
this.stipend = stipend;
}
@Override
public String toString() {
return "Director{" + "name=" + getName() + ", salary=" + getSalary() + "depattment=" + getDepartment() + "Stipend = "+stipend+"'}";
}
public double getStipend() {
return stipend;
}
public void setStipend(double stipend) {
this.stipend = stipend;
}
}
public class Sample1 {
public static void main(String args[]) throws IOException {
Employee e = new Employee("Alex", 7500);
Manager m = new Manager("HGBU","PD",10000);
Director d = new Director(1454,"Admin","Lary",45454);
System.out.println(e);
System.out.println(m);
System.out.println(d);
}
}
OUTPUT :
Employee{name=Alex, salary=7500.0}
Manager{name=PD, salary=10000.0depattment=HGBU}
Director{name=Lary, salary=45454.0depattment=AdminStipend = 1454.0'}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.