Page 259 - Introduction to Programming with Java: A Problem Solving Approach
P. 259
{
6.12 Specialized Methods—Accessors, Mutators, Boolean Methods 225
A method should perform one task. It should be written such that it accomplishes only the one thing that its name implies. For example, a getAge method should simply return its object’s age instance vari- able value and do nothing else. We mention this notion because there is sometimes a temptation to provide extra functionality to a method to avoid having to implement that functionality elsewhere. One particularly common faux pas (a French term meaning error in etiquette) is to add print statements to a method that doesn’t need to print. For example, a novice programmer might implement the getAge method like this:
public int getAge()
inappropriate print statement
System.out.println("Age = " + this.age);
return this.age;
} // end getAge
That getAge method might work fine for the novice programmer’s program, which takes into account the getAge method’s non-standard print statement. But if later on another programmer needs to work with the program and call the getAge method, the new programmer would be surprised to find the non-standard print statement. The new programmer would then either have to (1) accommodate the print statement or (2) remove it from the getAge method and check for any ripple effects. To avoid that scenario, you should include print statements in a method only if the purpose of the method is to print something.
The exception to the above rule is that it’s acceptable and helpful to temporarily add print statements to methods when you’re trying to debug a program. For example, if you think there’s some-
thing wrong with your getAge method, you might want to add the above print statement
to verify the correctness of the age value just before getAge returns it. If you add such de-
debug with temporary print statements.
Apago PDF Enhancer
bug print statements, don’t forget to remove them later on, once your program is working.
Mutator Methods
A mutator is a method that changes or “mutates” an object’s state by changing some or all of that object’s stored data—typically private data. For example, here is the mutator method for setting or changing a mouse’s percentGrowthRate instance variable:
public void setPercentGrowthRate(double percentGrowthRate)
{
this.percentGrowthRate = percentGrowthRate;
} // end setPercentGrowthRate
As evidenced by the setPercentGrowthRate method, mutator methods should be named with a “set” prefix. That’s why mutator methods are often called set methods.
An accessor allows you to read a private instance variable. A mutator allows you to update a private instance variable. If you provide a private instance variable with both an accessor and a simple mutator like the setPercentGrowthRate method above, it effectively converts that private instance vari- able into a public instance variable, and it breaks the encapsulation of that variable. There’s not much danger with having an accessor alone, but having a simple mutator allows an outsider to enter an unrea- sonable value that may produce erratic program operation. However, if you include con-
straint checking and perhaps correcting code in your mutators, they can serve as data filters Use mutator
that assign only proper data to your private instance variables. For example, here’s a setPercentGrowthRate mutator that filters out growth rates that are less than 100%:
to filter input.