《JAVA基础实验编程-.pdf》由会员分享,可在线阅读,更多相关《JAVA基础实验编程-.pdf(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、1 Java 实验报告实验日期:2011年 10月 17日实验机房与机号:实验班级:组员姓名学号:指导教师:刘寿强第三单元实验目的:掌握陈述面向对象的思想,通过创建类的实力来体现实验内容:第三单元;实验步骤:3-2 class Dog String color;int weight;public Dog(String color,int weight)this.color=color;this.weight=weight;public void showd()System.out.println(这是体重为+weight+斤,+颜色为+color+的狗);class Cat String co
2、lor;int weight;public Cat(String color,int weight)this.color=color;this.weight=weight;public void showc()System.out.println(这是体重为+weight+斤,+颜色为+color+的猫);public class A public static void main(String args)Dog d1=new Dog(黄色,100);Cat c1=new Cat(白色,200);d1.showd();c1.showc();3-3 class Car String ping;d
3、ouble price;String date;public Car(String ping,double price,String date)this.ping=ping;this.price=price;2 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.print
4、ln(汽车正在加速);public class A public static void main(String args)Car car=new Car(奇瑞,3000,保修终生!);car.show();car.js();3-4 public class ConsoleParams public static void main(String args)if(args.length20)System.out.println(数值较大);else System.out.println(数值较小或等于);3-6class SExam static int num=0;public SExam(
5、)num+;public class StaticExam public static void main(String args)SExam se1=new SExam();SExam se2=new SExam();SExam se3=new SExam();System.out.println(SExam.num+次);第四单元目的:掌握格式化转换异常,实验内容:第四单元步骤如下:publicclass NumberFormatExceptionText /*param args*/publicstaticvoid main(String args)/TODO Auto-generate
6、d method stub4 int 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+=+(iArray0
7、+iArray1);publicclass ArithmeticExceptionText /*param args*/publicstaticvoid main(String args)/TODO Auto-generated method stubtry int a=args.length;int b=42/a;catch(ArithmeticException e)System.out.println(除数不能够为零啊!);5 import java.util.Random;publicclass NegativeIndexExceptionDemo publicintgetNumber
8、OfRandomIndex(int iArray)throwsNegativeIndexException Random random=new Random();int index=random.nextInt();if(index0)thrownewNegativeIndexException(this.getClass().getName()+.getNumberOfRandomIndex(int)方法不能够处理自己所抛出的异常:索引值+index+产出异常 );index=index%iArray.length;return iArrayindex;publicstaticvoid ma
9、in(String args)int iArray=0,1,3,4,5,6,7,8,9;int randomNumber=0;try randomNumber=newNegativeIndexExceptionDemo().getNumberOfRandomIndex(iArray);catch(NegativeIndexException e)System.out.println(e+nmian方法有能力处理该异常t);randomNumber=Math.round(float)(Math.random()*10);System.out.println(产生随机结果为+randomNumbe
10、r);6 publicclass NegativeIndexException extends Exception public NegativeIndexException()super();public NegativeIndexException(String message)super(message);第五单元一、实验目的:熟悉掌握输入输出流二、实验内容:第五单元三、实验步骤练习 5-1 校验用户密码格式(1)程序代码import java.util.Scanner;public class CheckPW public static void main(String args)Sy
11、stem.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(密码符合组合规律。);else System.out.println(密码不符合组合规律。);(2)运行结果7 图 5-1 练习 5-2 转换中文格式日期(1)程序代码import java.text.ParseExc
12、eption;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;public class ChineseDate public static void main(String args)Date date=new Date(10,9,2011);String dateFormatStr=北京时间:ntH:mm:ssntEntyyyy-M-dd;SimpleDateFormat simpleFormat=new SimpleDateFormat(dateFormatStr);System
13、.out.println(simpleFormat.format(date);(2)运行结果8 图 5-2 练习 5-3 万年历(3)程序代码import java.text.SimpleDateFormat;import java.util.*;public class CalendarAuto public static void main(String args)System.out.println(请输入年月(格式:年月):);Scanner scanner=new Scanner(System.in);int params=new int2;for(int i=0;i12)param
14、s1=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;i6;i+)9 fo
15、r(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 daysOfMonth(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;10 (4)运行结果图 5-3
限制150内