11.7 Program: Employees and toString() Note: This is VERY similar to the Example
ID: 3938776 • Letter: 1
Question
11.7 Program: Employees and toString()
Note: This is VERY similar to the Example from this chapter
Make a class Employee with attributes name (type String) and salary (type double).
Make a class Manager inherit from Employee. Add an instance variable, named department, of type String. Supply a method toString that prints the manager’s name, department, and salary.
Make a class Executive inherit from Manager (no additional attributes). Supply appropriate toString methods for all classes.
Provide default and non-default contractors for all classes as well as getters and setters for all attributes.
Supply a test program that tests these classes and methods.
For objects created and printed as:
The output would be:
NOTE: This example is the test case I will be using
L
Lab Submission
11.7.1: Program: Employees and toString()
Instructions
Deliverables
Main.java
Executive.java
Employee.java
Manager.java
We will expect the above file(s) to be submitted
Compile command
javac Main.java Executive.java Employee.java Manager.java -Xlint:all
We will use this command to compile your code
Submit your files below by dragging and dropping into the area or choosing a file on your hard drive.
Main.java
Executive.java
Employee.java
Manager.java
Drag files to left, or
Instructions
Deliverables
Main.java
,Executive.java
,Employee.java
andManager.java
We will expect the above file(s) to be submitted
Compile command
javac Main.java Executive.java Employee.java Manager.java -Xlint:all
We will use this command to compile your code
Submit your files below by dragging and dropping into the area or choosing a file on your hard drive.
Main.java
Executive.java
Employee.java
Manager.java
Drag files to left, or
Explanation / Answer
1) Employee.java
public class Employee {
String name;
double salary;
public Employee() {
}
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;
}
public String toString(){//overriding the toString() method
return "Employee: name = "+name+" salary = "+salary;
}
}
2) Manager.java
public class Manager extends Employee {
String department;
public Manager() {
}
public Manager(String name, double salary, String department) {
super(name, salary);
this.department = department;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String toString(){//overriding the toString() method
return "Manager: name = "+name+" salary = "+salary+" department = "+department;
}
}
3) Executive.java
public class Executive extends Manager{
public Executive() {
}
public Executive(String name, double salary, String department) {
super(name, salary, department);
}
public String toString(){//overriding the toString() method
return "Executive: name = "+name+" salary = "+salary+" department = "+department;
}
}
4) Main.java
public class Main {
public static void main(String[] args) {
Employee Employee("Bob Smith",20000);
Manager two = new Manager("Sue Jones", 40000, "Human Resources");
Executive three = new Executive("Carl Turner",60000,"Operations");
System.out.println(one);
System.out.println(two);
System.out.println(three);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.