I need help: Here\'s a description of the program, it\'s kind of long. I am supp
ID: 441384 • Letter: I
Question
I need help:
Here's a description of the program, it's kind of long.
I am supposed to write a class named Employee that has the following fields:
* name. The name field refewrences 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 and 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.
I am using Eclipse. I'm only getting two errors in the EmployeeDemo.java program. I typed in the errors as comments. Please help me. I have to submit this code to my instructor using the website Hyper grade, when i submit it this is what it says:
Anyways here's my code:
public class Employee
{
//declare the fields
private String name;
private int id;
private String department;
private String position;
public Employee(String n, int num, String dept, String pos)
{
name = n;
id = num;
department = dept;
position = pos;
}
//create methods
public void setName(String n)
{
name = n;
}
public String getName()
{
return name;
}
public void setID(int num)
{
id = num;
}
public int getID()
{
return id;
}
public void setDepartment (String dept)
{
department = dept;
}
public String getDepartment()
{
return department;
}
public void setPosition(String pos)
{
position = pos;
}
public String getPosition()
{
return position;
}
}
public class EmployeeDemo
{
public static void showEmployee(Employee e)
{
System.out.println("Employee: (" + e.getName() + ") (" + e.getID() + ") (" + e.getDepartment() + ") (" + e.getPosition() + ")");
}
public static void main(String[] args)
{
/* test constructors */
Employee e1 = new Employee(); //ERROR HERE: "The constructor employee is undifened".
Employee e2 = new Employee("Billy Mitchell", 1050200); //ERROR HERE: "The constructorThe constructor employee(String, Int)is undifened.
Employee e3 = new Employee("Steve Wieve", 1049100, "Marketing", "Tester");
showEmployee(e1);
showEmployee(e2);
showEmployee(e3);
/* test mutators */
e1.setName("Walter Day");
showEmployee(e1);
e1.setDepartment("Twin Galaxies");
showEmployee(e1);
e1.setPosition("Referee");
showEmployee(e1);
e1.setID(1);
showEmployee(e1);
e2.setDepartment("Hot Sauce");
e2.setPosition("Lead");
showEmployee(e2);
e3.setName("X");
e3.setID(50);
e3.setDepartment("Y");
e3.setPosition("Z");
showEmployee(e3);
}
}
This is what the program output should look like:(Twin Galaxies) (Referee)
Explanation / Answer
/* there is a rule in java that there can be only one and only public class in a single java file so remove public access modifier from class Employee secondly two parameters constructor is not defined and rest of the code is the following */ class Employee { //declare the fields private String name; private int id; private String department; private String position; public Employee() { } public Employee(String n, int num, String dept, String pos) { name = n; id = num; department = dept; position = pos; } //create methods public void setName(String n) { name = n; } public String getName() { return name; } public void setID(int num) { id = num; } public int getID() { return id; } public void setDepartment (String dept) { department = dept; } public String getDepartment() { return department; } public void setPosition(String pos) { position = pos; } public String getPosition() { return position; } } public class EmployeeDemo { public static void showEmployee(Employee e) { System.out.println("Employee: (" + e.getName() + ") (" + e.getID() + ") (" + e.getDepartment() + ") (" + e.getPosition() + ")"); } public static void main(String[] args) { /* test constructors */ Employee e1 = new Employee(); //ERROR HERE: "The constructor employee is undifened". Employee e2 = new Employee("Billy Mitchell", 1050200,"Computers","head of department");//here number of arguments must be same as the number of arguments given in the paramerterized constructor //ERROR HERE: "The constructorThe constructor employee(String, Int)is undifened. Employee e3 = new Employee("Steve Wieve", 1049100, "Marketing", "Tester"); showEmployee(e1); showEmployee(e2); showEmployee(e3); /* test mutators */ e1.setName("Walter Day"); showEmployee(e1); e1.setDepartment("Twin Galaxies"); showEmployee(e1); e1.setPosition("Referee"); showEmployee(e1); e1.setID(1); showEmployee(e1); e2.setDepartment("Hot Sauce"); e2.setPosition("Lead"); showEmployee(e2); e3.setName("X"); e3.setID(50); e3.setDepartment("Y"); e3.setPosition("Z"); showEmployee(e3); } }