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

                7.10 Problem Solving with Multiple Driven Classes 279
 /************************************************************
*
GarageDoorDriver.java
Dean & Dean
This simulates installation and testing.
************************************************************/
*
*
*
import java.util.Scanner;
public class GarageDoorDriver
{
}
Apago PDF Enhancer
public static void main(String[] args)
{
}
// end main
GarageDoorSystem system;
Scanner stdIn = new Scanner(System.in);
// Install system
system = new GarageDoorSystem();
// Test system
System.out.print("Enter number of operations: ");
system.run(stdIn.nextInt());
// end class GarageDoorDriver
Figure 7.22 Driver for Garage Door program
The system instance variable gives the controller a reference to the system that includes it. We’ll want the constructor to initialize or re-initialize the system, state, and motorDirection instance variables.
The state and motorDirection instance variables represent electromechanical relays or solid- state flip flops (primitive electronic memory elements). These primitive memory elements keep track of the current state of the controller—its current mode of operation. In the physical controller these primi- tive memory elements determine what the controller does. The actionEvent method changes the values of these primitive memory elements when it’s called by a switch. Every time the actionEvent method is called, the state increments by one, modulo four. Modular four means the value of state cycles through four values like this: 0, 1, 2, 3, 0, 1, 2, and so on. Also, whenever the motor stops, it reverses direc- tion. That is, whenever state changes to an even value (0 or 2), the motorDirection variable toggles to its opposite boolean value.
Figure 7.24 contains the code that defines the switches. Each switch is characterized by a named in- stance constant, NORMALLY_OPEN. NORMALLY_OPEN is a switch contact property. If NORMALLY_OPEN is true, when you push the switch, its contacts close. On the other hand, if NORMALLY_OPEN is false, when you push the switch, its contacts open. So, when you push on either of the two limit switches, its contacts open and this stops current flow to the motor. The instance variable, use, tells how the switch fits into the system. The instance variable, controller, is a reference to the controller to which the switch is attached. (If there were two garage door systems, a particular switch might be associated with either




































































   311   312   313   314   315