周漢濤-2013013003-實驗3(共21頁)_第1頁
周漢濤-2013013003-實驗3(共21頁)_第2頁
周漢濤-2013013003-實驗3(共21頁)_第3頁
周漢濤-2013013003-實驗3(共21頁)_第4頁
周漢濤-2013013003-實驗3(共21頁)_第5頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、實驗三 內(nèi)部(nib)類、常用類及異常類的用法1實驗(shyn)目的(1)掌握(zhngw)Java中內(nèi)部類的用法、異常類的用法(2)掌握Java中String類常用方法、StringBuilder類的用法;(3)掌握System類、Date類、Calender類、DateFormat類、NumberFormat類、Random類與BigInteger及BigDecimal類的用法;(4)掌握Java中正則表達式的基本用法;2實驗內(nèi)容實驗題1 修改實驗二實驗題4,聲明一個局部變量String text = ;然后通過循環(huán)把數(shù)組中的成員(有序)添加到text中,修改方法JOptionPane.s

2、howMessageDialog();參數(shù)以顯示text。實驗代碼Product類package .nwsuaf.jp.p4.data;public class Product private String name;private float price;protected static int count;public Product(String name,float price)super(); = name;this.price = price;public void SetName(String name) = name;public String GetName()return

3、name;public void SetPrice(float price)this.price = price;public float GetPrice()return price;public int count()return count;/因為compareTo是作用于字符串的比較,所以(suy)這里用了一下Float類進行了轉(zhuǎn)化/public int compareTo(Product product)return new Float(GetPrice().compareTo(product.GetPrice(); Mp3Player類package .nwsuaf.jp.p4.d

4、ata;public class Mp3Player extends Productprivate String memory;public Mp3Player(String name,String memory,float price)super(name,price);this.memory = memory;count+;public void Setmemory(String memory)this.memory = memory;public String Getmemory()return memory;public String toString()return GetName(

5、)+ +(+Getmemory()+ MB+)+,+GetPrice()+ +RMB;Mobile類package .nwsuaf.jp.p4.data;public class Mobile extends Productprivate String type;public Mobile(String name,String type,float price)super(name,price);this.type=type;count+;public void Settype(String type)this.type = type;public String Gettype()return

6、 type;public String toString()return GetName()+ +Gettype()+ MB+,+GetPrice()+ +RMB;Comparable類package .nwsuaf.jp.p4.data;public interface Comparable public int compareTo(Product product);Store類package .nwsuaf.jp.p4;import javax.swing.JOptionPane;import .nwsuaf.jp.p4.data.Mobile;import .nwsuaf.jp.p4.d

7、ata.Mp3Player;import .nwsuaf.jp.p4.data.Product;public class Store public static void main(String args) Mobile phone1 = new Mobile(E365,China Mobile,1780.0f);Mobile phone2 = new Mobile(M330,China Mobile,1450.0f);Mp3Player player1 = new Mp3Player(Meizo X3,256 MB,399.0f);Mp3Player player2 = new Mp3Pla

8、yer(Meizo E5,215 MB,580.0f);Mp3Player player3 = new Mp3Player(Xlive XM MP3 Play,256 MB,930.0f);int count1=player3.count();Product products = phone1,phone2,player1,player2,player3; for(int i = 0;i products.length; i+) for(int j= i+1;j products.length; j+) if(pareTo(productsj)0) Product temp = product

9、sj; productsj = productsi; productsi = temp; String text = ;for(int m = 0;m products.length;m+)text += productsm.toString()+n;JOptionPane.showMessageDialog(null, The products are: nn+text+nThere are +count1+ products);實驗(shyn)結(jié)果 圖1 實驗(shyn)結(jié)果實驗(shyn)題2 用StringBuiler text = ;替換(t hun)String text = ;然

10、后通過循環(huán)使用StringBuiler類的append方法向text中把數(shù)組中的成員(按價格有序)添加到text中,修改方法JOptionPane.showMessageDialog()的參數(shù)以顯示text。運行結(jié)果如圖3-1所示。圖3-1實驗代碼Product類package .nwsuaf.jp.p4.data;public class Product private String name;private float price;protected static int count;public Product(String name,float price)super(); = nam

11、e;this.price = price;public void SetName(String name) = name;public String GetName()return name;public void SetPrice(float price)this.price = price;public float GetPrice()return price;public int count()return count;/因為compareTo是作用于字符串的比較,所以這里用了一下Float類進行(jnxng)了轉(zhuǎn)化/public int compareTo(Product produc

12、t)return new Float(GetPrice().compareTo(product.GetPrice(); Mp3Player類package .nwsuaf.jp.p4.data;public class Mp3Player extends Productprivate String memory;public Mp3Player(String name,String memory,float price)super(name,price);this.memory = memory;count+;public void Setmemory(String memory)this.m

13、emory = memory;public String Getmemory()return memory;public String toString()return GetName()+ +(+Getmemory()+ MB+)+,+GetPrice()+ +RMB;Mobile類package .nwsuaf.jp.p4.data;public class Mobile extends Productprivate String type;public Mobile(String name,String type,float price)super(name,price);this.ty

14、pe=type;count+;public void Settype(String type)this.type = type;public String Gettype()return type;public String toString()return GetName()+ +Gettype()+ MB+,+GetPrice()+ +RMB;Comparable類package .nwsuaf.jp.p4.data;public interface Comparable public int compareTo(Product product);Store類package .nwsuaf

15、.jp.p4;import javax.swing.JOptionPane;import .nwsuaf.jp.p4.data.Mobile;import .nwsuaf.jp.p4.data.Mp3Player;import .nwsuaf.jp.p4.data.Product;public class Store public static void main(String args) Mobile phone1 = new Mobile(E365,China Mobile,1780.0f);Mobile phone2 = new Mobile(M330,China Mobile,1450

16、.0f);Mp3Player player1 = new Mp3Player(Meizo X3,256 MB,399.0f);Mp3Player player2 = new Mp3Player(Meizo E5,215 MB,580.0f);Mp3Player player3 = new Mp3Player(Xlive XM MP3 Play,256 MB,930.0f);int count1=player3.count();Product products = phone1,phone2,player1,player2,player3; for(int i = 0;i products.le

17、ngth; i+) for(int j= i+1;j products.length; j+) if(pareTo(productsj)0) Product temp = productsj; productsj = productsi; productsi = temp; StringBuilder text = new StringBuilder();for(int m = 0;m products.length;m+)text.append(productsm.toString()+n);JOptionPane.showMessageDialog(null, The products a

18、re: nn+text+nThere are +count1+ products);實驗(shyn)結(jié)果 圖2 實驗(shyn)結(jié)果實驗(shyn)題3 從網(wǎng)上加載一個門戶網(wǎng)站首頁文件,用所學正則表達式知識,提取出其中所有郵箱地址。提示:讀取文件的語句為BufferedReader br = new BufferedReader(new FileReader(d:sharedxxx.html);while(line= br.readLine()!=null)parse(line);基本要求:編寫parse方法,完成上述功能。實驗代碼import java.io.BufferedReader;i

19、mport java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Mile public static void parse(String text)String regex = w.-+w.-+.w+;Pattern p = Ppile(regex);Matcher m = p.matcher(text);while(m.find()

20、String result = m.group();System.out.println(result);public static void main(String args) tryBufferedReader br = new BufferedReader(new FileReader(D:text1.txt);String text = ;while(text = br.readLine() != null)parse(text);catch (FileNotFoundException e)e.printStackTrace();catch(IOException e)e.print

21、StackTrace();實驗(shyn)結(jié)果 圖3.1 要打開(d ki)的文檔 圖3.2 搜索(su su)結(jié)果實驗(shyn)題4 修改實驗二實驗題4,在Product類中添加銷售日期屬性及銷售額屬性,在sell方法中對其初始化,比較Date類與Calender類的用法,最后使用SimpleDateFormat類對銷售日期進行格式化;用類DecimalFormat的相關(guān)方法格式化屬性sales,熟悉DecimalFormat的用法。運行結(jié)果如圖3-2所示。圖3-2實驗代碼Product類package .nwsuaf.jp.p4.data;import java.util.Calenda

22、r;import java.util.Date;public class Product private String name;private float price; private int sales;protected static int count;private Date date = new Date();public Product(String name,float price,int sales)super(); = name;this.price = price;this.sales = sales;public String Getdate() String s =

23、String.format(%tY-%tm-%td %tH:%tM:%tS.%tL,date); return s;public void SetName(String name) = name;public String GetName()return name;public void SetPrice(float price)this.price = price;public float GetPrice()return price;public int count()return count;public void setSales(int sale)this.sales = sale;

24、 public String GetSales() String Sales = String.format(%,d,sales); return Sales; public int compareTo(Product product)return new Float(GetPrice().compareTo(product.GetPrice(); Mp3Player類package .nwsuaf.jp.p4.data;public class Mp3Player extends Productprivate String memory;public Mp3Player(String nam

25、e,String memory,float price,int sales)super(name,price,sales);this.memory = memory;count+;public void Setmemory(String memory)this.memory = memory;public String Getmemory()return memory;public String toString()return GetName()+: +GetPrice()+ +RMB+,date and time of sale:+Getdate()+,sales:+GetSales()+

26、¥;Mobile類package .nwsuaf.jp.p4.data;public class Mobile extends Productprivate String type;public Mobile(String name,String type,float price,int sales)super(name,price,sales);this.type=type;count+;public void Settype(String type)this.type = type;public String Gettype()return type;public String toStr

27、ing()return GetName()+: +GetPrice()+ +RMB+,date and time of sale:+Getdate()+,sales:+GetSales()+¥;Comparable類package .nwsuaf.jp.p4.data;public interface Comparable public int compareTo(Product product);Store類package .nwsuaf.jp.p4;import java.util.Calendar;import javax.swing.JOptionPane;import .nwsuaf

28、.jp.p4.data.Mobile;import .nwsuaf.jp.p4.data.Mp3Player;import .nwsuaf.jp.p4.data.Product;public class Store public static void main(String args) Mobile phone1 = new Mobile(E365,China Mobile,1780.0f,106800);Mobile phone2 = new Mobile(M330,China Mobile,1450.0f,116000);Mp3Player player1 = new Mp3Player

29、(Meizo X3,256 MB,399.0f,46500);Mp3Player player2 = new Mp3Player(Meizo E5,215 MB,580.0f,17400);Mp3Player player3 = new Mp3Player(Xlive XM MP3 Play,256 MB,930.0f,7980);int count1=player3.count();Product products = phone1,phone2,player1,player2,player3;for(int i = 0;i products.length; i+)for(int j= i+

30、1;j products.length; j+)if(pareTo(productsj)0)Product temp = productsj;productsj = productsi;productsi = temp; String message = ;for(int m = 0; mproducts.length;m+)message += productsm.toString()+n;JOptionPane.showMessageDialog(null, The products are: nn+message+nThere are +count1+ products);實驗(shyn

31、)結(jié)果 圖4 實驗(shyn)結(jié)果實驗(shyn)題 5 編寫一個程序,輸入一個班某門課程成績,統(tǒng)計及格人數(shù)、不及格人數(shù)平均分。為此設(shè)計一個異常類,當輸入的成績小于0分或大于100分時,拋出異常,程序?qū)⒉蹲竭@個異常,并作出相應處理?;疽螅?編寫完整程序輸入分數(shù)大于100分異常類: SuppressWarnings(serial) public class update extends Exception public String ShowMistake() return 輸入分數(shù)(fnsh)大于100,請重新輸入; (2)輸入分數(shù)(fnsh)小于0分類: SuppressWarnings

32、(serial) public class downdate extends Exception public String ShowMistake() return 輸入分數(shù)(fnsh)小于0,請重新輸入:; 異常拋出類及主方法: import java.util.Scanner; public class Score public void ThrowMistake(double fraction) throws downdate,update if(fraction 100) throw new update();/分數(shù)大于100異常拋出 public static void main(

33、String args) int down = 0,up = 0; double fraction,average,sum = 0; Score score = new Score(); System.out.println(請輸入班級總?cè)藬?shù):); SuppressWarnings(resource) Scanner i = new Scanner(System.in); int n = i.nextInt(); SuppressWarnings(resource) Scanner fra = new Scanner(System.in); for (int j = 1;j = 60&frac

34、tion =100) up+;/計算及格(j g)人數(shù) sum = sum +fraction; Down = n - up;/計算(j sun)不及格人數(shù) average = sum/n;/求平均(pngjn)分數(shù) System.out.println(平均分數(shù): + average); System.out.println(不及格人數(shù): + down); System.out.println(及格人數(shù): + up); 實驗結(jié)果 圖5.1 輸入分數(shù) 圖5.2 計算結(jié)果實驗題6 設(shè)計類ReflectTester,該類中有一方法copy(Object obj),該方法能夠創(chuàng)建一個和參數(shù)obj同樣

35、類型的對象,然后把obj對象中所有屬性復制到新建對象中,并將它返回?;?jbn)要求:設(shè)計相關(guān)類完成上述功能(只要求復制簡單JavaBean,且每個JavaBean的每個屬性都有public類型的getXxx()和setXxx()方法)。實驗(shyn)代碼ReflectTester類import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class ReflectTester public Object c

36、opy(Object obj) throws ExceptionClassclassType = obj.getClass();System.out.println(class name:+classType.getName();Object objectofcopy = classType.getConstructor(new Class).newInstance(new Object);Field fileds = classType.getDeclaredFields();for(Field file:fileds)String fieldName = file.getName();St

37、ring 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(setMethodNa

38、me,file.getType();Object value = getMethod.invoke(obj,new Object);System.out.println(fieldName+,+value);setMethod.invoke(objectofcopy, new Objectvalue);return objectofcopy;public static void main(String args) throws ExceptionProduct product = new Product(How to learn java,80.0f);System.out.println(product.toString(); Product productofcopy = (Product)new ReflectTester().copy(product);System.out.println(Copy information:n+name,+productofcopy.GetName()+n+price:+productofcopy.GetPrice();Pr

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論