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

                Door initially down.
Enter number of operations: 8
Enter 'b' for button or 'e' for end
Already stopped. Enter 'b': b
Button switch hit. Door moving up.
Enter 'b' for button or 'e' for end
Upper limit switch hit. Door is up.
Enter 'b' for button or 'e' for end
Button switch hit. Door moving down.
switch: e
switch: e
switch: b
Enter 'b' for button or 'e' for end switch: b
Button switch hit. Door stopped by button.
Enter 'b' for button or 'e' for end
Already stopped. Enter 'b': b
switch: e
7.10 Problem Solving with Multiple Driven Classes 277
The down switch stops downward travel by opening its contacts and stopping the door at its lower limit. The down switch contacts close again when the door starts to go up.
The controller interprets the information from the various switches, and it operates the motor that raises and lowers the door. The system has four distinct states: Door stopped after going down, which we’ll call state #0. Door going up, which we’ll call state #1. Door stopped after going up, which we’ll call state #2. Door going down, which we’ll call state #3.
Here’s the kind of thing we want our program to do: Sample session:
 Apago PDF Enhancer
Button switch hit. Door moving up.
Enter 'b' for button or 'e' for end
Upper limit switch hit. Door is up.
Enter 'b' for button or 'e' for end
Button switch hit. Door moving down.
switch: e
switch: b
Enter 'b' for button or 'e' for end switch: e
Lower limit switch hit. Door is down.
Now that we’ve described the problem and said what we want the program to do, let’s analyze the problem to see how the program might be organized.
The up an down end switches are hard wired, and the push button contains a radio
transmitter. But from a modeling viewpoint, we can think of the push button as being
hard wired like the end switches. So the two end switches and the push button can be just three instances of a generic thing called a “switch.” This suggests that we write a Switch class and construct three objects from it—an upSwitch, a downSwitch, and a button.
Although the three switches are similar to each other, they are all different from the controller that gathers information from them and does things to change the state of the door. So it makes sense to use a separate class for the controller—a GarageDoorController class. We’ll have our driver construct one object from it—a control.
There is also the door, which is what we really care about. You can think of the door in the nar- row sense as just another component, or you can think of it in the broader sense as the system—the GarageDoorSystem. A system is an object that contains other objects (its components), and it knows about its components. In our current example, a GarageDoorSystem object contains the door, the con- troller that moves the door, and the three switches that send signals to the controller.
  Use a separate class for each type of thing.
 




























































   309   310   311   312   313