Java Reference
In-Depth Information
Summary of the clock display
It is worth looking for a minute at the way this example uses abstraction to divide the
problem into smaller parts. Looking at the source code of the class ClockDisplay , you
will notice that we just create a NumberDisplay object without being particularly inter-
ested in what that object looks like internally. We can then call methods ( increment ,
getValue ) of that object to make it work for us. At this level, we simply assume that
increment will correctly increment the display's value, without being concerned with how
it does it.
In real-world projects, these different classes are often written by different people. You might
already have noticed that all these two people have to agree on is what method signatures the
class should have and what they should do. Then one person can concentrate on implementing
the methods, while the other person can just use them.
The set of methods an object makes available to other objects is called its interface. We shall
discuss interfaces in much more detail later in this topic.
Exercise 3.31 Challenge exercise Change the clock from a 24-hour clock to a 12-
hour clock. Be careful: This is not as easy as it might at first seem. In a 12-hour clock,
the hours after midnight and after noon are not shown as 00:30, but as 12:30. Thus,
the minute display shows values from 0 to 59, while the hour display shows values from
1 to 12!
Exercise 3.32 There are (at least) two ways in which you can make a 12-hour clock. One
possibility is to just store hour values from 1 to 12. On the other hand, you can simply leave the
clock to work internally as a 24-hour clock but change the display string of the clock display
to show 4:23 or 4.23pm when the internal value is 16:23 . Implement both versions. Which
option is easier? Which is better? Why?
Another example of object interaction
We shall now examine the same concepts with a different example, using different tools. We
are still concerned with understanding how objects create other objects and how objects call
each other's methods. In the first half of this chapter, we have used the most fundamental
technique to analyze a given program: code reading. The ability to read and understand
source code is one of the most essential skills for a software developer, and we will need to
apply it in every project we work on. However, sometimes it is beneficial to use additional
tools in order to help us gain a deeper understanding about how a program executes. One
tool we will now look at is a debugger.
A debugger is a
software tool that
helps in examining
how an application
executes. It can be
used to find bugs.
A debugger is a program that lets programmers execute an application one step at a time. It
typically provides functions to stop and start a program at selected points in the source code,
and to examine the values of variables.
Search WWH ::

Custom Search