In: Computer Science
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method).
You will create a new class called Manager that inherits from the class Employee. Add a field named department of type String. Supply a method toStringthat prints the manager's name, department, and salary. Remember, you may not change anything in the Employee class.
You will then create a test class that uses the Manager class. The test class should prompt the user to enter name, department and salary. This information should be stored in an array. Upon entry of 3 employee records, the program should output the information you entered.
Your program should be able to handle a maximum of 3 entries.
You may write the program as a console application or using dialog boxes.
Please remember to include the needed comments at the top of your .java file which will identify this
Hand in:
1. A typed copy of the algorithm
2. The printed copy of your UML Class Diagram
3. The printed copy of your working source code with comments
1. ALGORITHM:
Algorithm to create 3 Manager objects which inherits the employee class:
Start:
Step 1:
1. Create class Employee
2. Add instance variable name (string) and salary(number)
3. Create constructor to set name to blank and salary = 0
4. Create another constructor to set name and salary same as passed parameter values
5. Create setters for name and salary which sets the value of name and salary same as passed variables as parameters.
6. Create getters for name and salary which returns name and salary.
7. Create a toString method which displays employee name and employee salary in formatted output
Step 2:
8. Create a new class Manager which extends Employee
9. Add instance variable department(string)
10. Create a getter for department that returns the department value
11. Create a setter for department that sets the department value same as passed in parameter.
12. Create a toString method which calls Employee’s toString() and add deprtament details in it.
Step 3:
13. Create last class TestManager
14. Create array of Manager class object having size 3.
15. Create name, salary and department for all objects and put in array.
16. Display the manager details for all 3 manager objects using toString method.
2. CLASSDIAGRAM:
The given problem has two classes Employee and Manager where the manager extends the Employee class. All the data members and member functions are shown in the below diagram:
3. PROGRAM:
The solution will have 3 classes Employee, Manager and TestManager where TestManager is driver class to call the other classes by instantiating their objects.
Employee.java:
public class Employee {
private String name;
private int salary;
//default constructor
public Employee(){
name = "";//set name to blank
salary = 0;//set salary to 0
}
public Employee(String name, int salary){//parameterised constructor
this.name=name;//set name same as passed name
this.salary=salary;//set salary same as passed salary
}
//getters for the instance variables
public String getName() {
return name;//return name
}
public int getSalary() {
return salary; //return salary
}
//setter for setting the values
public void setName(String name) {
this.name = name;//set name as passed variable
}
public void setSalary(int salary) {
this.salary = salary;//set salary as passed variable
}
public String toString() {//return formatted details about employee
return "\n\n================\n\nEmployee details:\n================\nEmployee Name: " + name + "\nSalary: " + salary ;
}
}
Manager.java:
public class Manager extends Employee{
private String department;
//getter for department
public String getDepartment() {
return department; //return department
}
//setter for department
public void setDepartment(String department) {
this.department = department;//set department same as passed value
}
@Override
public String toString() {//print formatted details of manager employee
return super.toString()+"\nType: Manager\nDepartment: " + department;//super.toString will print the employee other details
}
}
TestManager.java:
import java.util.Scanner;//to use scanner object while reading inputs
public class TestManager {
public static void main(String[] args) {
Scanner scObject = new Scanner(System.in);//create object of Scanner to read inputs
//local variables to read the values from user
String nm, dept;
int salary;
Manager[] managerArray = new Manager[3];//and array of employees which holds 3 objects
managerArray[0] = new Manager();//put first object of manager at index 0
managerArray[1] = new Manager();//put second object of manager at index 1
managerArray[2] = new Manager();//put third object of manager at index 2
for(int i = 0; i < 3; i++) {
System.out.println("Enter details for manager: "+ (i+1)+"\n=============\n");
System.out.print("Enter Name: ");
nm = scObject.nextLine();
System.out.print("Enter Salary: ");
salary = scObject.nextInt();
scObject.nextLine();//remove \n after reading an integer
System.out.print("Enter Department: ");
dept = scObject.nextLine();
//set the read out values to the manager object at index i
managerArray[i].setName(nm);
managerArray[i].setSalary(salary);
managerArray[i].setDepartment(dept);
}//end for when objects are set for all employees
for(int i = 0; i < 3; i++) {//this loop will print all 3 objects of manager stores in array
System.out.println(managerArray[i]);
}//end for
}//end main
}//end class
4. OUTPUT: