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

                282 Chapter 7 Object-Oriented Programming—Additional Details
four component objects in the system. The constructor instantiates all of the component objects and initial- izes all of its instance reference variables with references to those component objects. It initializes state to 0, corresponding to the door-down position, and in the Controller constructor call, it passes this state value to the new control object to synchronize that object’s state with the state of the complete system. The setState method provides a way for the subordinate control object to keep the system’s state synchronized with the controller’s state immediately after the controller takes action that changes the state.
 /********************************************************************
*
GarageDoorSystem.java
Dean & Dean
This represents a garage door.
********************************************************************/
*
*
*
import java.util.Scanner;
public class GarageDoorSystem
{
private int state;
Apago PDF Enhancer
// 0=down, 1=goingUp, 2=up, 3=goingDn
// upper limit switch
private Switch downSwitch; // lower limit switch
private Controller control;
private Switch upSwitch;
private Switch button;
// electronic pushbutton
//****************************************************************
public GarageDoorSystem()
{
this.state = 0;
System.out.println("Door initially down.");
this.control = new Controller(this, this.state);
this.upSwitch =
new Switch(false, this.control, "Upper limit");
this.downSwitch =
new Switch(false, this.control, "Lower limit");
this.button = new Switch(true, this.control, "Button");
// end constructor
//****************************************************************
}
public void setState(int state)
{
}
this.state = state;
//****************************************************************
Figure 7.25a
GarageDoorSystem class for the Garage Door program—Part A























































   314   315   316   317   318