java程序设计实验二

上传人:第*** 文档编号:34232437 上传时间:2018-02-22 格式:DOC 页数:4 大小:88.50KB
返回 下载 相关 举报
java程序设计实验二_第1页
第1页 / 共4页
java程序设计实验二_第2页
第2页 / 共4页
java程序设计实验二_第3页
第3页 / 共4页
java程序设计实验二_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《java程序设计实验二》由会员分享,可在线阅读,更多相关《java程序设计实验二(4页珍藏版)》请在金锄头文库上搜索。

1、1JAVA 程序设计实验报告姓 名刘创 学 号 132054137 指导教师 邢珍珍 成 绩设备名称及软件环境 Win8 JDK JRE 环境实验名称类的继承与封装 实验日期 2016 年一实验内容1. 类的继承与封装:定义抽象类 Shape(形状)其中有抽象方法用来求某形状的周长和面积;定义 Shape类的子类 Circle(圆形) 、Triangle(三角形) 、Rect(矩形)其中包括该形状的位置、大小信息并实现求其周长和面积的方法。假设当前有圆心为(100,100)半径为 60 的圆,左上角坐标为(0,200) ,水平宽度为 120,垂直高度为 80 的矩形,以及三个顶点坐标分别为(2

2、00,200) 、 (300,400) 、(150,350)的三角形,请在控制台输出每个形状的相关信息,及所有形状的周长和面积的和。2.接口的定义与实现:通过接口和实现接口的类来完成上一题目。二重点及难点1. 熟练掌握类、类的数据成员和成员方法的定义与实现;2. 熟练掌握抽象类与类的继承的相关内容;3. 熟练掌握接口与接口的实现的相关内容;4. 熟练掌握 public、private、static、final、abstract 等修饰符的作用。三理论分析或算法分析首先构造一个二维顶点信息类 Point2D 方便以后的使用。其次构造一个 shape 抽象基类,分别被子类 Circle(圆形) 、

3、Triangle(三角形) 、Rect(矩形)所继承。在三角形面积计算中采取三对顶点构成的向量进行叉积取半,其中注意取绝对值,周长采用二维空间向量求其长度等,进而实现对任意形状的三角形计算其面积周长。最后再用接口技术实现以上功能。2四实现方法(含实现思路、程序流程图和源程序列表等)1. 复习有关 Java 中类、类的继承、接口、接口的实现的相关内容;2. 根据题目要求编写需要的抽象类和其子类;3. 根据题目要求编写相应的 main 方法完成程序;4. 根据题目要求编写需要的接口和实现该接口的类;3五实验结果分析(含执行结果验证、输出显示信息、图形、调试过程中所遇的问题及处理方法等)实验一:用继

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

5、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+ +p

6、3.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

7、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;4public float Area()Poin2D pp1 = new Poin2D();Poin2D pp2 = new Poin2D();pp1.x = p2.x - p1.x;pp1.y = p2.y - p1.y;pp2.x = p

8、3.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(T

9、he 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(Ci

10、rcle 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.prin

11、tln(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.Pe

12、rimeter();System.out.println(the area of the Triangle is +triangle.Area();5/ 矩形的输出位置及周长面积计算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()

13、;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 perimete

14、r 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 implements IShape public class Triangle implements IShape public class Rect implements IShape 五结论通过本次实验,有关 Java 中类、类的继承、接口、接口的实现的相关内容。对 java中的继承、接口有了一定的深度了解,掌握public、private、static、final、abstract 等修饰符的作用。报告提交日期(注意:内容写不下时可另附页。 )

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号