我正在做一个 UML,但我不太确定如何执行我应该这样做的这些 mutator 方法:
+turnOn(): void //sets on to true
+turnOff(): void //sets on to false
+channelUp(): void //increases channel by 1 if on, rolls to 1 after maximum
+channelDown(): void //decreases channel by 1 if on, rolls to maximum after 1
+volumeUp(): void //increases the volume by 1 if on and less than maximum
+volumeDown(): void //decreases volume by 1 if on and greater than 0
+toString( ): String //returns the current state(instance variable values)
我现在的代码:(请记住,mutator 部分是不正确的)
public class TV {
private int volume;
private int channel;
private boolean on;
private int maxVolume;
private int maxChannel;
TV() {
volume = 1;
channel = 1;
on = false;
maxVolume = 1;
maxChannel = 1;
}
public int getChannel() {
return channel;
}
public int getVolume() {
return volume;
}
public boolean isOn() {
return on;
}
public int getMaxChannel() {
return maxChannel;
}
public int getMaxVolume() {
return maxVolume;
}
public void setChannel(int i) {
if (isOn() && i >= 1 && i <= maxChannel) channel = i;
}
public void setVolume(int i) {
if (isOn() && i >= 0 && i <= maxVolume) volume = i;
}
public void setMaxChannel(int i) {
maxChannel = i;
}
public void setMaxVolume(int i) {
maxVolume = i;
}
// + turnOn() * * This is where the mutator methods begin I need help here * *
// if (channel == maxChannel) channel = 1;
// else channel++;
//if (channel == 1) channel = max;
//else channel--;
// if (volume == maxVolume) volume = 1;
// else channel++;
//if (volume == 1) volume = max;
// else channel--;
public string toString() {
return "channel: " + channel + "\nvolume: " + volume +
"\non: " + on + "\nmax Channel: " + maxChannel +
"\nmax Volume: " + maxVolume;
}
}