实验三 内部类常用类及异常类的用法文档格式.docx
《实验三 内部类常用类及异常类的用法文档格式.docx》由会员分享,可在线阅读,更多相关《实验三 内部类常用类及异常类的用法文档格式.docx(24页珍藏版)》请在冰豆网上搜索。
@Override
publicStringtoString(){
returngetName()+"
"
+getPrice()+"
RMB"
}
publicintcompareTo(Objecto){
Productproduct=(Product)o;
returnnewInteger(getPrice()).compareTo(product.getPrice());
publicabstractvoidoutput(Productp);
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
this.name=name;
publicintgetPrice(){
returnprice;
publicvoidsetPrice(intprice){
this.price=price;
privateStringname;
privateintprice;
publicProduct(){
}
publicProduct(Stringname,intprice){
super();
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
}
Mp3Player类
publicclassMp3PlayerextendsProduct{
publicMp3Player(Stringn,intp){
super(n,p);
publicvoidoutput(Productp)
{
System.out.println(Mp3Player.class.toString());
Mobile类
publicclassMobileextendsProduct{
publicvoidoutput(Productp)
System.out.println(Mobile.class.toString());
publicMobile(Stringn,intp)
{
super(n,p);
Store类
packagecn.edu.nwsuaf.jp.p4;
importcn.edu.nwsuaf.jp.p4.data.Mobile;
importjava.util.Arrays;
importcn.edu.nwsuaf.jp.p4.data.Product;
importcn.edu.nwsuaf.jp.p4.data.Mp3Player;
importjavax.swing.JOptionPane;
publicclassStore{
staticintcount=0;
Mp3Playerp1=newMp3Player("
MeizoX3(256MB)"
399);
Mp3Playerp2=newMp3Player("
MeizoE5(512MB)"
580);
Mp3Playerp3=newMp3Player("
XliveXMMp3Play(256MB)"
930);
Mobilem1=newMobile("
E365onChinaMobile"
1780);
Mobilem2=newMobile("
E3330onChinaMobile"
1450);
Product[]product1={p1,p2,p3,m1,m2};
Arrays.sort(product1);
Stringtext="
"
for(inti=product1.length-1;
i>
=0;
i--)
text+=product1[i]+"
\n"
count++;
}
JOptionPane.showMessageDialog(null,"
Theproductsare:
\n\n"
+text+"
\nThereare"
+count+"
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类。
Mp3Playerp1=newMp3Player("
Mp3Playerp3=newMp3Player("
Product[]product1={p1,p2,p3,m1,m2};
Arrays.sort(product1);
StringBuffertext=newStringBuffer();
text.append(product1[i]).append("
count++;
JOptionPane.showMessageDialog(null,"
\nThereare"
+count+"
实验题3从网上加载一个门户网站首页文件,用所学正则表达式知识,提取出其中所有邮箱地址。
提示:
读取文件的语句为
BufferedReaderbr=newBufferedReader(newFileReader("
d:
\\shared\xxx.html"
));
while((line=br.readLine())!
=null)
parse(line);
基本要求:
编写parse方法,完成上述功能。
Mail类
packageproj3;
importjava.io.BufferedReader;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassMail{
privatestaticvoidparse(Stringtext){
Stringregex="
[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"
Patternp=Ppile(regex);
Matcherm=p.matcher(text);
while(m.find()){
Stringresult=m.group();
System.out.println(result);
}
try{
BufferedReaderbr=newBufferedReader(newFileReader("
\\1.txt"
Stringtext="
while((text=br.readLine())!
=null){
parse(text);
}catch(F