aspose file tools*
The moose likes Beginning Java and the fly likes Learning about Setters and Getters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Learning about Setters and Getters" Watch "Learning about Setters and Getters" New topic
Author

Learning about Setters and Getters

Charles Angemeyer
Greenhorn

Joined: Dec 18, 2012
Posts: 22
Hi, I'm learning about setters and getters and I worked through a modified example from the Java Head First book. Below is a program that asks you for your dog's weight then prints out one of several barks. Small dogs get "yip, yip", etc.
My example works and I found a way to include some validation so overall I'm pleased but can you
look at it and tell me if there are any modifications that you would make? I'm just looking for some feedback so I don't develop bad habits. Thank you very much.



Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2318
    
  49
I guess your request is based on the understanding that you are a beginner and so I'll just point out some simple things:

1. When comparing objects for equality (and Strings are objects in java) use equals and not ==
2. Comments should match the code. You say "//Validation code to insure that it is a positive int " but the code just checks if it is an int and allows negative ints.
3. I would have the saying() method return a String and print it out in your main method.
4. If you've learned about Exceptions, setWeight() should throw an Exception if you pass a negative value to it.
Charles Angemeyer
Greenhorn

Joined: Dec 18, 2012
Posts: 22
Tony,
Thanks very much.
Your type comments were exactly what I was looking for.
Charles Angemeyer
Greenhorn

Joined: Dec 18, 2012
Posts: 22
I implemented all of the earlier suggestions but I need help on my Setter Method.
I did quite of bit of googling on exception handling before posting this follow up.
If the user enters a negative number, my setter method throws a red exception error in my console window and stops the program dead in its tracks.
Therefore I know that I'm not coding this properly.
Ideally I would like the user to receive a prompt that states, "Sorry, negative numbers not allowed," and then give them the ability to try again.
Can someone help me with my code in my Setter method?
Thanks very much.





Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2318
    
  49
Ideally I would like the user to receive a prompt that states, "Sorry, negative numbers not allowed," and then give them the ability to try again.

There are two easy ways of doing this:
1. Check the value before you call setWeight and if it is less than 1 display the message and then prompt for input again.
2. Catch the IllegalArgumentException thrown by the setWeight method and then display the message and then prompt for input again.

BTW is a weight of 0 legal?
Charles Angemeyer
Greenhorn

Joined: Dec 18, 2012
Posts: 22
Thanks very much.
I will work through my example based on your suggestions and no, zero should not be allowed so I will revisit that as well.
Many thanks.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Learning about Setters and Getters