Geo Kinkladze

Greenhorn
+ Follow
since Sep 22, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Geo Kinkladze

The basic test that I always try and remember is the "Has a relationship" i.e.

Does Chapter have a test (or set of tests) or does a test have a chapter (or set of chapters). It would seem more logical to me that a chapter has a set of tests, certainly from all the books I seem to be finding myself looking at these days.

14 years ago
[quote] I like the idea of the method which returns different values depending on the person asking, but that is not an ordinary get method. [/quote]

Yeah, sorry I probably shouldn't have used that example. I guess I should explain where I used this type of getter.

In my current company we have a system that holds data for parts we produce. We set up manningLevel and partWeight as int, having been told that under no circumstances would they ever need to be anything but. This was good for about two years, but then the business started making smaller more intricate parts. Suddenly we needed 0.2 manning levels and 5.6 grammes in addition to the existing 1,2 or 3 man jobs weighing 160-500 grammes. We changed the int to double in the part class and got clobbered. I still shudder thinking about the hours I lost. You see we had tons of getters that we had built up over the years, all expecting an int, with calculations built thereon. So from that day I resolved to make getters identify themselves.. Not all of course, just those I marked as "suspect". Then I made the method keep a record of who called them. I guess the solution was worse than the problem but I am getting over it. I've resolved to "knock it down and start over" one day, when I understand Java better. Thats probably why I'm here.
14 years ago
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.
14 years ago
in the printout method instead of having this:



you should have this:



and this uses your getter method.

14 years ago