import java.awt.*;

// An instance of this class models the accelerator of the cruise control 
// mechanism.  It can be turned on (i.e., pressed down to increase speed) 
// or off (i.e., released to decrease speed). The instance has a window 
// (a Frame) that contains the status of the accelerator as well as an 
// output-on-or-off toggle button. Accelerator has its own thread of 
// execution. Its method run appears in superclass ClockedFrame.
public class Accelerator extends ClockedFrame {

// The components that go on the Frame
	static final String [] buttons= {"      Turn output on      ",
		"      Turn output off     "};
	private Button button= new Button(buttons[0]);
	private Label statusLabel= new Label("     accelerator is off     ");

private boolean status= false;  //status = "accelerator is on"

// Constructor: an accelerator, which relies on clock c.
public Accelerator(SystemClock c) {
    super(c, "Accelerator");
    add("North", statusLabel);
    add("South", button);
    resize(100,60);
    pack();
    move(280,100);
    show();
  	}
	
// Turn accelerator off
public synchronized void turnOff() {
	status= false;
	statusLabel.setText("accelerator is off/up");
	}
	
// Turn accelerator on
public synchronized void turnOn() {
	status= true;
	statusLabel.setText("accelerator is on/down");
	}
	
// Yield the value 1 for "accelerator on" and 0 for "accelerator off"
public synchronized double readValue() {
	if (status) return 1;
	return 0;
	}
	
// If a button was pressed, process it; otherwise, return result
// of super.action. The button is the output-toggle button
public boolean action(Event e, Object arg) {
	if (arg.equals(button.getLabel())) {
		// Handle press of the output-toggle button
			CruiseControl.outputToggle= !CruiseControl.outputToggle;
			if (arg.equals(buttons[0]))
				 button.setLabel(buttons[1]);
			else button.setLabel(buttons[0]);
		return true;
		}
    return super.action(e, arg);
  	}
}
 