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

                280 Chapter 7 Object-Oriented Programming—Additional Details
 /*******************************************************************
*
Controller.java
Dean & Dean
This class models controller with sensors attached.
********************************************************************/
*
*
*
public class Controller
{
}
// end class Controller
private GarageDoorSystem system;
private int state;
// 0=down, 1=goingUp, 2=up, 3=goingDown
private boolean motorDirection = false;
// true = go up
//*****************************************************************
public Controller(GarageDoorSystem system, int state)
{
}
// end actionEvent
this.system = system;
this.state = state;
if (state < 2)
{
}
this.motorDirection = true;
// end constructAorpago PDF Enhancer //*****************************************************************
}
public void actionEvent()
{
this.state++;
this.state %= 4;
if (this.state % 2 == 0)
{
}
this.motorDirection = !this.motorDirection;
system.setState(this.state);
Figure 7.23 Controller class for the Garage Door program
system’s controller.) The constructor initializes these three values. The hit method prints a message that identifies which switch was hit. Then it calls control’s actionEvent method in the Controller class.
The instance constant and instance variables in Figure 7.24 expand the UML class diagram for the Switch class to this:























































   312   313   314   315   316