简体   繁体   中英

Java code works, but unsure if I have fulfilled the requirements (objects and classes)

I got this homework requirement, and I've written a solution for it. But unsure if I got the answer right. Code works and compiles.

Question

And this is my java file (2 file)

First is the primary file

public class Employee
{
String name;
int idNumber;
String department;
String position;

public Employee(String emName, int idNum, String depart, String pos)
{
    name = emName;
    idNumber = idNum;
    department= depart;
    position = pos;
}

public Employee(String emName, int idNum)
{
    name = emName;
    idNumber = idNum;
    department= "";
    position = "";
}

public Employee()
{
    name = "";
    idNumber = 0;
    department= "";
    position = "";
}

//set name
public void setName(String nameSet)
{
    name = nameSet;
}

//get name
public String getName()
{
    String nameGet = name;
    return nameGet;
}

//set idnum
public void setID(int idSet)
{
    idNumber = idSet;
}

//get idnum
public int getId()
{
    int idGet = idNumber;
    return idGet;
}

//
//set department
public void setDep(String depSet)
{
    department = depSet;
}

//get department
public String getDep()
{
    String depGet = department;
    return depGet;
}

//
//set position
public void setPos(String posSet)
{
    position = posSet;
}

//get position
public String getPos()
{
    String posGet = position;
    return posGet;
}

}

Second adds data and prints data

       public class Employee_test
{
public static void main(String[] args)
{
    Employee employee1 = new Employee("Susan Meyers", 47899, "Accounting", "Vice President");
    Employee employee2 = new Employee("Mark Jones", 39119, "IT", "Programmer"); //use second with Mark
    //Employee employee2 = new Employee("Mark Jones", 39119);
    Employee employee3 = new Employee("Joy Rogers", 81774, "Manufacturing", "Engineer"); //use third with Joy
    //Employee employee3 = new Employee();

    System.out.println("Name           ID Number   Department     Position");
    System.out.println("");
    System.out.println(employee1.getName() + "   " + employee1.getId() +  "       " + employee1.getDep() + "     " + employee1.getPos());
    System.out.println(employee2.getName() + "     " + employee2.getId() +  "       " + employee2.getDep() + "             " + employee2.getPos());
    System.out.println(employee3.getName() + "     " + employee3.getId() +  "       " + employee3.getDep() + "  " + employee3.getPos());
    System.out.println("");
}

}

Here's the output when I run the employee_test file.

Output terminal

What am I confused at.

Question says

Make sure you use all three constructors. Use the first constructor with Susan, the second with Mark and the third one with Joy.

How can the output be achieved if the question wants the 2nd employee to use a constructor that only have (name, id number) and third one has nothing (). Thanks in advance.

Use setters to fill in the other data:

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

Employee employee2 = new Employee("Mark Jones", 39119);
employee2.setDep("IT");
employee2.setPos("Programmer");

Employee employee3 = new Employee();
employee3.setName("Joy Rogers");
employee3.setID(81774);
employee3.setDep("Manufacturing");
employee3.setPos("Engineer");

Note that from a design aspect, all fields should be private , and ID should also be final , since ID is not going to change once the employee is created. The name field could change if the person changes their name (marriage, witness protection, whatever) and the other fields could change from time to time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM