練習9(第九章習題)
阿新 • • 發佈:2017-11-07
als 設置 rgs 調試 test ack col on() sim
package cn.ch.w; public class TestSimpleCircle { private double radius; // 構造方法,有參構造 public void Circle(double radius) { this.radius = radius; } //方法的重載,參數不同 // 構造方法,無參構造 public void 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 cn.ch.w; public class DemoCircle { public static void main(String[] args) { TestSimpleCircle circle1=new TestSimpleCircle(); double area=circle1.getArea(); System.out.println(area); TestSimpleCircle circle2=new TestSimpleCircle(); 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()); } }
9-2
package cn.ch.w; public class SimpleCircle { private double radius; public SimpleCircle() { this.radius=1; } public SimpleCircle(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) { SimpleCircle cir1=new SimpleCircle(); System.out.println("半徑是"+cir1.radius+"面積是"+cir1.getArea()); SimpleCircle cir2=new SimpleCircle(10); System.out.println("半徑是"+cir2.radius+"面積是"+cir2.getArea()); } }
9-3
package cn.ch.w; 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 cn.ch.w; 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.channel+"聲音是"+tv2.volumeLevel); tv2.channelUp(); System.out.println("電視節目是"+tv2.channel+"聲音是"+tv2.volumeLevel); tv2.channelUp(); System.out.println("電視節目是"+tv2.channel+"聲音是"+tv2.volumeLevel); tv2.channelUp(); System.out.println("電視節目是"+tv2.channel+"聲音是"+tv2.volumeLevel); tv2.volumeUp(); System.out.println("電視節目是"+tv2.channel+"聲音是"+tv2.volumeLevel); tv2.volumeUp(); System.out.println("電視節目是"+tv2.channel+"聲音是"+tv2.volumeLevel); tv2.volumeUp(); System.out.println("電視節目是"+tv2.channel+"聲音是"+tv2.volumeLevel); } }
練習9(第九章習題)