In: Computer Science
Class Employee (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);
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.
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);
}
}
********************************** Manager.java *************************************************
public class Manager extends Employee{
public String department;
public Manager(String name,String salary,String
department){
super(name,salary);
this.department = department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDepartment() {
return this.department;
}
public String toString(){
return(super.strName +" manager of
department "+this.department+ " has a salary of " +
super.strSalary);
}
}
*********************************** TestDriver.java ***************************************
import java.util.Scanner;
public class TestDriver {
public static void main(String[] args) {
Scanner s = new
Scanner(System.in);
int i = 1;
int n = s.nextInt();
s.nextLine();
Manager[] arr;
if(n<=3)
arr = new Manager[n];
else
arr = new Manager[3];
while(i<=arr.length) {
System.out.println("Enter "+i+"th manager's name: ");
String name =
s.nextLine();
System.out.println("Enter "+i+"th manager's salary: ");
String salary =
s.nextLine();
System.out.print("Enter "+i+"th manager's department: ");
String
department = s.nextLine();
arr[i-1] = new
Manager(name,salary,department);
i++;
}
i=0;
while(i<arr.length) {
System.out.println(arr[i].toString());
i++;
}
}
}
************************************ Console_Output ScreenShot *******************************************
If you will face any doubt in code, let me know through comment.
Thanks :)