Page 339 - Introduction to Programming with Java: A Problem Solving Approach
P. 339
For this case, there are no descriptions for the constructors, the accessors, or the mutators. There is a line of asterisks above the first mutator, but not above the subsequent mutator and accessors. These omissions make the program more readable by grouping similar things together.
8.3 Helper Methods
In the first four chapters, we solved essentially every problem we addressed in just one module—the main method in one class. As problems get bigger, however, it becomes more and more necessary to partition them into subproblems, each of which has a manageable size. We started doing this in Chapter 5 when our main method called on some of Java’s API methods for help. Then in Chapter 6 and Chapter 7, we split our programs into two classes—a driver class, which contained the main method, and a driven class, which contained all other methods. At the end of Chapter 7 we introduced the concept of multiple driven classes, each of which contained other methods. Then, part of the partitioning came from splitting the program into two or more classes, and part of the partitioning came from defining multiple methods in each class. This enabled the main method in the driver class to delegate most of its work to methods in other classes.
In a broad sense, you could say that all of the other methods called by code in the main method are “helper methods”—they help the main method do its job. In other words, in a broad sense, any method that is called by another method is a helper method—the called method helps the calling method. The calling method is a client, and the called method (the broad-sense helper method) is a server.
You can narrow the definition of helper method by restricting it to a called method that happens to be
in the same class as the calling method. In the previous section, the Student constructor of Figure 8.2a
calls two of the same class’s methods, setFirst and setLast, in Figure 8.2b. Presumably, these muta- Apago PDF Enhancer
tors were written to allow a user to change the instance variables in an object after the object was originally initialized. But once their code is written, why not reuse it? By including calls to these two ordinary meth- ods in the constructor, we avoid duplication of the code in the called methods. Because the setFirst and setLast mutator methods each include a significant amount of error-checking code that helps the con- structor do its job, this organization helps divide the problem into smaller chunks.
You can narrow the definition of helper method even more. Up to this point, all methods we’ve covered have used the public access modifier. These public methods are part of the class’s interface, because they are responsible for the communication between an object’s data and the outside world. Sometimes, you’ll want to create a method that is not part of the interface; instead it just supports the operation of other methods within its own class. This special type of method—a method that is in the same class and has a private access modifier—is often called a helper method.
For example, suppose you’re asked to write a program that handles order entries for sports-uniform shirts. For each shirt order, the program should prompt the user for a shirt’s primary color and its trim color. For each color selection, the program should perform the same input validation. It should verify that the entered color is one of three values—w, r, or y, for white, red, or yellow. That input validation code is non- trivial. It’s in charge of:
• Prompting the user for a color entry.
• Checkingwhethertheentryisvalid.
• Repeating the prompt if the entry is invalid.
• Converting the single-character color entry to a full-word color value.
These four tasks are a coherent group of activities. Therefore, it’s logical to encapsulate them (bundle them together) in a separate module. The fact that the Shirt constructor needs to perform this coherent group of activities two separate times provides an additional reason to encapsulate them in a separate module.
8.3 HelperMethods 305