实验05集合类与泛型Word格式文档下载.docx

上传人:b****6 文档编号:18045809 上传时间:2022-12-13 格式:DOCX 页数:14 大小:94.27KB
下载 相关 举报
实验05集合类与泛型Word格式文档下载.docx_第1页
第1页 / 共14页
实验05集合类与泛型Word格式文档下载.docx_第2页
第2页 / 共14页
实验05集合类与泛型Word格式文档下载.docx_第3页
第3页 / 共14页
实验05集合类与泛型Word格式文档下载.docx_第4页
第4页 / 共14页
实验05集合类与泛型Word格式文档下载.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

实验05集合类与泛型Word格式文档下载.docx

《实验05集合类与泛型Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《实验05集合类与泛型Word格式文档下载.docx(14页珍藏版)》请在冰豆网上搜索。

实验05集合类与泛型Word格式文档下载.docx

publicclassMobileextendsProduct

{

publicMobile(Stringname,Floatprice){

super(name,price);

}

@Override

publicvoidoutput(Productp){

System.out.println(Mobile.class.toString());

}

}

Mp3Player 

publicclassMp3PlayerextendsProduct

publicMp3Player(Stringname,Floatprice){

System.out.println(Mp3Player.class.toString());

}

Product类

publicabstractclassProductimplementsComparable{

privateStringName;

privateFloatprice;

publicstaticintcount=0;

publicProduct(Stringname,Floatprice){

super();

Name=name;

this.price=price;

++count;

publicStringgetName(){

returnName;

publicvoidsetName(Stringname){

publicFloatgetPrice(){

returnprice;

publicvoidsetPrice(Floatprice){

publicstaticintgetCount(){

returncount;

publicstaticvoidsetCount(intcount){

Product.count=count;

publicStringtoString(){

returngetName()+"

"

+getPrice()+"

RMB"

;

publicabstractvoidoutput(Productp);

publicintcompareTo(Objecto){

//TODOAuto-generatedmethodstub

Productproduct=(Product)o;

returnnewFloat(getPrice()).compareTo(product.getPrice());

publicvoidsell(inti){

Store类

packagejpd;

importjava.util.ArrayList;

importjava.util.Arrays;

importjava.util.Iterator;

importjp.Mobile;

importjp.Mp3Player;

importjp.Product;

publicclassStore{

publicstaticvoidmain(String[]args){

Mp3Playerp1=newMp3Player("

MeizoX3(256MB)"

399.0f);

Mp3Playerp2=newMp3Player("

MeizoE5(512MB)"

580.0f);

Mp3Playerp3=newMp3Player("

XliveXMMp3Play(256MB)"

930.0f);

Mobilem1=newMobile("

E365onChinaMobile"

1780.0f);

Mobilem2=newMobile("

E3330onChinaMobile"

1450.0f);

Product[]products={p1,p2,p3,m1,m2};

Arrays.sort(products);

ArrayList<

Product>

list=newArrayList<

();

for(inti=0;

i<

products.length;

i++){

list.add(products[i]);

}

//用StringBuilder输出

StringBuildertext=newStringBuilder();

for(Productproduct:

products){

text.append(product);

text.append("

\n"

);

System.out.println("

用StringBuilder输出:

"

sales:

+"

+text+"

Thereare"

+Product.getCount()+"

products."

//用迭代器输出

Iterator<

it=list.iterator();

用迭代器输出:

while(it.hasNext()){

System.out.println(it.next());

+Product.getCount()+"

实验题2项目包含类见实验题1。

(1)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用TreeSet存放,最后用StringBuiler存放并将其输出。

(2)用迭代器(iterator)将实例对象输出。

importjava.util.TreeSet;

TreeSet<

mytree=newTreeSet<

mytree.add(products[i]);

it=mytree.iterator();

实验题3项目包含类见实验题1。

(1)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用HashMap与TreeMap存放,最后用StringBuiler存放并将其输出。

importjava.util.Collection;

importjava.util.TreeMap;

TreeMap<

Float,Product>

map=newTreeMap<

map.put(products[i].getPrice(),products[i]);

Collection<

values=map.values();

//得到全部的产品对象

it=values.iterator();

System.out.println("

*实验题4有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包cn.edu.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类。

(1)设计比较器类ProductComparator类,ProductComparator类实现接口Comparator接口,重写其中compare(Objectobject1,Objectobject2)方法,实现了product对象按照名称排序。

(2)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用HashMap与TreeMap存放,最后用StringBuiler存放并将其输出。

(3)用迭代器(iterator)将实例对象输出。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 军事

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

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