JAVA基础实验编程.doc

上传人:豆**** 文档编号:29964641 上传时间:2022-08-02 格式:DOC 页数:10 大小:193.50KB
返回 下载 相关 举报
JAVA基础实验编程.doc_第1页
第1页 / 共10页
JAVA基础实验编程.doc_第2页
第2页 / 共10页
点击查看更多>>
资源描述

《JAVA基础实验编程.doc》由会员分享,可在线阅读,更多相关《JAVA基础实验编程.doc(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

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()System.out.println(这是体重为+weight+斤,+颜色为+color+的狗);class CatString colo

2、r;int weight;public Cat(String color,int weight)this.color=color;this.weight=weight;public void showc()System.out.println(这是体重为+weight+斤,+颜色为+color+的猫);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;dou

3、ble price;String date;public Car(String ping,double price,String date)this.ping=ping;this.price=price;this.date=date;public void show()System.out.println(ping+轿车,价格为+price+date);public void qd()System.out.println(汽车正在启动);public void sc()System.out.println(汽车正在刹车);public void js()System.out.println(汽

4、车正在加速);public class Apublic static void main(String args)Car car=new Car(奇瑞,3000,保修终生!);car.show();car.js();3-4public class ConsoleParamspublic static void main(String args)if (args.length20)System.out.println(数值较大);elseSystem.out.println(数值较小或等于);3-6class SExamstatic int num=0;public SExam() num+;p

5、ublic class StaticExampublic static void main(String args)SExam se1=new SExam();SExam se2=new SExam();SExam se3=new SExam();System.out.println(SExam.num+次);第四单元目的:掌握格式化转换异常,实验内容:第四单元步骤如下:public class NumberFormatExceptionText /* * param args */public static void main(String args) / TODO Auto-generat

6、ed method stubint iArray=0,0; try iArray0=Integer.parseInt(args0); iArray1=Integer.parseInt(args1);catch(ArrayIndexOutOfBoundsException e) System.out.println(用户输入的数据不完整,程序使用默认值代替:); catch(NumberFormatException e) System.out.println(用户输入的不全是数值型的数据,程序使用默认值替代:); System.out.println(iArray0+iArray1+=+(iA

7、rray0+iArray1);public class ArithmeticExceptionText /* * param args */public static void main(String args) / TODO Auto-generated method stubtryint a=args.length;int b=42/a;catch(ArithmeticException e)System.out.println(除数不能够为零啊!);import java.util.Random;public class NegativeIndexExceptionDemo public

8、 int getNumberOfRandomIndex(int iArray)throws NegativeIndexExceptionRandom random=new Random();int index=random.nextInt();if(index0) throw new NegativeIndexException(this.getClass().getName()+.getNumberOfRandomIndex(int)方法不能够处理自己所抛出的异常:索引值+index+产出异常); index=index%iArray.length;return iArrayindex;pu

9、blic static void main(String args) int iArray=0,1,3,4,5,6,7,8,9;int randomNumber=0;tryrandomNumber=new NegativeIndexExceptionDemo().getNumberOfRandomIndex(iArray);catch(NegativeIndexException e)System.out.println(e+nmian方法有能力处理该异常t);randomNumber=Math.round(float)(Math.random()*10);System.out.println

10、(产生随机结果为+randomNumber);public class NegativeIndexException extends Exceptionpublic NegativeIndexException()super();public NegativeIndexException(String message)super(message);第五单元一、实验目的:熟悉掌握输入输出流二、实验内容:第五单元三、实验步骤 练习5-1 校验用户密码格式(1) 程序代码import java.util.Scanner;public class CheckPWpublic static void m

11、ain(String args)System.out.println(请输入密码:);Scanner scanner=new Scanner(System.in);String inputString=scanner.nextLine();String regular=a-zA-Z$_a-zA-Z$_0-97,19;if(inputString.matches(regular)System.out.println(密码符合组合规律。);elseSystem.out.println(密码不符合组合规律。);(2) 运行结果图5-1练习5-2 转换中文格式日期(1) 程序代码import java

12、.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;public class ChineseDatepublic static void main(String args)Date date=new Date(10,9,2011);String dateFormatStr=北京时间:ntH:mm:ssntEntyyyy-M-dd;SimpleDateFormat simpleFormat=new SimpleDateFormat(dateForm

13、atStr);System.out.println(simpleFormat.format(date);(2) 运行结果图5-2练习5-3 万年历(3) 程序代码import java.text.SimpleDateFormat;import java.util.*;public class CalendarAutopublic static void main(String args)System.out.println(请输入年月(格式:年 月):);Scanner scanner=new Scanner(System.in);int params=new int2;for(int i=0

14、;i12)params1=params1%13;if(params10)params1=12+params1%13;Calendar calendar=Calendar.getInstance();calendar.set(params0,params1-1,1);int firstIndex=calendar.get(Calendar.DAY_OF_WEEK)-1;char title=日,一,二,三,四,五,六;int daysArray=new int67;int day=1;for(int i=firstIndex;i7;i+)daysArray0i=day+;for(int i=1;

15、i6;i+)for(int j=0;jdaysOfMonth(params0,params1)i=6;break;daysArrayij=day+;for(int i=0;i7;i+)System.out.print(titlei+t);System.out.println();for(int i=0;i6;i+)for(int j=0;j7;j+)if(daysArrayij!=0)System.out.print(daysArrayij+t);else System.out.print( t);System.out.println();private static int daysOfMo

16、nth(int year,int month)switch(month)case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;case 4:case 6:case 9:case 11:return 30;case 2:if(year % 4 = 0 & year % 100 !=0) return 29;if(year % 100 = 0 & year % 400 !=0)return 29;return 28;default:return 0;(4) 运行结果图5-310-结0 =0 = 0 % &0= % ( 0 0 )

17、) ) . ! (+ = (+ ; ) )+ . + + ) 0 ( + 0 + (+= +; ( 六三二 ) . ( ), , ( ) = ) ( ) . +) & 0 = ) ( ) 式月入( * 代年万-结) ( . ) ( - 时北 ) 0( ( . 代日式中 -图结)合符码( . )。合组( . ) ( ,0 -_- ) ) . ( )码请( . 代格码验 骤单五内出输输:目单) ( ) ( ) +为随 . 0) . . ) 异处能方 ( . ) ( 0= , ,0= ) ( % = )常出 值常抛己理法 +( ) ( 0 ) ) ) . )!为能 . / = / * * ) +0

18、+ + ( ;)替默使据的值不入 . ) ):值默使整不输 ) ; )0 ( = 0= / * * 下单第常异化掌单)+. ( . ) ) = )( ) +)( 0 )于小数 . )值 . 0 00 ) * -) +( += ) ( . )0 . = () -).) .)!终0,奇 = ) ( )速正车( . ) )正车 ) )正 . ) ) +格,+ ( )( = . ) ( -) ) ),白 = )0黄 ) ) 为, 重这 ) ) )+ +色+斤 +重这 )( . ) , -骤元元容体体实过通象面握:单强刘师 号名 班实 机机 日0 期报验0 报 机班 号 强握通体元骤 , ) +斤+ ) 重 ) 黄)=白) ) ( ) = ) (, ) ) 车 .车速 奇终.) ( )+ + * 0 0 ) 数) (+ ) ) . .掌常 0 ( ; 输使) 值默; +0) * / = 能! . ) 0 )( 法抛值出)=% 0 . 方异) .0 随 ) ( ( 单:内骤 验格 () . 0 ).组。 符合 式 时 ) )-万 (式 = ) .) () = )( ) ( = + + + . ) +( +( ! ) 0 ( 0% 0 0结 10

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