Page 365 - Introduction to Programming with Java: A Problem Solving Approach
P. 365

                8.13 GUI Track: Problem Solving with CRC Cards (Optional) 331
to know how the method is implemented, because it’s encapsulated, but we can use a plausible guess to shed light on what it does. For example, get’s parameter could be a switch index that steers the control flow to a particular case, where there’s code that returns the value of the instance variable that corresponds to that index number.
The problem with using an index number to identify one of many instance variables is that simple integers don’t convey much meaning. But you know a solution to this problem. All you have to do is make each such index number a named constant. Then, for the distinguishing method argument, use the named constant instead of the number. That’s how the Calendar class implements its generic get method. And it’s at least as easy for a user to remember one get method with different named-constant arguments as it would be to remember different get-method names.
Armed with this concept, you should now be able to see what the rest of the code in our CalendarDemo program is doing. It gets the current day of the year and the current hour of the day. Then it adds a user- input number of days to the current day and a user-input number of hours to the current hour. Then it uses Calendar’s generic set method (which probably works like Calendar’s generic get method) to mu- tate the object’s instance variables for day-of-year and hour. Finally, it prints out the mutated time.
The Calendar class nicely illustrates the value of using pre-written software. It really is easier to learn to use that class than it is to write a program that does what it does. Moreover, other people’s code sometimes illustrates techniques that may be applicable to code you write. However, the Calendar class also illustrates the kinds of penalties associated with using pre-written software. The biggest penalty is usu- ally the time you have to spend to locate and figure out what’s available. Another penalty is that what you find may not exactly match your immediate needs, and you might have to provide extra code to adapt the pre-written software to your current program. Such penalties motivate many programmers to say, “Oh heck, I’ll just write it myself.” SoAmpetimaegs thoat’s tPheDthiFng toEdon, bhutain tnhecloengrun you’ll be ahead if you take time to learn about what others have already developed.
8.13 GUI Track: Problem Solving with CRC Cards (Optional)
When you begin a new design, there’s often a period of head-scratching when you’re trying to figure out what your classes should be and what they should do. Section 8.6 presented a formal top-down recipe, but sometimes you just need to “muck around” or brainstorm for awhile to get your thinking straight.
Even when you’re just mucking around and brainstorming, it’s still helpful to write things down. To provide a minimal structure for this informal activity, several years ago computer scientists Kent Beck and Ward Cunningham8 suggested using old-fashioned 3" 􏰃 5" file cards, with a pencil and eraser. Their idea was to allocate one card to each proposed class, with three kinds of information on each card: (1) At the top, put a class name. (2) Below and on the left, make a list of active verb phrases that described what that class will do. (3) Below and on the right, make a list of other classes with which the current class interacts—either actively as a client or passively as a server. The acronym, CRC, helps you remember the kinds of informa- tion each card should have. The first ‘C’ stands for “Class.” The ‘R’ stands for “Responsibility.” The last ‘C’ stands for “Collaboration.”
When several different people are participating in a brainstorming session, pencils, erasers, and little white cards might indeed be the best medium to employ. But when you’re the only designer, it might be more fun to use little windows on your computer screen. The program presented in Figure 8.13 sets up simu- lated CRC cards on your computer screen so you can do just that.
     8 OOPSLA ’89 Conference Proceedings.
Explore your options.
























































































   363   364   365   366   367