1、JAVA实验5Java语言程序设计实验报告实验序号:05实验项目名称:继承和多态学号xx姓名xx专业、班xx实验地点xx实验室指导教师xx时间2012/5/22一、实验目的及要求目的:理解继承与多态的概念,熟练掌握使用方法二、实验设备(环境)1.windows操作系统2.MyEclipse软件三、实验内容与步骤1、 设计一个数据类型判断类Polymorphism,使用重载、装箱等技术判断一个不带等号的Java表达式的结果的数据类型。2、 设计一个链表结点类LinkNode,此类可以存放int、long、float、double、byte、short、String、StringBuffer类型的
2、数据。用此类:a、随机产生100个整数(范围自定)的链表,在生成的过程中从小到大排列,然后输出;b、随机产生100个6个英文字母的单词的链表,在生成的过程中从小到大排列,然后输出。(关注装箱和拆箱相关概念)3、 A、在main()中使用LinkNode类创建4个实例,并赋予不同的值(long、double、 StringBuffer、MyDate),然后使用Object中默认的toString()方法(从超级父类继承而来) 显示结果。B、继承LinkNode类创建新类LinkNodeS,在其中重写Object中默认的toString()方法(将结点的value转换成对应的字符串),main()
3、中用LinkNodeS类同样创建4个实例,并赋予和上面同样的值(long、double、StringBuffer、MyDate),观察使用新的toString()方法的效果,体会继承与多态。(MyDate的日期toString用标准格式)4、有来自4类(鸟类、昆虫类、爬行类和鱼类)的100个动物聚在一起开会,商议和另一个动物部落打仗事宜,会议要求每个动物都要报告自己所属的动物类别和自己的天赋,以便选拔人才、组织兵力迎战。设计:用Animal作为基类,鸟类、昆虫类、爬行类和鱼类各作为Animal的子类设计类层次结构,设计时运用继承、重写并设计多态机制,同时对每个子类至少要添加一个其描述的动物特有
4、的行为和一个特有的属性,以更准确地描述子类对象。使用:用循环随机生成这100个动物装入动物数组,要对每个动物进行编号和随机命名,用循环让每个参会的动物报告自己的类别和天赋。 Animal内容如下: Class Animal public int id; /编号 public String name;/名字 /public String type; /类别:鸟类、昆虫类、爬行类和鱼类之一 public void showType()/在console上秀自己的类别 System.out.println(“My ID is “+id+” and I am just an animal, my ty
5、pe is going to be determined later.”); public void showTalent()/在console上秀自己的天赋特长 System.out.println(“I dont know what I am talented at.”);(请认真观察和体会Java的继承和多态)四、实验结果与数据处理第一题:public class Judge public static void main(String args) System.out.println(Polymorphism.getType(1+2+3); System.out.println(Pol
6、ymorphism.getType(1234589001l*123456789); System.out.println(Polymorphism.getType(1/3.0); System.out.println(Polymorphism.getType(123.456f*123); System.out.println(Polymorphism.getType(A); class Polymorphism private final static String INT_TYPE = int; private final static String LONG_TYPE = long; pr
7、ivate final static String DOUBLE_TYPE = double; private final static String FLOAT_TYPE = float; private final static String CHAR_TYPE = char; private final static String BYTE_TYPE = byte; private final static String SHORT_TYPE = short; private final static String BOOLAEN_TYPE = boolean; public stati
8、c String getType(int i) return INT_TYPE; public static String getType(long l) return LONG_TYPE; public static String getType(double d) return DOUBLE_TYPE; public static String getType(float f) return FLOAT_TYPE; public static String getType(char c) return CHAR_TYPE; public static String getType(byte
9、 by) return BYTE_TYPE; public static String getType(short s) return SHORT_TYPE; public static String getType(boolean bo) return BOOLAEN_TYPE; public static String getType(Object obj) return obj != null ? obj.toString().split()0 : null; 第二、三题:public class myLinkNode public static void main(String arg
10、s) int100(); englishString100(); LinkNode aLong=new LinkNode(123456789l); LinkNode aDouble=new LinkNode(123.456); LinkNode aStringBuffer=new LinkNode(abcde); LinkNode aDate=new LinkNode(2011-11-11 12:13:24); System.out.println(aLong.toString(); System.out.println(aDouble.toString(); System.out.print
11、ln(aStringBuffer.toString(); System.out.println(aDate.toString(); LinkNodeS anotherLong=new LinkNodeS(123456789l); LinkNodeS anotherDouble=new LinkNodeS(123.456); LinkNodeS anotherStringBuffer=new LinkNodeS(abcde); LinkNodeS anotherDate=new LinkNodeS(2011-11-11 12:13:24); System.out.println(anotherL
12、ong.toString(); System.out.println(anotherDouble.toString(); System.out.println(anotherStringBuffer.toString(); System.out.println(anotherDate.toString(); static void int100() LinkNode theFirst=new LinkNode(-1); LinkNode aPointer=theFirst; LinkNode theEnd=new LinkNode(1000); aPointer.nextNode=theEnd
13、; for(int i=0;i(Integer)aPointer.nextNode.value) aPointer=aPointer.nextNode; aNode.nextNode=aPointer.nextNode; aPointer.nextNode=aNode; aPointer=theFirst; for(int i=0;i100;i+) aPointer=aPointer.nextNode; aPointer.display(); static void englishString100() LinkNode theFirst=new LinkNode(aaaaaa); LinkN
14、ode aPointer=theFirst; LinkNode theEnd=new LinkNode(zzzzzz); aPointer.nextNode=theEnd; int intCharCode; for(int i=0;i100;i+) StringBuffer aString=new StringBuffer(); aPointer=theFirst; aString.setLength(0); for (int j = 0; j = 65 & intCharCode = 97 & intCharCode 0) aPointer=aPointer.nextNode; aNode.
15、nextNode=aPointer.nextNode; aPointer.nextNode=aNode; aPointer=theFirst; for(int i=0;i100;i+) aPointer=aPointer.nextNode; aPointer.display(); class LinkNode public Object value; public LinkNode nextNode; public LinkNode(Object obj) value=obj; public void display() System.out.println(this.value); clas
16、s LinkNodeS extends LinkNode public LinkNodeS(Object obj) super(obj); public String toString() String aString; aString=+value; return aString; 第四题:public class Animals /* * param args */ public static void main(String args) StringBuffer names; names=new StringBuffer100; int intCharCode; StringBuffer
17、 aName=new StringBuffer(); for (int i = 0; i 100; i+) aName.setLength(0); for (int j = 0; j = 65 & intCharCode = 97 & intCharCode = 122) intCharCode = 65 + (int) (Math.random() * 58); aName.append(char)intCharCode); namesi=new StringBuffer(aName); Animal animals=new Animal100; int aNum,flag; for(int
18、 i=0;i100;i+) aNum=30+(int)(Math.random()*70); flag=(int)(Math.random()*4); if(flag=0) animalsi=new Bird(i+1,namesi,bird,flying,aNum); else if(flag=1) animalsi=new Insect(i+1,namesi,insect,jumping,aNum); else if(flag=2) animalsi=new Reptilia(i+1,namesi,reptilia,creeping,aNum); else animalsi=new Fish
19、(i+1,namesi,fish,swimming,aNum); for(int i=0;i100;i+) animalsi.showType(); animalsi.showTalent(); if(animalsi instanceof Bird) Bird aBird=(Bird) animalsi; aBird.showMyFly(); else if(animalsi instanceof Insect) Insect anInsect=(Insect) animalsi; anInsect.showMyJump(); else if(animalsi instanceof Rept
20、ilia) Reptilia aReptilia=(Reptilia) animalsi; aReptilia.showMyCreep(); else Fish aFish=(Fish) animalsi; aFish.showMySwim(); class Animal public int id; /编号 public StringBuffer name;/名字 public String type; /类别:鸟类、昆虫类、爬行类和鱼类之一 public String talent; public void showType()/在console上秀自己的类别 System.out.pri
21、nt(My ID is +id+ and my name is +name+.+ I am just a +type+.); public void showTalent()/在console上秀自己的天赋特长 System.out.print(I am talented at +talent+.); class Bird extends Animal public int flySpeed; public Bird(int id,StringBuffer name,String type,String talent,int flySpeed) this.id=id; this.name=na
22、me; this.type=type; this.talent=talent; this.flySpeed=flySpeed; public void showMyFly() System.out.println(And I can fly +flySpeed+ kilometres per hour.); class Insect extends Animal public int jumpHeight; public Insect(int id,StringBuffer name,String type,String talent,int jumpHeight) this.id=id; t
23、his.name=name; this.type=type; this.talent=talent; this.jumpHeight=jumpHeight; public void showMyJump() System.out.println(And I can jump +jumpHeight+cm high.); class Reptilia extends Animal public int creepSpeed; public Reptilia(int id,StringBuffer name,String type,String talent,int creepSpeed) thi
24、s.id=id; this.name=name; this.type=type; this.talent=talent; this.creepSpeed=creepSpeed; public void showMyCreep() System.out.println(And I can creep +creepSpeed+ kilometres per hour.); class Fish extends Animal public int swimSpeed; public Fish(int id,StringBuffer name,String type,String talent,int
25、 swimSpeed) this.id=id; this.name=name; this.type=type; this.talent=talent; this.swimSpeed=swimSpeed; public void showMySwim() System.out.println(And I can swim +swimSpeed+ kilometres per hour.); 运行结果:intlongdoublefloatchar13 14 79 83 95 96 98 100 104 106 108 112 122 127 134 154 158 187 204 211 216
26、278 306 336 344 344 357 378 383 384 386 388 388 391 392 412 423 426 433 443 447 448 455 462 476 478 486 488 489 492 495 498 504 509 510 531 564 572 603 603 617 623 627 631 635 643 663 691 705 707 713 722 727 733 742 743 765 787 788 803 804 807 820 820 841 844 848 861 873 884 892 921 939 940 956 964 975 976 981 999 AboRZx BOUbzD CNmcRP CyMBvZ DLEEnZ EvYVht GZmNMR Gkgeix Gywnwy HCAOeZ HqhBZV HvYloU JrhiVW KPuJZO Khxymc MtTpUA NeNwKY NkzzeW NxJrgX NysQcY PFCIwC PJrTFM PdMuwA QOsODB QTbWYG QzalAs QzjtPi RpguMH SbJaaW SeGRLj ShCOsb TIUJUW TWvtTk TaHoZv UUpqIP UmjNoD VZ
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1