第九次作业圆、电视机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
1
 
package kk;
 
public class circle {
     private double radius;
 
        // 构造方法,有参构造
        public circle(double radius) {
            this.radius = radius;
        }
        //方法的重载,参数不同
        // 构造方法,无参构造
        public circle() {
            this.radius = 1;
        }
 
        // 求圆面积的方法
        public double getArea() {
            return radius * radius * Math.PI;
 
        }
 
        // 求圆周长的方法
        public double getPerimeter() {
            return 2 * Math.PI * radius;
        }
        public void setRadius(double newRadius) {
            this.radius=newRadius;
        }
}
package kk;
 
public class Dmc {
      public static void main(String[] args) {
            circle circle1=new circle();
            double area=circle1.getArea();
            System.out.println(area);
            circle circle2=new circle(10);
            System.out.println(circle2.getArea());
            System.out.println(circle1.getPerimeter());
            System.out.println(circle2.getPerimeter());
            double ridius=10;
            double areaCircle=Math.PI*ridius*ridius;
            System.out.println(areaCircle);
            circle2.setRadius(5);
            System.out.println(circle2.getArea());
             
        }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
  
package kk;
 
public class Scircle {
     private double radius;
        public Scircle() {
            this.radius=1;
        }
        public Scircle(double radius){
            this.radius=radius;
        }
        public double getArea() {
            return Math.PI*radius*radius;
        }
        public double getPerimeter() {
            return 2*Math.PI*radius;
        }
        public static void main(String[] args) {
            Scircle cir1=new Scircle();
            System.out.println("The area of the circle of radius "+cir1.radius+" is "+cir1.getArea());
            Scircle cir2=new Scircle(10);
            System.out.println("The area of the circle of radius "+cir2.radius+" is "+cir2.getArea());
        }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 3
  
  
  
package kk;
public class TV {
    public int channel=1;
    public int volumeLevel=1;
    public boolean on=false;
    public void turnOn() {
        on =true;
        System.out.println("地址为"+new TV());
        System.out.println("号的电视已经打开");
    }
    public void turnOff() {
        on=false;
        System.out.println("电视已经关闭");
    }
    public int getChannel() {
        return channel;
    }
    public void setChannel(int channel) {
        if(on) {
            System.out.println("电视机已开,可开始调台");
            if(channel>=1&&channel<=120) {
                this.channel = channel;
                System.out.println("频道已经调到 "+channel+" 号台");
            }else {
                System.out.println("你要调的频道不存在");
            }
        }else {
            System.out.println("电视机关着的时候不能调台");
        }
    }
    public int getVolumeLevel() {
        return volumeLevel;
    }
    public void setVolumeLevel(int volumeLevel) {
        if(on) {
            System.out.println("电视机已开,声音大小可调");
            if(volumeLevel>=1&&volumeLevel<=7) {
                this.volumeLevel = volumeLevel;
                System.out.println("声音的大小设置成 "+volumeLevel);
            }
            else {
                System.out.println("音量大小超过范围");
            }
        }else {
            System.out.println("电视机关着的时候不能调声音");
        }
         
    }
    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--;
        }
    }
     
     
}
package kk;
 
public class CTV {
     public static void main(String[] args) {
       TV tv1=new TV();
       tv1.turnOn();
       tv1.setChannel(77);
       tv1.setVolumeLevel(9);     
       TV tv2=new TV();
       tv2.turnOn();
       System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
       tv2.channelUp();
       System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
       tv2.channelUp();
       System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
       tv2.channelUp();
       System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
       tv2.volumeUp();
       System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
       tv2.volumeUp();
       System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
       tv2.volumeUp();
       System.out.println("TV2's channel is "+tv2.channel+" and volume is "+tv2.volumeLevel);
              
   }
 
}

  

posted @   安静惠  阅读(230)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示