java第九次作業:第九章例題3個,第九次第九章

來源:互聯網
上載者:User

java第九次作業:第九章例題3個,第九次第九章

作業1:

例題9.1 製作圓類,根據圓的半徑求出周長及面積

package com.swift;//抽象的方法構成類,把屬性和方法進行封裝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 com.swift;public class DemoCircle {    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());    }}

作業2:

把上邊的兩個圓的類合并成一個類,並實現同樣功能

package com.swift;public class SimpleCircle {    private double radius;    public SimpleCircle() {        this.radius=1;    }    public SimpleCircle(double radius){        this.radius=radius;    }    public double getArea() {        // TODO Auto-generated method stub        return Math.PI*radius*radius;    }    public double getPerimeter() {        return 2*Math.PI*radius;    }        public static void main(String[] args) {        SimpleCircle cir1=new SimpleCircle();        System.out.println("The area of the circle of radius "+cir1.radius+" is "+cir1.getArea());        SimpleCircle cir2=new SimpleCircle(10);        System.out.println("The area of the circle of radius "+cir2.radius+" is "+cir2.getArea());    }}

作業3:

例題9.3 造一台電視機,並且實現調頻道和調聲音大小功能

package com.swift;public class TV {    public int channel=1;    public int volumeLevel=1;    public boolean on=false;        public TV() {            }    public void turnOn() {        on =true;        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("電視機關著的時候是不能調聲音的");        }            }    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 com.swift;public class TestTV {    public static void main(String[] args) {//        TV tv1=new TV();//        tv1.turnOff();//        tv1.setChannel(30);//        tv1.setVolumeLevel(3);                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);                    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.