11.6作业 电视机和圆
//1.
//例题9.1 制作圆类,根据圆的半径求出周长及面积 package com.atlu; //抽象的方法构成类,把属性和方法进行封装 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.atlu; 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.
//作业2:把上边的两个圆的类合并成一个类,并实现同样功能 package com.atlu; 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()); System.out.println("The perimeter of the circle of radius "+cir1.radius+" is "+cir1.getPerimeter()); SimpleCircle cir2=new SimpleCircle(10); System.out.println("The area of the circle of radius "+cir2.radius+" is "+cir2.getArea()); System.out.println("The perimeter of the circle of radius "+cir2.radius+" is "+cir2.getPerimeter()); } }
//3.
//例题9.3 造一台电视机,并且实现调频道和调声音大小功能 package com.atlu; 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.atlu; 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(); tv2.setChannel(130); // tv2.setVolumeLevel(3); 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); } }
//4.
package com.atlu; public class StaticDemo { public static void main(String args[]){ new StaticDemo().fun(); } public void fun(){ System.out.println("Hello World!!!"); } }
//5.
package com.atlu; public class DemoPerson { private String name; private int age; public DemoPerson(String name,int age){ this.name=name; this.age=age; } public boolean compare(DemoPerson1 per){ if(this.name.equals(per.name)&&this.age==per.age){ return true; }else{ return false; } } public String getName(){ return this.name; } public int getAge(){ return this.age; } public static void main(String args[]){ DemoPerson1 per1=new DemoPerson1("张三",30); DemoPerson1 per2=new DemoPerson1("张三",30); if(per1.compare(per2)){ System.out.println("是同一个人!"); } } };
//6.
package com.atlu; public class DemoPerson1 { String name; int age; public DemoPerson1(String name,int age){ this.name=name; this.age=age; } public boolean compare(DemoPerson1 per){ if(this==per){//地址相等 return true; } if(this.name.equals(per.name)&&this.age==per.age){ return true; }else{ return false; } } public String getName(){ return this.name; } public int getAge(){ return this.age; } public static void main(String args[]){ DemoPerson1 per1=new DemoPerson1("张三",30); DemoPerson1 per2=new DemoPerson1("张三",30); if(per1.compare(per2)){ System.out.println("是同一个人!"); } } };
//7.
package com.atlu; class DemoPerson2{ private String name; private int age; public DemoPerson2(String name,int age){ this.name=name; this.age=age; } public void fun(DemoPerson2 temp){ temp.name="李四"; temp.age=33; } public String getName(){ return this.name; } public int getAge(){ return this.age; } public static void main(String args[]){ DemoPerson2 per=new DemoPerson2("张三",30); per.fun(per);{ System.out.println(per.getName()+"-->"+per.getAge()); } } }
//8.
package com.atlu; class DemoPerson3{ private String name; private int age; public DemoPerson3(String name,int age){ this.name=name; this.age=age; } public String getName(){ return this.name; } public int getAge(){ return this.age; } public static void main(String args[]){ DemoPerson3 per1=new DemoPerson3("张三",30); DemoPerson3 per2=new DemoPerson3("张三",30); if(per1.getName().equals(per2.getName())&&per1.getAge()==per2.getAge()){ System.out.println("是同一个人!"); } } };