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

                4.13 boolean Variables 137
 /*******************************************************************
*
GarageDoor.java
Dean & Dean
This simulated operation of a garage door.
*******************************************************************/
*
*
*
import java.util.Scanner;
public class GarageDoor
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String entry;
boolean upDirection = true;
boolean inMotion = false;
// user's entry - enter key or q
// Is the current direction up?
// Is garage door currently moving?
System.out.println("GARAGE DOOR OPENER SIMULATOR\n");
do
{
System.out.print("Press Enter, or enter 'q' to quit: ");
entry = stdIn.nextLine();
     Apago PDF Enhancer
if (entry.equals(""))
// pressing Enter generates ""
// button toggles run state
! operator toggles motion every time
{
inMotion = !inMotion;
if (inMotion)
    {
}
else
{
if (upDirection)
{
}
else
{
}
System.out.println("moving down");
System.out.println("moving up");
System.out.println("stopped");
upDirection = !upDirection; // direction reverses at stop
    }
// end GarageDoor class
}
// end main
}
while (entry.equals(""));
}
}
// end if entry = ""
! operator toggles direction when stopped
Figure 4.18 GarageDoor program







































   169   170   171   172   173