Page 363 - Introduction to Programming with Java: A Problem Solving Approach
P. 363
8.12 Problem Solving with the API Calendar Class (Optional) 329
referred to by the parameter. The safest and most understandable way to refer to these two objects is to use the this prefix to refer to the calling object and the reference-parameter prefix to refer to the other object. However, it’s OK to omit the this when referring to the calling object, and you’ll see this done quite fre- quently. It makes the code more compact.
8.12 Problem Solving with the API Calendar Class (Optional)
Although textbooks (including ours) ask you to write little programs that manipulate times and dates, if you get serious about times and dates, you will discover it’s a hornet’s nest of different number
bases, different length months, leap years, daylight savings time, different time zones, and
many different formatting conventions. For serious time and date work, you should use
Java API prewritten software. Unfortunately, it’s not always easy to find the right Java class. This is a case in point, because most of the methods in the obvious classes, Time and Date, are obsolete. Usually, you should use the Calendar class instead. Figure 8.12 contains an example program that exercises some of the methods in the Calendar class.
The Calendar class is in the java.util package. To include it in your program, you could use this import statement:
import java.util.Calendar;
However, since the Calendar class is in the same package as the Scanner class, which this program also needs, it’s easier to make both classes available simultaneously with this one “wildcard” import
statement:
Apago PDF Enhancer
import java.util.*;
In the first declaration, the program loads StdIn with a reference to an instance of the Scanner class. In the second declaration, the program loads time with a reference to an instance of the Calendar class. Notice, however, that the program creates the Calendar object in a strange way. For a reason we’ll explainlaterinChapter13,youcan’tjustusenew Calendar()directly.Instead,youhavetousethe getInstance method. If you look up the getInstance method in the Java API documentation for the Calendar class, you’ll see that this method has a static modifier, so it’s a class method. How do you invoke a class method? Think back to how you invoked Math-class methods in Chapter 5. Instead of using an instance variable before the method name, you use the class name. How does getInstance work? We’re not supposed to know, because it’s an encapsulated module, but it probably internally instantiates a Calendar object, initializes it with the current time, and then returns a reference to that object. Although this is not the standard way to instantiate new objects, it works. The Java API includes several examples of this indirect type of object construction.
For the rest of the program, you can forget about how the time object was created and use it like you would any other object to call instance methods in its own class. The first print statement uses Calendar’s getTime method to retrieve the time information, and then it prints it all out as shown in the first line of the sample session.
The next two statements use the object reference with get methods to retrieve two particular instance
variable values. But wait! There’s something wonderfully strange about these two get methods. They’re not two separate methods like getDayOfYear and getHour would be. They’re both the same method—one method called just plain get. Instead of using the method name to identify the instance variable that will be retrieved, the designers of this class decided to use an int parameter value to identify that variable. We don’t have
Use ID number in argument to select one of many similar variables.
Don’t reinvent the wheel.