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

                276 Chapter 7 Object-Oriented Programming—Additional Details
   FractionDriver
Fraction
                           line#
a
main
b
line#
Fraction
n
d
Fraction
n
printIt
this
num
obj1
den
quot
num
obj2
     den
quot
output
                12
                  10
3
4
 0
                   11
0
                   12
0.00
                   25
                                     27
3
  26
4
 0.75
                    12
obj1
                      13
                  10
3
 0
                   11
0
                   12
0.00
                   18
               25
3
1
 3
                26
1
                27
3.00
                     13
obj2
                 15
obj1
              34
Apago
PDF
Enhancer
3 / 4 􏰀 0.75
          16
obj2
                 34
3 / 1 􏰀 3.00
                                          Figure 7.20 Trace of Fraction program in Figures 7.18 and 7.19
It’s no big deal to drive more than one driven class from a single driver. In fact, you already saw us do
it back in Chapter 5 when statements in a single main method called methods from more than one wrapper
class, like Integer and Double. The only thing to remember is that when you’re compiling the driver,
the compiler must be able to find all the driven classes. If they are pre-built classes they must be part of the
java.lang package or you must import them. If they are classes you write, they should be in the same
3
Example—Garage Door Opener
As an example, suppose you want to write a program that models the operation of a garage door opener. A typical system contains four control components—a push button, a normally closed up switch, a normally closed down switch, and a controller. The push button starts the door moving, or if the door is moving and has not yet reached the end of its normal travel, the push button stops the door. Whenever the door stops, its travel direction reverses, and the next push-button push makes the door go in the direction opposite to the direction it was going before it stopped. The up switch stops upward travel by opening its contacts and stopping the door at its upper limit. The up switch contacts close again when the door starts to go down.
3 It’s possible to put your own classes in your own packages in separate directories and import them as you import pre-built classes. You can learn how to do this in Appendix 4. However, if all your driven classes are in the same directory as your driver class, it’s not necessary to package and import them, and we assume this to be the case throughout the body of this book.
directory as your driver.
 
















   308   309   310   311   312