第三次作业
2、编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类E。要求:
(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()方法,在speak方法中输出“咿咿呀呀......”的信息。
(2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法中输出“小样的,不错嘛!会说话了!”的信息。
(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”的信息。
(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功能。
1 public class E { 2 public static void main(String[] args) { 3 String s="add"; 4 Monkey b1=new Monkey("add"); 5 People b2=new People("sss"); 6 b1.speak(); 7 b2.think(); 8 b2.speak(); 9 } 10 11 } 12 class Monkey{ 13 private String s; 14 public Monkey(String s){ 15 this.s=s; 16 17 } 18 public void speak(){ 19 System.out.println("咿咿呀呀....."); 20 } 21 } 22 class People extends Monkey{ 23 24 public People(String s) { 25 super(s); 26 27 } 28 public void speak(){ 29 System.out.println("小样的,不错嘛!会说话了!"); 30 } 31 public void think(){ 32 System.out.println("别说话!认真思考!"); 33 } 34 35 } 36
3、按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。
1 public class D { 2 } 3 class rectangle{ 4 private int length; 5 private int wide; 6 public rectangle(int length,int wide){ 7 this.length=length; 8 this.wide=wide; 9 } 10 public int getL(int length){ 11 return length; 12 } 13 public int getW(int wide){ 14 return wide; 15 } 16 public void setL(int length){ 17 this.length=length; 18 } 19 public void setW(int wide) { 20 this.wide=wide; 21 } 22 public void count(int length,int wide){ 23 int area; 24 area=wide*length; 25 } 26 }
1 public class cuboid extends D{ 2 private int length1; 3 private int wide1; 4 private int high; 5 public cuboid(int length1,int wide1,int high){ 6 this.length1=length1; 7 this.wide1=wide1; 8 this.high=high; 9 } 10 public int getl(int length1) { 11 return length1; 12 } 13 public int getw(int wide1) { 14 return wide1; 15 } 16 public int geth(int high) { 17 return high; 18 } 19 public void setl(int length1){ 20 this.length1=length1; 21 } 22 public void setw(int wide1){ 23 this.wide1=wide1; 24 } 25 public void seth(int high){ 26 this.high=high; 27 } 28 public void count1(int length1,int wide1,int high){ 29 int bulk; 30 bulk=length1*wide1*high; 31 } 32 }
1 public class TestD { 2 public static void main(String[] args){ 3 int length; 4 int wide; 5 rectangle b1=new rectangle(2,3); 6 cuboid b2=new cuboid(4,5,6); 7 b1.count(2,3); 8 b2.count1(4,5,6); 9 System.out.println("矩形的面积为: "+(b1.getL(2)*b1.getW(3))); 10 System.out.println("长方体的体积为: "+b2.getl(4)*b2.getw(5)*b2.geth(6)); 11 } 12 }
4、编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能。
1 public class F { 2 3 } 4 class Vehicle { 5 private int wheels; 6 private int weight; 7 public Vehicle(int wheels,int weight){ 8 this.wheels=wheels; 9 this.weight=weight; 10 } 11 public int getW(int wheels){ 12 return wheels; 13 } 14 public int getw(int weight) { 15 return weight; 16 } 17 public void setW(int wheels) 18 { 19 this.wheels=wheels; 20 } 21 public void setw(int weight) 22 { 23 this.weight=weight; 24 } 25 public void println(int wheels,int weight) { 26 System.out.println("输出车轮个数为: "+wheels+"车重为: "+weight); 27 } 28 }
1 public class ff { 2 3 } 4 class Car extends F{ 5 private int loader; 6 public Car(int loader) { 7 this.loader=loader; 8 } 9 public int getC(int loader){ 10 return loader; 11 } 12 public void setc(int loader) { 13 this.loader=loader; 14 } 15 public void println(int loader) { 16 System.out.println("载人数为: "+loader); 17 } 18 }
1 public class Truck extends ff{ 2 private int payload; 3 public Truck(int payload){ 4 this.payload=payload; 5 } 6 public int getT(int payload){ 7 return payload; 8 } 9 public void sett(int loader) { 10 this.payload=payload; 11 } 12 public void println(int payload) { 13 System.out.println("有载重量为: "+payload); 14 } 15 }
1 public class TestF { 2 public static void main(String[] args){ 3 Vehicle b1=new Vehicle(4,100); 4 Car b2=new Car(4); 5 Truck b3=new Truck(50); 6 b1.println(4, 100); 7 b2.println(4); 8 b3.println(50); 9 } 10 }
本次作业不是特别难,翻阅书籍后能够自己写出来。。不过还是说明了一句话:遇上难点就查阅书籍,书籍能够给于你正确的解法亦或是思路。