JAVA程序设计实验二.doc_第1页
JAVA程序设计实验二.doc_第2页
JAVA程序设计实验二.doc_第3页
JAVA程序设计实验二.doc_第4页
JAVA程序设计实验二.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JAVA程序设计实验报告姓 名刘创学 号132054137指导教师邢珍珍成 绩设备名称及软件环境Win8 JDK JRE环境实验名称类的继承与封装实验日期2016年一实验内容1. 类的继承与封装:定义抽象类Shape(形状)其中有抽象方法用来求某形状的周长和面积;定义Shape类的子类Circle(圆形)、Triangle(三角形)、Rect(矩形)其中包括该形状的位置、大小信息并实现求其周长和面积的方法。假设当前有圆心为(100,100)半径为60的圆,左上角坐标为(0,200),水平宽度为120,垂直高度为80的矩形,以及三个顶点坐标分别为(200,200)、(300,400)、(150,350)的三角形,请在控制台输出每个形状的相关信息,及所有形状的周长和面积的和。2.接口的定义与实现:通过接口和实现接口的类来完成上一题目。二重点及难点1. 熟练掌握类、类的数据成员和成员方法的定义与实现;2. 熟练掌握抽象类与类的继承的相关内容;3. 熟练掌握接口与接口的实现的相关内容;4. 熟练掌握public、private、static、final、abstract等修饰符的作用。三理论分析或算法分析首先构造一个二维顶点信息类Point2D方便以后的使用。其次构造一个shape抽象基类,分别被子类Circle(圆形)、Triangle(三角形)、Rect(矩形)所继承。在三角形面积计算中采取三对顶点构成的向量进行叉积取半,其中注意取绝对值,周长采用二维空间向量求其长度等,进而实现对任意形状的三角形计算其面积周长。最后再用接口技术实现以上功能。四实现方法(含实现思路、程序流程图和源程序列表等)1. 复习有关Java中类、类的继承、接口、接口的实现的相关内容;2. 根据题目要求编写需要的抽象类和其子类;3. 根据题目要求编写相应的main方法完成程序;4. 根据题目要求编写需要的接口和实现该接口的类;五实验结果分析(含执行结果验证、输出显示信息、图形、调试过程中所遇的问题及处理方法等)实验一:用继承实现实验二:使用接口实现的结果与上面结果相同以上的实验所涉及到的类有:部分代码展示1. public class Poin2D int x;int y;2. public abstract class Shape public abstract float Perimeter();public abstract float Area();3.public abstract class Circle extends Shape public int radius;public Circle(Poin2D p, int radius) super(p,radius,radius);public void PrintPos() public float Perimeter()public float Area() 4. public abstract class Triangle extends Shape public Triangle (Poin2D p1, Poin2D p2,Poin2D p3) super(p1, p2, p3);public void PrintPos() System.out.println(the pos of the Triangle P (X,Y):P1:(+p1.x+ +p1.y+) P2:(+p2.x+ +p2.y+) P3:(+p3.x+ +p3.y+);public float Perimeter()Poin2D pp1 = new Poin2D();Poin2D pp2 = new Poin2D();Poin2D pp3 = new Poin2D();pp1.x = p2.x - p1.x;pp1.y = p2.y - p1.y;pp2.x = p3.x - p1.x;pp2.y = p3.y - p1.y;pp3.x = p3.x - p2.x;pp3.y = p3.y - p2.y;float edge1 = (float) (Math.sqrt(pp1.x * pp1.x + pp1.y * pp1.y) );float edge2 = (float) (Math.sqrt(pp2.x * pp2.x + pp2.y * pp2.y) );float edge3 = (float) (Math.sqrt(pp3.x * pp3.x + pp3.y * pp3.y) );float perimeter = edge1+edge2+edge3;return perimeter;public float Area()Poin2D pp1 = new Poin2D();Poin2D pp2 = new Poin2D();pp1.x = p2.x - p1.x;pp1.y = p2.y - p1.y;pp2.x = p3.x - p1.x;pp2.y = p3.y - p1.y;float area = (float)( Math.abs(pp1.x*pp2.y-pp1.y*pp2.x)/2;return area;5.public abstract class Rect extends Shape public Rect(Poin2D p, int width, int height) super(p, width, height);public void PrintPos() System.out.println(Pos: X:+pos.x+ Y:+pos.y);System.out.println(The width,height of the Rect is +width+, + height);public float Perimeter()float perimeter = (float) (2 * (width + height) );return perimeter;public float Area()float area = (float) width * height;return area;6.public class Test public static void main(Stringargs) / 圆的输出位置及周长面积计算System.out.println(Circle is calculating);int radius = 60;Poin2D p = new Poin2D();p.x = 100;p.y = 100;Circle circle = new Circle(p, radius) ;circle.PrintPos();System.out.println(the perimeter of the Circle is +circle.Perimeter();System.out.println(the area of the Circle is +circle.Area();/ 三角形三点位置以及周长面积计算System.out.println(Triangle is calculating);Poin2D p1 = new Poin2D();p1.x = 200;p1.y = 200;Poin2D p2 = new Poin2D();p2.x = 300;p2.y = 400;Poin2D p3 = new Poin2D();p3.x = 150;p3.y = 350;Triangle triangle = new Triangle(p1, p2, p3) ;triangle.PrintPos();System.out.println(the perimeter of the Triangle is +triangle.Perimeter();System.out.println(the area of the Triangle is +triangle.Area();/ 矩形的输出位置及周长面积计算System.out.println(Rect is calculating);p.x = 0;p.y = 200;int width = 120;int height = 80;Rect rect = new Rect(p,width,height) ;rect.PrintPos();System.out.println(the perimeter of the Rect is +rect.Perimeter();System.out.println(the area of the Rect is +rect.Area();/ 输出总面积和总周长System.out.println(All perimeter and area is calculating);float sumPerimeter = circle.Perimeter() + triangle.Perimeter() + rect.Perimeter();float sumArea = circle.Area() + triangle.Area()+ rect.Area();System.out.println(The perimeter of all is +sumPerimeter);System.out.println(The area of the all is +sumArea);7.接口实现部分代码 将shape定义为Interface 其他类进行impeletements实现接口即可 public interface IShape public abstract float Perimeter();public abstract float Area();public class Circle implement

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论