Java Reference
In-Depth Information
Were this display connected to a real clock, this method would be called once every 60 seconds
by the electronic timer of the clock. For now, we just call it ourselves to test the display.
Concept:
Methods can call
methods of other
objects using dot
notation. This is
called an external
method call .
When the timeTick method is called, it first executes the statement
minutes.increment();
This statement calls the increment method of the minutes object. Thus, when one of the
methods of the ClockDisplay object is called, it in turn calls a method of another object to do
part of the task. A method call to a method of another object is referred to as an external method
call. The syntax of an external method call is
object . methodName ( parameter-list )
This syntax is known as dot notation. It consists of an object name, a dot, the method name, and
parameters for the call. It is particularly important to appreciate that we use the name of an ob-
ject here and not the name of a class. We use the name minutes rather than NumberDisplay .
The timeTick method then has an if statement to check whether the hours should also be incre-
mented. As part of the condition in the if statement, it calls another method of the minutes object:
getValue . This method returns the current value of the minutes. If that value is zero, then we know
that the display just rolled over and we should increment the hours. That is exactly what the code does.
If the value of the minutes is not zero, then we're done. We don't have to change the hours in
that case. Thus, the if statement does not need an else part.
We should now also be able to understand the remaining three methods of the ClockDisplay class
(see Code 3.4). The method setTime takes two parameters—the hour and the minute—and sets
the clock to the specified time. Looking at the method body, we can see that it does so by calling
the setValue methods of both number displays (the one for the hours and the one for the minutes).
Then it calls updateDisplay to update the display string accordingly, just as the constructor does.
The getTime method is trivial—it just returns the current display string. Because we always
keep the display string up to date, this is all there is to do.
Finally, the updateDisplay method is responsible for updating the display string so that the
string correctly reflects the time as represented by the two number display objects. It is called
every time the time of the clock changes. It works by calling the getDisplayValue methods
of each of the NumberDisplay objects. These methods return the value of each separate num-
ber display. It then uses string concatenation to concatenate these two values, with a colon in
the middle, to a single string.
Exercise 3.30 Given a variable
Printer p1;
which currently holds a reference to a printer object, and two methods inside the Printer
class with the headers
public void print(String filename, boolean doubleSided)
public int getStatus(int delay)
write two possible calls to each of these methods.
 
Search WWH ::




Custom Search