计科1101_JavaExp5_.doc_第1页
计科1101_JavaExp5_.doc_第2页
计科1101_JavaExp5_.doc_第3页
计科1101_JavaExp5_.doc_第4页
计科1101_JavaExp5_.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java语言程序设计实验报告实验序号:05实验项目名称:继承和多态学号姓名专业、班实验地点文波指导教师韩志龙时间2013/6/3一、实验目的及要求目的:理解继承与多态的概念,熟练掌握使用方法二、实验设备(环境)1.windows操作系统2.MyEclipse软件三、实验内容与步骤1、 设计一个数据类型判断类Polymorphism,使用重载、装箱等技术判断一个不带等号的Java表达式的结果的数据类型。2、 设计一个链表结点类LinkNode,此类可以存放int、long、float、double、byte、short、String、StringBuffer类型的数据。用此类:a、随机产生100个整数(范围自定)的链表,在生成的过程中从小到大排列,然后输出;b、随机产生100个6个英文字母的单词的链表,在生成的过程中从小到大排列,然后输出。(关注装箱和拆箱相关概念)3、 A、在main()中使用LinkNode类创建4个实例,并赋予不同的值(long、double、 StringBuffer、MyDate),然后使用Object中默认的toString()方法(从超级父类继承而来) 显示结果。B、继承LinkNode类创建新类LinkNodeS,在其中重写Object中默认的toString()方法(将结点的value转换成对应的字符串),main()中用LinkNodeS类同样创建4个实例,并赋予和上面同样的值(long、double、StringBuffer、MyDate),观察使用新的toString()方法的效果,体会继承与多态。(MyDate的日期toString用标准格式)4、有来自4类(鸟类、昆虫类、爬行类和鱼类)的100个动物聚在一起开会,商议和另一个动物部落打仗事宜,会议要求每个动物都要报告自己所属的动物类别和自己的天赋,以便选拔人才、组织兵力迎战。设计:用Animal作为基类,鸟类、昆虫类、爬行类和鱼类各作为Animal的子类设计类层次结构,设计时运用继承、重写并设计多态机制,同时对每个子类至少要添加一个其描述的动物特有的行为和一个特有的属性,以更准确地描述子类对象。使用:用循环随机生成这100个动物装入动物数组,要对每个动物进行编号和随机命名,用循环让每个参会的动物报告自己的类别和天赋。 Animal内容如下:Class Animalpublic 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 type is going to be determined later.”);public void showTalent()/在console上秀自己的天赋特长System.out.println(“I dont know what I am talented at.”);(请认真观察和体会Java的继承和多态)四、实验结果与数据处理第一题:public class wmtpublic static void main(String args) System.out.println(Polymorphism.getType(5678901234l*987654321); System.out.println(Polymorphism.getType(34+6+8); System.out.println(Polymorphism.getType(u); System.out.println(Polymorphism.getType(6/5.0); System.out.println(Polymorphism.getType(456.123f*789); class Polymorphism private final static String INT_TYPE = int; private final static String LONG_TYPE = long; private 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 static 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 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 wmt public static void main(String args) int100();englishString100();LinkNode aLong=new LinkNode(123456789l);LinkNode aDouble=new LinkNode(123.456);LinkNode aStringBuffer=new LinkNode(abcde);LinkNode aDate=new LinkNode(2013-6-3 21:16:28);System.out.println(aLong.toString();System.out.println(aDouble.toString();System.out.println(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(2013-6-3 21:16:28);System.out.println(anotherLong.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;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);LinkNode 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.nextNode=aPointer.nextNode;aPointer.nextNode=aNode;aPointer=theFirst;for(int i=0;i100;i+)aPointer=aPointer.nextNode;aPointer.display();class LinkNodepublic Object value;public LinkNode nextNode;public LinkNode(Object obj)value=obj;public void display()System.out.println(this.value);class LinkNodeS extends LinkNodepublic LinkNodeS(Object obj) super(obj);public String toString()String aString;aString=+value;return aString;第四题:public class Animals public static void main(String args) StringBuffer names;names=new StringBuffer100;int intCharCode;StringBuffer 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 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);elseanimalsi=new Fish(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 Reptilia)Reptilia aReptilia=(Reptilia) animalsi;aReptilia.showMyCreep();elseFish 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.print(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 Animalpublic int flySpeed;public Bird(int id,StringBuffer name,String type,String talent,int flySpeed)this.id=id;=name;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 Animalpublic int jumpHeight;public Insect(int id,StringBuffer name,String type,String talent,int jumpHeight)this.id=id;=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 Animalpublic int creepSpeed;public Reptilia(int id,StringBuffer name,String type,String talent,int creepSpeed)this.id=id;=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 Animalpublic int swimSpeed;public Fish(int id,StringBuffer name,String type,String talent,int swimSpeed)this.id=id;=name;this.type=type;this.talent=talent;this.swimSpeed=swimSpeed;public void showMySwim()System.out.println(And I can swim +swimSpeed+ kilometres per hour.);第一题的运行结果:longintchardoublefloat第二,三题的运行结果:13 14 79 83 95 96 98 100 104 106 108 112 122 127 134 154 158 187 204 211 216 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 VZgrpe VgarLj VsYGBw WTUBeD XiHAxB XjYNGN XkKdCY XpGyEA YOhfJr YQUyrj YqxAaj aBXyeI aGETCE aHpmYC aoKVci cEWkZU ckenPC cugDfh dCqQdJ dGRwfo giWSKg grwrlg gvTuAT hiHsvu iwUACw jPWqOQ jQacMO jySrKK kUUbNd lKtJpi lLCuQK ltZiQf mTotuV mcmItE nVPFXV nfOxcw owlFFl pEBqWf qEsmnH qFQacJ quXbpo rKmnwF rbUdiS sBmgoU sWuzuW sztCTa tBRNzq tDrcGk tFSptN taynIr tknmyn twtwfE uikuaG wILaVh wXLHPq wjNOfp wzsFkj xPeUgg xcoUsz xeQoTz yPIWUh yciARh yhsvOb yxiZNg LinkNode6e1408LinkNodef62373LinkNode1f33675LinkNode7c6768123456789123.456abcde2013-6-3 21:16:28第四题的运行结果:My ID is 1 and my name is bXnpIL. I am just a reptilia.I am talented at creeping.And I can creep 59 kilometres per hour.My ID is 2 and my name is eTMNzf. I am just a bird.I am talented at flying.And I can fly 86 kilometres per hour.My ID is 3 and my name is bLqtWe. I am just a fish.I am talented at swimming.And I can swim 78 kilometres per hour.My ID is 4 and my name is sVaemA. I am just a bird.I am talented at flying.And I can fly 63 kilometres per hour.My ID is 5 and my name is LkWaiP. I am just a reptilia.I am talented at creeping.And I can creep 72 kilometres per hour.My ID is 6 and my name is ziXJzT. I am just a bird.I am talented at flying.And I can fly 94 kilometres per hour.My ID is 7 and my name is CiEwVi. I am just a fish.I am talented at swimming.And I can swim 54 kilometres per hour.My ID is 8 and my name is Gatvyg. I am just a bird.I am talented at flying.And I can fly 65 kilometres per hour.My ID is 9 and my name is VGwgcK. I am just a fish.I am talented at swimming.And I can swim 46 kilometres per hour.My ID is 10 and my name is FUxEIv. I am just a reptilia.I am talented at creeping.And I can creep 30 kilometres per hour.My ID is 11 and my name is UbVsbg. I am just a bird.I am talented at flying.And I can fly 96 kilometres per hour.My ID is 12 and my name is vXblum. I am just a insect.I am talented at jumping.And I can jump 90cm high.My ID is 13 and my name is LagIKr. I am just a reptilia.I am talented at creeping.And I can creep 66 kilometres per hour.My ID is 14 and my name is pHfoiS. I am just a fish.I am talented at swimming.And I can swim 57 kilometres per hour.My ID is 15 and my name is edueqq. I am just a insect.I am talented at jumping.And I can jump 56cm high.My ID is 16 and my name is mbeaUq. I am just a fish.I am talented at swimming.And I can swim 54 kilometres per hour.My ID is 17 and my name is hIeQkD. I am just a bird.I am talented at flying.And I can fly 66 kilometres per hour.My ID is 18 and my name is LAoNkR. I am just a reptilia.I am talented at creeping.And I can creep 65 kilometres per hour.My ID is 19 and my name is UGoCzS. I am just a bird.I am talented at flying.And I can fly 35 kilometres per hour.My ID is 20 and my name is lMQNxC. I am just a fish.I am talented at swimming.And I can swim 70 kilometres per hour.My ID is 21 and my name is tPvlog. I am just a bird.I am talented at flying.And I can fly 95 kilometres per hour.My ID is 22 and my name is lUefIe. I am just a insect.I am talented at jumping.And I can jump 34cm high.My ID is 23 and my name is loxkko. I am just a reptilia.I am talented at creeping.And I can creep 99 kilometres per hour.My ID is 24 and my name is uutSjT. I am just a reptilia.I am talented at creeping.And I can creep 59 kilometres per hour.My ID is 25 and my name is fSplYx. I am just a reptilia.I am talented at creeping.And I can creep 84 kilometres per hour.My ID is 26 and my name is QsRAji. I am just a bird.I am talented at flying.And I can fly 46 kilometres per hour.My ID is 27 and my name is tylZwu. I am just a bird.I am talented at flying.And I can fly 81 kilometres per hour.My ID is 28 and my name is mCZDId. I am just a reptilia.I am talented at creeping.And I can creep 33 kilometres per hour.My ID is 29 and my name is LWAvgq. I am just a bird.I am talented at flying.And I can fly 82 kilometres per hour.My ID is 30 and my name is rHFZAW. I am just a fish.I am talented at swimming.And I can swim 58 kilometres per hour.My ID is 31 and my name is ucDEDC. I am just a reptilia.I am talented at creeping.And I can creep 93 kilometres per hour.My ID is 32 and my name is TaBgkg. I am just a fish.I am talented at swimming.And I can swim 66 kilometres per hour.My ID is 33 and my name is YQHPGg. I am just a insect.I am talented at jumping.And I can jump 71cm high.My ID is 34 and my name is RwXQkn. I am just a reptilia.I am talented at creeping.And I can creep 36 kilometres per hour.My ID is 35 and my name is QhUIPE. I am just a reptilia.I am talented at creeping.And I can creep 47 kilometres per hour.My ID is 36 and my name is fkXwmj. I am just a bird.I am talented at flying.And I can fly 52 kilometres per hour.My ID is 37 and my name is yfwtZx. I am just a fish.I am talented at swimming.And I can swim 71 kilometres per hour.My ID is 38 and my name is bAfdGA. I am just a reptilia.I am talented at creeping.And I can creep 31 kilometres per hour.My ID is 39 and my name is JwphLS. I am just a bird.I am talented at flying.And I can fly 73 kilometres per hour.My ID is 40 and my name is SVOeym. I am just a reptilia.I am talented at creeping.And I can creep 54 kilometres per hour.My ID is 41 and my name is ELsxQh. I am just a insect.I am talented at jumping.And I can jump 44cm high.My ID is 42 and my name is XJVcsZ. I am just a bird.I am talented at flying.And I can fly 83 kilometres per hour.My ID is 43 and my name is RLFUzL. I am just a reptilia.I am talented at creeping.And I can creep 32 kilometres per hour.My ID is 44 and my name is vzwugQ. I am just a bird.I am talented at flying.And I can fly 60 kilometres per hour.My ID is 45 and my name is CVpkKI. I am just a reptilia.I am talented at creeping.And I can creep 62 kilometres per hour.My ID is 46 and my name is dIqWhi. I am just a bird.I am talented at flying.And I can fly 40 kilometres per hour.My ID is 47 and my name is jCqYSF. I am just a bird.I am talented at flying.And I can fly 74 kilometres per hour.My ID is 48 and my name is LnqWBI. I am just a bird.I am talented at flying.And I can fly 39 kilometres per hour.My ID is 49 and my name is SUQGhU. I am just a insect.I am talented at jumping.And I can jump 64cm high.My ID is 50 and my name is ZbUtFN. I am just a reptilia.I am talented at creeping.And I can creep 51 kilometres per hour.My ID is 51 and my name is dOGhpk. I am just a reptilia.I am talented at creeping.And I can creep 81 kilometres per hour.My ID is 52 and my name is EAdFAt. I am just a insect.I am talented at jumping.And I can jump 69cm high.My ID is 53 and my name is CtTzkq. I am just a insect.I am talented at jumping.And I can jump 55cm high.My ID is 54 and my name is XdOeXi. I am just a bird.I am talented at flying.And I can fly 54 kilometres per hour.My ID is 55 and my name is jUENwr. I am just a bird.I am talented at flying.And I can fly 83 kilometres per hour.My ID is 56 and my name is MYMFcm. I am just a bird.I am talented at flying.And I can fly 58 kilometres per hour.My ID is 57 and my name is dEVcGX. I am just a fish.I am talented at swimming.And I can swim 44 kilometres per hour.My ID is 58 and my name is OjARZU. I am just a reptilia.I am talented at creeping.And I can creep 86 kilometres per hour.My ID is 59 and my name is XMCgzt. I am just a reptilia.I am talented at creeping.And I can creep 76 kilometres per hour.My ID is 60 and my name is HySAjB. I am just a fish.I am talented at swimming.And I can swim 63 kilometres per hour.My ID is 61 and my name is rmbrvn. I am just a reptilia.I am talented at creeping.And I can creep 43 kilometres per hour.My ID is 62 and my name is DwUTXU. I am just a fish.I am talented at swimming.And I can swim 58 kilometres per hour.My ID is 63 and my name is XLVFGT. I am just a insect.I am

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论