实验三 +五.docx

上传人:b****1 文档编号:2740927 上传时间:2023-05-04 格式:DOCX 页数:37 大小:663.75KB
下载 相关 举报
实验三 +五.docx_第1页
第1页 / 共37页
实验三 +五.docx_第2页
第2页 / 共37页
实验三 +五.docx_第3页
第3页 / 共37页
实验三 +五.docx_第4页
第4页 / 共37页
实验三 +五.docx_第5页
第5页 / 共37页
实验三 +五.docx_第6页
第6页 / 共37页
实验三 +五.docx_第7页
第7页 / 共37页
实验三 +五.docx_第8页
第8页 / 共37页
实验三 +五.docx_第9页
第9页 / 共37页
实验三 +五.docx_第10页
第10页 / 共37页
实验三 +五.docx_第11页
第11页 / 共37页
实验三 +五.docx_第12页
第12页 / 共37页
实验三 +五.docx_第13页
第13页 / 共37页
实验三 +五.docx_第14页
第14页 / 共37页
实验三 +五.docx_第15页
第15页 / 共37页
实验三 +五.docx_第16页
第16页 / 共37页
实验三 +五.docx_第17页
第17页 / 共37页
实验三 +五.docx_第18页
第18页 / 共37页
实验三 +五.docx_第19页
第19页 / 共37页
实验三 +五.docx_第20页
第20页 / 共37页
亲,该文档总共37页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

实验三 +五.docx

《实验三 +五.docx》由会员分享,可在线阅读,更多相关《实验三 +五.docx(37页珍藏版)》请在冰点文库上搜索。

实验三 +五.docx

实验三+五

实验三面向对象编程

(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){

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

Persona=list.get(i);

a.print();

}

}

}

classPerson{

publicStringPname;

publicintPnum;

publicPerson(Stringpname,in

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

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2