河北工业大学java实验三报告.docx

上传人:b****5 文档编号:28881051 上传时间:2023-07-20 格式:DOCX 页数:18 大小:135.62KB
下载 相关 举报
河北工业大学java实验三报告.docx_第1页
第1页 / 共18页
河北工业大学java实验三报告.docx_第2页
第2页 / 共18页
河北工业大学java实验三报告.docx_第3页
第3页 / 共18页
河北工业大学java实验三报告.docx_第4页
第4页 / 共18页
河北工业大学java实验三报告.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

河北工业大学java实验三报告.docx

《河北工业大学java实验三报告.docx》由会员分享,可在线阅读,更多相关《河北工业大学java实验三报告.docx(18页珍藏版)》请在冰豆网上搜索。

河北工业大学java实验三报告.docx

河北工业大学java实验三报告

实验三面向对象程序设计

实验目的:

通过编程和上机实验理解Java语言是如何体现面向对象编程基本思想,熟悉类的封装方法以及如何创建类和对象,熟悉成员变量和成员方法的特性,熟悉类的继承性和多态性的作用,熟悉包、接口的使用方法,掌握OOP方式进行程序设计的方法。

实验要求:

1、编写程序实现类的定义和使用。

2、编写不同成员和不同成员方法修饰方法的程序。

3、编写体现类的继承性(成员变量、成员方法、成员变量隐藏)的程序和多态性(成员方法重载、构造方法重载)的程序。

实验内容:

1、下面给出了一个矩形类Rectangle,其中含有多个构造方法。

上机编译并运行此程序,观察运行结果,体会其中不同构造方法的设计和使用。

classRectangle

{

privateintwidth;

privateintlength;

Rectangle()

{

length=30;

width=20;

}

Rectangle(intl,intw)

{

length=l;

width=w;

}

Rectangle(Rectangler)

{

width=r.getWidth();

length=r.getLength();

}

intgetWidth()

{

returnwidth;

}

intgetLength()

{

returnlength;

}

}

publicclassCRctngle

{

publicstaticvoidmain(Stringarg[])

{

Rectanglex1=newRectangle();

Rectanglex2=newRectangle(50,40);

Rectanglex3=newRectangle(x1);

System.out.println(x1.getLength());

System.out.println(x1.getWidth());

System.out.println(x2.getLength());

System.out.println(x2.getWidth());

System.out.println(x3.getLength());

System.out.println(x3.getWidth());

}

}

2、程序功能:

通过两个类StaticDemo、TestDemo说明静态变量/方法与实例变量/方法的区别,程序源代码如下。

classStaticDemo

{

staticintx;

inty;

publicstaticintgetX()

{

returnx;

}

publicstaticvoidsetX(intnewX)

{

x=newX;

}

publicintgetY()

{

returny;

}

publicvoidsetY(intnewY)

{

y=newY;

}

}

publicclassTestDemo

{

publicstaticvoidmain(String[]args)

{

StaticDemoa=newStaticDemo();

StaticDemob=newStaticDemo();

System.out.println("静态变量x="+StaticDemo.getX());

System.out.println("实例变量x="+b.getY());

a.setX

(1);

a.setY

(2);

b.setX(3);

b.setY(4);

System.out.println("静态变量=a.x"+StaticDemo.getX());

System.out.println("实例变量=a.y"+a.getY());

System.out.println("静态变量=b.x"+StaticDemo.getX());

System.out.println("实例变量=b.y"+b.getY());

}

}

二、继承和多态的作用

1、编译并运行下面的程序,观察分析运行结果,体会程序中super和this的用法,进一步理解变量隐藏和方法重写的概念。

classSuperClss

{

intx;

SuperClss()

{

x=10;

}

voiddoClss()

{

System.out.println("SuperClss.doClass()");

}

}

classSubClssextendsSuperClss

