实验三异常类与内部类

上传人:豆浆 文档编号:780404 上传时间:2017-05-14 格式:DOC 页数:16 大小:307.50KB
返回 下载 相关 举报
实验三异常类与内部类_第1页
第1页 / 共16页
实验三异常类与内部类_第2页
第2页 / 共16页
实验三异常类与内部类_第3页
第3页 / 共16页
实验三异常类与内部类_第4页
第4页 / 共16页
实验三异常类与内部类_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《实验三异常类与内部类》由会员分享,可在线阅读,更多相关《实验三异常类与内部类(16页珍藏版)》请在金锄头文库上搜索。

1、信 息 工 程 学 院 1 Java 程序设计 实验/实习报告学院:信息工程学院 班级:软件工程 123 姓名:刘杰 学号:2012013413 成绩:多线程异常处理1、实验/实习过程实验题 1 修改实验二实验题 4,声明一个局部变量 String text = ;然后通过循环把数组中的成员(有序)添加到 text 中,修改方法 JOptionPane.showMessageDialog();参数以显示text。实验过程:与上次实验中的实验四相同,只是添加一个局部变量 String text = ,赋值到 text 中,通过 JOptionPane.showMessageDialog();方法

2、输出。实验代码:Store 类:package cn.edu.nwsuaf.jp.p4;import java.util.Arrays;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p4.data.MP3Player;import cn.edu.nwsuaf.jp.p4.data.Mobile;import cn.edu.nwsuaf.jp.p4.data.Product;public class Store public static void main (String args) Mobile mobile1=new Mobi

3、le(E365 on China Mobile,1780.0f);Mobile mobile2 = 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 =mobile1,mobile2,pla

4、yer1,player2,player3;Arrays.sort(products);String text=;for (int a=0;a protected String name;protected float price;protected static int count; protected Product (String name,float price) this.name=name;this.price=price;+count;public String getName() return name;public float getPrice() return price;p

5、ublic static int getCount() return count;public int compareTo(Product product) return new Float(product.getPrice().compareTo(price);实验截图:实验题 2 用 StringBuiler text = ;替换 String text = ;然后通过循环 使用 StringBuiler类的 append 方法向 text 中把数组中的成员(按价格有序)添加到 text 中,修改方法JOptionPane.showMessageDialog()的参数以显示 text。运行

6、结果如图 3-1 所示。图 3-1思考问题:对比分析 StringBuiler 与 String 的区别。答:一个 String 对象的长度是固定的,不能改变它的内容,或者是附加新的字符至 String对象中。您也许会使用+来串联字符串以达到附加新字符或字符串的目的,但+会产生一个新的 String 实例。而使用 StringBuiler 类中的 append 方法则是将新的字符串添加到已经开辟的内存中,不会增加新的内存来存放新增的字符串。使用 StringBuiler 类中的 append 方法效率比较高,并且使用 StringBuiler 类中的 append 方法可以完全替代 Strin

7、g 类。实验过程:在前面代码中使用了 StringBuiler 类中的 append 方法。实验代码:(只展示 Store 类)Store 类:package cn.edu.nwsuaf.jp.p4;import java.util.Arrays;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p4.data.MP3Player;import cn.edu.nwsuaf.jp.p4.data.Mobile;import cn.edu.nwsuaf.jp.p4.data.Product;public class Store publi

8、c static void main (String args) Mobile mobile1=new Mobile(E365 on China Mobile,1780.0f);Mobile mobile2 = 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 MP3

9、Player,256,930.0f);Product products =mobile1,mobile2,player1,player2,player3;Arrays.sort(products);StringBuilder text=new StringBuilder();for (int a=0;a public String type;public float price;public static int count;public int salePrice;public String saleDate;public Product(String type, float price,

10、int salePrice)this.type = type;this.price = price;this.salePrice = salePrice;+count;public int compareTo(Product product)return new Float(product.price).compareTo(price);public String SalePrice()DecimalFormat formatSaleMoney = new DecimalFormat(sales: #,#,#0 ¥.);String result = formatSaleMoney.forma

11、t(salePrice);return result;public String SaleDate()Date nowTime = new Date();SimpleDateFormat matter = new SimpleDateFormat(date and time of sale: yyyy-MM-dd HH:mm:ss.S);return matter.format(nowTime);package cn.edu.nwsuaf.jp.p4.data;public class Mobile extends Product public static int mobileCount;p

12、ublic Mobile(String type, float price, int salePrice)super(type, price, salePrice);mobileCount+;public String toString()return type + : + price + RMB, + SaleDate() + SalePrice();package cn.edu.nwsuaf.jp.p4.data;public class Mp3Player extends Product public static int Mp3PlayerCount;public Mp3Player

13、(String type, float price, int salePrice)super(type, price, salePrice);Mp3PlayerCount+;public String toString()return type + : + price + RMB, + SaleDate() + SalePrice();package cn.edu.nwsuaf.jp.p4;import java.util.Arrays;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p4.data.*;public class S

14、tore public static void main(String args) / TODO Auto-generated method stubMobile mobile1 = new Mobile(E365, 1780f, 106800);Mobile mobile2 = new Mobile(m330, 1450f, 116000);Mp3Player player1 = new Mp3Player(Meizo X3, 399, 7980);Mp3Player player2 = new Mp3Player(Meizo E5, 580, 17400);Mp3Player player

15、3 = new Mp3Player(Xlive XM MP3 Play, 930, 46500);Product products=mobile1, mobile2, player1, player2, player3;Arrays.sort(products);StringBuilder text = new StringBuilder();for(int i = 0; i 100) throw new beyondException (成绩不能大于100!);public static void main (String args) int i=1;int passnum=0;int no

16、passnum=0;System.out.println(请输入学生成绩, 以“-1” 结束!);System.out.println(第 +i+位同学的成绩为:);SuppressWarnings(resource)Scanner reader = new Scanner(System.in);double sum=0;double x= reader.nextDouble();while (x!=-1) tryjudge(x);if(x100) passnum= passnum+1;sum+=x; catch(subException e1) System.out.println(e1);i-=1;catch(beyondExcep

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号