• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with Constructor!

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!


 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mike Atoms
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
would the constructor look something like this?

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No ...

How would you invoke it ? You need to pass some parameters to it.

Here is a hint...



try to adapt this to your task.
 
Mike Atoms
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you really creating an Employee inside your Employee constructor???
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




This is what you want...
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
Suggestions:
  • 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
  •  
    Mike Atoms
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic