JAVA基础实验编程

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1Java实验报告实验日期:2011年10月17日实验机房与机号:实验班级:组员姓名学号:指导教师:刘寿强第三单元实验目的:掌握陈述面向对象的思想,通过创建类的实力来体现实验内容:第三单元;实验步骤:3-2classDog{Stringcolor;intweight;publicDog(Stringcolor,intweight){this.color=color;this.weight=weight;}publicvoidshowd(){System.out.println(这是体重为+weight+斤,+颜色为+color+的狗);}}classCat{Stringcolor;intweight;publicCat(Stringcolor,intweight){this.color=color;this.weight=weight;}publicvoidshowc(){System.out.println(这是体重为+weight+斤,+颜色为+color+的猫);}}publicclassA{publicstaticvoidmain(String[]args){Dogd1=newDog(黄色,100);Catc1=newCat(白色,200);d1.showd();c1.showc();}}3-3classCar{Stringping;doubleprice;Stringdate;publicCar(Stringping,doubleprice,Stringdate){this.ping=ping;this.price=price;2this.date=date;}publicvoidshow(){System.out.println(ping+轿车,价格为+price+date);}publicvoidqd(){System.out.println(汽车正在启动);}publicvoidsc(){System.out.println(汽车正在刹车);}publicvoidjs(){System.out.println(汽车正在加速);}}publicclassA{publicstaticvoidmain(String[]args){Carcar=newCar(奇瑞,3000,保修终生!);car.show();car.js();}}3-4publicclassConsoleParams{publicstaticvoidmain(String[]args){if(args.length2){return;}inta=Integer.parseInt(args[0]);intb=Integer.parseInt(args[1]);intc;c=a+b;System.out.println(a+++b+=+c);}}3-5importjava.io.*;publicclassExampleD{3publicstaticvoidmain(String[]args){intnum=20;if(num20)System.out.println(数值较大);elseSystem.out.println(数值较小或等于);}}3-6classSExam{staticintnum=0;publicSExam(){num++;}}publicclassStaticExam{publicstaticvoidmain(String[]args){SExamse1=newSExam();SExamse2=newSExam();SExamse3=newSExam();System.out.println(SExam.num+次);}}第四单元目的:掌握格式化转换异常,实验内容:第四单元步骤如下:publicclassNumberFormatExceptionText{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstub4intiArray[]={0,0};try{iArray[0]=Integer.parseInt(args[0]);iArray[1]=Integer.parseInt(args[1]);}catch(ArrayIndexOutOfBoundsExceptione){System.out.println(用户输入的数据不完整,程序使用默认值代替:);}catch(NumberFormatExceptione){System.out.println(用户输入的不全是数值型的数据,程序使用默认值替代:);}System.out.println(iArray[0]+++iArray[1]+=+(iArray[0]+iArray[1]));}}publicclassArithmeticExceptionText{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubtry{inta=args.length;intb=42/a;}catch(ArithmeticExceptione){System.out.println(除数不能够为零啊!);}5}}importjava.util.Random;publicclassNegativeIndexExceptionDemo{publicintgetNumberOfRandomIndex(int[]iArray)throwsNegativeIndexException{Randomrandom=newRandom();intindex=random.nextInt();if(index0){thrownewNegativeIndexException(this.getClass().getName()+.getNumberOfRandomIndex(int[])方法不能够处理自己所抛出的异常:索引值+index+产出异常);}index=index%iArray.length;returniArray[index];}publicstaticvoidmain(String[]args){int[]iArray={0,1,3,4,5,6,7,8,9};intrandomNumber=0;try{randomNumber=newNegativeIndexExceptionDemo().getNumberOfRandomIndex(iArray);}catch(NegativeIndexExceptione){System.out.println(e+\nmian方法有能力处理该异常\t);randomNumber=Math.round((float)(Math.random()*10));}System.out.println(产生随机结果为+randomNumber);}6}publicclassNegativeIndexExceptionextendsException{publicNegativeIndexException(){super();}publicNegativeIndexException(Stringmessage){super(message);}}第五单元一、实验目的:熟悉掌握输入输出流二、实验内容:第五单元三、实验步骤练习5-1校验用户密码格式(1)程序代码importjava.util.Scanner;publicclassCheckPW{publicstaticvoidmain(String[]args){System.out.println(请输入密码:);Scannerscanner=newScanner(System.in);StringinputString=scanner.nextLine();Stringregular=^[a-zA-Z@$_][a-zA-Z@$_0-9]{7,19};if(inputString.matches(regular)){System.out.println(密码符合组合规律。);}elseSystem.out.println(密码不符合组合规律。);}}(2)运行结果7图5-1练习5-2转换中文格式日期(1)程序代码importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.Locale;publicclassChineseDate{publicstaticvoidmain(String[]args){Datedate=newDate(10,9,2011);StringdateFormatStr=北京时间:\n\tH:mm:ss\n\tE\n\tyyyy-M-dd;SimpleDateFormatsimpleFormat=newSimpleDateFormat(dateFormatStr);System.out.println(simpleFormat.format(date));}}(2)运行结果8图5-2练习5-3万年历(3)程序代码importjava.text.SimpleDateFormat;importjava.util.*;publicclassCalendarAuto{publicstaticvoidmain(String[]args){System.out.println(请输入年月(格式:年月):);Scannerscanner=newScanner(System.in);intparams[]=newint[2];for(inti=0;i2&&scanner.hasNextInt();i++){params[i]=scanner.nextInt();}if(params[1]12)params[1]=params[1]%13;if(params[1]0)params[1]=12+params[1]%13;Calendarcalendar=Calendar.getInstance();calendar.set(params[0],params[1]-1,1);intfirstIndex=calendar.get(Calendar.DAY_OF_WEEK)-1;chartitle[]={'日','一','二','三','四','五','六'};intdaysArray[][]=newint[6][7];intday=1;for(inti=firstIndex;i7;i++){daysArray[0][i]=day++;}for(inti=1;i6;i++){9for(intj=0;j7;j++){if(daydaysOfMonth(params[0],params[1])){i=6;break;}daysArray[i][j]=day++;}}for(inti=0;i7;i++){System.out.print(title[i]+\t);}System.out.println();for(inti=0;i6;i++){for(intj=0;j7;j++){if(daysArray[i][j]!='\0')System.out.print(daysArray[i][j]+\t);elseSystem.out.print(\t);}System.out.println();}}privatestaticintdaysOfMonth(intyear,intmonth){switch(month){case1:case3:case5:case7:case8:case10:case12:return31;case4:case6:case9:case11:return30;case2:if(year%4==0&&year%

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功