ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:94.27KB ,
资源ID:5084598      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5084598.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验05集合类与泛型.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

实验05集合类与泛型.docx

1、实验05集合类与泛型实验五 集合类与泛型1实验目的(1)掌握ArrayList类与LinkedList类的用法;(2)掌握TreeSet类的用法;(3)掌握Map接口及其实现类的用法(4)掌握泛型的用法。2实验内容实验题1 有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包cn.edu.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类, Product类实现Comparable接口,重写了Comparable接口中方法compareTo,实现了product对象按照价格排序。基本

2、要求:(1)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用ArrayList与LinkedList存放,最后用StringBuiler存放并将其输出。(2)用迭代器(iterator)将实例对象输出(要求用加强型for循环)。Mobile类package jp;public class Mobile extends Product public Mobile(String name, Float price) super(name, price); Override public void output(Product p) System.out.pri

3、ntln( Mobile.class.toString(); Mp3Player类package jp;public class Mp3Player extends Product public Mp3Player(String name, Float price) super(name, price); Override public void output(Product p) System.out.println( Mp3Player.class.toString(); Product类package jp;public abstract class Product implements

4、 Comparable private String Name; private Float price; public static int count = 0; public Product(String name, Float price) super(); Name = name; this.price = price; +count; public String getName() return Name; public void setName(String name) Name = name; public Float getPrice() return price; publi

5、c void setPrice(Float price) this.price = price; public static int getCount() return count; public static void setCount(int count) Product.count = count; Override public String toString() return getName() + , + getPrice() + RMB; public abstract void output(Product p); Override public int compareTo(O

6、bject o) / TODO Auto-generated method stub Product product = (Product) o; return new Float(getPrice().compareTo(product.getPrice(); public void sell(int i) / TODO Auto-generated method stub Store类package jpd;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import jp.Mobil

7、e;import jp.Mp3Player;import jp.Product;public class Store public static void main(String args) Mp3Player p1 = new Mp3Player(Meizo X3 (256MB), 399.0f); Mp3Player p2 = new Mp3Player(Meizo E5 (512MB), 580.0f); Mp3Player p3 = new Mp3Player(Xlive XM Mp3Play(256MB), 930.0f); Mobile m1 = new Mobile(E365 o

8、n China Mobile, 1780.0f); Mobile m2 = new Mobile(E3330 on China Mobile, 1450.0f); Product products = p1, p2, p3, m1, m2 ; Arrays.sort(products); ArrayList list = new ArrayList(); for (int i = 0; i products.length; i+) list.add(productsi); / 用StringBuilder输出 StringBuilder text = new StringBuilder();

9、for (Product product : products) text.append(product); text.append(n); System.out.println(用StringBuilder输出:); System.out.println(sales: + n + text + There are + Product.getCount() + products.); / 用迭代器输出 Iterator it = list.iterator(); System.out.println(用迭代器输出:); System.out.println(sales:); while (it

10、.hasNext() System.out.println(it.next(); System.out.println(There are + Product.getCount() + products.); 实验题2 项目包含类见实验题1。基本要求:(1)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用TreeSet存放,最后用StringBuiler存放并将其输出。(2)用迭代器(iterator)将实例对象输出。Mobile类package jp;public class Mobile extends Product public Mobile(St

11、ring name, Float price) super(name, price); Override public void output(Product p) System.out.println( Mobile.class.toString(); Mp3Player类package jp;public class Mp3Player extends Product public Mp3Player(String name, Float price) super(name, price); Override public void output(Product p) System.out

12、.println( Mp3Player.class.toString(); Product类package jp;public abstract class Product implements Comparable private String Name; private Float price; public static int count = 0; public Product(String name, Float price) super(); Name = name; this.price = price; +count; public String getName() retur

13、n Name; public void setName(String name) 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; Override public String toString() return getName(

14、) + , + getPrice() + RMB; public abstract void output(Product p); Override public int compareTo(Object o) / TODO Auto-generated method stub Product product = (Product) o; return new Float(getPrice().compareTo(product.getPrice(); public void sell(int i) / TODO Auto-generated method stub Store类package

15、 jpd;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import java.util.TreeSet;import jp.Mobile;import jp.Mp3Player;import jp.Product;public class Store public static void main(String args) Mp3Player p1 = new Mp3Player(Meizo X3 (256MB), 399.0f); Mp3Player p2 = new Mp3Play

16、er(Meizo E5 (512MB), 580.0f); Mp3Player p3 = new Mp3Player(Xlive XM Mp3Play(256MB), 930.0f); Mobile m1 = new Mobile(E365 on China Mobile, 1780.0f); Mobile m2 = new Mobile(E3330 on China Mobile, 1450.0f); Product products = p1, p2, p3, m1, m2 ; Arrays.sort(products); TreeSet mytree = new TreeSet(); f

17、or (int i = 0; i products.length; i+) mytree.add(productsi); StringBuilder text = new StringBuilder(); for (Product product : products) text.append(product); text.append(n); System.out.println(用StringBuilder输出:); System.out.println(sales: + n + text + There are + Product.getCount() + products.); Ite

18、rator it = mytree.iterator(); System.out.println(用迭代器输出: + n + sales:); while (it.hasNext() System.out.println(it.next(); System.out.println(There are + Product.getCount() + products.); 实验题3 项目包含类见实验题1。基本要求:(1)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用HashMap与TreeMap存放,最后用StringBuiler存放并将其输出。(2)用迭代

19、器(iterator)将实例对象输出。Mobile类package jp;public class Mobile extends Product public Mobile(String name, Float price) super(name, price); Override public void output(Product p) System.out.println( Mobile.class.toString(); Mp3Player类package jp;public class Mp3Player extends Product public Mp3Player(String

20、 name, Float price) super(name, price); Override public void output(Product p) System.out.println( Mp3Player.class.toString(); Product类package jp;public abstract class Product implements Comparable private String Name; private Float price; public static int count = 0; public Product(String name, Flo

21、at price) super(); Name = name; this.price = price; +count; public String getName() return Name; public void setName(String name) Name = name; public Float getPrice() return price; public void setPrice(Float price) this.price = price; public static int getCount() return count; public static void set

22、Count(int count) Product.count = count; Override public String toString() return getName() + , + getPrice() + RMB; public abstract void output(Product p); Override public int compareTo(Object o) / TODO Auto-generated method stub Product product = (Product) o; return new Float(getPrice().compareTo(pr

23、oduct.getPrice(); public void sell(int i) / TODO Auto-generated method stub Store类package jpd;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Iterator;import java.util.TreeMap;import jp.Mobile;import jp.Mp3Player;import jp.Product;public class Store pu

24、blic static void main(String args) Mp3Player p1 = new Mp3Player(Meizo X3 (256MB), 399.0f); Mp3Player p2 = new Mp3Player(Meizo E5 (512MB), 580.0f); Mp3Player p3 = new Mp3Player(Xlive XM Mp3Play(256MB), 930.0f); Mobile m1 = new Mobile(E365 on China Mobile, 1780.0f); Mobile m2 = new Mobile(E3330 on Chi

25、na Mobile, 1450.0f); Product products = p1, p2, p3, m1, m2 ; Arrays.sort(products); TreeMap map = new TreeMap(); for (int i = 0; i products.length; i+) map.put(productsi.getPrice(), productsi); StringBuilder text = new StringBuilder(); for (Product product : products) text.append(product); text.appe

26、nd(n); System.out.println(用StringBuilder输出:); System.out.println(sales: + n + text + There are + Product.getCount() + products.); Collection values = map.values();/ 得到全部的产品对象 Iterator it = values.iterator(); System.out.println(用迭代器输出: + n + sales:); while (it.hasNext() System.out.println(it.next();

27、System.out.println( There are + Product.getCount() + products.); *实验题4 有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包cn.edu.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类。基本要求:(1)设计比较器类ProductComparator类,ProductComparator类实现接口Comparator接口,重写其中compare(Object object1,Object object2)方法,实现了product对象按照名称排序。(2)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用HashMap与TreeMap存放,最后用StringBuiler存放并将其输出。(3)用迭代器(iterator)将实例对象输出。

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1