Write a class named Employee that has the following fields
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 variables 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 variables as arguments and assigns them to
the appropriate fields: employee’s name, employee’s ID number( the department and
position fields should be assigned an empty string “”).
- a default constructor that assigns empty strings to the name, department, and position
fields, and 0 to the idNumber field.
Write appropriate 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 that creates three Employee objects to hold the following data:
NameID NumberDepartmentPosition
Susan Meyers47899AccountingVice President
Mark Jones39119ITProgrammer
Joy Rogers81774MarketingEngineer
The program should store this data in the three objects and then display the data for each employee on the screen.
Im not sure if im doing this right, but here\'s my source code
package employee;
public class Employee {
private String name;
private String position;
private String dept;
private int idnumber;
public Employee()
{
name = null;
position = null;
dept = null;
idnumber = 0;
}
public Employee(String name, String position, String dept, int idnumber)
{
this.name = name;
this.position = \" \";
this.dept = \" \";
this.idnumber = idnumber;
}
public String getName()
{
return name;
}
public String getPosition()
{
return position;
}
public String getDept()
{
return dept;
}
public int getID()
{
return idnumber;
}
}
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\");
emp3.setPosition(\"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 Position: Engineer


