第08周实验枚举泛型常用类讲解

上传人:桔**** 文档编号:512817560 上传时间:2023-03-05 格式:DOCX 页数:16 大小:195.70KB
返回 下载 相关 举报
第08周实验枚举泛型常用类讲解_第1页
第1页 / 共16页
第08周实验枚举泛型常用类讲解_第2页
第2页 / 共16页
第08周实验枚举泛型常用类讲解_第3页
第3页 / 共16页
第08周实验枚举泛型常用类讲解_第4页
第4页 / 共16页
第08周实验枚举泛型常用类讲解_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《第08周实验枚举泛型常用类讲解》由会员分享,可在线阅读,更多相关《第08周实验枚举泛型常用类讲解(16页珍藏版)》请在金锄头文库上搜索。

1、Java程序设计实验西南交大 信息学院 陈帆(2014)实验内容第08周实验枚举泛型常用类成绩姓名学号班级专业日期2014年月日【实验目的】-字符串处理掌握枚举的使用掌握泛型的使用掌握常用类:日期,日历,【实验内容】1、编辑、编译、运行卜面java程序publicclassTestEnum/*最普通的枚举*/publicenumColorSelectred,green,yellow,blue;/*枚举也可以象一般的类一样添加方法和属性,你可以为它添加静态和非静态的属性或方法,这一切都象你在一的类中做的那样.*/publicenumSeason/枚举列表必须写在最前面,否则编译出错winter,

2、spring,summer,fall;privatefinalstaticStringlocation=Phoenix;publicstaticSeasongetBest()if(location.equals(Phoenix)returnwinter;elsereturnsummer;/*还可以有构造方法*/publicenumTemp/*通过括号赋值,而且必须有带参构造器和一属性跟方法,否则编译出错*赋值必须是都赋值或都不赋值,不能一部分赋值一部分不赋值*如果不赋值则不能写构造器,赋值编译也出错*/absoluteZero(-459),freezing(32),boiling(212),p

3、aperBurns(451);privatefinalintvalue;publicintgetValue()returnvalue;第1页共16页/构造器默认也只能是private,从而保证构造函数只能在内部使用Temp(intvalue)this.value=value;publicstaticvoidmain(String口args)/*枚举类型是一种类型,用于定义变量,以限制变量的赋值赋值时通过枚举名.值”来取得相关枚举中的值*/ColorSelectm=ColorSelect.blue;switch(m)/*注意:枚举重写了ToString(),说以枚举变量的值是不带前缀的*所以为b

4、lue而卜ColorSelect.blue*/casered:System.out.println(colorisred);break;casegreen:System.out.println(colorisgreen);break;caseyellow:System.out.println(colorisyellow);break;caseblue:System.out.println(colorisblue);break;System.out.println(遍历ColorSelect中白值);/*通过values(威得枚举值的数组*/for(ColorSelectc:ColorSelec

5、t.values()System.out.println(c);System.out.println(枚举ColorSelect中的值有:+ColorSelect.values().length+个);/*ordinal()返回枚举值在枚举中的索引位置,从0开始*/System.out.println(ColorSelect.red.ordinal();/0System.out.println(ColorSelect.green.ordinal();/1System.out.println(ColorSelect.yellow.ordinal();/2System.out.println(Co

6、lorSelect.blue.ordinal();/3/*枚举默认实现了java.lang.Comparabl戢口*/System.out.println(ColorSpareTo(ColorSelect.green);System.out.println(Season.getBest();for(Tempt:Temp.values()/*通过getValue()取得相关枚举的值*/System.out.println(t+”的值是+t.getValue();要求:(1)分析该程序,写出运行结果【实验结果与分析】2、编辑、编译、运行下面java程序classGenprivateTob;定义泛型

7、成员变量publicGen(Tob)this.ob=ob;)publicTgetOb()returnob;)publicvoidsetOb(Tob)this.ob=ob;)publicvoidshowTyep()System.out.println(T的实际类型是:+ob.getClass().getName();)publicclassGenDemopublicstaticvoidmain(String口args)定义泛型类Gen的一个Integer版本GenintOb=newGen(88);intOb.showTyep();inti=intOb.getOb();System.out.pri

8、ntln(value=+i);System.out.println();定义泛型类Gen的一个String版本GenstrOb=newGen(HelloGen!);strOb.showTyep();Strings=strOb.getOb();System.out.println(value=+s);要求:(1)分析该程序,写出运行结果【实验结果与分析】3、编辑并运行下面程序,理解Date、SimpleDateFormat类的使用用Data类不带参数的构造方法创建日期,要求日期的输出格式是:星期小时分秒importjava.util.*;importjava.text.*;classTestDa

9、teDemopublicstaticvoidmain(Stringargs口)Date时间newDate();SimpleDateFormats=newSimpleDateFormat(EHH时mm分ss秒);System.out.println(s.format(时间);)要求:运行程序,给出正确的程序运行结果,理解正规式的使用。【实验结果与分析】4、编辑并运行下面程序,理解Calendar类的使用importjava.util.*;importjavax.swing.JOptionPane;publicclassTestDateDemopublicstaticvoidmain(String

10、agrs)Stringstr=JOptionPane.showInputDialog(输入第一个年份:);intfirstYear=Integer.parseInt(str);str=JOptionPane.showInputDialog(输入月份:);intfirstMonth=Integer.parselnt(str);str=JOptionPane.showInputDialog(日期:”);intfirstDay=Integer.parselnt(str);str=JOptionPane.showInputDialog(输入第二个年份:);intsecondYear=Integer.p

11、arselnt(str);str=JOptionPane.showInputDialog(输入月份:);intsecondMonth=Integer.parselnt(str);str=JOptionPane.showInputDialog。日期:);intsecondDay=Integer.parseInt(str);Calendarcalendar=Calendar.getInstance();calendar.set(firstYear,firstMonth,firstDay);longtimeOne=calendar.getTimeInMillis();System.out.print

12、ln(timeOne);calendar.set(secondYear,secondMonth,secondDay);longtimeTwo=calendar.getTimeInMillis();Datedate1=newDate(timeOne);Datedate2=newDate(timeTwo);if(date2.equals(date1)System.out.println(两个日期相同);elseif(date2.after(date1)System.out.println(第二个日期大);elseif(date2.before(date1)System.out.println(第一

13、个日期大);longdays=(timeOne-timeTwo)/(1000*60*60*24);System.out.println(firstYear+年+firstMonth+月+firstDay+日);System.out.println(secondYear+年+secondMonth+月+secondDay+日);System.out.println(相隔天数+days);要求:运行程序,给出正确的程序运行结果,分析程序的功能。【实验结果与分析】5、编辑并运行下面程序,理解BigInteger类的使用importjava.math.BigInteger;importjava.uti

14、l.*;classTestNumDemopublicstaticvoidmain(String口args)longa=2222321,b=2462355,c=16;BigIntegerx,y,z,ans;x=BigInteger.valueOf(a);y=BigInteger.valueOf(b);z=BigInteger.valueOf(c);System.out.println(x=+x+ty=+y+tz=+z);ans=x.add(y);加运算System.out.println(x+y=+ans);ans=x.subtract(y);减运算System.out.println(x-y=+ans);ans=x.multiply(y);乘运算System.out.println(x*y=+ans);ans=z.divide(y);除运算System.out.println(z/y=+ans);ans=x.mod(z);/模运算System.out.println(zmody=+ans);if(pareTo(x)=0

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 市场营销

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号