




已閱讀5頁(yè),還剩23頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
實(shí)驗(yàn)三 內(nèi)部類、常用類及異常類的用法1實(shí)驗(yàn)?zāi)康模?)掌握J(rèn)ava中內(nèi)部類的用法、異常類的用法 (2)掌握J(rèn)ava中String類常用方法、StringBuilder類的用法;(3)掌握System類、Date類、Calender類、DateFormat類、NumberFormat類、Random類與BigInteger及BigDecimal類的用法; (4)掌握J(rèn)ava中正則表達(dá)式的基本用法;2實(shí)驗(yàn)內(nèi)容實(shí)驗(yàn)題1 修改實(shí)驗(yàn)二實(shí)驗(yàn)題4,聲明一個(gè)局部變量String text = ;然后通過(guò)循環(huán)把數(shù)組中的成員(有序)添加到text中,修改方法JOptionPane.showMessageDialog();參數(shù)以顯示text。package .nwsuaf.jp.p4.data;public abstract class Product implements Comparableprotected String name;protected float price;protected static int count;public Product(String name, float price) super(); = name;this.price = price;count+;public String getName() return name;public void setName(String name) = name;public float getPrice() return price;public void setPrice(float price) this.price = price;public static int getCount() return count;public static void setCount(int count) Product.count = count;public int compareTo(Product p)return new Float(p.getPrice().compareTo(price);package .nwsuaf.jp.p4.data;import .nwsuaf.jp.p4.data.Product;public class Mobile extends Productpublic Mobile(String name,float price)super(name,price);Overridepublic String toString() return name+price+RMB;package .nwsuaf.jp.p4.data; import .nwsuaf.jp.p4.data.Product; public class Mp3Player extends Product public int memory; public Mp3Player(String name,int memory,float price) super(name,price); this.memory=memory; public int getMemory() return memory; public void setMemory(int memory) this.memory = memory; Override public String toString() return name+(+memory+MB),+price+RMB; package .nwsuaf.jp.p4;import java.util.Arrays;import javax.swing.JOptionPane;.nwsuaf.jp.p4.data.Mp3Player;import .nwsuaf.jp.p4.data.Mobile;import .nwsuaf.jp.p4.data.Product;public class Store public static void main(String args) Mobile m1=new Mobile(E365 on China Mobile, 1780.0f);Mobile m2=new Mobile(M330 on China Mobile, 1450.0f); Mp3Player p1=new Mp3Player(Meizo X3, 256, 399.0f); Mp3Player p2=new Mp3Player(Meizo E5, 512, 580.0f); Mp3Player p3=new Mp3Player(Xlive XM Mp3Player, 256, 930.0f); Product ps=m1,m2,p1,p2,p3; Arrays.sort(ps); String text=; for(int a=0;aps.length;a+) text+=psa.toString()+n; JOptionPane.showMessageDialog(null,The products are:+nn+text+nn+There are+Product.getCount()+products.); 實(shí)驗(yàn)題2 用StringBuiler text = ;替換String text = ;然后通過(guò)循環(huán)使用StringBuiler類的append方法向text中把數(shù)組中的成員(按價(jià)格有序)添加到text中,修改方法JOptionPane.showMessageDialog()的參數(shù)以顯示text。運(yùn)行結(jié)果如圖3-1所示。圖3-1思考問(wèn)題:對(duì)比分析StringBuiler與String的區(qū)別。解:一個(gè)String對(duì)象的長(zhǎng)度是固定的,不能改變它的內(nèi)容,或者是附加新的字符至String對(duì)象中。您也許會(huì)使用+來(lái)串聯(lián)字符串以達(dá)到附加新字符或字符串的目的,但+會(huì)產(chǎn)生一個(gè)新的String實(shí)例。而使用StringBuiler類中的append方法則是將新的字符串添加到已經(jīng)開(kāi)辟的內(nèi)存中,不會(huì)增加新的內(nèi)存來(lái)存放新增的字符串。使用StringBuiler類中的append方法效率比較高,并且使用StringBuiler類中的append方法可以完全替代String類。Storel類package .nwsuaf.jp.p4;import java.util.Arrays;import javax.swing.JOptionPane;import .nwsuaf.jp.p4.data.Mp3Player;import .nwsuaf.jp.p4.data.Mobile;import .nwsuaf.jp.p4.data.Product;public class Store public static void main(String args) Mobile m1=new Mobile(E365 on China Mobile, 1780.0f);Mobile m2=new Mobile(M330 on China Mobile, 1450.0f); Mp3Player p1=new Mp3Player(Meizo X3, 256, 399.0f); Mp3Player p2=new Mp3Player(Meizo E5, 512, 580.0f); Mp3Player p3=new Mp3Player(Xlive XM Mp3Player, 256, 930.0f); Product ps=m1,m2,p1,p2,p3; Arrays.sort(ps); /String text=; StringBuilder text=new StringBuilder(); for(int a=0;aps.length;a+) text.append(psa+n); JOptionPane.showMessageDialog(null,The products are:+nn+text+nn+There are+Product.getCount()+products.); 實(shí)驗(yàn)題3 從網(wǎng)上加載一個(gè)門(mén)戶網(wǎng)站首頁(yè)文件,用所學(xué)正則表達(dá)式知識(shí),提取出其中所有郵箱地址。提示:讀取文件的語(yǔ)句為BufferedReader br = new BufferedReader(new FileReader(d:sharedxxx.html);while(line= br.readLine()!=null)parse(line);基本要求:編寫(xiě)parse方法,完成上述功能。package text3.d3;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Email public Email()private static void parse(String text)String regex=w.-+w.-+.w+;Pattern p=Ppile(regex);Matcher m=p.matcher(text);while (m.find()String string =m.group();System.out.println(string);public static void main(String args)tryBufferedReader reader=new BufferedReader(new FileReader(f:sina.htm);String text=;while(text=reader.readLine()!=null)parse(text); catch(IOException e) e.printStackTrace(); 實(shí)驗(yàn)題4 修改實(shí)驗(yàn)二實(shí)驗(yàn)題4,在Product類中添加銷(xiāo)售日期屬性及銷(xiāo)售額屬性,在sell方法中對(duì)其初始化,比較Date類與Calender類的用法,最后使用SimpleDateFormat類對(duì)銷(xiāo)售日期進(jìn)行格式化;用類DecimalFormat的相關(guān)方法格式化屬性sales,熟悉DecimalFormat的用法。運(yùn)行結(jié)果如圖3-2所示。package .nwsuaf.jp.p4.data;public abstract class Product implements Comparable protected String name;protected float price;protected static int count; protected Product (String name,float price) =name;this.price=price;+count;public String getName() return name;public float getPrice() return price;public static int getCount() return count;public int compareTo(Product product) return new Float(product.getPrice().compareTo(price);package .nwsuaf.jp.p4.data;public class Mobile extends Product public Mobile (String name,float price) super(name,price);public String toString() return name+price+RMB;package .nwsuaf.jp.p4.data;public class Mp3Player extends Product public int memory;public Mp3Player(String name,int memory,float price) super(name,price);this.memory=memory;public int getMemory() return memory;public void setMemory(int memory) this.memory = memory;Overridepublic String toString() return name+(+memory+MB),+price+RMB;package .nwsuaf.jp.p4;import java.util.Arrays;import javax.swing.JOptionPane;import .nwsuaf.jp.p4.data.Mp3Player;import .nwsuaf.jp.p4.data.Mobile;import .nwsuaf.jp.p4.data.Product;public class Store public static void main (String args) Mobile m1=new Mobile(E365 on China Mobile,1780.0f);Mobile m2=new Mobile(M330 on China Mobile,1450.0f);Mp3Player player1= new Mp3Player(Meizo X3,256,399.0f);Mp3Player player2= new Mp3Player(Meizo E5,512,580.0f);Mp3Player player3= new Mp3Player(Xlive XM MP3Player,256,930.0f);Product products =m1,m2,player1,player2,player3;Arrays.sort(products);String text=;for (int a=0;aproducts.length;+a) text+= productsa.toString()+n;JOptionPane.showMessageDialog(null, The products are:+nn+text+nn+There are +Product.getCount()+ products.);圖3-2實(shí)驗(yàn)題 5 編寫(xiě)一個(gè)程序,輸入一個(gè)班某門(mén)課程成績(jī),統(tǒng)計(jì)及格人數(shù)、不及格人數(shù)平均分。為此設(shè)計(jì)一個(gè)異常類,當(dāng)輸入的成績(jī)小于0分或大于100分時(shí),拋出異常,程序?qū)⒉蹲竭@個(gè)異常,并作出相應(yīng)處理。基本要求: 編寫(xiě)完整程序?qū)嶒?yàn)過(guò)程:先設(shè)計(jì)Input類,其中包含judge函數(shù)用來(lái)判斷輸入的成績(jī)是否合法并分別輸出不合法原因。在主方法中實(shí)現(xiàn)拋出異常。其次設(shè)計(jì)兩個(gè)異常類分別是:subException和beyondException實(shí)現(xiàn)成績(jī)大于100分和小于0分的異常。最后在主函數(shù)中編寫(xiě)成績(jī)輸入方法。package text3.d5;public class subException extends ArithmeticExceptionprivate static final long serialVersionUID =1L;public subException () super();public subException(String string) super(string);package text3.d5;public class beyondException extends ArithmeticException private static final long serialVersionUID =1L;public beyondException ()super();public beyondException (String string) super(string);package text3.d5;import java.util.Scanner;public class Input public static void judge (double i) throws subException,beyondException if(i100) throw new beyondException (成績(jī)不能大于100!);public static void main (String args) int i=1;int passnum=0;int nopassnum=0;System.out.println(請(qǐng)輸入學(xué)生成績(jī),以“-1”結(jié)束!);System.out.println(第+i+位同學(xué)的成績(jī)?yōu)?);SuppressWarnings(resource)Scanner reader = new Scanner(System.in);double sum=0;double x= reader.nextDouble();while (x!=-1) tryjudge(x);if(x60) passnum= passnum+1;sum+=x; catch(subException e1) System.out.println(e1);i-=1;catch(beyondException e2) System.out.println(e2);i-=1;i=i+1;System.out.println(第+i+位學(xué)生的成績(jī)?yōu)椋?;x=reader.nextDouble();System.out.println(平均分為:+sum/(nopassnum+passnum);System.out.println(及格人數(shù)為:+passnum);System.out.println(不及格人數(shù)為:+nopassnum);*實(shí)驗(yàn)題6 設(shè)計(jì)類ReflectTester,該類中有一方法copy(Object obj),該方法能夠創(chuàng)建一個(gè)和參數(shù)obj同樣類型的對(duì)象,然后把obj對(duì)象中所有屬性復(fù)制到新建對(duì)象中,并將它返回。基本要求:設(shè)計(jì)相關(guān)類完成上述功能(只要求復(fù)制簡(jiǎn)單JavaBean,且每個(gè)JavaBean的每個(gè)屬性都有public類型的getXxx()和setXxx()方法)。package text.d6;public class Product private String name;private float price;public String getName() return name;public void setName(String name) = name;public float getPrice() return price;public void setPrice(float price) this.price = price;public Product(String name,float price) / TODO 自動(dòng)生成的構(gòu)造函數(shù)存根super();=name;this.price=price;public Product() / TODO 自動(dòng)生成的構(gòu)造函數(shù)存根super();package text3.d6;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import text.d6.Product;public class reflectTester public reflectTester() / TODO 自動(dòng)生成的構(gòu)造函數(shù)存根SuppressWarnings( rawtypes, unchecked )public Object copy(Object obj) throws InstantiationException,IllegalAccessException,IllegalArgumentException,InvocationTargetException,NoSuchMethodException,SecurityException Class classType=obj.getClass();System.out.println(Class+classType.getName();Object objectOfCopy = classType.getConstructor(new Class).newInstance(new Object);Field fields = classType.getDeclaredFields();for(Field field:fields)String fieldName = field.getName();String firstLetter = fieldName.substring(0,1).toUpperCase();String getMethodName = get+firstLetter + fieldName.substring(1);String setMethodName = set+firstLetter + fieldName.substring(1);Method getMethod = classType.getMethod(getMethodName,new Class);Method setMethod = classType.getMethod(setMethodName,field.getType();Object value = getMethod.invoke(obj,new Object);System.out.println(fieldName+,+value);setMethod.invoke(objectOfCopy, new Objectvalue);re
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 汽車(chē)行業(yè)項(xiàng)目工程師供應(yīng)商職責(zé)
- 城鎮(zhèn)學(xué)困生綜合幫扶計(jì)劃
- 遼寧省鐵嶺市調(diào)兵山市2025屆九年級(jí)下學(xué)期中考三模數(shù)學(xué)試卷(含解析)
- 2025年培訓(xùn)項(xiàng)目總結(jié)及2025年培訓(xùn)計(jì)劃
- 微課程制作教學(xué)質(zhì)量監(jiān)控心得體會(huì)
- 醫(yī)療場(chǎng)所設(shè)備更新措施
- 公共藝術(shù)裝置裝飾項(xiàng)目風(fēng)險(xiǎn)應(yīng)對(duì)措施
- 新蘇教版三年級(jí)科學(xué)下冊(cè)教學(xué)安排計(jì)劃
- 2025年船舶安全運(yùn)營(yíng)管理計(jì)劃
- JL1901年度品牌形象內(nèi)部審核計(jì)劃
- 2025廣西公需科目真題續(xù)集(附答案)
- 2025年 浙江“三支一扶”招募筆試考試試卷附答案
- 2025年全國(guó)統(tǒng)一高考數(shù)學(xué)試卷(全國(guó)一卷)含答案
- (正式版)SH∕T 3548-2024 石油化工涂料防腐蝕工程施工及驗(yàn)收規(guī)范
- 數(shù)據(jù)中心巡檢機(jī)器人解決方案
- 鐵路危險(xiǎn)貨物運(yùn)輸及貨物安檢查危技術(shù)業(yè)務(wù)考核題庫(kù)
- 露天礦山安全生產(chǎn)責(zé)任制
- 某市印染紡織公司清潔生產(chǎn)審核報(bào)告全文
- 山西特崗教師招聘考試真題
- 小學(xué)年級(jí)組長(zhǎng)工作總結(jié)二年級(jí)
- 員工反違章承諾書(shū)
評(píng)論
0/150
提交評(píng)論