预览加载中,请您耐心等待几秒...
1/3
2/3
3/3

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

《JAVA程序设计》课程实验报告 专业:网络工程 班级:551 学号:10 姓名:高慧鹏 日期:2016年4月26日 实验题目 类的继承与封装 实验目的 熟练掌握类、类的数据成员和成员方法的定义与实现; 熟练掌握抽象类与类的继承的相关内容; 熟练掌握接口与接口的实现的相关内容; 熟练掌握public、private、static、final、abstract等修饰符的作用。 实验内容 类的继承与封装: 定义抽象类Shape(形状)其中有抽象方法用来求某形状的周长和面积;定义Shape类的子类Circle(圆形)、Triangle(三角形)、Rect(矩形)其中包括该形状的位置、大小信息并实现求其周长和面积的方法。 假设当前有圆心为(100,100)半径为60的圆,左上角坐标为(0,200),水平宽度为120,垂直高度为80的矩形,以及三个顶点坐标分别为(200,200)、(300,400)、(150,350)的三角形,请在控制台输出每个形状的相关信息,及所有形状的周长和面积的和。 接口的定义与实现: 通过接口和实现接口的类来完成上一题目。 实验步骤 题目一 问题分析 子类继承父类,构造类的对象来实现 算法描述 主要代码及运行结果 importjava.applet.Applet; importjava.awt.Graphics; abstractclassShapes{ publicintx,y; publicintwidth,height; publicShapes(intx,inty,intwidth,intheight){ this.x=x; this.y=y; this.width=width; this.height=height; } abstractdoublegetArea(); abstractdoublegetPerimeter(); } classCircleextendsShapes{ //圆 publicdoubler; publicdoublegetArea(){ return(r*r*Math.PI); } publicdoublegetPerimeter(){ return(2*Math.PI*r); } publicCircle(intx,inty,intwidth,intheigh){ super(x,y,width,heigh); r=(double)width/2.0; } } classSquareextendsShapes{ //矩形 publicdoublegetArea(){ return(width*height); } publicdoublegetPerimeter(){ return(2*width+2*height); } publicSquare(intx,inty,intwidth,intheight){ super(x,y,width,height); } } classTriangleextendsShapes{ //三角形 publicdoublec; publicdoublegetArea(){ return(0.5*width*height); } publicdoublegetArea1(){ return(0.5*width*height); } publicdoublegetPerimeter(){ return(width+height+c); } publicTriangle(intx,inty,intbase,intheight){ super(x,y,base,height); c=Math.sqrt(width*width+height*height); } } publicclasstestextendsApplet{ SquareBox=newSquare(0,200,120,80); Triangletri_1=newTriangle(200,200,5,10); Triangletri_2=newTriangle(300,400,0,0); Triangletri_3=newTriangle(150,350,0,0); CircleOval=newCircle(100,100,60,60);// publicvoidpaint(Graphicsg){ intarg0[]={tri_1.x,tri_2.x,tri_3.x},arg1[]={tri_1.y,tri_2.y,tri_3.y}; g.draw