欢迎来到天天文库
浏览记录
ID:35222174
大小:49.00 KB
页数:5页
时间:2019-03-22
《实验报告(实验三)》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、北京林业大学2013学年—2014学年第2学期Java程序设计基础实验报告书专业:电子班级:姓名:学号:实验地点:任课教师:黄儒乐实验题目:自定义异常与异常处理,常用类使用实验环境:Windows2000/XP;局域网实验要求:1.对所有实验内容中涉及到的命令均要亲自实践,对照实验指导书进行。2.把实验中出现的问题以及最后解决的情况反馈到实验报告中。3.程序作业的源代码要求反馈到实验报告中。实验内容:1.自定义异常及使用(1)自定义一个异常类,命名为AuthException。(2)设计一个LoginAuthCheck类,其中的verify(Stringusername,Stringpas
2、sword)方法用于验证用户名和密码是否正确,如果用户名不是admin,密码不是123,则抛出AuthException异常,并通过AuthException对象传递错误信息。(3)设计一个Example类,提示用户依次输入用户名,密码(使用Scanner类来实现),并通过LoginAuthCheck类的verify方法验证是否正确。2.比较日期的大小按模板要求,将【代码1】~【代码8】替换为Java程序代码。DateExampleimportjava.util.*;classDateExample{publicstaticvoidmain(Stringargs[]){Scannerrea
3、d=newScanner(System.in);System.out.println("输入第一个日期的年份:");intyearOne=read.nextInt();System.out.println("输入该年的月份:");intmonthOne=read.nextInt();System.out.println("输入该月份的日期:");intdayOne=read.nextInt();System.out.println("输入第二个日期的年份:");intyearTwo=read.nextInt();System.out.println("输入该年的月份:");intmonth
4、Two=read.nextInt();System.out.println("输入该月份的日期:");intdayTwo=read.nextInt();Calendarcalendar=【代码1】//初始化日历对象。【代码2】//将calendar的时间设置为yearOne年monthOne月dayOne日longtimeOne=【代码3】//calendar表示的时间转换成毫秒。【代码4】//将calendar的时间设置为yearTwo年monthTwo月dayTwo日longtimeTwo=【代码5】//calendar表示的时间转换成毫秒Datedate1=【代码6】//用timeO
5、ne做参数构造date1Datedate2=【代码7】//用timeTwo做参数构造date2if(date2.equals(date1)){System.out.println("两个日期的年、月、日完全相同");}elseif(date2.after(date1)){System.out.println("您输入的第二个日期大于第一个日期");}elseif(date2.before(date1)){System.out.println("您输入的第二个日期小于第一个日期");}longdiff=【代码8】//计算两个日期相隔天数System.out.println(“相隔天数:”+d
6、iff);}}实验目的:掌握异常处理的概念和运用掌握Date类等常用类的使用实现方法:1.自定义异常及使用实现AuthException为Exception的一个子类实现报错,LoginAuthCheck类实现对输入信息匹配并决定是否通过,主函数实现调用报错和匹配的方法。代码如下:packageSystem.out;importjava.util.Scanner;classAuthExceptionextendsException{Stringmessage;publicAuthException(){message="您输入的用户名或密码错误,请重新输入";}publicStringto
7、String(){returnmessage;}}classLoginAuthCheck{publicvoidverify(Stringusername,Stringpassword)throwsAuthException{if(!(username.equals("admin")&&password.equals("123"))){throw(newAuthException());}elseSystem.out.pr
此文档下载收益归作者所有