Help with beginner assignment
I've had to write a class on my own, which I have, and then I need to apply it to another program that creates three objects that holds specific data. The class seems right to me, it holds spots for an Employee name, Employee Idnumber, the department and position of the employee as well.
The second half of this question asks me to plug in specific information into three objects in a separate program. For instance, 1 employee will be Susan Meyers. Their IDnumber will be 47899. Their department is Accounting. Their position is Vice President.
Having a brain fart right now on how to go about this. Any help would be appreciated, specifically an example on how to accomplish the above one so I can finish the others confidently.
Below is the class I wrote first.
Code :
public class Employee
{
//Fields
private String name; //Employees name
private int idnumber; //Employees ID number
private String department; //Department the employee works at
private String position; //Employees position
//Constructor
public Employee(String nam, int idnum, String dep, String pos)
{
name = nam;
idnumber = idnum;
department = dep;
position = pos;
}
//The setname that accepts argument for employees name
public void setName(String nam)
{
name = nam;
}
//The setIdnumber accepts argument for employees id number
public void setIdnumber(int idnum)
{
idnumber = idnum;
}
//The setDepartment accepts argument for employees department
public void setDepartment(String dep)
{
department = dep;
}
//The setPosition accepts argument for employees position
public void setPosition(String pos)
{
position = pos;
}
//The getName method returns the name of the employee
public String getName()
{
return name;
}
//The getIdnumber method returns the employees ID number
public int getIdnumber()
{
return idnumber;
}
//The getDepartment method returns the employees department name
public String getDepartment()
{
return department;
}
//The getPosition method returns the employees position
public String getPosition()
{
return position;
}
}
I started coding and this is what direction I was heading
Code :
import java.util.Scanner;
public class EmployeeTest
{
public static void main(String[] args)
{
String testName1; //Holds employee names
int testIdnumber1; //Holds employee idnumbers
String testDepartment1; //Holds employee departments
String testPosition1; // Holds employee positions
//Creates a Scanner object for keyboard input
Scanner keyword = new Scanner(System.in);
testName1 = ("Susan Meyers");
System.out.println(testName1);
}
}
I was going to do this for each one, but it doesn't seem very efficient? Is there an easier or cleaner way to do this?
Re: Help with beginner assignment
The Employee class seems usable. Looks like that portion of the assignment is going well so far.
The part about utilizing the class from a program looks like the place you are stuck. To me you are asking for someone to write the code to do one of the employees, so you can copy-paste to complete the others.
Great thinking. You broke the big problem down into smaller parts that you can more easily understand. If you knew how to do one of them, you could do the others.
Your problem is no longer setting up three employees, but setting up just one. Break this new, smaller problem down into even smaller steps and see if that is something you can write code for. If you have problems breaking down steps, post more questions.
Re: Help with beginner assignment
This is what I have so far.
Code :
public class EmployeeTest
{
public static void main(String[] args)
{
String testName1; //Holds employee names
int testIdnumber1; //Holds employee idnumbers
String testDepartment1; //Holds employee departments
String testPosition1; // Holds employee positions
//Creates a Scanner object for keyboard input
Scanner keyword = new Scanner(System.in);
testName1 = ("Susan Meyers");
System.out.println(testName1);
testIdnumber1 = 47899;
System.out.println(testIdnumber1);
testDepartment1 = ("Accounting");
System.out.println(testDepartment1);
testPosition1 = ("Vice President");
System.out.println(testPosition1);
}
}
It seems to get the job done, but now I'm wondering why I even bothered writing the class to begin with if I'm not using it. How can I incorporate the code I typed in the Employee class into the EmployeeTest program?
Re: Help with beginner assignment
I think I got it!
Code :
public class EmployeeTest
{
public static void main(String[] args)
{
Employee a = new Employee("Susan Meyers", 47899, "Accounting", "Vice President");
System.out.println("Name: " + a.getName());
System.out.println("ID: " + a.getIdnumber());
System.out.println("Department: " + a.getDepartment());
System.out.println("Position: " + a.getPosition());
Employee b = new Employee("Mark Jones", 39119, "IT", "Programmer");
System.out.println("\nName: " + b.getName());
System.out.println("ID: " + b.getIdnumber());
System.out.println("Department: " + b.getDepartment());
System.out.println("Position: " + b.getPosition());
Employee c = new Employee("Joy Rogers", 81774, "Manufacturing", "Engineer");
System.out.println("\nName: " + c.getName());
System.out.println("ID: " + c.getIdnumber());
System.out.println("Department: " + c.getDepartment());
System.out.println("Position: " + c.getPosition());
}
}
Re: Help with beginner assignment
Looks like the code is taking shape nicely.
Make sure it is all formatted well, javadoc in place, etc.
I don't know if the (school)class has gone over it yet, but the Employee class may benefit from a toString method.