Java Write a class named Employee that has the following fie

Java

Write a class named Employee that has the following fields: name. the name field references a String object that holds the employee\'s name. idNumber. The idNumber is an int variable that holds the employee\'s ID number department. The department field references a String object that holds the name of the department where the employee works. position. The position field references a String object that holds the employee\'s job title. The class should have the following constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate fields employee\'s name, employee\'s ID number, department, and position. A constructor that accepts the following values as arguments and assigns them to the appropriate fields: employee\'s name and ID number. The department, and position fields should be assigned an empty string (\" \"). A no-arg constructor that assigns empty strings (\" \") to the name, department, and position fields, and 0 to the idNumber field. Write a ppropriate mutator methods that store values in these fields and accessor methods that return the values in these fields. Once you have written the class, write a separate program the creates three Employee objects to hold the following data: Name ID Number Department Position Susan Meyers 47899 Accounting Vice President Mark Jones 39119 IT Programmer Joy Rogers 81774 Manufacturing Engineer The program should store this data in three objects and then display the data for each employee on the screen.

Solution

EmployeeTest.java


public class EmployeeTest {

   public static void main(String[] args) {
       Employee emp1 = new Employee(\"Susan Meyers\",47899 , \"Accounting\",\"Vice President\");
       Employee emp2 = new Employee(\"Mark Jones\", 39119 );
       emp2.setPosition(\"IT Programmer\");
       emp2.setDepartment(\"Computer Science\");
       Employee emp3 = new Employee();
       emp3.setName(\"Joy Rogers\");
       emp3.setIdNumber(81774 );
       emp3.setDepartment(\"Mechanical Engineer\");
       emp3.setPosition(\"Manufacturing Engineer\");
       System.out.println(\"First employee details......\");
       System.out.println(emp1.toString());
       System.out.println(\"Second employee details......\");
       System.out.println(emp2.toString());
       System.out.println(\"Third employee details......\");
       System.out.println(emp3.toString());      
   }

}

Employee.java


public class Employee {
   private String name;
   private int idNumber;
   private String department;
   private String position;
   public Employee(String name, int idNumber, String department, String position){
       this.name = name;
       this.idNumber = idNumber;
       this.department = department;
       this.position = position;
   }
   public Employee(String name, int idNumber){
       this.name = name;
       this.idNumber = idNumber;
       this.department = \"\";
       this.position = \"\";
   }
   public Employee(){
       this.name = \"\";
       this.idNumber = 0;
       this.department = \"\";
       this.position = \"\";
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getIdNumber() {
       return idNumber;
   }
   public void setIdNumber(int idNumber) {
       this.idNumber = idNumber;
   }
   public String getDepartment() {
       return department;
   }
   public void setDepartment(String department) {
       this.department = department;
   }
   public String getPosition() {
       return position;
   }
   public void setPosition(String position) {
       this.position = position;
   }  
   public String toString(){
       return \"Employee Name: \"+getName()+\" ID Number: \"+getIdNumber()+\" Department: \"+getDepartment()+\" Position: \"+getPosition();
   }
}

Output:

First employee details......
Employee Name: Susan Meyers ID Number: 47899 Department: Accounting Position: Vice President
Second employee details......
Employee Name: Mark Jones ID Number: 39119 Department: Computer Science Position: IT Programmer
Third employee details......
Employee Name: Joy Rogers ID Number: 81774 Department: Mechanical Engineer Position: Manufacturing Engineer

Java Write a class named Employee that has the following fields: name. the name field references a String object that holds the employee\'s name. idNumber. The
Java Write a class named Employee that has the following fields: name. the name field references a String object that holds the employee\'s name. idNumber. The

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site