




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、信息工程學(xué)院1Java程序設(shè)計實驗/實習(xí)報告學(xué)院:信息工程學(xué)院班級:軟件工程123姓名:劉杰學(xué)號:2012013413成績:多線程異常處理一、實驗/實習(xí)過程實驗題1修改實驗二實驗題4,聲明一個局部變量Stringtext=然后通過循環(huán)把數(shù)組中的成員(有序)添加到text中,修改方法JOptionPane.showMessageDialog();參數(shù)以顯示text。實驗過程:與上次實驗中的實驗四相同,只是添加一個局部變量Stringtext=,賦值到text中,通過JOptionPane.showMessageDialog();方法輸出。實驗代碼:Store類:.nwsuaf.jp.p4;imp
2、ortjava.util.Arrays;importjavax.swing.JOptionPane;.nwsuaf.jp.p4.data.MP3Player;.nwsuaf.jp.p4.data.Mobile;.nwsuaf.jp.p4.data.Product;publicclassStorepublicstaticvoidmain(Stringargs)Mobilemobile1=newMobile(E365onChinaMobile,1780.0f);Mobilemobile2=newMobile(M330onChinaMobile,1450.0f);MP3Playerplayer1=n
3、ewMP3Player(MeizoX3,256,399.0f);MP3Playerplayer2=newMP3Player(MeizoE5,512,580.0f);MP3Playerplayer3=newMP3Player(XliveXMMP3Player,256,930.0f);Productproducts=mobile1,mobile2,player1,player2,player3;Arrays.sort(products);Stringtext=;for(inta=0;aproducts.length;+a)text+=productsa.toString()+n;JOptionPa
4、ne.showMessageDialog(null,Theproductsare:+nn+text+nn+Thereare+Product.getCount()+products.);實驗報告的內(nèi)容與格式按任課教師的要求書寫。Mobile類:.nwsuaf.jp.p4.data;publicclassMobileextendsProductpublicMobile(Stringname,floatprice)super(name,price);publicStringtoString()returnname+price+RMB;MP3Palyer類:.nwsuaf.jp.p4.data;pub
5、licclassMP3PlayerextendsProductpublicintmemory;publicMP3Player(Stringname,intmemory,floatprice)super(name,price);this.memory=memory;publicintgetMemory()returnmemory;publicvoidsetMemory(intmemory)this.memory=memory;OverridepublicStringtoString()returnname+(+memory+MB),+price+RMB;Product類:.nwsuaf.jp.p
6、4.data;publicabstractclassProductimplementsComparableprotectedStringname;protectedfloatprice;protectedstaticintcount;protectedProduct(Stringname,floatprice)=name;this.price=price;+count;publicStringgetName()returnname;publicfloatgetPrice()returnprice;publicstaticintgetCount()returncount;publicintcom
7、pareTo(Productproduct)returnnewFloat(product.getPrice().compareTo(price);實驗截圖:實驗題2用StringBuilertext=;替換Stringtext=”;然后通過循環(huán)使用StringBuiler類的append方法向text中把數(shù)組中的成員(按價格有序)添加到text中,修改方法JOptionPane.showMessageDialog()的參數(shù)以顯示text。運(yùn)行結(jié)果如圖3-1所示。圖3-1思考問題:對比分析StringBuiler與String的區(qū)別。答:一個String對象的長度是固定的,不能改變它的內(nèi)容,或者
8、是附加新的字符至String對象中。您也許會使用+來串聯(lián)字符串以達(dá)到附加新字符或字符串的目的,但+會產(chǎn)生一個新的String實例。而使用StringBuiler類中的append方法則是將新的字符串添加到已經(jīng)開辟的內(nèi)存中,不會增加新的內(nèi)存來存放新增的字符串。使用StringBuiler類中的append方法效率比較高,并且使用StringBuiler類中的append方法可以完全替代String類。實驗過程:在前面代碼中使用了StringBuiler類中的append方法。實驗代碼:(只展示Store類)Store類:.nwsuaf.jp.p4;importjava.util.Arrays;i
9、mportjavax.swing.JOptionPane;.nwsuaf.jp.p4.data.MP3Player;.nwsuaf.jp.p4.data.Mobile;.nwsuaf.jp.p4.data.Product;publicclassStorepublicstaticvoidmain(Stringargs)Mobilemobile1=newMobile(E365onChinaMobile,1780.0f);Mobilemobile2=newMobile(M330onChinaMobile,1450.0f);MP3Playerplayer1=newMP3Player(MeizoX3,2
10、56,399.0f);MP3Playerplayer2=newMP3Player(MeizoE5,512,580.0f);MP3Playerplayer3=newMP3Player(XliveXMMP3Player,256,930.0f);Productproducts=mobile1,mobile2,player1,player2,player3;Arrays.sort(products);StringBuildertext=newStringBuilder();for(inta=0;aproducts.length;+a)text.append(productsa+n);JOptionPa
11、ne.showMessageDialog(null,Theproductsare:+nn+text+nn+Thereare+Product.getCount()+products.);實驗題3從網(wǎng)上加載一個門戶網(wǎng)站首頁文件,用所學(xué)正則表達(dá)式知識,提取出其中所有郵箱地址。提示:讀取文件的語句為BufferedReaderbr=newBufferedReader(newFileReader(d:sharedxxx.html);while(line=br.readLine()!=null)parse(line);基本要求:編寫parse方法,完成上述功能。實驗過程:這個實驗主要是編寫prase方法中
12、的正則表達(dá)式的使用,了解讀取文件的語句。實驗代碼:packagetext3.d3;importjava.io.BufferedReader;importjava.io.FileReader;importjava.io.IOException;importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassEmailpublicEmail()/TODO自動生成的構(gòu)造函數(shù)存根privatestaticvoidparse(Stringtext)Stringregex=w.-+w.-+.w+;Patternp=Ppile(r
13、egex);Matcherm=p.matcher(text);while(m.find()Stringstring=m.group();System.out.println(string);publicstaticvoidmain(Stringargs)/TODO自動生成的方法存根trySuppressWarnings(resource)BufferedReaderreader=newBufferedReader(newFileReader(f:sina.htm);Stringtext=;while(text=reader.readLine()!=null)parse(text);catch(
14、IOExceptione)e.printStackTrace();實驗截圖:區(qū).口題勺Javadoc邁育明貝曲后3M洞用,第沁EmailJ呂四庖宅程詞CAPirogriarn片1已劉敢aj呂7binjEi慚訓(xùn)便灼(201拜11月30日下牛!5方7沁2)jubaoiSkvipjinccirmjubaogwipssinarccn實驗題4修改實驗二實驗題4,在Product類中添加銷售日期屬性及銷售額屬性,在sell方法中對其初始化,比較Date類與Calender類的用法,最后使用SimpleDateFormat類對銷售日期進(jìn)行格式化;用類DecimalFormat的相關(guān)方法格式化屬性sales,
15、熟悉DecimalFormat的用法。運(yùn)行結(jié)果如圖3-2所示。ITheproductsare:E3-65:17SO.ORMB,dat&andtimeofM33014&OJORr.lB.dateandtineofsale:2013-05-1422*341054psale5:116pM0.XlrveXMFJIP3Play:930.0RMB,dateandtimeofsale:2fl13-C5-1422fl3:41.C54,sales:4BP64M)MeiioE5:580.flRL1B,datear)dtimeofsale:2fl13-05-1422)3:41.-054,sales:17,400Me
16、iioX3:399.CFiMB,dateandtimeofsate:Z013-05-1422*3:41.OM.sale-si/.gfiOV.Threare5producis.確走圖3-2實驗過程:主要使用了上次實驗的代碼,增加了DecimalFormat方法和SimpleDateFormat類。實驗代碼:.nwsuaf.jp.p4.data;importjava.util.Date;importjava.text.DecimalFormat;importjava.text.SimpleDateFormat;publicabstractclassProductimplementsComparab
17、lepublicStringtype;publicfloatprice;publicstaticintcount;publicintsalePrice;publicStringsaleDate;publicProduct(Stringtype,floatprice,intsalePrice)this.type=type;this.price=price;this.salePrice=salePrice;+count;publicintcompareTo(Productproduct)returnnewFloat(product.price).compareTo(price);publicStr
18、ingSalePrice()DecimalFormatformatSaleMoney=newDecimalFormat(sales:#,#,#0.);Stringresult=formatSaleMoney.format(salePrice);returnresult;publicStringSaleDate()DatenowTime=newDate();SimpleDateFormatmatter=newSimpleDateFormat(dateandtimeofsale:yyyy-MM-ddHH:mm:ss.S);returnmatter.format(nowTime);.nwsuaf.j
19、p.p4.data;publicclassMobileextendsProductpublicstaticintmobileCount;publicMobile(Stringtype,floatprice,intsalePrice)super(type,price,salePrice);mobileCount+;publicStringtoString()returntype+:+price+RMB,+SaleDate()+SalePrice();.nwsuaf.jp.p4.data;publicclassMp3PlayerextendsProductpublicstaticintMp3Pla
20、yerCount;intsalePrice)+SaleDate()+SapublicMp3Player(Stringtype,floatprice,super(type,price,salePrice);Mp3PlayerCount+;publicStringtoString()returntype+:+price+RMB,lePrice();.nwsuaf.jp.p4;importjava.util.Arrays;importjavax.swing.JOptionPane;.nwsuaf.jp.p4.data.*;publicclassStorepublicstaticvoidmain(St
21、ringargs)/TODOAuto-generatedmethodstubMobilemobile1=newMobile(E365,1780f,106800);Mobilemobile2=newMobile(m330,1450f,116000);Mp3Playerplayer1Mp3Playerplayer2Mp3Playerplayer346500);=newMp3Player(Meizo=newMp3Player(Meizo=newMp3Player(XliveX3,399,7980);E5,580,17400);XMMP3Play,930,Productproducts=mobile1
22、,mobile2,player1,player2,player3;Arrays.sort(products);StringBuildertext=newStringBuilder();for(inti=0;iproducts.length;+i)text.append(productsi);text.append(n);JOptionPane.showMessageDialog(null,Theproductsare:nn+text+nThereare+Product.count+products.);實驗截圖:Theproductsare:365:1780.0Rh1B.dateandtime
23、ofsa3e:20l34)51422:03:41.054,sales:106.SOQ.M330:1450.0RMB,dateandtimeofsale伽3&4422:03:41.O54hsales:116.000V.XliveXMMP3Ptay:930.GRMB.dateandtimeofsale:201305d422:03:41.054,sales:46F500TMeizoE5:580.0RMB.dateandtime&rsale:2013-05-1422:03!41.054,53|65:17,400.MeizoX3:399.0RMB+dateandOmeofsale:2013-05-142
24、2:03:41+054hsales:719B0.Thereare5products.實驗題5編寫一個程序,輸入一個班某門課程成績,統(tǒng)計及格人數(shù)、不及格人數(shù)平均分。為此設(shè)計一個異常類,當(dāng)輸入的成績小于0分或大于100分時,拋出異常,程序?qū)⒉蹲竭@個異常,并作出相應(yīng)處理。基本要求:編寫完整程序?qū)嶒炦^程:先設(shè)計Input類,其中包含judge函數(shù)用來判斷輸入的成績是否合法并分別輸出不合法原因。在主方法中實現(xiàn)拋出異常。其次設(shè)計兩個異常類分別是:subException和beyondException實現(xiàn)成績大于100分和小于0分的異常。最后在主函數(shù)中編寫成績輸入方法。實驗代碼:Input類:packag
25、etext3.d5;importjava.util.Scanner;publicclassInputpublicstaticvoidjudge(doublei)throwssubException,beyondExceptionif(i100)thrownewbeyondException(成績不能大于100!);publicstaticvoidmain(Stringargs)inti=1;intpassnum=0;intnopassnum=0;System.out.println(請輸入學(xué)生成績,以“-1”結(jié)束!);System.out.println(第+i+位同學(xué)的成績?yōu)?);Suppr
26、essWarnings(resource)Scannerreader=newScanner(System.in);doublesum=0;doublex=reader.nextDouble();while(x!=-1)tryjudge(x);if(x100)passnum=passnum+1;sum+=x;catch(subExceptione1)System.out.println(e1);i-=1;catch(beyondExceptione2)System.out.println(e2);i-=1;i=i+1;System.out.println(第+i+位學(xué)生的成績?yōu)椋?;x=read
27、er.nextDouble();System.out.println(平均分為:+sum/(nopassnum+passnum);System.out.println(及格人數(shù)為:+passnum);System.out.println(不及格人數(shù)為:+nopassnum);subException類:packagetext3.d5;publicclasssubExceptionextendsArithmeticExceptionprivatestaticfinallongserialVersionUID=1L;publicsubException()super();publicsubExce
28、ption(Stringstring)super(string);beyondException類:packagetext3.d5;publicclassbeyondExceptionextendsArithmeticExceptionprivatestaticfinallongserialVersionUID=1L;publicbeyondException()super();publicbeyondException(Stringstring)super(string);實驗截圖:恿軀園hvadoc曉聲町券哈鳥Sffll礙uBSSlt:InputJavaC:ProgramFiIesJava
29、jre7binjavaw.exe(2013年12月呂E下牛1146;呂2)謁切焯輛爲(wèi)曠7穌!I-2住然3,05別庇戲即tion:邂稲曲創(chuàng)56紅朗生n觸圮66-1哲磚汩1.0及馭戲;1不反琲加專:1實驗題6設(shè)計類ReflectTester,該類中有一方法copy(0bjectobj),該方法能夠創(chuàng)建一個和參數(shù)obj同樣類型的對象,然后把obj對象中所有屬性復(fù)制到新建對象中,并將它返回。基本要求:設(shè)計相關(guān)類完成上述功能(只要求復(fù)制簡單JavaBean,且每個JavaBean的每個屬性都有public類型的getXxx()和setXxx()方法)。實驗過程:這個實驗感覺很難,開始有點找不到頭緒,從網(wǎng)
30、上找了寫資料,參照別人的代碼編寫的。先創(chuàng)建一個Product類,添加屬性和get、set方法和構(gòu)造函數(shù)。接下來創(chuàng)建一個reflectTester類,編寫一個copy方法實現(xiàn)類對象的復(fù)制。實驗代碼:reflectTester類:packagetext3.d6;importjava.lang.reflect.Field;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;publicclassreflectTesterpublicreflectTester()/TODO自動生成的構(gòu)造函數(shù)存
31、根SuppressWarnings(rawtypes,unchecked)publicObjectcopy(Objectobj)throwsInstantiationException,IllegalAccessException,IllegalArgumentException,InvocationTargetException,NoSuchMethodException,SecurityExceptionClassclassType=obj.getClass();System.out.println(Class+classType.getName();ObjectobjectOfCopy=
32、classType.getConstructor(newClass).newInstance(newObject);Fieldfields=classType.getDeclaredFields();for(Fieldfield:fields)StringfieldName=field.getName();StringfirstLetter=fieldName.substring(0,1).toUpperCase();StringgetMethodName=get+firstLetter+fieldName.substring(1);StringsetMethodName=set+firstL
33、etter+fieldName.substring(1);MethodgetMethod=classType.getMethod(getMethodName,newClass);MethodsetMethod=classType.getMethod(setMethodName,field.getType();Objectvalue=getMethod.invoke(obj,newObject);System.out.println(fieldName+,+value);setMethod.invoke(objectOfCopy,newObjectvalue);returnobjectOfCop
34、y;publicstaticvoidmain(Stringargs)throwsInstantiationException,IllegalAccessException,IllegalArgumentException,InvocationTargetException,NoSuchMethodException,SecurityExceptionProductproduct=newProduct(HowtolearnJava,85.6f);ProductproductOfCopy=(Product)newreflectTester().copy(product);System.out.println(Copyinfomation:+productOfCopy.getName()+&+productOfCopy.getPrice();Product類:packagetext3.d6;publicclassProductprivateStringname;privatefloatprice;publicStringgetName()returnname;publicvoidsetName(Stringname)=name;publicfloatgetPrice
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 人力資源咨詢顧問合同書
- 產(chǎn)品售后服務(wù)情況說明表
- 制造業(yè)企業(yè)知識產(chǎn)權(quán)保護(hù)策略研究
- 光纜遷移施工方案范本
- 橡膠跑道施工方案
- 涼山道路施工方案
- TCSMT YB011-2024 天然氣發(fā)熱量測量裝置測試技術(shù)規(guī)范
- 建筑工程人工費(fèi)承包合同-@-1
- 物業(yè)地面處理方案
- 透水砼路面施工方案
- 精細(xì)化工工藝學(xué)-第1章緒論講解課件
- 仰拱棧橋計算
- 中醫(yī)婦科 月經(jīng)過多課件
- 2022年江西制造職業(yè)技術(shù)學(xué)院單招語文試題及答案解析
- 穆斯林太巴熱咳慶念詞文
- 商標(biāo)一級授權(quán)書模板
- 軟硬結(jié)合板的設(shè)計制作與品質(zhì)要求課件
- 民營醫(yī)院組織架構(gòu)圖示
- 慢性心功能不全護(hù)理查房
- 初中 初二 物理 凸透鏡成像規(guī)律實驗(習(xí)題課) 教學(xué)設(shè)計
- 消防維保方案 (詳細(xì)完整版)
評論
0/150
提交評論