您好,欢迎访问三七文档
实验三内部类、常用类及异常类的用法1.实验目的(1)掌握Java中内部类的用法、异常类的用法(2)掌握Java中String类常用方法、StringBuilder类的用法;(3)掌握System类、Date类、Calender类、DateFormat类、NumberFormat类、Random类与BigInteger及BigDecimal类的用法;(4)掌握Java中正则表达式的基本用法;2.实验内容实验题1修改实验二实验题4,声明一个局部变量Stringtext=;然后通过循环把数组中的成员(有序)添加到text中,修改方法JOptionPane.showMessageDialog();参数以显示text。packagecn.edu.nwsuaf.jp.p4.data;publicabstractclassProductimplementsComparableProduct{protectedStringname;protectedfloatprice;protectedstaticintcount;publicProduct(Stringname,floatprice){super();this.name=name;this.price=price;count++;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicfloatgetPrice(){returnprice;}publicvoidsetPrice(floatprice){this.price=price;}publicstaticintgetCount(){returncount;}publicstaticvoidsetCount(intcount){Product.count=count;}publicintcompareTo(Productp){returnnewFloat(p.getPrice()).compareTo(price);}}packagecn.edu.nwsuaf.jp.p4.data;importcn.edu.nwsuaf.jp.p4.data.Product;publicclassMobileextendsProduct{publicMobile(Stringname,floatprice){super(name,price);}@OverridepublicStringtoString(){returnname+price+RMB;}}packagecn.edu.nwsuaf.jp.p4.data;importcn.edu.nwsuaf.jp.p4.data.Product;publicclassMp3PlayerextendsProduct{publicintmemory;publicMp3Player(Stringname,intmemory,floatprice){super(name,price);this.memory=memory;}publicintgetMemory(){returnmemory;}publicvoidsetMemory(intmemory){this.memory=memory;}@OverridepublicStringtoString(){returnname+(+memory+MB),+price+RMB;}}packagecn.edu.nwsuaf.jp.p4;importjava.util.Arrays;importjavax.swing.JOptionPane;importcn.edu.nwsuaf.jp.p4.data.Mp3Player;importcn.edu.nwsuaf.jp.p4.data.Mobile;importcn.edu.nwsuaf.jp.p4.data.Product;publicclassStore{publicstaticvoidmain(String[]args){Mobilem1=newMobile(E365onChinaMobile,1780.0f);Mobilem2=newMobile(M330onChinaMobile,1450.0f);Mp3Playerp1=newMp3Player(MeizoX3,256,399.0f);Mp3Playerp2=newMp3Player(MeizoE5,512,580.0f);Mp3Playerp3=newMp3Player(XliveXMMp3Player,256,930.0f);Product[]ps={m1,m2,p1,p2,p3};Arrays.sort(ps);Stringtext=;for(inta=0;aps.length;a++){text+=ps[a].toString()+\n;}JOptionPane.showMessageDialog(null,Theproductsare:+\n\n+text+\n\n+Thereare+Product.getCount()+products.);}}实验题2用StringBuilertext=;替换Stringtext=;然后通过循环使用StringBuiler类的append方法向text中把数组中的成员(按价格有序)添加到text中,修改方法JOptionPane.showMessageDialog()的参数以显示text。运行结果如图3-1所示。图3-1思考问题:对比分析StringBuiler与String的区别。解:一个String对象的长度是固定的,不能改变它的内容,或者是附加新的字符至String对象中。您也许会使用+来串联字符串以达到附加新字符或字符串的目的,但+会产生一个新的String实例。而使用StringBuiler类中的append方法则是将新的字符串添加到已经开辟的内存中,不会增加新的内存来存放新增的字符串。使用StringBuiler类中的append方法效率比较高,并且使用StringBuiler类中的append方法可以完全替代String类。Storel类packagecn.edu.nwsuaf.jp.p4;importjava.util.Arrays;importjavax.swing.JOptionPane;importcn.edu.nwsuaf.jp.p4.data.Mp3Player;importcn.edu.nwsuaf.jp.p4.data.Mobile;importcn.edu.nwsuaf.jp.p4.data.Product;publicclassStore{publicstaticvoidmain(String[]args){Mobilem1=newMobile(E365onChinaMobile,1780.0f);Mobilem2=newMobile(M330onChinaMobile,1450.0f);Mp3Playerp1=newMp3Player(MeizoX3,256,399.0f);Mp3Playerp2=newMp3Player(MeizoE5,512,580.0f);Mp3Playerp3=newMp3Player(XliveXMMp3Player,256,930.0f);Product[]ps={m1,m2,p1,p2,p3};Arrays.sort(ps);//Stringtext=;StringBuildertext=newStringBuilder();for(inta=0;aps.length;a++){text.append(ps[a]+\n);}JOptionPane.showMessageDialog(null,Theproductsare:+\n\n+text+\n\n+Thereare+Product.getCount()+products.);}}实验题3从网上加载一个门户网站首页文件,用所学正则表达式知识,提取出其中所有邮箱地址。提示:读取文件的语句为BufferedReaderbr=newBufferedReader(newFileReader(d:\\shared\xxx.html));while((line=br.readLine())!=null)parse(line);基本要求:编写parse方法,完成上述功能。packagetext3.d3;importjava.io.BufferedReader;importjava.io.FileReader;importjava.io.IOException;importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassEmail{publicEmail(){}privatestaticvoidparse(Stringtext){Stringregex=[\\w[.-]+@[\\w[.-]]+\\.[\\w]+;Patternp=Pattern.compile(regex);Matcherm=p.matcher(text);while(m.find()){Stringstring=m.group();System.out.println(string);}}publicstaticvoidmain(String[]args){try{BufferedReaderreader=newBufferedReader(newFileReader(f:\\sina.htm));Stringtext=;while((text=reader.readLine())!=null){parse(text);}}catch(IOExceptione){e.printStackTrace();}}}实验题4修改实验二实验题4,在Product类中添加销售日期属性及销售额属性,在sell方法中对其初始化,比较Date类与Calender类的用法,最后使用SimpleDateFormat类对销售日期进行格式化;用类DecimalFormat的相关方法格式化属性sales,熟悉DecimalFormat的用法。运行结果如图3-2所示。packagecn.edu.nwsuaf.jp.p4.data;publicabstractclassProductimplementsComparableProduct{protectedStringname;protectedfloatprice;protectedstaticintcount;protectedProduct(Stringname,floatprice){this.name=name;this.price=price;++count;}publicStringgetName(){returnname;}publicfloatgetPrice(){returnprice;}publicstaticintgetCount(){returncount;}publicintcompareTo(Productproduct){returnnewFloat(product.getPrice()).compareTo(price);}}packagecn.edu.nwsuaf.jp.p4.data;publicclassMobileextendsProduct{publicMobile(Stringname,floatprice){super(name,price);}publicStringtoString(){returnname+price+RMB;}}packagecn.edu.nwsuaf.jp.p4.data;publicclassMp3PlayerextendsProduct{publicintmemory;publicMp3Player(Stringnam
本文标题:JAVA实验3
链接地址:https://www.777doc.com/doc-4514654 .html