JAVA基础实验编程_第1页
JAVA基础实验编程_第2页
JAVA基础实验编程_第3页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java实验报告实验日期:2011年10月17日实验机房与机号: 实验班级: 组员姓名学号:指导教师:刘寿强第三单兀实验目的:掌握陈述面向对象的思想,通过创建类的实力来体现实验内容:第三单元;实验步骤:3-2class DogString color;int weight;public Dog(String color,int weight)this.color=color;this.weight=weight;public void showd()class CatString color;int weight;public Cat(String color,int weight) this

2、.color=color; this.weight=weight;public void showc()public class Apublic static void main(String args)Dog d1= new Dog("黄色",100);Cat c1= new Cat(" 白色",200); d1.showd();c1.showc();3-3class CarString ping;double price;String date;public Car(String ping,double price,String date) this

3、.ping=ping; this.price=price; this.date=date;public void show()public void qd() public void sc()public void js()public class Apublic static void main(String args)Car car=new Car(" 奇瑞",3000," 保修终生!");car.show(); car.js();凹 D:XPROGRA*!JCKEATlGE20ttl. exe终逾韋车.价格为? 车正在抑速?ress an V ke

4、y toc o n t in it匕l_0 0 03-4public class ConsoleParamspublic static void main(String args) if (args .l ength<2)return;int a=lnteger.parselnt(argsO);int b=lnteger.parselnt(args1); int c;c=a+b;public class ExampleDpublic static void main(String args) int num=20;if(num>20)else3-6class SExam stati

5、c int num=0;public SExam()num+;public class StaticExampublic static void main(String args)SExam se1=new SExam();SExam se2=new SExam();SExam se3=new SExam();第四单元目的:掌握格式化转换异常,实验内容:第四单元 步骤如下:public class NumberFormatExcepti on Text /* param args*/public static void main( Stri ng args) /TODOAuto-ge nera

6、ted method stubint iArray=0,0;tryiArray0 =ln teger.parse Int (args0);iArray1=Integer.parseInt (args1); catch (ArraylndexOutOfBoundsExcepti on e)System. out .println( "用户输入的数据不完整,程序使用默认值代替:"); catch (NumberFormatException e)System. out .println("用户输入的不全是数值型的数据,程序使用默认值替代:");System.

7、 out .println(iArray0+ yi);氐 rk-obl«inx I 删 Javidoe 曉"+" +iArray1+"="+(iArray0+iArraLl«cliar ati on 昱 Uoziso旨 匸£UwnbrForm =atEc1 eptiofiTestt Jiva Applicft+idn C : "kFrogt- wti Filas'1- 用户输入的數据不完整,程序便用默认值代替:0+0=0public classArithmeticExcepti on Text /* par

8、am args*/public static void main( Str in g args) /TODO Auto-ge nerated method stubtryint a=args. len gthint b=42/a;catch (ArithmeticExcepti on e) System. out .println("除数不能够为零啊!");<ternrii n-atedL.> Ar 1 ttme ti cExcept 1 onT ex七 Ja#宜 Appli catz on C: Troigr sin File除数不能够为零啊!importpu

9、blic classNegative In dexExcepti on Demo public int getNumberOfRa ndoml ndex(int iArray) throwsNegative In dexExcepti onRan dom ran dom=new Ran dom();int in dex=ra ndom.n extI nt();if (index<0)newthrowNegative In dexExcepti on(this .getClass().getName()+".getNumberOfRa ndoml ndex(i nt)方法不能够处

10、理自己所抛岀的异常:索引值"+index+ "产岀异常“);in dex=in dex%iArray.len gth ;return iArrayi ndex;public static voidmain( Str in g args) int iArray=0,1,3,4,5,6,7,8,9;int ran domNumber=0;try ran domNumber= newNegativeI ndexExceptio nDemo().getNumberOfRa ndoml ndex(iArray); catch (NegativeIndexException e)Sys

11、tem. out .println(e+"nmian方法有能力处理该异常 t");randomNumber=Math. round ( float )(Math.random ()*10);System. out .println( "产生随机结果为"+randomNumber);public classNegativeindexException extendsExceptionpublicNegative In dexExcepti on() super ();publicNegative In dexExcepti on( Stri ng mess

12、age)super (message);第五单元一、实验目的:熟悉掌握输入输出流二、实验内容:第五单元三、实验步骤练习5-1校验用户密码格式(1) 程序代码public class CheckPWpublic static void main( Str in g args)Scanner sca nner=new Sca nn er(System.i n);String inputString=scanner.nextLine();String regular="Aa-zA-Z$_a-zA-Z$_0-97,19"if(i nputStri ng.matches(regula

13、r)else(2) 运行结果图5-1练习5-2转换中文格式日期(1) 程序代码public class Chin eseDatepublic static void main(String args)Date date=new Date(10,9,2011);String dateFormatStr="北京时间:ntH:mm:ssntEntyyyy-M-dd" SimpleDateFormat simpleFormat=new SimpleDateFormat(dateFormatStr);(2) 运行结果图5-2练习5-3万年历(3) 程序代码public class C

14、ale ndarAutopublic static void main( Stri ng args)Scanner sea nner=new Sca nn er(System.i n);int params=new in t2;for(i nt i=0;i<2&&sca nn er.hasNextl nt();i+)paramsi=sca nner.n extI nt(); if(params1>12)params1=params1%13;if(params1<0)params1=12+params1%13;Cale ndar cale ndar=Cale n

15、dar.getl nsta nce();cale ndar.set(params0,params1-1,1);int first In dex=cale ndar.get(Cale ndar.DAY_OF_WEEK)-1; char title='日','一','二','三','四','五','六'int daysArray=new int6 7;int day=1;for(i nt i=first In dex;i<7;i+)daysArray0i=day+;for(i nt i=1;i<6;i+)for(i nt j=0;j<7;j+)if(day>daysOfM on th(params0,params1)i=6;break;daysArrayij=day+;for(i nt i=0;i<7;i+)for(i nt i=0;i<6;i+)for(i nt j=0;j<7;j+)private static int daysOfM on th(i nt year,i nt mon th)switch(mo nth)case 1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论