• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with sets  RSS feed

 
James Leon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I started Java recently and been learning about sets. I thought I understood the basics of sets, but I do not think I do It is for an assignment which is why I don't want people to just give me the answer, but rather point me in the right direction. I looked through all my books to no avail and my tutor is taking too long to reply to my e-mail.

There is an Employee Class, which has three instant variables of type String: first name, second name, and emplyoeeNumber and an instant variable of type int called sales.

I have to create a new class called Payroll and declare a private instant variable called employeeSet which is capable of referencing Employee objects that are to be held in alphanumeric order of their employee numbers, and make it so that employeeSet is assigned a suitable empty set object when a new instance of Payroll is created



I then have to write a public instance method in the PayRoll class called enrollEmployee() which takes a string representation of an employee number as its argument and returns no value. The method should create an instance of Employee and then add it to the set referenced by the instance variable employeeSet.

All I got so far is this:



I just have no idea on what I am doing. I don't even know if the small amount I have done is right. I know it is simple, but I keep on over thinking things. Maybe it will make more sense if I have a rest, but haven't really got time to rest.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The enrollEmployee() method should NOT take an employee number as argument - that's backwards. It should take the first and last names as arguments, determine a new employeeNumber that is unique from all others in the employeeSet, create a new Employee instance and add that instance to the employeeSet, and possibly return a referemnce to that new Employee instance.
 
Rajdeep Biswas
Ranch Hand
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are going right.
I then have to write a public instance method in the PayRoll class called enrollEmployee() which takes a string representation of an employee number as its argument and returns no value. The method should create an instance of Employee and then add it to the set referenced by the instance variable employeeSet.

Since just from employee number, you can not create an Employee instance (can you?), you needed to have stored all the Employee objects somewhere already like List or Employee[] array or some other collection already, because you need to have the corresponding details like firstName, secondName too!

Now, as your requirement goes enrollEmployee should be used for addition of an employee to the employeeSet, it should be like this:

Moving to your set:
declare a private instant variable called employeeSet which is capable of referencing Employee objects that are to be held in alphanumeric order of their employee numbers

So you have got the set right, now you need the ordering too, right?
Questions to ask:
1. Does Employee class have default sorting implementation that you need now (Hint: Comparable interface)
2. Or, while instantiating the TreeSet, you need to inform it on the needed ordering because the object type (Employee) itself does not provides this inherent information (Hint: Comparator interface)

One more thing to ponder about your enrollEmployee method. It just does not looks right thing to me to create an Employee with employeeNumber. Are the Employees arleady created and stored somewhere, so you are enrolling them just for payroll here, OR, you need to create the Employee from firstName and secondName parameters to the method, then generate employeeNumber and then store it in the employeeSet.

I believe this info will help you.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you specifically been told to use a tree set? How are you allocating employee ID numbers?
I think I agree with FK that you take name (etc) and use that to create a new Employee instance and the Employer/Payroll object allocates the ID number. Then you put a reference to the Employee object into the Set. I think it would be awkward to keep a reference directly to the Employee object in the Employer object because you don't know how many Employees there will be. You can put Employee objects into all sorts of data structures, which you can read about in the Collections Framework. With a few exceptions (mostly where the class' name contains Hash) you can put your references into the different collections and do anything with the objects and find them again easily.
Now, about the IDs. You must supply some means of ordering if you are using a tree set. If you go back to the Collections Framework link and go down about 8 clicks, you will find a section called object ordering. Read that. It is good. Maybe have a rest and read it tomorrow. But you will now know how things are ordered and how things are put into a sorted set (e.g. a tree set). You enter your first object, then if the second is “smaller” it goes to the left, if it is “larger” it goes to the right and if it is the “same” it is simply ignored. This is where the problem about ID numbers arises. A binary tree (as hides behind a tree set) should look like the diagrams in this link. Lots on each side, and all the branches about the same length. But if your ID generator simply produces increasing values, you will get a completely different structure.
Ob₁↘Ob₂↘Ob₃↘Ob₄↘...etc.
Now you have everything going to the right, so your nice balanced tree has turned into a linked list. Rather than filling or searching it in logarithmic time, it is going to require linear time to enter a value or search for it, so you will get slower performance unless you find some way to randomise IDs. Or a different data structure.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!