实验05集合类与泛型.docx

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

实验05集合类与泛型.docx

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

实验05集合类与泛型.docx

实验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对象按照价格排序。

基本要求:

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

(2)用迭代器(iterator)将实例对象输出(要求用加强型for循环)。

Mobile类

packagejp;

publicclassMobileextendsProduct

{

publicMobile(Stringname,Floatprice){

super(name,price);

}

@Override

publicvoidoutput(Productp){

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

}

}

Mp3Player 类

packagejp;

publicclassMp3PlayerextendsProduct

{

publicMp3Player(Stringname,Floatprice){

super(name,price);

}

@Override

publicvoidoutput(Productp){

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

}

}

Product类

packagejp;

publicabstractclassProductimplementsComparable{

privateStringName;

privateFloatprice;

publicstaticintcount=0;

publicProduct(Stringname,Floatprice){

super();

Name=name;

this.price=price;

++count;

}

publicStringgetName(){

returnName;

}

publicvoidsetName(Stringname){

Name=name;

}

publicFloatgetPrice(){

returnprice;

}

publicvoidsetPrice(Floatprice){

this.price=price;

}

publicstaticintgetCount(){

returncount;

}

publicstaticvoidsetCount(intcount){

Product.count=count;

}

@Override

publicStringtoString(){

returngetName()+","+getPrice()+"RMB";

}

publicabstractvoidoutput(Productp);

@Override

publicintcompareTo(Objecto){

//TODOAuto-generatedmethodstub

Productproduct=(Product)o;

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

}

publicvoidsell(inti){

//TODOAuto-generatedmethodstub

}

}

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);

ArrayListlist=newArrayList();

for(inti=0;i

list.add(products[i]);

}

//用StringBuilder输出

StringBuildertext=newStringBuilder();

for(Productproduct:

products){

text.append(product);

text.append("\n");

}

System.out.println("用StringBuilder输出:

");

System.out.println("sales:

"+"\n"+text+"Thereare"

+Product.getCount()+"products.");

//用迭代器输出

Iteratorit=list.iterator();

System.out.println("用迭代器输出:

");

System.out.println("sales:

");

while(it.hasNext()){

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

}

System.out.println("Thereare"+Product.getCount()+"products.");

}

}

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

基本要求:

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

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

Mobile类

packagejp;

publicclassMobileextendsProduct

{

publicMobile(Stringname,Floatprice){

super(name,price);

}

@Override

publicvoidoutput(Productp){

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

}

}

Mp3Player 类

packagejp;

publicclassMp3PlayerextendsProduct

{

publicMp3Player(Stringname,Floatprice){

super(name,price);

}

@Override

publicvoidoutput(Productp){

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

}

}

Product类

packagejp;

publicabstractclassProductimplementsComparable{

privateStringName;

privateFloatprice;

publicstaticintcount=0;

publicProduct(Stringname,Floatprice){

super();

Name=name;

this.price=price;

++count;

}

publicStringgetName(){

returnName;

}

publicvoidsetName(Stringname){

Name=name;

}

publicFloatgetPrice(){

returnprice;

}

publicvoidsetPrice(Floatprice){

this.price=price;

}

publicstaticintgetCount(){

returncount;

}

publicstaticvoidsetCount(intcount){

Product.count=count;

}

@Override

publicStringtoString(){

returngetName()+","+getPrice()+"RMB";

}

publicabstractvoidoutput(Productp);

@Override

publicintcompareTo(Objecto){

//TODOAuto-generatedmethodstub

Productproduct=(Product)o;

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

}

publicvoidsell(inti){

//TODOAuto-generatedmethodstub

}

}

Store类

packagejpd;

importjava.util.ArrayList;

importjava.util.Arrays;

importjava.util.Iterator;

importjava.util.TreeSet;

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);

TreeSetmytree=newTreeSet();

for(inti=0;i

mytree.add(products[i]);

}

StringBuildertext=newStringBuilder();

for(Productproduct:

products){

text.append(product);

text.append("\n");

}

System.out.println("用StringBuilder输出:

");

System.out.println("sales:

"+"\n"+text+"Thereare"

+Product.getCount()+"products.");

Iteratorit=mytree.iterator();

System.out.println("用迭代器输出:

"+"\n"+"sales:

");

while(it.hasNext()){

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

}

System.out.println("Thereare"+Product.getCount()+"products.");

}

}

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

基本要求:

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

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

Mobile类

packagejp;

publicclassMobileextendsProduct

{

publicMobile(Stringname,Floatprice){

super(name,price);

}

@Override

publicvoidoutput(Productp){

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

}

}

Mp3Player 类

packagejp;

publicclassMp3PlayerextendsProduct

{

publicMp3Player(Stringname,Floatprice){

super(name,price);

}

@Override

publicvoidoutput(Productp){

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

}

}

Product类

packagejp;

publicabstractclassProductimplementsComparable{

privateStringName;

privateFloatprice;

publicstaticintcount=0;

publicProduct(Stringname,Floatprice){

super();

Name=name;

this.price=price;

++count;

}

publicStringgetName(){

returnName;

}

publicvoidsetName(Stringname){

Name=name;

}

publicFloatgetPrice(){

returnprice;

}

publicvoidsetPrice(Floatprice){

this.price=price;

}

publicstaticintgetCount(){

returncount;

}

publicstaticvoidsetCount(intcount){

Product.count=count;

}

@Override

publicStringtoString(){

returngetName()+","+getPrice()+"RMB";

}

publicabstractvoidoutput(Productp);

@Override

publicintcompareTo(Objecto){

//TODOAuto-generatedmethodstub

Productproduct=(Product)o;

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

}

publicvoidsell(inti){

//TODOAuto-generatedmethodstub

}

}

Store类

packagejpd;

importjava.util.ArrayList;

importjava.util.Arrays;

importjava.util.Collection;

importjava.util.Iterator;

importjava.util.TreeMap;

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);

TreeMapmap=newTreeMap();

for(inti=0;i

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

}

StringBuildertext=newStringBuilder();

for(Productproduct:

products){

text.append(product);

text.append("\n");

}

System.out.println("用StringBuilder输出:

");

System.out.println("sales:

"+"\n"+text+"Thereare"

+Product.getCount()+"products.");

Collectionvalues=map.values();//得到全部的产品对象

Iteratorit=values.iterator();

System.out.println("用迭代器输出:

"+"\n"+"sales:

");

while(it.hasNext()){

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

}

System.out.println("Thereare"+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(Objectobject1,Objectobject2)方法,实现了product对象按照名称排序。

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

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

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

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

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

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