JAVA基礎(chǔ)實驗編程
Java 實驗報告實驗日期:2011年10月17日 實驗機房與機號: 實驗班級: 組員姓名學(xué)號: 指導(dǎo)教師:劉壽強 第三單元實驗?zāi)康模赫莆贞愂雒嫦驅(qū)ο蟮乃枷?,通過創(chuàng)建類的實力來體現(xiàn)實驗內(nèi)容:第三單元;實驗步驟: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 color;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;double 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("汽車正在加速");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.length<2)return;int a=Integer.parseInt(args0);int b=Integer.parseInt(args1);int c;c=a+b;System.out.println(a+" +"+b+" ="+c);3-5import java.io.*;public class ExampleDpublic static void main(String args)int num=20;if(num>20)System.out.println("數(shù)值較大");elseSystem.out.println("數(shù)值較小或等于");3-6class SExamstatic 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();System.out.println(SExam.num+"次");第四單元目的:掌握格式化轉(zhuǎn)換異常,實驗內(nèi)容:第四單元步驟如下:public class NumberFormatExceptionText /* * param args */public static void main(String args) / TODO Auto-generated method stubint iArray=0,0; try iArray0=Integer.parseInt(args0); iArray1=Integer.parseInt(args1);catch(ArrayIndexOutOfBoundsException e) System.out.println("用戶輸入的數(shù)據(jù)不完整,程序使用默認值代替:"); catch(NumberFormatException e) System.out.println("用戶輸入的不全是數(shù)值型的數(shù)據(jù),程序使用默認值替代:"); System.out.println(iArray0+"+"+iArray1+"="+(iArray0+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("除數(shù)不能夠為零??!");import java.util.Random;public class NegativeIndexExceptionDemo public int getNumberOfRandomIndex(int iArray)throws NegativeIndexExceptionRandom random=new Random();int index=random.nextInt();if(index<0) throw new NegativeIndexException(this.getClass().getName()+".getNumberOfRandomIndex(int)方法不能夠處理自己所拋出的異常:索引值"+index+"產(chǎn)出異常"); index=index%iArray.length;return iArrayindex;public 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("產(chǎn)生隨機結(jié)果為"+randomNumber);public class NegativeIndexException extends Exceptionpublic NegativeIndexException()super();public NegativeIndexException(String message)super(message);第五單元一、實驗?zāi)康模菏煜ふ莆蛰斎胼敵隽鞫?、實驗?nèi)容:第五單元三、實驗步驟 練習(xí)5-1 校驗用戶密碼格式(1) 程序代碼import java.util.Scanner;public class CheckPWpublic static void main(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("密碼符合組合規(guī)律。");elseSystem.out.println("密碼不符合組合規(guī)律。");(2) 運行結(jié)果圖5-1練習(xí)5-2 轉(zhuǎn)換中文格式日期(1) 程序代碼import java.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(dateFormatStr);System.out.println(simpleFormat.format(date);(2) 運行結(jié)果圖5-2練習(xí)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;i<2&&scanner.hasNextInt();i+)paramsi=scanner.nextInt();if(params1>12)params1=params1%13;if(params1<0)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;i<7;i+)daysArray0i=day+;for(int i=1;i<6;i+)for(int j=0;j<7;j+)if(day>daysOfMonth(params0,params1)i=6;break;daysArrayij=day+;for(int i=0;i<7;i+)System.out.print(titlei+"t");System.out.println();for(int i=0;i<6;i+)for(int j=0;j<7;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;(4) 運行結(jié)果圖5-3