{

intx;

SubClss()

{

super();

x=100;

}

voiddoClss()

{

System.out.println("subClss.doCLss()");

}

voiddoDemo()

{

intx;

x=1000;

super.doClss();

doClss();

System.out.println("super.x="+super.x);

System.out.println("this.x="+this.x);

System.out.println("x="+x);

}

}

publicclassSuper

{

publicstaticvoidmain(Stringarg[])

{

SubClsss=newSubClss();

s.doDemo();

}

}

2、编译并运行下面的程序,分析运行结果,体会其中方法重载的用法,进一步理解方法重载的概念。

classFather

{voidspeak()

{System.out.println("IamFather!

");}

voidspeak(Strings)

{System.out.println("Ilike"+""+s+".");}

}

publicclassOverLoadingDemo

{publicstaticvoidmain(Stringargs[])

{Fatherx=newFather();

x.speak();

x.speak("music");

}

}

三、接口的定义和使用

某个类为接口中的抽象方法书写语句并定义实在的方法体,称为该类实现可这个接口。

示例如下:

publicclassInterfaceTest

{publicstaticvoidmain(Stringargs[])

{doublex;

Circley=newCircle(2.0);

x=y.calculate_area();

System.out.println("\ntheareais:

"+x+"\n");

}

}

interfaceCal_area

{doublePI=3.14;

doublecalculate_area();

}

classCircleimplementsCal_area

{doubler;

Circle(doubler)

{this.r=r;}

publicdoublecalculate_area()

{returnPI*r*r;}

}

四、包的定义和使用

创建自定义包Mypackage

在包中创建类

编写使用包Mypackage中Test_YMD类的程序

运行结果:

五、编程题,编写程序并写出运行结果

1、创建一个桌子Table类,该类中有桌子名称、重量、桌面宽度、长度及桌子高度属性。

其中有:

(1)构造函数初始化所有数据成员;

(2)Area():

计算桌面的面积;

(3)Display():

在屏幕上输出所有数据成员的值;

(4)ChangeWeight(int):

改变桌子重量的函数;

(5)在main()中实现创建一个桌子对象,计算桌面的面积,改变桌子重量,并在屏幕上输出所有桌子数据成员的值。

classTable{

publicStringname;

publicintweight,X,Y,high;

publicTable(Stringnwname,intnwweight,intnwX,intnwY,intnwhigh)

{

this.name=nwname;

this.weight=nwweight;

this.X=nwX;

this.Y=nwY;

this.high=nwhigh;

}

publicintArea(intX,intY)

{

intArea=X*Y;

returnArea;

}

publicvoidChangeWeight(intNewWeight)

{

weight=NewWeight;

}

publicvoidDisplay()

{

System.out.println("TheTable'sName:

Name="+name);

System.out.println("TheTable'sSize:

X="+X+",Y="+Y);

System.out.println("TheTable'sArea:

Area="+Area(X,Y));

System.out.println("TheTable'sWeight:

Weight="+weight);

System.out.println("TheTable'sHigh:

High="+high);

}

publicstaticvoidmain(Stringarg[])

{

System.out.println("NowDisplayMyTable'sInformation:

");

Tabletb=newTable("Mytable",170,10,15,75);

tb.Display();

System.out.println("NowChangeMyTable'sWeightto200!

");

tb.ChangeWeight(200);

System.out.println("NowDisplayMyTable'sNewInformation:

");

tb.Display();

}

}

结果:

2、创建一个名称为Pay的类,该类包括工作小时、每小时工资、扣缴率、应得工资总额和实付工资等5个双精度型的成员变量。

创建3个重载的应得工资computeNetPay()方法。

应得工资是工时乘以每小时工资的计算结果。

当computeNetPay()接收代表小时、扣缴率和工资率的数值时,计算出应得工资=工作小时*每小时工资*(1—扣缴率)*(1—工资率)。

当computeNetPay()接收两个参数时,扣缴率假定为15%,计算出应得工资=工作小时*每小时工资*(1—0.15)*(1—工资率)

当computeNetPay()接收一个参数时,扣缴率假定为15%,每小时工资率为4.65%。

同时编写一个测试类,该测试类的main方法测试所有3个重载的方法。

classPay

{

doublework_hour,price=100,deduct_rate;

doubleshould_pay,real_pay=0;

publicdoublecomputeNetPay(doublework_hour)

{

should_pay=work_hour*this.price*(1-0.15)*(1-0.0465);

returnshould_pay;

}

publicdoublecomputeNetPay(doublework_hour,doubleprice_rate)

{

should_pay=work_hour*this.price*(1-0.15)*(1-price_rate);

returnshould_pay;

}

publicdoublecomputeNetPay(doublework_hour,doubleprice_rate,double

deduct_rate)

{

should_pay=work_hour*this.price*(1-deduct_rate)*(1-price_rate);

returnshould_pay;

}

publicvoiddisplay()

{

System.out.println("Theemployeeshouldbepay$"+should_pay);

}

publicstaticvoidmain(Stringargs[])

{

Paypay=newPay();

System.out.println("Thefirstemployeeworked8hour");

puteNetPay(8.0);

pay.display();

System.out.println("Thefirstemployeeworked8hourandprice_rate8%");

puteNetPay(8.0,0.08);

pay.display();

System.out.println("Thefirstemployeeworked8hourandprice_rate10%anddeduct_rateis18%");

puteNetPay(8.0,0.1,0.18);

pay.display();

}

}结果:

3、商店销售某一件商品,商店每天公布统一的折扣(discount)。

同时允许销售人员在销售时灵活掌握价格(price),在统一折扣的基础上,对一次购入10件以上者,还可以销售9.5折优惠。

现已知当天5名售货员的销售情况为:

售货员编号(num)销售件数(quantity)销售单价(price)

1013126.8

2218125.6

32510124.8

10845123.4

901100121.5

编写销售商品类Sale和含有main方法的公共类Test,计算当天此商品的总销售额sum,以及每件商品的平均售价,并在显示器上显示。

(3)定义接口Shape及其抽象方法getArea()和getPerimeter()用于计算图形和面积和周长。

定义类Rectangle(矩形)、类Circle(圆形)、类Triangle(三角形),要求这些类继承点类Coordinates()并实现接口的抽象方法。

importjava.io.*;

classSale

{

doublediscount,price;

intsale_sum=0;

doublesum=0.0,price_avg=0.0;

publicvoidchange_discount(doublediscount)

{

this.discount=discount;

}

publicvoidSaleCount(intperson_sale_num,doubleprice)

{

if(person_sale_num<10)

{

sum+=price*discount*person_sale_num;

}

else

{

sum+=0.95*price*discount*person_sale_num;

}

sale_sum+=person_sale_num;

price_avg=sum/sale_sum;

//returnsale_sum;

}

publicvoiddisplay()

{

System.out.println("Thetotalsalenamberis:

"+sale_sum);

System.out.println("Thetotalsalemoneyis:

"+sum);

System.out.println("Theavgofthepriceis:

"+price_avg);

}

}

classTest

{

publicstaticvoidmain(Stringargs[])throwsIOException

{

Salesale=newSale();

System.out.println("Pressthediscount:

");

BufferedReaderstdin=newBufferedReader(newInputStreamReader(System.in));

doubledisc=Double.parseDouble(stdin.readLine());

sale.change_discount(disc);

System.out.println("Thesaler101saled3goodsatprice126.8;");

sale.SaleCount(3,126.8);

System.out.println("Thesaler221saled8goodsatprice125.6;");

sale.SaleCount(8,125.6);

System.out.println("Thesaler325saled10goodsatprice124.8;");

sale.SaleCount(10,124.8);

System.out.println("Thesaler108saled45goodsatprice123.4;");

sale.SaleCount(45,123.4);

System.out.println("Thesaler901saled100goodsatprice121.5;");

sale.SaleCount(100,121.5);

sale.display();

}

}

结果:

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

当前位置:首页 > 初中教育 > 语文

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

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