Christopher,
I don't quite understand the difference between the getter and setter methods and why they're used
I would guess you are confused because you don't understand why you create programs which force you to use
instead of
If we were to add another variable to the clock class such as
String alarmTime;
and in the Setter Method put the following pseudocode:
void setAlarmTime (String t)
if t is between 6:00am and 8:00 am
then set AlarmTime to t;
Else
System.out.println("Sorry but Mum specifically says you must get up in time for School/work/chores");
set AlarmTime to 8:00 am;
Do you see why you would force the use of this settermethod in this case. It stops the alarm being set outside an allowed range. So in this case nobody could just type
and steal a lie in.
Equally by not allowing the method to be publicly available you stop the following from happening
Seriously though.. it helps stop variables from going outside ranges they're not supposed to, it allows you to force some extra code to be run when a variable changes and all this can be done in one neat little place inside the setter method.
A good reason to have a getter method would be one that returns a string representing your location. You could force the method making the call (or asking you your location) to identify themselves.
if personAsking = wife then return "at office"
if personAsking = boss then return "with customer"
if personAsking = golfbuddy then return "on my way"
I'm sure you can work out your own advantages with this
I noticed as well that the void modifier for the methods still produce a value, so what is the difference between that and using a return value?
This one is harder to get into your brain and understand what you are saying but what I think you mean is that you have noticed that the void method makes something equal something so therefore (in your mind) it produces a value. I've heard it best explained this way (Barry Burd I think in "OO for dummies"): With most Void methods you are only interested in the side effect of the method, i.e the method carries something out for you and then quietly dies.
So the time setter method sets the time of the clock whereas the getter method gives you an answer. With a getter method you ask the question and the answer given to you is the return value of the method. With the setter method you didn't ask a question and therefore didn't get an answer.
Hope I didn't confuse you further with all this.