233k views
0 votes
Use Java to 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; 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 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.

User Mr Sam
by
5.2k points

1 Answer

2 votes

Answer:

Hi Mitutk! Very good question for Java programmers to test knowledge on classes, constructors and mutator methods. Please find the answer with explanation below.

Step-by-step explanation:

Constructors in Java can be declared with different parameters. Declaring the properties of the Employee object as private, such as the name, id, department and position as in this example, is good design practice as it helps to keep data secure. We can get and set these properties by accessor methods as declared below. The second program can be written a few different ways, try using the set methods for storing the different info instead of the constructors used.

Employee.java

class Employee {

private String employeeName;

private int idNumber;

private String department;

private String position;

public Employee() {

employeeName = "";

idNumber = 0;

department = "";

position = "";

}

public Employee(String name, int id, String dept, String pos) {

employeeName = name;

idNumber = id;

department = dept;

position = pos;

}

public Employee(String name, int id) {

employeeName = name;

idNumber = id;

department = "";

position = "";

}

public void set_employee_name(String empName) {

employeeName = empName;

}

public String get_employee_name() {

return employeeName;

}

public void set_employeeId(int empId) {

idNumber = empId;

}

public int get_employeeId() {

return idNumber;

}

public void set_department(String dept) {

department = dept;

}

public String get_department() {

return department;

}

public void set_position(String pos) {

position = pos;

}

public String get_position() {

return position;

}

}

EmployeeProgram.java

import Employee.java;

public class EmployeeProgram {

public static void main(String args[]) {

Employee emp1 = new Employee("Susan Meyers", 47899, "Accounting", "Vice President");

Employee emp2 = new Employee("Mark Jones", 39119, "IT", "Programmer");

Employee emp3 = new Employee("Joy Rogers", 81774, "Manufacturing", "Engineer");

System.out.println(emp1.get_employee_name());

System.out.println(emp2.get_employee_name());

System.out.println(emp3.get_employee_name());

}

}

User Mvlupan
by
6.0k points