Java Reference
In-Depth Information
// Channel must be from MIN_CHANNEL to MAX_CHANNEL
if(newChannel>=MIN_CHANNEL && newChannel<=MAX_CHANNEL) {
channel = newChannel;
}
System.out.println(make + " DVD Player tuned to channel: " +
channel);
return channel;
}
public int channelUp() {
if(!power) {
// If the power is off
return 0;
// Nothing works
}
// Wrap channel round to MIN_CHANNEL when MAX_CHANNEL is reached
channel = channel<MAX_CHANNEL ? ++channel : MIN_CHANNEL;
System.out.println(make + " DVD Player tuned to channel: " +
channel);
return channel;
}
public int channelDown() {
if(!power) {
// If the power is off
return 0;
// Nothing works
}
// Wrap channel round to MAX_CHANNEL when MIN_CHANNEL is reached
channel = channel>MIN_CHANNEL ? --channel : MAX_CHANNEL;
System.out.println(make + " DVD Player tuned to channel: " +
channel);
return channel;
}
private String make = null;
private boolean power = false;
private final int MIN_VOLUME = 0;
private final int MAX_VOLUME = 100;
private int volume = MIN_VOLUME;
private final int MIN_CHANNEL = 0;
private final int MAX_CHANNEL = 99;
private int channel = MIN_CHANNEL;
}
Directory "TryRemoteControl"
Of course, you could continue to define classes for other kinds of devices that use the remote, but these
two are sufficient to demonstrate the principle.
Let's see how you can use the RemoteControl interface and these two classes in a working example.
Search WWH ::




Custom Search