I've got an exercise for class that wants me to create an employee class that has three instance variables- a first name (String), last name (String), and a monthly salary (double). Then it wants me to provide a constructor to initialize the three instance variables. Then it wants me to provide a set and get method for each instance variable. If the monthly salary is not positive, do not set its value. Here'es what I have so far. Please point me in the right direction. I've spent several hours already trying to get this to work or to simply understand the problem better. If possible simply help with some pseudocode to get me going and then I will do my best to work the code out on my own. Many Thanks!
you simply decalre three isntance fields, a constructor that takes 3 parameters that you then assign to the instance variables (if values are valid, otherwise throw IllegalArgumentException) ...
then create simple getters and setters (note that you have to check for valid values here too) to retrieve and manipulate the values.
JDBCSupport - An easy to use, light-weight JDBC framework -
Joined: Aug 29, 2009
would the constructor look something like this?
Joined: Feb 23, 2009
How would you invoke it ? You need to pass some parameters to it.
Here is a hint...
try to adapt this to your task.
Joined: Aug 29, 2009
Here's where I am now! Still getting an error when I try to create new employee? I appreciate the help! I really want to get to the point where I really understand java and I know it's going to take a lot of practice!
You want to change how you pass in the employee's name. Right now you are validating your Name variable, but then assigning it the values of fName and lName. This make the validation of Name pointless. You should instead pass in the fName and lName variables, validate them, and then combine them into the Name variable. Also you didn't use the proper convention when naming the "Name" variable. It should be a lower case "n." Uppercase suggest it is a class name.
Finally I disagree with the usage of throwing an exception when invalid input is entered:
1. It's going to terminate your program (unless you catch it), which I doubt is what your teacher/professor wants from the validation (probably wants an error message displayed and then a prompt to reenter)
2. Exceptions should be used when the system fails to meet the demands of the user, not when the user fails to meet the demands of the system. (If you need me to explain this more just ask)
I would be happy with code throwing an IllegalArgumentException there. That is the usual practice when a constructor argument or method argument cannot be validated.
Any method (or constructor) offers a contract to the user: "If you supply an argument which meets these requirements . . . , then I guarantee to . . . "
If you pass an invalid argument (eg negative salary) then that contract is invalidated; the method may do anything it wants, returning an incorrect result, going into an infinite loop, doing nothing at all, etc. It is far better to throw an Exception so the calling method "knows" something has gone wrong.
The design problem is more complicated than you suggested. In fact you have fName lName and Name. You have the same information in two fields. It is then possible to change the fields so fName = "Campbell" lName = "Ritchie" and Name = "Sebastian Janisch", ( ) which I am sure would breach your intended class invariants. There are also problems with the constructor; I can't see where fName and lName are passed, so they would default to null, and this risks Exceptions if those fields are used. And, even better, you are liable to have a Name set to "null null".
Change the constructor heading to public Employee(String fName, String lName, double salary)
Delete the Name field
If you ever need name, make it from fName + " " + lName
Remember you may need the this.fName = fName; idiom in the constructor
Joined: Aug 29, 2009
After searching for info on constructors here I found that I may not need to be so explicit in the constructor. Here is the code that I have come up with. If you have suggestions for shortening the code please let me know. Thanks to all for their help and insight. Beleive me I will continue to need wise counsel in the future.
Joined: Oct 13, 2005
You can only have one Employee with that constructor, Michael Adams.
Surely you want to pass the names to the constructor. And if you use the idiom where you call "set" methods from the constructor, label those methods with the final keyword.