In: Computer Science
ALL IN JAVA
public class Employee
{
public String strName, strSalary;
public Employee()
{
strName = " ";
strSalary = "$0";
}
public Employee(String Name, String Salary)
{
strName = Name;
strSalary = Salary;
}
public void setName(String Name)
{
strName = Name;
}
public void setSalary(String Salary)
{
strSalary = Salary;
}
public String getName()
{
return strName;
}
public String getSalary()
{
return strSalary;
}
public String toString()
{
return(strName + " has a salary of
" + strSalary);
}
}
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 toString that 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.
// Employee.java
public class Employee
{
public String strName, strSalary;
public Employee()
{
strName = " ";
strSalary = "$0";
}
public Employee(String Name, String Salary)
{
strName = Name;
strSalary = Salary;
}
public void setName(String Name)
{
strName = Name;
}
public void setSalary(String Salary)
{
strSalary = Salary;
}
public String getName()
{
return strName;
}
public String getSalary()
{
return strSalary;
}
public String toString()
{
return(strName + " has a salary of
" + strSalary);
}
}
//end of Employee.java
// Manager.java
public class Manager extends Employee
{
// department
private String department ;
// default constructor to set department to empty
string
public Manager()
{
super(); // calls Employee
constructor
department = "";
}
// parameterized constructor
public Manager(String Name, String Salary, String
Department)
{
super(Name, Salary); // calls
Employee constructor
department = Department; // sets
department
}
// return String representation of Manager
public String toString()
{
// return the string representation
of employee and append the department
return super.toString()+" works in
department "+department;
}
}
//end of Manager.java
// EmployeeTester.java
import java.util.Scanner;
public class EmployeeTester {
public static void main(String[] args) {
// create an array of 3
Employee
Employee[] employees = new
Employee[3];
String name, salary,
department;
Scanner sc = new
Scanner(System.in);
// loop to input details of 3
Managers
for(int
i=0;i<employees.length;i++)
{
System.out.print("Enter Manager name: ");
name =
sc.nextLine();
System.out.print("Enter Manager salary: ");
salary =
sc.nextLine();
System.out.print("Enter Manager department: ");
department =
sc.nextLine();
System.out.println();
employees[i] =
new Manager(name, salary, department);
}
// display the details of the
managers entered
System.out.println("Manager
details:");
for(int
i=0;i<employees.length;i++)
System.out.println(employees[i]);
}
}
//end of EmployeeTester.java
Output: