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 answer this by creating a code for a java eclipse. Make sure the code runs smoothly. Also post screen shots of it running
Program
Employee.java
/**
* Create a class Employee
* With attributes of name and salary
* Appropriate constructors,getters and setters to manage employee
attributes
* Use toString override to formatted attributes display
* @author deept
*
*/
public class Employee {
//Attributes
private String name;
private double salary;
//Default constructor set name as blank and salary as
0.00
public Employee() {
name="";
salary=0.00;
}
//Parameterized constructor set name as passed name
and salary as passed value
public Employee(String name,double salary) {
this.name=name;
if(salary<0) {
this.salary=0.00;
}
else {
this.salary=salary;
}
}
//Getters or accessors
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
//Setters or mutators
public void setName(String name) {
this.name = name;
}
public void setSalary(double salary) {
if(salary<0) {
this.salary=0.00;
}
else {
this.salary=salary;
}
}
//Override toString() for formatted display
public String toString() {
return "Employee name: "+name+",
Salary: $"+String.format("%.2f",salary);
}
}
Manager.java
/**
* Class Manager
* Inherit from Employee
* With additional attribute department
* Appropriate constructors,getters and setters to manage manger
attribute
* Use toString override to formatted attributes display
* @author deept
*
*/
public class Manager extends Employee{
//Attribute
private String department;
//Default constructor,set department as blank
public Manager() {
super();
department="";
}
//Parameterized constructor,set department as
blank
public Manager(String name,double salary,String
department) {
super(name,salary);
this.department=department;
}
//Getter
public String getDepartment() {
return department;
}
//Setter
public void setDepartment(String department) {
this.department = department;
}
//Override toString() for formatted display
public String toString() {
return super.toString()+",
Department: "+department;
}
}
Test.java
import java.util.Scanner;
/**
* Create a test class
* Create manage object array of maximum 3 Managers
* Display all Manger details
* @author deept
*
*/
public class Test {
public static void main(String[] args) {
//Scanner object
Scanner sc=new
Scanner(System.in);
//Create 3 Manager object
array
Manager managers[]=new
Manager[3];
//Prompt to enter manager
details
for(int i=0;i<3;i++) {
System.out.println("Enter Manager"+(i+1)+" details:-");
System.out.print("Enter name: ");
String
name=sc.nextLine();
System.out.print("Enter department: ");
String
department=sc.nextLine();
System.out.print("Enter salary: ");
double
salary=sc.nextDouble();
//Set Manger
into array
managers[i]=new
Manager(name,salary,department);
sc.nextLine();
System.out.println();
}
//Display manager's details
System.out.println("\nDisplay
Manager details:-");
for(int i=0;i<3;i++) {
System.out.println(managers[i]);
}
}
}
------------------------------------------------------------
Output
Enter Manager1 details:-
Enter name: Micheal
Enter department: Finanace
Enter salary: 20000
Enter Manager2 details:-
Enter name: Adorn
Enter department: IT
Enter salary: 22000
Enter Manager3 details:-
Enter name: Mark
Enter department: Marketting
Enter salary: 18000
Display Manager details:-
Employee name: Micheal, Salary: $20000.00, Department:
Finanace
Employee name: Adorn, Salary: $22000.00, Department: IT
Employee name: Mark, Salary: $18000.00, Department: Marketting