1、设计一个数据类型判断类Polymorphism,使用重载、装箱等技术判断一个不带等号的Java表达式的结果的数据类型。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 public 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" ; private final static String String_TYPE = "String" ; 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 (String Str) { return String_TYPE; } public static String getType (Object obj) { return obj != null ? obj.toString().split("@" )[0 ] : null ; } public static void main (String[] args) { System.out.println("100是" +getType(100 )+"类型" ); System.out.println("1ds是" +getType("1ds" )+"类型" ); System.out.println("8==9是" +getType(8 ==9 )+"类型" ); System.out.println("100.15是" +getType(100.15 )+"类型" ); System.out.println("a是" +getType('a' )+"类型" ); System.out.println("100+222.5是" +getType(100 +222.5 )+"类型" ); System.out.println("100+'a'是" +getType(100 +'a' )+"类型" ); System.out.println("5.9/54是" +getType(5.9 /54 )+"类型" ); System.out.println("599999999L是" +getType(599999999L )+"类型" ); } }
2、设计一个链表结点类LinkNode 此类可以存放int、long、float、double、byte、short、String、StringBuffer类型的数据。用此类:a、随机产生100个整数(范围自定)的链表,在生成的过程中从小到大排列,然后输出;b、随机产生100个6个英文字母的单词的链表,在生成的过程中从小到大排列,然后输出。(关注装箱和拆箱相关概念 Integer String)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 public class LinkNode { public Object value; public LinkNode nextNode; public LinkNode (Object value,LinkNode nextNode) {this .value=value; this .nextNode=nextNode;} public LinkNode () {} public LinkNode (Object value) {this .value=value;} } package text5; public class RandomInt { public static void main (String[] args) { StringBuffer StrB=new StringBuffer (); LinkNode firstNode=new LinkNode (0 ); firstNode.nextNode=new LinkNode (0 ); for (int i=0 ;i<105 ;i++) { LinkNode aNew=new LinkNode (); LinkNode compare=firstNode.nextNode; LinkNode before=firstNode; compare=firstNode.nextNode; int aNum=(int )(Math.random()*1001 ); aNew=new LinkNode (aNum); if (i==0 ) { compare=aNew; continue ; } while (aNum>Integer.parseInt(compare.value.toString())) {if (compare.nextNode!=null ) {compare=compare.nextNode; before=before.nextNode; } else {before=before.nextNode; break ; } } if (before!=compare) {before.nextNode=aNew; aNew.nextNode=compare;} else { compare.nextNode=aNew; } } for (int i=1 ;i<103 ;i++) { if (i>2 ) {System.out.print(i-2 +": " +firstNode.value+" " );} firstNode=firstNode.nextNode; if ((i-2 )%10 ==0 ) System.out.println(); } } }package text5; public class RandomStr { public static void main (String[] args) { StringBuffer StrB=new StringBuffer (); LinkNode firstNode=new LinkNode ("first" ); firstNode.nextNode=new LinkNode (" " ); for (int i=0 ;i<105 ;i++) { LinkNode aNew=new LinkNode (); LinkNode compare=firstNode.nextNode; LinkNode before=firstNode; compare=firstNode.nextNode; StrB=new StringBuffer (); for (int j=0 ;j<6 ;j++) {int aNum=(int )(Math.random()*26 ); char aChar=(char )('a' +aNum); StrB.append(aChar); } aNew=new LinkNode (StrB); if (i==0 ) { compare=aNew; continue ; } while (StrB.toString().compareTo(compare.value.toString())>0 ) {if (compare.nextNode!=null ) {compare=compare.nextNode; before=before.nextNode; } else {before=before.nextNode; break ; } } if (before!=compare) {before.nextNode=aNew; aNew.nextNode=compare;} else { compare.nextNode=aNew; } } for (int i=1 ;i<103 ;i++) { if (i>2 ) {System.out.print(i-2 +": " +firstNode.value+" " );} firstNode=firstNode.nextNode; if ((i-2 )%10 ==0 ) System.out.println(); } } }
3、A、在main()中使用上题的LinkNode类创建4个实例 并赋予不同的值(long、double、 StringBuffer、MyDate),然后使用Object中默认的toString()方法(从超级父类继承而来) 显示结果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class LinkNodes extends LinkNode { public LinkNodes (Object value,LinkNode nextNode) {this .value=value; this .nextNode=nextNode;} public LinkNodes () {} public LinkNodes (Object value) {this .value=value;} public String toString (long longNum) { String str=longNum+"" ; return str; } public String toString (double doubleNum) {String str=doubleNum+"" ; return str; } public String toString (StringBuffer StrBuffer) {String str=StrBuffer+"" ; return str; } public String toString (MyDate myDateNum) {String str=myDateNum.year+" " +myDateNum.month+" " +myDateNum.day+" " +myDateNum.hour+" " +myDateNum.minute+" " +myDateNum.second; return str; } }
B、继承LinkNode类创建新类LinkNodeS 在其中重写Object中默认的toString()方法(将结点的value转换成对应的字符串),main()中用LinkNodeS类同样创建4个实例,并赋予和上面同样的值(long、double、StringBuffer、MyDate),观察使用新的toString()方法的效果,体会继承与多态。(MyDate的日期toString用标准格式)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class UseOfLinkNodes { public static void main (String[] args) { StringBuffer strBuffer=new StringBuffer ("aaa" ); MyDate myDate=new MyDate (); LinkNode longNum=new LinkNode (594785L ); LinkNode doubleNum=new LinkNode (599.5 ); LinkNode StringBuffer=new LinkNode (strBuffer); LinkNode myDateNum=new LinkNode (myDate); System.out.println(longNum.value.toString()); System.out.println(doubleNum.value.toString()); System.out.println(StringBuffer.value.toString()); System.out.println(myDateNum.value.toString()); System.out.println(); LinkNodes longNum2=new LinkNodes (594785L ); LinkNodes doubleNum2=new LinkNodes (599.5 ); LinkNodes StringBuffer2=new LinkNodes (strBuffer); LinkNodes myDateNum2=new LinkNodes (myDate); String myDateNum2Str=myDateNum2.toString((MyDate)myDateNum2.value); System.out.println(longNum2.toString((long )longNum2.value)); System.out.println(doubleNum2.toString((double )doubleNum2.value)); System.out.println(StringBuffer2.toString((StringBuffer)StringBuffer2.value)); System.out.println(myDateNum2Str); } }
4、:用Animal作为基类,鸟类、昆虫类、爬行类和鱼类作为Animal的子类设计类 有来自4个类别(鸟类、昆虫类、爬行类和鱼类)的100个动物聚在一起开会,商议和另一个动物部落打仗事宜,会议要求每个动物都要报告自己所属的动物类别和自己的天赋,以便选拔人才、组织兵力出战。 设计:用Animal作为基类,鸟类、昆虫类、爬行类和鱼类各作为Animal的子类设计类层次结构,设计时运用继承、重写并设计多态机制,同时对每个子类至少要添加一个其描述的动物特有的行为和一个特有的属性,以更准确地描述子类对象。 使用:用循环随机生成这100个动物装入动物数组,要对每个动物进行编号和随机命名,用循环让每个参会的动物报告自己的类别和天赋。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 public class Animal { public int id; public StringBuffer name; public String type; public void showType () { System.out.println("My ID is “+id+” and I am just an animal, my type is going to be determined later." ); } public void showTalent () { System.out.println("I don’t know what I am talented at right now." ); } public void showName () { System.out.println("My name is " +this .name); } } class Fish extends Animal { public Fish (int id,StringBuffer name) {this .id=id; this .name=name; this .type="fish" ; } public void showType () { System.out.println("My ID is " +id+" and I am just an animal, my type is fish" ); } public void showTalent () { System.out.println("I am talented at swiming" ); } } class Bird extends Animal { public Bird (int id,StringBuffer name) {this .id=id; this .name=name; this .type="bird" ; } public void showType () { System.out.println("My ID is " +id+" and I am just an animal, my type is bird" ); } public void showTalent () { System.out.println("I am talented at flying" ); } } class Insect extends Animal { public Insect (int id,StringBuffer name) {this .id=id; this .name=name; this .type="insect" ; } public void showType () { System.out.println("My ID is " +id+" and I am just an animal, my type is insect" ); } public void showTalent () { System.out.println("I am talented at working" ); } } class Reptile extends Animal { public Reptile (int id,StringBuffer name) {this .id=id; this .name=name; this .type="reptile" ; } public void showType () { System.out.println("My ID is " +id+" and I am just an animal, my type is reptile" ); } public void showTalent () { System.out.println("I am talented at crawling" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class AnimalRandom { public static void main (String[] args) { Animal[] animalArmy = new Animal [100 ]; for (int i = 0 ; i<100 ; i++) { int type = (int )( Math.random()*4 ); StringBuffer name = new StringBuffer (); for (int j = 0 ; j<4 ; j++) { int word = (int )( Math.random()*26 ); name.append( (char )(word + 'a' ) ); } switch (type) { case 0 : animalArmy[i] = new Bird (i, name); break ; case 1 : animalArmy[i] = new Fish (i, name); break ; case 2 : animalArmy[i] = new Insect (i, name); break ; case 3 : animalArmy[i] = new Reptile (i,name); } animalArmy[i].showName(); animalArmy[i].showType(); animalArmy[i].showTalent(); System.out.println(); } } }