Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Set of Employee objects  RSS feed

 
Alex O'Dowd
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two classes, Payroll and Employee.




I am struggling with these already for 5 days. Could anyone help, how implement enrollEmployee() which takes a string representation of an employee number as its argument and then add employees with employee number ?
And also, printEmployees() for Payroll class which takes no argument and returns no value ?

Thanks in advance !
Regards
 
Junilu Lacar
Sheriff
Posts: 11125
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would the enrollEmployee method in the Payroll class even be responsible for creating an Employee? The more appropriate design, IMO, would be:

That is, Employee instances are created outside of Payroll, then Payroll is told to enroll that new Employee.

It's unclear to me if the requirement given by OP is complete or even a requirement vs OP's interpretation of what should be done.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please don't post your question on somebody else's thread; fortunately I can split your question into a new thread. Always use the code button; I have edited your post with it and doesn't it look better
Junliu is right; you should pass employee objects to the payroll class. I can see other problems; you have a constructor which doesn't set the name. You can have an employee who goes through thier whole employment history without knowing their name. Make sure all constructors set up the name; you cannot rely on outside code calling the setXXX methods. You should therefore not try to enrol anybody with a number and no name. You may have to implement a getNextID method in the payroll class.
 
Alex O'Dowd
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Please don't post your question on somebody else's thread; fortunately I can split your question into a new thread. Always use the code button; I have edited your post with it and doesn't it look better
Junliu is right; you should pass employee objects to the payroll class. I can see other problems; you have a constructor which doesn't set the name. You can have an employee who goes through thier whole employment history without knowing their name. Make sure all constructors set up the name; you cannot rely on outside code calling the setXXX methods. You should therefore not try to enrol anybody with a number and no name. You may have to implement a getNextID method in the payroll class.


Thanks

Could you just give an example, few lines how to ?
Getting sick already with that.

Thansk
 
Liutauras Vilda
Marshal
Posts: 4624
316
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex O'Dowd wrote:Could you just give an example, few lines how to ?
Getting sick already with that.
If you're not looking to an exercise as to an interesting challenge, might this field (programming) is not for you. Telling that you're getting sick of that won't solve exercices, we're here to tackle problems rather than moan. Nor moralize So let's get back to your problem.

Please show us what have you tried after you got few good suggestions. In case you actually don't know where to start, try to revise your lecture slides. Such techniques had to be shown and documented in one of your class sessions.

Please show us, what material you got and which parts are not clear, so as a result you're not entirely sure where to start.
 
Alex O'Dowd
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Alex O'Dowd wrote:Could you just give an example, few lines how to ?
Getting sick already with that.
If you're not looking to an exercise as to an interesting challenge, might this field (programming) is not for you. Telling that you're getting sick of that won't solve exercices, we're here to tackle problems rather than moan. Nor moralize So let's get back to your problem.

Please show us what have you tried after you got few good suggestions. In case you actually don't know where to start, try to revise your lecture slides. Such techniques had to be shown and documented in one of your class sessions.

Please show us, what material you got and which parts are not clear, so as a result you're not entirely sure where to start.


I tried to code

As a result, while trying to run it I get error: Payroll cannot be applied to given types;
required: Employee
found: java.lang.String
reason: actual argument java.lang.String cannot be converted to Employee by method invocation conversion
?
 
Carey Brown
Bartender
Posts: 2979
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex O'Dowd wrote:I tried to code As a result, while trying to run it I get error: Payroll cannot be applied to given types;
required: Employee
found: java.lang.String
reason: actual argument java.lang.String cannot be converted to Employee by method invocation conversion

Well, according to the five lines of code that you've shown, it should work. Your definition of employeeSet in a prior post is correct, and it does require an Employee object. It appears that your five lines does defiine the local parameter employee as an Employee object. So, I am led to suspect that the five lines you posted are not those that are causing the error. The error message should contain a file name and line number. Verify that these are in sync with the latest version of your code. It may not be in this method but the place in your code where you are invoking the message. Perhaps you're passing a String to the method instead of the required Employee object.
 
Alex O'Dowd
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect I have screwed something in constructor , method is looking for an employee object,I am sending it a string object ?

 
Carey Brown
Bartender
Posts: 2979
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That piece of code looks good as well. You need to look closer at the code that calls enrollEmployee(). If you still can't find the problem then re-post your code in its entirety along with a cut-n-paste of the actual error message including the part that shows the line number.
 
Alex O'Dowd
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks all fine, dont really know where to look again.



Second class Employee


It all compiles fine, then when I try to run it :
Payroll aPayroll = new Payroll();
aPayroll.enrollEmployee("1200");

Error: line 1 - method enrollEmployee in class Payroll cannot be applied to given types;
required: Employee
found: java.lang.String
reason: actual argument java.lang.String cannot be converted to Employee by method invocation conversion


 
Carey Brown
Bartender
Posts: 2979
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex O'Dowd wrote:It all compiles fine, then when I try to run it :
Payroll aPayroll = new Payroll();
aPayroll.enrollEmployee("1200");

Error: line 1 - method enrollEmployee in class Payroll cannot be applied to given types;
required: Employee
found: java.lang.String
reason: actual argument java.lang.String cannot be converted to Employee by method invocation conversion

This is your problem, "1200" is a String and not an Employee object. I assume that this line of code came from your main class.

You would need something like this instead:

or
 
Alex O'Dowd
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey,

Thanks very much ! It worked.
Such a stupid mistake.
 
Carey Brown
Bartender
Posts: 2979
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. Glad I could help.
 
Liutauras Vilda
Marshal
Posts: 4624
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex,

Most of suggestions you have been given by others already, but still, lets recap.

I think you still can and need to improve Employee class. In fact, you haven't satisfied given instructions, which you distributed in your very first post in this thread.
Alex O'Dowd wrote:Could anyone help, how implement enrollEmployee() which takes a string representation of an employee number as its argument and then add employees with employee number

1. Assuming your full requirements are standard, Employee suppose to be allowed to instantiate only with his first name and last name.

2. Employee number suppose to be some kind of auto generated, isn't it? What happens when you get hired? Lady from HR gives you an employee card which being assigned automatically with the next number (+1 from previous). Try to imagine, in HR works more people, if they would try to assign employee numbers randomly, don't you think that two HR department employees could assign the same number to two different persons at some point? I think that is possible.
Do you know about static variables? How do they differ from instance variables?

3. Your instruction states what you need to do in enrollEmployee method. "implement enrollEmployee() which takes a string representation of an employee number as its argument". You passing Employee object currently, but this is not what is asked in your instructions (if you showed us actual ones and not the ones how you understood and interpreted). So, that method call would look similar to:
and actual method implementation:

I'm worrying that you misinterpret wrongly your requirements. Don't worry in case you did, that is common. Usually it takes time to understand them well and requires to read at least few times in order to capture everything. AND still sometimes turns out when you're half way through, that your assumptions made earlier were not that well, so you need to re-do some parts.

Some areas of your implementation contradicts what you stated in your initial requirements and how you got actually implemented.

Do you mind to post actual instructions?

The thing is, that you might come up with better implementation or design rather than how is asked in instructions, but when it comes down to marking your work, everything what you got different from what you have been asked, will be marked down, so it is very important to do exactly how you have been asked - unless your instructor gave you a freedom in making decisions.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!