版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實驗二常用類與異常處理 .甘肅政法學(xué)院本科生實驗報告(二)姓名:學(xué)院:專業(yè):班級:實驗課程名稱 :JavaSE 程序設(shè)計實驗日期 :2014 年 4 月 16 日指導(dǎo)教師及職稱 :實驗成績 :開課時間: 20142015 學(xué)年第 1 學(xué)期甘肅政法學(xué)院實驗管理中心印制1 / 16 doc 可編輯實驗題目 常用類與異常處理姓名 班級一、實驗?zāi)康?掌握 Java語言的異常處理機制。2掌握 Java語言中常用的實用類。3理解什么是異常。小組合作 否學(xué) 號4掌握異常處理方法:拋出異常、捕捉和處理異常。5掌握自定義異常類。6. 掌握 java String類的各個常用方法的應(yīng)用。7. 掌握 Date 類和
2、 Calendar類的各個常用方法的應(yīng)用。8. 掌握 StringTokenizer類的用法。9. 了解正則表達式的用法10. 掌握簡單的異常處理流程。11. 內(nèi)部類的應(yīng)用二實驗環(huán)境(1) 配置好環(huán)境變量的 PC 計算機(2) 應(yīng)用 Eclipse進行 Java代碼的編寫、調(diào)試及運行三、實驗內(nèi)容與步驟2 / 16 doc 可編輯out .println(out .println(String s6=s7=1、 String類的常用方法應(yīng)用。class StringExample public static void main(String args) String s1= new String
3、( you are a student ),s2= new String( how are you );if (s1.equals(s2) / 使用 equals 方法判斷 s1與 s2 是否相同System. else System. String s3=out .println(out .println(new String(s1 與s2 相同 );s1 與 s2 不相同 );22030219851022024 );if (s3.startsWith( 220302 ) / 判斷 s3 的前綴是否是 “ 220302 ”。System. String s4=s5=out .println(
4、 吉林省的身份證 );new String( 你 ),new String( 我 );if (pareTo(s5)0) System.elseSystem./ 按著字典序 s4 大于 s5 的表達式。 按字典序 s4 大于 s5 ); 按字典序 s4 小于 s5 );int position=0;String path= c:javajspA.javaposition=path.lastIndexOf( ); /System. out .println( c:javajspA.java置 : +position);String fileName=path.substring(12,18 );S
5、ystem. out .println( c:javajspA.java;獲取 path 中最后出現(xiàn)目錄分隔符號的位置中最后出現(xiàn) 的位/ 獲取 path 中 “A.java ”子字符串。中含有的文件名 : +fileName);new String( 100 ),new String( 123.678int n1=Integer. parseInt (s6);doubledouble System.n2=Double. parseDoublem=n1+n2;out .println(m);String s8=String. valueOf (m);串對象);/ 將s6 轉(zhuǎn)化成 int 型數(shù)據(jù)。
6、(s7); / 將s7 轉(zhuǎn)化成 double 型數(shù)據(jù)。/String 調(diào)用 valuOf(int n) 方法將 m轉(zhuǎn)化為字符3 / 16 doc 可編輯position=s8.indexOf( . );String temp=s8.substring(position+1);System. out .println( 數(shù)字 +m+ 有 +temp.length()+String s9= new String( ABCDEF );char a=s8.toCharArray(); / 將s8 存放到數(shù)組for ( int i=a. length -1;i=0;i-) 位小數(shù) ) ;a中。Syste
7、m. out .print( +ai);2、 Date類和Calenda類r 的各個常用方法的應(yīng)用:比較日期的大小。package p.i;import java.util.*;import javax.swing.JOptionPane;public class DateExamplepublic static void main(String args ) String str=JOptionPane.showInputDialog( int yearOne=Integer.parseInt(str); str=JOptionPane.showInputDialog( int monthO
8、ne=Integer.parseInt(str); str=JOptionPane.showInputDialog( int dayOne=Integer.parseInt(str); str=JOptionPane.showInputDialog( int yearTwo=Integer.parseInt(str); str=JOptionPane.showInputDialog( int monthTwo=Integer.parseInt(str); str=JOptionPane.showInputDialog( int dayTwo=Integer.parseInt(str);Cale
9、ndar calendar=Calendar.getInstance(); / calendar.set(yearOne, monthOne, dayOne);間設(shè)置為 yearOne 年monthOne 月dayOne 日輸入第一個日期的年份輸入該年的月份 :);輸入該月份的日期 :);輸入第二個日期的年份 :);輸入該年的月份 :);輸入該月份的日期 :);初始化日歷對象 /:);將calendar 的時long timeOne=calendar.getTimeInMillis(); /calendar秒calendar.set(yearTwo, monthTwo, dayTwo); /間
10、設(shè)置為 yearTwo 年monthTwo 月dayTwo 日long timeTwo=calendar.getTimeInMillis(); /calendar 秒。Date date1=new Date (timeOne/(1000*60*60*24);/造date1Date date2=new Date (timeTwo/(1000*60*60*24);/表示的時間轉(zhuǎn)換成毫將calendar 的時表示的時間轉(zhuǎn)換成毫用timeOne 做參數(shù)構(gòu)用timeTwo 做參數(shù)構(gòu)4 / 16 doc 可編輯年+monthOne+年+monthTwo+造date2if(date2.equals(date
11、1)System.out.println( else if(date2.after(date1) System.out.println( else if(date2.before(date1) System.out.println( 兩個日期的年、月、日完全相同 );您輸入的第二個日期大于第一個日期您輸入的第二個日期小于第一個日期););long days=(timeOne-timeTwo)/(1000*60*60*24);/System.out.println(yearOne+yearTwo+3、 處理大整數(shù) package j.u;計算兩個日期相隔天數(shù)月+dayOne+ 日和 月+dayT
12、wo+ 相隔 +days+ 天);/BigintegerExampleimport java.math.*;class BigIntegerExamplepublic static void main(String args) BigInteger n1= new BigInteger( 987654321987654321987654321 ),n2= new BigInteger( 123456789123456789123456789 ),result= null ;result=n1.add(n2); /n1 和n2做加法運算System. out .println( 和 : +res
13、ult.toString();result=n1.subtract(n2); /n1 和n2 做減法運算System. out .println( 差 : +result.toString();result=n1.multiply(n2); /n1 和n2 做乘法運算System. out .println( 積 : +result.toString();result=n1.divide(n2); /n1 和n2做除法運算System. out .println( 商 : +result.toString();BigInteger m= new BigInteger( 1968957 ),CO
14、UNT= new BigInteger( 0 ),ONE= new BigInteger( 1 ),TWO= new BigInteger( 2 );System. out .println(m.toString()+ 的因子有 : );for (BigInteger i=TWO;pareTo(m)0;i=i.add(ONE)5 / 16 doc 可編輯 個因子 ); if (n1.remainder(i).compareTo(BigInteger. ZERO)=0) COUNT=COUNT.add(ONE);System. out .print( +i.toString();System.
15、out .println( );System. out .println(m.toString()+ 一共有 +COUNT.toString()+4、 紅牛農(nóng)場 package w.m;/RedCowForm.javapublic class RedCowForm StringRedCowformName ;cow ; / 用內(nèi)部類 RedCow聲明對象 cowRedCowForm() RedCowForm(String s) cow =new RedCow(150,130,4500); / 創(chuàng)建 cowformName =s;public void showCowMess() cow .sp
16、eak();class RedCow / 內(nèi)部類的聲明String cowName= 紅牛 ;int height , weight , price ;RedCow( int h, int w, int p)height =h;weight =w;price =p;void speak() System. out .println( cowName+, 身高 : +height +cm 體重 : +weight +kg );System. out .println( 生活在 +formName );package w.m;/MainClass.java6 / 16 doc 可編輯public
17、class MainClass public static void main(String args) RedCowForm form =form.showCowMess(); new RedCowForm( 紅牛農(nóng)場 );5、檢查危險品 package e.d;/Check.javapublic class Check public static void Machine machine = String name = Goods goods =main(String args) new Machine(); 蘋果 , 炸藥 , 西服 , 硫酸 , 手表 , 硫磺 ;new Goodsna
18、me. length ; / 檢查 6 件物品for ( int i= 0;iname. length ;i+) goodsi = new Goods();if (i%2=0) goodsi.setIsDanger( false );goodsi.setName(namei);else goodsi.setIsDanger( true );goodsi.setName(namei);for ( int i= 0;igoods. length ;i+) try machine.checkBag(goodsi);System. out .println(goodsi.getName()+catch
19、 (DangerException e) e.toShow(); /e 調(diào)用 toShow() 方法System. out .println(goodsi.getName()+package e.d; 檢查通過 ); 被禁止 ! );/DangerException.java class DangerExceptionString message ;extends Exception 7 / 16 doc 可編輯public DangerException() message = 危險品 ! ;public void toShow() System. out .print( message +
20、 );package e.d;/Goods.javaclass Goods boolean isDanger ;String name;public void setIsDanger( boolean boo) isDanger = boo;public boolean isDanger() return isDanger ;public void setName(String s) name = s;public String getName() return name;package e.d;/Machine.javaclass Machine boolean isDanger ;Stri
21、ng name;public void setIsDanger( boolean boo) isDanger = boo;public void checkBag(Goods goods) throws DangerException if (goods.isDanger()= true )throw new DangerException();6、 StringTokenizer的應(yīng)用8 / 16 doc 可編輯package r.o;/FoundPrice.javaimport java.util.*;public class FoundPrice public static void m
22、ain(String args) String s= 牛奶 :89.8 元,香腸 :12.9 元 啤酒 :69 元 巧克力 :132String regex= 0123456789. ; / 匹配非數(shù)字的正則表達識String digitMess=s.replaceAll(regex, * );StringTokenizer fenxi= new StringTokenizer(digitMess);標記解析 digitMess 中的單詞元 ;/ 創(chuàng)建 fenxi, 用* 做分隔int number=fenxi.countTokens(); double sum=0;while (fenxi.
23、hasMoreTokens() String str=fenxi.nextToken();System. out .println(str);sum=sum+Double. parseDouble/fenxi 調(diào)用 countTokens() 方法返回單詞數(shù)量/fenxi 調(diào)用 nextToken() 方法返回單詞(str);System. out .println( 購物小票中的商品種類:System. out .println( 購物小票中的價格總額:7、正則表達式的應(yīng)用 package i.p; +number+ 種 ); +sum+ 元 );import java.util.regex.*;public class ReplaceIPpublic static String str = Pattern pattern; Matcher matcher; String regex = pattern = Pattern.void main(String args ) 登錄網(wǎng)站 : 53 ;d1,3.d1,3.d1,3.d1,3com
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 課題申報參考:教師教育神經(jīng)科學(xué)素養(yǎng)的模型構(gòu)建與提升路徑
- 2025年度個人協(xié)議合同范文匯編及法律適用指南4篇
- 醫(yī)院2025年度消防安全管理合同2篇
- 二零二五年度賣房資金墊付及管理協(xié)議4篇
- 騰訊2025年度企業(yè)郵箱遷移服務(wù)合同2篇
- 二零二五版高端奶粉品牌加盟管理合同范本頁2
- 二零二五年度城市公共自行車系統(tǒng)維護與升級合同4篇
- 2025年度勞動合同試用期加班費及休息休假規(guī)定3篇
- 個人商品運輸合同范本錦集
- 二零二五年度臨時工工資支付合同模板
- 加強教師隊伍建設(shè)教師領(lǐng)域?qū)W習(xí)二十屆三中全會精神專題課
- 2024-2025學(xué)年人教版數(shù)學(xué)七年級上冊期末復(fù)習(xí)卷(含答案)
- 2024年決戰(zhàn)行測5000題言語理解與表達(培優(yōu)b卷)
- 四年級數(shù)學(xué)上冊人教版24秋《小學(xué)學(xué)霸單元期末標準卷》考前專項沖刺訓(xùn)練
- 2025年慢性阻塞性肺疾病全球創(chuàng)議GOLD指南修訂解讀課件
- (完整版)減數(shù)分裂課件
- 銀行辦公大樓物業(yè)服務(wù)投標方案投標文件(技術(shù)方案)
- 第01講 直線的方程(九大題型)(練習(xí))
- 微粒貸逾期還款協(xié)議書范本
- 人教版七年級上冊數(shù)學(xué)全冊課時練習(xí)帶答案
- NBT 47013.4-2015 承壓設(shè)備無損檢測 第4部分:磁粉檢測
評論
0/150
提交評論