实现电视音量的加减

实现电视音量的加减

代码如下:

注:一个程序分为两个文件

第一部分
package Day05;

public class TV {
int channel = 1;//1<=channel<=120
int volumeLevel = 50;// 0<=volumeLevel<=100
boolean on = false;
/**
*
*/
public TV() {
}
/**
* @param channel
* @param volumeLevel
* @param on
*/
public TV(int channel, int volumeLevel, boolean on) {
this.channel = channel;
this.volumeLevel = volumeLevel;
this.on = on;
} public void turnOn() {
this.on = true;
} public void turnOff() {
this.on = false;
} public int getChannel() {
return channel;
}
public void setChannel(int channel) {
if (on && channel >= 1 && channel <= 120) {
this.channel = channel;
}
}
public int getVolumeLevel() {
return volumeLevel;
}
public void setVolumeLevel(int volumeLevel) {
if (on && volumeLevel >= 0 && volumeLevel <= 100) {
this.volumeLevel = volumeLevel;
}
} public void channelUp() {
if (channel < 120 && this.on) {
this.channel++;
}
} public void channelDown() {
if (channel > 1 && this.on) {
this.channel--;
}
} public void volumeUp() {
if (volumeLevel > 0 && this.on) {
this.volumeLevel++;
}
} public void volumeDown() {
if (volumeLevel > 0 && this.on) {
this.volumeLevel--;
}
}
}
第二部分
package Day05; public class testTV { public static void main(String[] args) {
TV tv1 = new TV();
tv1.turnOn();
tv1.setChannel(30);
tv1.setVolumeLevel(10);

System.out.println("tv1 channel: " + tv1.channel);
System.out.println("tv1 volume: " + tv1.volumeLevel);
System.out.println("tv1 on: " + tv1.on);

TV tv2 = new TV(10, 30, true);
System.out.println("tv2 channel: " + tv2.channel);
System.out.println("tv2 volume: " + tv2.volumeLevel);
System.out.println("tv2 on: " + tv2.on);
} }

posted @ 2017-06-20 18:52  辰峰  阅读(261)  评论(0)    收藏  举报