• 实现电视音量的加减


    实现电视音量的加减

    代码如下:

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

    第一部分
    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);
    } }

    只相信苦尽甘来
  • 相关阅读:
    计算机网络-数据结构-MAC帧头-IP头-TCP头-UDP头
    (考研)java网络编程
    多态(重点:方法的多态性和对象的多态性)
    JZOJ1497 景点中心 题解
    JZOJ1227 Coprime 题解
    JZOJ3966 Sabotage 题解
    JZOJ3056 数字 题解
    JZOJ3054 祖孙询问 题解
    【Luogu P2282】【JZOJ 4906】【NOIP2016提高组复赛】组合数问题 题解
    JZOJ4316【NOIP2015模拟11.5】Isfind 题解
  • 原文地址:https://www.cnblogs.com/F001li/p/7055771.html
Copyright © 2020-2023  润新知