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