Java Reference
In-Depth Information
}
public int channelUp() {
if(!power) // If the power is off
return 0; // Nothing works
// Wrap channel up to MIN _ CHANNEL when MAX _ CHANNEL is reached
channel = channel<MAX _ CHANNEL ? ++channel : MIN _ CHANNEL;
System.out.println(make + " "+ screensize + " inch TV tuned to channel: "
+ channel);
return channel;
}
public int channelDown() {
if(!power) // If the power is off
return 0; // Nothing works
// Wrap channel down to MAX _ CHANNEL when Min _ CHANNEL is reached
channel = channel>MIN _ CHANNEL ? --channel : MAX _ CHANNEL;
System.out.println(make + " "+ screensize + " inch TV tuned to channel: "
+ channel);
return channel;
}
private String make = null;
private int screensize = 0;
private boolean power = false;
private int MIN _ VOLUME = 0;
private int MAX _ VOLUME = 100;
private int volume = MIN _ VOLUME;
private int MIN _ CHANNEL = 0;
private int MAX _ CHANNEL = 999;
private int channel = 0;
}
This class implements all the methods declared in the RemoteControl interface, and each method
outputs a message to the command line so we'll know when it is called. Of course, if we missed out any
of the interface method definitions in the class, the class would be abstract and we would have to
declare it as such.
A VCR class might also implement RemoteControl :
public class VCR implements RemoteControl {
public VCR(String make) {
this.make = make;
}
public boolean powerOnOff() {
power = !power;
System.out.println(make + " VCR power "+ (power ? "on.":"off."));
Search WWH ::




Custom Search