实验三 +五.docx
- 文档编号:2740927
- 上传时间:2023-05-04
- 格式:DOCX
- 页数:37
- 大小:663.75KB
实验三 +五.docx
《实验三 +五.docx》由会员分享,可在线阅读,更多相关《实验三 +五.docx(37页珍藏版)》请在冰点文库上搜索。
实验三+五
实验三面向对象编程
(2)
1.实验目的
(1)掌握Java中的继承机制及包(package)、接口(interface)等的设计方法。
(2)掌握static、this、super等关键字的使用。
2.实验内容
实验题1有图形类的父类Shape,参照圆Circle类补充完整正方性Square和三角形Triangle类,并分析运行结果。
classShape{
voiddraw(){}
voiderase(){}
}
classCircleextendsShape{
voiddraw(){System.out.println("Circle.draw()");}
voiderase(){System.out.println("Circle.erase()");}
}
classSquareextendsShape{
voiddraw(){}
voiderase(){}
}
classTriangleextendsShape{
voiddraw(){}
voiderase(){}
}
publicclassShapes{
publicstaticShaperandShape(){
switch((int)(Math.random()*3)){
default:
//Toquietthecompiler
case0:
returnnewCircle();
case1:
returnnewSquare();
case2:
returnnewTriangle();
}
}
publicstaticvoidmain(String[]args){
Shape[]s=newShape[9];
//Fillupthearraywithshapes:
for(inti=0;i s[i]=randShape(); //Makepolymorphicmethodcalls: for(inti=0;i s[i].draw(); } } classShape{ voiddraw(){ System.out.println("Shape.draw()"); } voiderase(){ System.out.println("Shape.draw()"); } } classCircleextendsShape{ voiddraw(){ System.out.println("Circle.draw()"); } voiderase(){ System.out.println("Circle.erase()"); } } classSquareextendsShape{ voiddraw(){ System.out.println("Square.draw()"); } voiderase(){ System.out.println("Square.erase()"); } } classTriangleextendsShape{ voiddraw(){ System.out.println("Rriangle.draw()"); } voiderase(){ System.out.println("Triangle.erase()"); } } publicclassShapes{ publicstaticShaperandShape(){ switch((int)(Math.random()*3)){ default: //Toquietthecompiler case0: returnnewCircle(); case1: returnnewSquare(); case2: returnnewTriangle(); } } publicstaticvoidmain(String[]args){ Shape[]s=newShape[9]; //Fillupthearraywithshapes: for(inti=0;i s[i]=randShape(); //Makepolymorphicmethodcalls: for(inti=0;i 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;index text+=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;index text+=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){ LinkedList 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;i Persona=list.get(i); a.print(); } } } classPerson{ publicStringPname; publicintPnum; publicPerson(Stringpname,in
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验三 +五 实验