5 Replies - 9662 Views - Last Post: 24 May 2009 - 09:04 PM Rate Topic: -----

#1 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Employee class problem

Post icon  Posted 24 May 2009 - 07:11 PM

Hello! I have a little problem. 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. Once you have written the class, write a separate program that 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 Rodgers 81774 Manufacturing Engineer

The program should store this data in the three objects and then display the data for each employee screen.

Here's what I have done so far:

		public class Employee
		{
			private String name;
			private int idnumber;
			private String department;
			private String position;
			
			public Employee(String nm, int id, String dp, String ps)
			{
				name = nm;
				idnumber = id;
				department = dp;
				position = ps;
			}
			
			public Employee(String nm, int id)
			{
				name = nm;
				idnumber = id;
				department = " ";
				position = " ";	
			}
			
			public Employee()
			{
				name = " ";
				idnumber = 0;
				department = " ";
				position = " ";	
			}
			
			public String getName()
			{
				return name;
			}
			
			public int getIdnumber()
			{
				return idnumber;
			}
		
		}
	   


and also did this one:


public class EmployeeFile
{
	public static void main(String[] args)
	{
		Employee box = new Employee();
		
		box.setName(Susan Meyers);
		box.setName(Mark Jones);
		
		System.out.println("Name	  ID Number	  Department	  Position	");
		System.out.println("_____________________________________________________");
		System.out.println(box.getIdnumber() + " ");
	}
}



I know it this one is incomplete but I test it after each input since I am a newbie to java in order to make sure I am going in the right path. I am getting error messages on System.out.println(box.getIdnumber() + " "), and both box.setName(Susan Meyers);, and box.setName(Mark Jones). I am very confused as to what I am supposed to do so her's what I believe: This is not an user input program according to the directions so, the names, id numbers, and so on should be the ones supplied. Any help on correcting the errors and on the right way to finish this exercise as far as the directions are concerned is greatly appreciated.

Jay

Is This A Good Question/Topic? 0
  • +

Replies To: Employee class problem

#2 PJLabowski  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 87
  • Joined: 21-March 09

Re: Employee class problem

Posted 24 May 2009 - 07:15 PM

I'm not sure if you forgot to post it or not, but I don't see a setName method in your code
Was This Post Helpful? 0
  • +
  • -

#3 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Re: Employee class problem

Posted 24 May 2009 - 07:20 PM

I have it named as box.setName(Susan Meyers); is this wrong?
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Employee class problem

Posted 24 May 2009 - 07:25 PM

In your Employee class you dont have a setName method. ANyways, why not just do this instead
Employee box = new Employee("Susan", 47899, "Accounting", "Vice Pres");

Was This Post Helpful? 0
  • +
  • -

#5 PJLabowski  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 87
  • Joined: 21-March 09

Re: Employee class problem

Posted 24 May 2009 - 07:28 PM

no no. you use the method in your main is fine. the problem is in your class Employee you don't have that method at all. this is what i'm talking about.
public void setName(String inName)
{
	 name = inName;
}


Was This Post Helpful? 0
  • +
  • -

#6 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Re: Employee class problem

Posted 24 May 2009 - 09:04 PM

All your inputs helped me greatly thank you! Here's my final product:

public class Employee
		{
			// fields
			private String name;
			private int idnumber;
			private String department;
			private String position;
			
			// Constructors
			public Employee(String nm, int id, String dp, String ps)
			{
				name = nm;
				idnumber = id;
				department = dp;
				position = ps;
			}
			
			public Employee(String nm, int id)
			{
				name = nm;
				idnumber = id;
				department = " ";
				position = " ";	
			}
			
			public Employee()
			{
				name = " ";
				idnumber = 0;
				department = " ";
				position = " ";	
			}
			
			// Methods
			public String getName()
			{
				return name;
			}
			
			public int getIdnumber()
			{
				return idnumber;
			}
			
			public String getDepartment()
			{
				return department;
			}
		
		public String getPosition()
		{
			return position;
		}
				
		}
	   


and this:

public class EmployeeFile
{
	public static void main(String[] args)
	{
		
		//Different Employee instances.
		Employee box = new Employee("Susan Meyers", 47899, "Accounting", "Vice Pres");
		Employee box1 = new Employee("Mark Jones", 39119, "IT", "Programmer");
		Employee box2 = new Employee("Joy Rodgers", 81774, "Manufacturing", "Engineer");
		
		System.out.println("  Name		  ID Number	  Department	  Position	");
		System.out.println("_______________________________________________________");
		System.out.print(" " + box.getName() + "	" + box.getIdnumber() + "		  " + box.getDepartment());
		System.out.println("	  " + box.getPosition());
		System.out.print(" " + box1.getName() + "	  " + box1.getIdnumber() + "		  " + box1.getDepartment());
		System.out.println("			  " + box1.getPosition());
		System.out.print(" " + box2.getName() + "	 " + box2.getIdnumber() + "		  " + box2.getDepartment());
		System.out.println("   " + box2.getPosition());
	}
	


}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1