Write a class named Employee that has the following fields. name. The name field
ID: 3816980 • Letter: W
Question
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 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: The program should store this data in the three objects and then display the data for each employee on the screen.Explanation / Answer
public class Employee
{
String name;
int idNumber;
String department;
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 empname,int id)
{
this.name=empname;
this.idNumber=id;
this.department=NULL;
this.position=NULL;
}
public Employee()
{
this.name=empname;
this.idNumber=0;
this.department=NULL;
this.position=NULL;
}
public String getName() {
return name;
}
public String getId() {
return idNumber;
}
public String getDepartment() {
return department;
}
public String getPosition() {
return position;
}
}
class EmployeeDemo
{
public static void main(String args[]){
Employee e = new Employee("Susan Meyers",47899,"Accounting","Vice President");
Employee e1=new Employee("Mark jones",39119,"IT","Programmar");
Employee e2= new Employee(""Joy Rogers",81774,"Manufacturing","Engineer");
System.out.println("Employee name:"+e.getName());
System.out.println("Employee id:"+e.getId());
System.out.println("Employee department:"+e.getDepartment());
System.out.println("Employee position:"+e.getPosition());
System.out.println("Employee name:"+e1.getName());
System.out.println("Employee id:"+e1.getId());
System.out.println("Employee department:"+e1.getDepartment());
System.out.println("Employee position:"+e1.getPosition());
System.out.println("Employee name:"+e2.getName());
System.out.println("Employee id:"+e2.getId());
System.out.println("Employee department:"+e2.getDepartment());
System.out.println("Employee position:"+e2.getPosition());
}}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.