




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java程序设计实验 西南交大 信息学院 陈帆(2014)实验内容第08周实验 枚举 泛型 常用类成 绩姓 名学号班 级专 业日 期2014年 月 日【实验目的】-字符串处理u 掌握枚举的使用u 掌握泛型的使用u 掌握常用类:日期,日历,【实验内容】1、编辑、编译、运行下面java程序public class TestEnum /*最普通的枚举*/ public enum ColorSelect red, green, yellow, blue; /* 枚举也可以象一般的类一样添加方法和属性,你可以为它添加静态和非静态的属性或方法,这一切都象你在一般的类中做的那样. */ public enu
2、m Season / 枚举列表必须写在最前面,否则编译出错 winter, spring, summer, fall; private final static String location = Phoenix; public static Season getBest() if (location.equals(Phoenix) return winter; else return summer; /*还可以有构造方法*/ public enum Temp /*通过括号赋值,而且必须有带参构造器和一属性跟方法,否则编译出错 * 赋值必须是都赋值或都不赋值,不能一部分赋值一部分不赋值 * 如
3、果不赋值则不能写构造器,赋值编译也出错*/ absoluteZero(-459), freezing(32),boiling(212), paperBurns(451); private final int value; public int getValue() return value; /构造器默认也只能是private, 从而保证构造函数只能在内部使用 Temp(int value) this.value = value; public static void main(String args) /* * 枚举类型是一种类型,用于定义变量,以限制变量的赋值 赋值时通过枚举名.值来取得相
4、关枚举中的值 */ ColorSelect m = ColorSelect.blue; switch (m) /*注意:枚举重写了ToString(),说以枚举变量的值是不带前缀的 *所以为blue而非ColorSelect.blue */case red: System.out.println(color is red); break; case green: System.out.println(color is green); break; case yellow: System.out.println(color is yellow); break; case blue: System
5、.out.println(color is blue); break; System.out.println(遍历ColorSelect中的值); /*通过values()获得枚举值的数组*/ for (ColorSelect c : ColorSelect.values() System.out.println(c);System.out.println(枚举ColorSelect中的值有:+ColorSelect.values().length+个);/*ordinal()返回枚举值在枚举中的索引位置,从0开始*/System.out.println(ColorSelect.red.ord
6、inal(); /0System.out.println(ColorSelect.green.ordinal(); /1System.out.println(ColorSelect.yellow.ordinal(); /2System.out.println(ColorSelect.blue.ordinal(); /3/*枚举默认实现了java.lang.Comparable接口*/ System.out.println(ColorSpareTo(ColorSelect.green); System.out.println(Season.getBest(); for(Temp t:Temp.v
7、alues() /*通过getValue()取得相关枚举的值*/ System.out.println(t+的值是+t.getValue(); 要求:(1)分析该程序,写出运行结果【实验结果与分析】2、编辑、编译、运行下面java程序class Gen private T ob; /定义泛型成员变量 public Gen(T ob) this.ob = ob; public T getOb() return ob; public void setOb(T ob) this.ob = ob; public void showTyep() System.out.println(T的实际类型是: +
8、 ob.getClass().getName(); public class GenDemo public static void main(String args) /定义泛型类Gen的一个Integer版本 Gen intOb=new Gen(88); intOb.showTyep(); int i= intOb.getOb(); System.out.println(value= + i); System.out.println(-); /定义泛型类Gen的一个String版本 Gen strOb=new Gen(Hello Gen!); strOb.showTyep(); String
9、 s=strOb.getOb(); System.out.println(value= + s); 要求:(1)分析该程序,写出运行结果【实验结果与分析】_3、编辑并运行下面程序,理解Date、SimpleDateFormat类的使用 用Data类不带参数的构造方法创建日期,要求日期的输出格式是:星期 小时 分 秒import java.util.*; import java.text.*; class TestDateDemo public static void main(String args) Date 时间=new Date(); SimpleDateFormat s=new Sim
10、pleDateFormat(E HH时 mm分 ss秒); System.out.println(s.format(时间); 要求:运行程序,给出正确的程序运行结果,理解正规式的使用。【实验结果与分析】_4、编辑并运行下面程序,理解Calendar类的使用import java.util.*;import javax.swing.JOptionPane;public class TestDateDemo public static void main(String agrs) String str = JOptionPane.showInputDialog(输入第一个年份:); int fir
11、stYear = Integer.parseInt(str); str = JOptionPane.showInputDialog(输入月份:); int firstMonth = Integer.parseInt(str); str = JOptionPane.showInputDialog(日期:); int firstDay = Integer.parseInt(str); str = JOptionPane.showInputDialog(输入第二个年份:); int secondYear = Integer.parseInt(str); str = JOptionPane.showI
12、nputDialog(输入月份:); int secondMonth = Integer.parseInt(str); str = JOptionPane.showInputDialog(日期:); int secondDay = Integer.parseInt(str); Calendar calendar = Calendar.getInstance(); calendar.set(firstYear,firstMonth,firstDay); long timeOne = calendar.getTimeInMillis(); System.out.println(timeOne);
13、calendar.set(secondYear,secondMonth,secondDay); long timeTwo = calendar.getTimeInMillis(); Date date1=new Date(timeOne); Date date2=new Date(timeTwo); if(date2.equals(date1) System.out.println(两个日期相同); else if(date2.after(date1) System.out.println(第二个日期大); else if(date2.before(date1) System.out.prin
14、tln(第一个日期大); long days=(timeOne-timeTwo)/(1000*60*60*24); System.out.println(firstYear+年+firstMonth+月+firstDay+日); System.out.println(secondYear+年+secondMonth+月+secondDay+日); System.out.println(相隔天数 +days); 要求:运行程序,给出正确的程序运行结果,分析程序的功能。【实验结果与分析】5、编辑并运行下面程序,理解BigInteger类的使用 import java.math.BigInteger
15、; import java.util.*;class TestNumDemo public static void main(String args) long a = 2222321, b = 2462355, c = 16; BigInteger x, 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
16、.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(z mod y = + ans ); if(pareTo(x) = 0) System.out.pr
17、intln(1); new TestNumDemo().dis(); static void dis( ) BigInteger ans; BigInteger x = new BigInteger(1212434623673632); /BigInteger(int numBits, Random rnd) /构造一个随机产生的大整数,范围在0到2numBits 1之间 Random rr = new Random(); int numBits=100; BigInteger y = new BigInteger(numBits,rr); rr= new Random(); BigInteg
18、er z= new BigInteger(100,10,rr); /BigInteger(int bitLength, int certainty, Random rnd) /构造一个随机产生的,正的,指定长度的,可能是素数的大整数,参数certainty指明要进行多少次素数测试 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 = +
19、 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(z mod y = + ans ); if(pareTo(x) = 0) System.out.println(1); 要求:(1)运行程序,给出正确的程序运行结果,分析程序的功能。 (2)参考上面程序,编写程序验证大实数BigDecimal类,常常用在商业运
20、算中,用来获取高精度数据【实验结果与分析】6、 根据下面的要求,编辑编译程序,并对所编写的出进行测试假定银行的一个存取款系统有两类客户,一类是现金用户,一类是信用卡用户。银行对每个客户都要登记其姓名name,并为之分配一个唯一的账户号码id,现金用户还要记录其卡的类型(工资卡、借记卡、理财卡),而信用卡用户则根据其信用级别有一定的透支限额lineOfCredit(A级10000元、B级5000元、C级2000元、D级1000元)。每种客户都可以实现存deposit、取withdraw、和查询余额getBalance,信用卡用户还可以查询透支情况findOverdraw。对于现金用户,每次取款操
21、作只能在账户实际额度balance内操作,允许现金用户改变自己的帐户类型。(1)分析有哪些属性和方法可以作为两个子类的共同属性和方法,写出抽象类Account定义。abstract class Account private String name;public String id;private double balance;public void setBalance(double balance) this.balance = balance;public double getBalance() return balance;public Account(String name, Str
22、ing id, double balance) super(); = name;this.id = id;this.balance = balance;public Account(String name, String id) super(); = name;this.id = id;public void deposit(double amount)this.balance+=amount;abstract void withdraw(double amount); (2)分析CashAccount有那些新增的属性和方法,定义一个继承于Account的子
23、类CashAccount。public class CashAccount extends Account public String cashsort;public String getCashsort() return cashsort;public void setCashsort(String cashsort) this.cashsort = cashsort;public CashAccount(String name, String id, double balance, String cashsort) super(name, id, balance);this.cashsor
24、t = cashsort;public void withdraw(double amount)if(this.getBalance()=amount) this.setBalance(this.getBalance()-amount);elseSystem.out.println(错误);(3)分析CreditAccount有那些新增的属性和方法,然后定义一个继承于Account的子类CreditAccount,添加增加的属性和方法。public class CreditAccount extends Account double staticoverdraw;double overdraw
25、;public CreditAccount(String name, String id, double balance, double staticoverdraw, double overdraw) super(name, id, balance);this.staticoverdraw = staticoverdraw;this.overdraw = overdraw;public void findOverdraw()if(this.getBalance()=amount) this.setBalance(this.getBalance()-amount);elseSystem.out
26、.println(错误);(4) 请按照要求编写一个程序Test,用你所定义的类完成下列业务操作。A、用Account作为类型定义两个变量credit和debit,分别引用CreditAccount和CashAccount的对象,并完成存款500元的操作。B、每个对象完成取款200元的操作后再次取款400元,请输出各自的余额。C、可以通过credit查看引用对象的透支额吗,如果不能,怎样修改可以查看?public class Test public static void main(String args)Account credit=new CreditAccount(zqq,2009407
27、0149,0,500,0); Account debit=new CashAccount(zsq,20094070101,0,借记卡); credit.deposit(500); debit.deposit(500); credit.withdraw(200); debit.withdraw(200); credit.withdraw(400); debit.withdraw(400); System.out.println(credit balance +credit.getBalance(); System.out.println(cash balance +debit.getBalanc
28、e();要求:编辑上面的程序,给出运行结果,并对程序进行分析,理解掌握类、抽象类和继承的概念与应用。【实验结果与分析】_7、 定义一个Teacher类,该类提供教师的工号、姓名、职称信息,再定义一个课程类Course,描述了课程的编号、课程名称、理论课时和实验课时、学分以及每门课程都要指定一个教师,通过课程能够获得任课教师对象。要求:按照要求编写程序,给出程序运行结果。_【实验结果与分析】class Teacher private String TeacherNum; private String TeacherName; private String zicheng; public Teac
29、her(String TeacherNum,String TeacherName,String zicheng) this.TeacherNum=TeacherNum; this.TeacherName=TeacherName; this.zicheng=zicheng;public String getTeacherNum()return TeacherNum;public String getTeacherName()return TeacherName;public String getzicheng()return zicheng; public void returnTeacherI
30、nformations()System.out.println(TeacherNum+ +TeacherName+ +zicheng);class course private String courseID; private String courseName; private String theoryClass; private String experimentalClass; private int credit; private Teacher teacher; public course(String courseID,String courseName,String theoryClass,String experimentalClass, int credit,Teacher teacher) this.courseID=courseID; this.courseName=courseName; this.theoryCla
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026高考广东省汕头市2025届高三三模考试语文试题及参考答案
- 摄影棚拍摄现场场记人员聘用协议
- 高效通勤厂班车接送合作协议
- 生态农业园区土地租赁分成及生态补偿合同
- 城市地下管网拆除重建合同模板
- 车间内部承包经营与设备维护一体化合同范本
- 书香校园活动方案班级方案
- 公路改建工程拌和站方案
- 内部用水计量管理制度
- 公司工作软件管理制度
- 国家电力投资集团有限公司介绍
- 2025年广东省广州市花都区交通局建管中心招聘14人历年高频重点提升(共500题)附带答案详解
- 垃圾焚烧炉安装及方案
- 幼儿教师讲故事技巧培训
- 【MOOC】保险学概论-中央财经大学 中国大学慕课MOOC答案
- 【MOOC】学术交流英语-东南大学 中国大学慕课MOOC答案
- 压力容器安全承诺书
- 汽车厂房布置与规划
- 河北农业大学现代科技学院《试验设计与数据处理实验》2022-2023学年第一学期期末试卷
- 材料力学-山东科技大学中国大学mooc课后章节答案期末考试题库2023年
- 《机器人驱动与运动控制》全套教学课件
评论
0/150
提交评论