s[i].draw();
}
}
实验题2有两个类:
MobileManagement和Mobile,分别描述如图3.7所示两部手机名称及价格,类MobileManagement在包cn.edu.nwsuaf.jp.p3中,而Mobile在包cn.edu.nwsuaf.jp.p3.data中。
它们代码如下。
运行MobileManagement.java,你应该看到如图3.8所示结果。
[基本要求]在空白处填写相关代码并修改上面程序,使程序能够显示两部手机的价格和数量,运行结果如图3.9所示。
E365,1780RMBM330,1450RMB
图3.4手机及价格图
图3.5运行结果图
程序Mobile.java源代码:
publicclassMobile{
/**Holdsthenameofthemobile.*/
privateStringname;
/**Holdsthepriceofthemobile.*/
privatefloatprice;
/**Createsanewmobileobject.*/
publicMobile(Stringname,floatprice){
this.name=name;
this.price=price;
}
/**Getsthenameofthemobile.*/
publicStringgetName(){
returnname;
}
/**Getsthepriceofthemobile.*/
publicfloatgetPrice(){
returnprice;
}
}
程序MobileManagement.java源代码:
importjavax.swing.JOptionPane;
publicclassMobileManagement{
/**Definestheentrypointoftheapplication.*/
publicstaticvoidmain(String[]args){
//Createstwomobilephoneobjects.
Mobilemobile1=newMobile("E365",1780);
Mobilemobile2=newMobile("M330",1450);
//Displaysthetwomobilephonesinadialogbox.
JOptionPane.showMessageDialog(null,"Mobilephones:
\n\n"+mobile1.getName()+"\n"+mobile2.getName());
}
}
图3.6运行结果图
。
packagecn.edu.nwsuaf.jp.p3.data;
publicclassMobile{
publicStringname;
publicfloatprice;
publicMobile(Stringname,floatprice){
this.name=name;
this.price=price;
}
publicStringgetName(){
returnname;
}
publicfloatgetPrice(){
returnprice;
}
}
packagecn.edu.nwsuaf.jp.p3;
importjavax.swing.JOptionPane;
importcn.edu.nwsuaf.jp.p3.data.Mobile;
publicclassMobileManagement{
/**Definestheentrypointoftheapplication.*/
publicstaticvoidmain(String[]args){
//Createstwomobilephoneobjects.
Mobilemobile1=newMobile("E365",1780);
Mobilemobile2=newMobile("M330",1450);
//Displaysthetwomobilephonesinadialogbox.
JOptionPane.showMessageDialog(null,"Mobilephones:
\n\n"+mobile1.getName()+"\n"+mobile2.getName());
JOptionPane.showMessageDialog(null,"Mobilephones:
\n\n"+mobile1.getName()+mobile1.getPrice()+"\n"+mobile2.getName()+mobile2.getPrice());
}
}
实验题3有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包cn.edu.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类,Product和Store代码如下:
Store.java源代码:
packagecn.edu.nwsuaf.jp.p4;
importjava.util.Arrays;
importjavax.swing.JOptionPane;
importcn.edu.nwsuaf.jp.p4.data.Mobile;
importcn.edu.nwsuaf.jp.p4.data.Mp3Player;
importcn.edu.nwsuaf.jp.p4.data.Product;
publicclassStore{
//**Definestheentrypointoftheapplication.*/
publicstaticvoidmain(String[]args){
//Createstwomobilephoneobjects.
Mobilemobile1=newMobile("ChinaMobile","E365",1780);
Mobilemobile2=newMobile("ChinaMobile","M330",1450);
Mp3Playerplayer1=newMp3Player("MeizoX3",256,399);
Mp3Playerplayer2=newMp3Player("MeizoE5",512,580);
Mp3Playerplayer3=newMp3Player("XliveXMMP3Play",
256,930);
Product[]products={mobile1,mobile2,player1,player2,player3};
Arrays.sort(products);
Stringtext="";
for(intindex=0;indextext+=products[index].toString()+"\n";
//Displaysthetwomobilephonesinadialogbox.
JOptionPane.showMessageDialog(null,"Theproducts
are:
\n\n"+text+"\nThereare"+Product.getCount()
+"products.");
}
}
Product.java源代码:
packagecn.edu.nwsuaf.jp.p4.data;
publicabstractclassProductimplementsComparable{
/**Holdsthenameoftheproduct.*/
protectedStringname;
/**Holdsthepriceoftheproduct.*/
protectedfloatprice;
/**Holdsthenumberofproducts.*/
protectedstaticintcount;
/**Createanewproductobject.*/
protectedProduct(Stringname,floatprice){
this.name=name;
this.price=price;
++count;
}
/**Getsthenameoftheproduct.*/
publicStringgetName(){
returnname;
}
/**Getsthepriceoftheproduct.*/
publicfloatgetPrice(){
returnprice;
}
/**Getsthenumberofproducts.*/
publicstaticintgetCount(){
returncount;
}
/**Comparesthisproductwiththegivenproduct.*/
publicintcompareTo(Productproduct){
returnnewFloat(product.getPrice()).compareTo(price);
}
}
[基本要求]设计类Mobile和类MP3Player,使它们和类Product、Store组成一个完整的程序,且运行结果如图3.10所示。
图3.7运行结果图
packagecn.edu.nwsuaf.jp.p4.data;
publicabstractclassProductimplementsComparable{
protectedStringname;
protectedfloatprice;
protectedstaticintcount;
protectedProduct(Stringname,floatprice){
this.name=name;
this.price=price;
++count;
}
/**Getsthenameoftheproduct.*/
publicStringgetName(){
returnname;
}
/**Getsthepriceoftheproduct.*/
publicfloatgetPrice(){
returnprice;
}
/**Getsthenumberofproducts.*/
publicstaticintgetCount(){
returncount;
}
/**Comparesthisproductwiththegivenproduct.*/
publicintcompareTo(Productproduct){
returnnewFloat(product.getPrice()).compareTo(price);
}
}
packagecn.edu.nwsuaf.jp.p4.data;
importcn.edu.nwsuaf.jp.p4.data.Product;
publicclassMobileextendsProduct{
publicStringtype;
publicMobile(Stringtype,Stringname,floatprice){
super(name,price);
this.type=type;
}
publicStringgetType(){
returnthis.type;
}
publicStringtostring(){
returntype+"on"+name+","+price+"RMB";
}
}
packagecn.edu.nwsuaf.jp.p4.data;
publicclassMP3PlayerextendsProduct{
publicintmemory;
publicMP3Player(Stringname,intmemory,floatprice){
super(name,price);
this.memory=memory;
}
publicintgetMemory(){
returnthis.memory;
}
publicStringtoString(){
returnname+"("+memory+"MB),"+price+"RMB";
}
}
packagecn.edu.nwsuaf.jp.p4;
importjava.util.Arrays;
importjavax.swing.JOptionPane;
importcn.edu.nwsuaf.jp.p4.data.Mobile;
importcn.edu.nwsuaf.jp.p4.data.MP3Player;
importcn.edu.nwsuaf.jp.p4.data.Product;
publicclassStore{
//**Definestheentrypointoftheapplication.*/
publicstaticvoidmain(String[]args){
//Createstwomobilephoneobjects.
Mobilemobile1=newMobile("ChinaMobile","E365",1780);
Mobilemobile2=newMobile("ChinaMobile","M330",1450);
MP3Playerplayer1=newMP3Player("MeizoX3",256,399);
MP3Playerplayer2=newMP3Player("MeizoE5",512,580);
MP3Playerplayer3=newMP3Player("XliveXMMP3Play",
256,930);
Product[]products={mobile1,mobile2,player1,player2,player3};
Arrays.sort(products);
Stringtext="";
for(intindex=0;indextext+=products[index].toString()+"\n";
//Displaysthetwomobilephonesinadialogbox.
JOptionPane.showMessageDialog(null,"Theproductsare:
\n\n"+text+"\nThereare"+Product.getCount()+"products.");
}
}
实验题4用LIST存放对象。
利用面向对象的思想,创建以下类:
Person类,包含Person的姓名和身份证号码。
Student类,首先是一个Person,除此之外,包含学生的语文、数学、英文课的成绩。
Teacher类,首先是一个Person,除此之外,包含教师的工资。
①请创建
姓名张三
身份证号12310000
的Person对象。
②请创建
姓名"李四
身份证号12320000
语文:
89
数学:
93
英文:
94
的Student对象。
③请创建
姓名王五
身份证号12330000
工资:
3000
的Teacher对象。
④将这些对象存放在List中,并打印出List中存放的内容。
importjava.util.*;
publicclassMylist{
publicstaticvoidmain(String[]args){
LinkedListlist=newLinkedList();
Personp1=newPerson("张三",12310000);
Studentp2=newStudent("李四",12320000,89,93,94);
Teacherp3=newTeacher("王五",12330000,3000);
list.add(p1);
list.add(p2);
list.add(p3);
for(inti=0;iPersona=list.get(i);
a.print();
}
}
}
classPerson{
publicStringPname;
publicintPnum;
publicPerson(Stringpname,in