第8周作业1——类与对象练习之TV

  抱歉,迟了。

public class TV {
	int channel =1;//Default channel is 1
	int volumeLevel = 1;//Default volume level is 1
	boolean on =false;//By default TV is off

	public TV(){

	}

	public void turnOn(){
		on=true;
	}

	public void turnOff(){
		on=false;
	}
	public void setChannel(int newChannel){
		if (on&&newChannel >= 1&&newChannel <=120)
			channel=newChannel;
	}

	public void setVolume(int newVolumeLevel){
		if(on&&newVolumeLevel>=1&&newVolumeLevel<=7)
			volumeLevel = newVolumeLevel;
	}

	public void channelUp(){
		if(on&&channel<120)
			channel++;
	}

	public void channelDown(){
		if(on&&channel>1)
			channel--;
	}

	public void volumeUP(){
		if(on&&volumeLevel<7)
			volumeLevel++;
	}

	public void volumeDown(){
		if(on&&volumeLevel>1)
			volumeLevel--;
	}
}
这一个程序也不知道为什么运行不了,我的猜想就是就像这是个遥控器,电视机还没开,所以遥控器对电视的操作没反应。

public class TestTV {
	public static void main(String[] args){
		TV tv1 = new TV();
		tv1.turnOn();
		tv1.setChannel(30);
		tv1.setVolume(3);

		TV tv2 = new TV();
		tv2.turnOn();
		tv2.channelUp();
		tv2.channelUp();
		tv2.volumeUP();

		System.out.println("tv1's channel is "+tv1.channel
				+" and volume level is "+tv1.volumeLevel);
		System.out.println("tv2's channel is "+tv2.channel
				+" and volume level is "+tv2.volumeLevel);
	}

}


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值