




已阅读5页,还剩24页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java语言程序设计实验报告实验序号:04实验项目名称:构造方法学号xx姓名xx专业、班xx实验地点xx实验室指导教师xx时间2012/4/15一、实验目的及要求目的:1. 掌握java中类及其方法的设计,特别注意构造方法的设计。2. 掌握类及其方法的使用过程,并体会类的设计与类的使用两个阶段的关系。3. 掌握与类相关的各种语法机制。4. 进一步习惯java的命名风格。本次实验强制要求使用java命名风格。二、实验设备(环境)1.windows操作系统2.MyEclipse软件三、实验内容与步骤编程序:1、 设计一个时间类MyDate:(日期均大于1900年)A、 其中包括年、月、日、时、分、秒。其他成员变量可根据需要增设;B、 可以获得时间的年、月、日、小时、分钟、秒数;C、 计算日期的dayOfWeek(星期几); D、 /实现日期加减一个整数后得到新的日期(加减天数);E、 输入任意一个月份,将此月的日历输出(按星期格式);F、 输入任意年份,将此年的年历输出;G、 实现两个时间相减得到的小时数、分钟数和秒数;H、 输出时间,要求三种格式:yyyy年MM月dd日hh小时mm分ss秒;yyyy-MM-dd hh:mm:ssyyyyMMddhhmmssI、 设计三个个构造方法。1、参数只有年月日的构造方法,小时分钟秒设为0;2、完全参数的构造方法;3、如果使用无参数的构造方法则初始化为系统时间,系统时间的获得方法:SimpleDateFormat myDateFormat =new SimpleDateFormat(yyyyMMddHHmmss);java.util.Date date0=new java.util.Date();strDate=myDateFormat.format(date0);strDate中就是有yyyyMMddHHmmss时间格式的字符串,从中可以取到各段时间数据(程序源代码的最开始要加import java.text.SimpleDateFormat;)J、 MyDate的使用:用无参数构造方法创建实例,然后按三种方法输出时间;用年月日参数构造方法创建实例,时间为2008年8月8日0点0分0秒;并用三种方法输出时间用全参数构造方法创建实例,时间为2008年8月8日20点0分0秒,并用三种方法输出将两个时间相减,计算差值共多少小时数、或者分钟数、或者秒数,输出输入2011,4,输出月历;输入2011,输出年历;K、 注意理解构造方法重载。L、 不要直接使用Java提供的Date类,那样就达不到练习的效果了。2、 设计一个字符串链表节点类:a) 其中包括两个成员变量:Value(String类型)和下一个节点nextNode,方法有:setValue(),getValue()和display();b) 用此类生成一个循环链表来解决斗地主发牌的问题c) 注意观察:类引用的使用、浅复制和类中对类自身的引用。3、 设计一个用于管理银行客户的类BankCustomer:仅描述客户的几个重要方面:帐号、身份证号、姓名、联系方式、密码、账户余额。所有的成员变量均用private访问控制,因此每一个成员变量就要有相应的存取器(getter和setter,即获取和设置其值的相应的成员方法。需要setter还是getter,还是两者都要,视情况而定)成员方法:开户(开户时必须要有身份证号),系统自动生成帐号,帐号使用系统时间(格式:yyyyMMddHHmmss14位),初始密码为“666666”。注意开户和构造方法之间的关系。存钱、取钱、显示账户信息、修改密码(密码最短要六位)怎样在main中使用这个类,自行安排,要表现出你设计的类的各个方面,并在main中用英语加以注释四、实验结果与数据处理import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.text.SimpleDateFormat;public class D /* * param args */static int intInputValue;public static void main(String args) int year=0,month=0,day=0,hour=0,minute=0,second=0;/-计算日期的星期-System.out.println(计算日期的星期,请输入正确的日期:);if(getIntegerInput(年)year=intInputValue;if(getIntegerInput(月)month=intInputValue;if(getIntegerInput(日)day=intInputValue;myDate A=new myDate(year,month,day);int theTotalDays=A.numOfDays();outputTheWeek(theTotalDays);/-显示月历-System.out.println(输入一个年月,将显示该月的日历:);if(getIntegerInput(年)year=intInputValue;if(getIntegerInput(月)month=intInputValue;calendar(year,month);/-显示年历-if(getIntegerInput(输入一个年份,将显示全年的日历)year=intInputValue;for(int i=1;i=12;i+)calendar(year,i);/-计算两个时间的差-System.out.println(输入两个时间,计算其相差多长时间);System.out.println(请输入第一个时间);if(getIntegerInput(时)hour=intInputValue;if(getIntegerInput(分)minute=intInputValue;if(getIntegerInput(秒)second=intInputValue;myDate B=new myDate(2012,5,1,hour,minute,second);int aHour=0,aMinute=0,aSecond=0;System.out.println(请输入第二个时间);if(getIntegerInput(时)aHour=intInputValue;if(getIntegerInput(分)aMinute=intInputValue;if(getIntegerInput(秒)aSecond=intInputValue;B.difference(aHour, aMinute, aSecond);/-以三种方式输出时间-System.out.println(请输入一个正确的日期和时间:);if(getIntegerInput(年)year=intInputValue;if(getIntegerInput(月)month=intInputValue;if(getIntegerInput(日)day=intInputValue;if(getIntegerInput(时)hour=intInputValue;if(getIntegerInput(分)minute=intInputValue;if(getIntegerInput(秒)second=intInputValue;myDate C=new myDate(year,month,day,hour,minute,second);C.timeOutput();static void calendar(int year,int month)myDate aDate=new myDate(year,month,1);int theTotalDays=aDate.numOfDays();System.out.println( +year+年+month+月);System.out.println(-);System.out.println( 日 一 二 三 四 五 六);int week=dayOfWeek(theTotalDays);for(int i=0;iweek%7;i+)System.out.print( );/打印1号前的空格int dayOfMonth=aDate.Day(year,month);for(int i=1;ih)if(second=s)secondcut=second-s;elsesecondcut=second+60-s;minute-;if(minute=m)minutecut=minute-m;elseminutecut=minute+60-m;hour-;hourcut=hour-h;else if(hour=h)if(minute=m)if(second=s)secondcut=second-s;elsesecondcut=second+60-s;minute-;minutecut=minute-m;elseif(s=second)secondcut=s-second;elsesecondcut=s+60-second;m-;minutecut=m-minute;hourcut=0;elseif(s=second)secondcut=s-second;elsesecondcut=s+60-second;m-;if(m=minute)minutecut=m-minute;elseminutecut=m+60-minute;h-;hourcut=h-hour;System.out.println(两时间相差+hourcut+小时+minutecut+分+secondcut+秒);public int numOfDays()/在1901年1月1日的基础上计算当前日期是第几天int totalDays=0;for(int y=1901;yyear;y+)for(int m=1;m=12;m+)totalDays+=Day(y,m);for(int m=1;mmonth;m+)totalDays+=Day(year,m);totalDays+=day;/在1991.1.1的基础上算当前日期是第几天return totalDays;public int Day(int year,int month)/用来确定任意一年任意一月的天数int numDays=0;switch(month)case 1:case 3:case 5:case 7:case 8:case 10:case 12:numDays=31;/大月的情况break;case 4:case 6:case 9:case 11:numDays=30;/小月的情况break;case 2:/闰年的情况if(year%4=0)&!(year%100=0)|(year%400=0)numDays=29;elsenumDays=28;break;return numDays;public class beat/* * param args */public static void main(String args) beatLandlordShuffle();static void beatLandlordShuffle()String points=1,2,3,4,5,6,7,8,9,t,J,Q,K;String suits=黑,红,梅,方;Node theFirst=new Node(,大王);theFirst.nextNode=new Node(,小王);Node cardPointer=theFirst.nextNode;for(int i=0;i4;i+)for(int j=0;j13;j+)cardPointer.nextNode=new Node(suitsi,pointsj);cardPointer=cardPointer.nextNode;cardPointer.nextNode=theFirst;cardPointer=theFirst;doSystem.out.println(cardPointer.getCard();cardPointer=cardPointer.nextNode;while(cardPointer!=theFirst);int cardToPickup=(int)(Math.random()*1000);int numberOfCardsLeft=54;cardPointer=theFirst;Node theFirstShuffled;if(cardToPickup=0)theFirstShuffled=theFirst;theFirst=theFirst.nextNode;/numberOfCardsLeft-;elsefor(int i=0;icardToPickup-1;i+)cardPointer=cardPointer.nextNode;theFirstShuffled=cardPointer.nextNode;cardPointer.nextNode=cardPointer.nextNode.nextNode;/numberOfCardsLeft-;numberOfCardsLeft-;Node shuffledRear=theFirstShuffled;for(int i=0;i53;i+)cardPointer=theFirst;cardToPickup=(int)(Math.random()*numberOfCardsLeft);if(cardToPickup=0)shuffledRear.nextNode=theFirst;shuffledRear=shuffledRear.nextNode;theFirst=theFirst.nextNode;elsefor(int j=0;jcardToPickup-1;j+)cardPointer=cardPointer.nextNode;shuffledRear.nextNode=cardPointer.nextNode;shuffledRear=shuffledRear.nextNode;cardPointer.nextNode=cardPointer.nextNode.nextNode;numberOfCardsLeft-;shuffledRear.nextNode=theFirstShuffled;shuffledRear=theFirstShuffled;System.out.print(The player1s cards:);cardPointer=display(shuffledRear,17);System.out.println();System.out.print(The player2s cards:);cardPointer=display(cardPointer,17);System.out.println();System.out.print(The player3s cards:);cardPointer=display(cardPointer,17);System.out.println();System.out.print(The left cards:);cardPointer=display(cardPointer,3);static Node display(Node thePointer,int aNum)for(int i=0;i5)flag=true;elseSystem.out.println(密码不少于6位,请重新输入);aStudent.changPW(oldPW,newPW,2);System.out.println(密码修改成功!);break;case 5:System.exit(0);default:break;static void Menu()System.out.println(请选择相应的功能编号:);System.out.println(1.存钱 2.取钱 3.显示账户信息);System.out.println(4.修改密码 5.退出);public static boolean getDoubleInput(String aPrompt,double inputValue)String strInput;double anDouble=0.0;trySystem.out.print(aPrompt+: );BufferedReader br=new BufferedReader(new InputStreamReader(System.in);strInput=br.readLine();anDouble=Double.parseDouble(strInput);catch(IOException e)System.out.println(IO错误!请重新运行程序。);return false;catch(NumberFormatException e)System.out.println(你的输入可能有格式错误!请重新运行程序。);return false;dblInputValue=anDouble;return true;public static boolean getStringInput(String aPrompt)String str=;trySystem.out.print(aPrompt+: );BufferedReader br=new BufferedReader(new InputStreamReader(System.in);str=br.readLine();catch(IOException e)System.out.println(IO错误!请重新运行程序。);return false;strInputValue=str;return true;public static boolean getIntegerInput(String aPrompt) /返回值输入值/若返回false,则输入出错,输入的值不能用。若返回true,输入正确,输入值可以用int anInteger=0;String strInput=null;trySystem.out.print(aPrompt+: );BufferedReader br=new BufferedReader(new InputStreamReader(System.in);strInput=br.readLine();anInteger=Integer.parseInt(strInput);catch(IOException e)System.out.println(IO错误!请重新运行程序。);return false;catch(NumberFormatException e)System.out.println(你的输入可能有格式错误!请重新运行程序。);return false;intInputValue=anInteger;return true;class BankCustomerprivate String customerID;/在创建账户时由用户选择确定,比如身份证号private String accountNo;/系统时间String customerName;/用户输入String customertelephone;/用户输入private float balance;/开始为0,开户后存钱private String password;/开始时为666666public void OpenAnAccount(String id,String name,String tel)SimpleDateFormat myDateFormat =new SimpleDateFormat(yyyyMMddHHmmss);java.util.Date date0=new java.util.Date();/strDate=myDateFormat.format(date0);customerID=id;accountNo=myDateFormat.format(date0);customerName=name;customertelephone=tel;balance=0;password=666666;public void setName(String aName)customerName=aName; public String getCustomerName()return customerName;public String getCustomerID()return customerID;public String getAccountNo()return accountNo;public void save(float amount)balance+=amount;public void displayBalance()System.out.println(账户余额: +balance);public void withdraw(float amount, String pw)if(!checkPW(pw)System.out.println(密码错误!);return;if(balanceamount)System.out.println(余额不足!);return;balance-=amount;System.out.println(取出现金+amount+元。);public boolean changPW(String oldPW,String newPW,int i)if(i=1)if(!checkPW(oldPW) System.out.println(密码错误!); return false; elsepassword=newPW;return true;private boolean checkPW(String pw)if(pareTo(pw)=0)return true;elsereturn false;运行结果:计算日期的星期,请输入正确的日期:年: 2012月: 5日: 1该天为星期二输入一个年月,将显示该月的日历:年: 2012月: 4 2012年4月- 日 一 二 三 四 五 六 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输入一个年份,将显示全年的日历: 2012 2012年1月- 日 一 二 三 四 五 六 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 2012年2月- 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2026学年吉林省四平市铁西区某中学高二上学期开学英语试卷(解析版)
- 2025年河北邢台内丘县人力资源和社会保障局就业见习245个岗位考前自测高频考点模拟试题及答案详解(易错题)
- 2025湖北武汉市中南财经政法大学教师招录模拟试卷及答案详解(全优)
- 企业文化建设方案模板跨行业
- 2025内蒙古鄂尔多斯市水发燃气有限公司招聘6人模拟试卷及完整答案详解1套
- 《初中物理力学平衡原理及其应用教案》
- 2025贵州安顺学院高层次人才引进考前自测高频考点模拟试题及答案详解(必刷)
- 2025湖南怀化市溆浦县卫健局招聘乡镇卫生院编外专技人员20人考前自测高频考点模拟试题附答案详解(突破训练)
- 企业知识产权保护登记及维护方案
- 2025黑龙江齐齐哈尔市讷河市发展和改革局所属事业单位选调9人考前自测高频考点模拟试题及答案详解(历年真题)
- 2025教科版三年级科学上册教学计划、教学设计(附目录)
- 木质素降解微生物促进秸秆饲料化营养价值提升的机制研究
- 深圳2025中考英语真题及答案
- 全科医学进修汇报
- 六年级下学期英语期末考试质量分析
- 三基培训及知识课件
- 监控运维:方案与实施
- 河南历史课件
- 全国青少年“学宪法、讲宪法”知识竞赛题库及答案
- 单元四-一般道路驾驶(教案)
- 油库消防培训课件
评论
0/150
提交评论