类的继承与封装

上传人:第*** 文档编号:34262654 上传时间:2018-02-22 格式:DOC 页数:7 大小:87KB
返回 下载 相关 举报
类的继承与封装_第1页
第1页 / 共7页
类的继承与封装_第2页
第2页 / 共7页
类的继承与封装_第3页
第3页 / 共7页
类的继承与封装_第4页
第4页 / 共7页
类的继承与封装_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《类的继承与封装》由会员分享,可在线阅读,更多相关《类的继承与封装(7页珍藏版)》请在金锄头文库上搜索。

1、1JAVA 程序设计实验报告姓 名 霍奋伟 学 号 10 班 级 1420552 成 绩设备名称及软件环境 IDE 8.1实验名称 类的继承与封装 实验日期 2016.4.201实验内容1.类的继承与封装:定义抽象类 Shape(形状)其中有抽象方法用来求某形状的周长和面积;定义 Shape类的子类 Circle(圆形) 、Triangle(三角形) 、Rect(矩形)其中包括该形状的位置、大小信息并实现求其周长和面积的方法。假设当前有圆心为(30,70)半径为 50 的圆,左上角坐标为(20,200) ,水平宽度为120,垂直高度为 80 的矩形,以及三个顶点坐标分别为(200,200) 、

2、 (300,400) 、(150,350)的三角形,请在控制台输出每个形状的相关信息,及所有形状的周长和面积的和。2.接口的定义与实现:通过接口和实现接口的类来完成上一题目。2重点及难点类、类的数据成员和成员方法的定义与实现;抽象类与类的继承;接口与接口的实现public、private、static、final、abstract 等修饰符的作用。23理论分析或算法分析在接口中定义两个函数,分别实现面积和周长的功能;定义三个类实现其接口,在类中实现面积和周长两个函数。在 main 函数中测试。实验步骤1. 复习有关 Java 中类、类的继承、接口、接口的实现的相关内容;2. 根据题目要求编写需

3、要的抽象类和其子类;3. 根据题目要求编写相应的 main 方法完成程序;4. 根据题目要求编写需要的接口和实现该接口的类;5. 根据题目要求编写相应的 main 方法完成程序;6. 调试代码,完善程序。34实现方法(含实现思路、程序流程图和源程序列表等)1.抽象类import java.applet.Applet;import java.awt.*;import java.awt.geom.*;abstract class Shapes public double x,y;public double width,height;public Shapes(double x,double y,d

4、ouble width,double height) this.x=x;this.y=y;this.width=width;this.height=height;abstract double getArea();abstract double getPeimeter();class Square extends Shapes public double getArea()return width*height;public double getPerimeter()return(2*width+2*height);public Square(double x,double y,double

5、width,double height) super(x, y, width, height);Overridedouble getPeimeter() return 0;class Triangle extends Shapes public double c;public double getArea()return(0.5*width*height);public double getPerimeter()return(width+height+c);public Triangle(double x,double y,double width,double height) super(x

6、, y, width, height);c=Math.sqrt(width*width+height*height);Overridedouble getPeimeter() return 0;class Cricle extends Shapespublic Cricle(double x,double y,double width,double height) super(x, y, width, height);r=(double)width/2.0;Overridedouble getPeimeter() return 0;public class hfw1 extends Apple

7、tSquare Box=new Square(20,200,120,80);Cricle Oval=new Cricle(30,70,100,100);double b=Math.sqrt(Math.pow(150-200),2)+Math.pow(350-200),2);double c=Math.sqrt(Math.pow(150-300),2)+Math.pow(350-400),2);Triangle tri=new Triangle(0,0,b,c);public void paint(Graphics g) /输出长方形的信息g.drawRect(20,200,120,80); g

8、.drawString (Box Area:+Box.getArea(),20,300);g.drawString (Box Perimeter:+Box.getPerimeter(),20,320);/输出圆的信息g.drawOval(30,70,100,100);g.drawString (Oval Area:+Oval.getArea(),150,100);g.drawString (Oval Perimeter:+Oval.getPerimeter(),150,120);/输出三角形的信息Graphics2D g2=(Graphics2D)g; int x1=150,200,300,1

9、50; int y1=350,200,400,350;GeneralPath polygon=new GeneralPath(0,x1.length+y1.length);polygon.moveTo(x10,y10 );for(int index=1;indexx1.length;index+) polygon.lineTo(x1index, y1index);polygon.closePath(); g2.draw(polygon);g.drawString ( Triangle Area:+tri.getArea(),280,280);g.drawString (Triangle Per

10、imeter:+tri.getPerimeter(),290,300); 4 public double r;public double getArea()return(r*r*Math.PI);public double getPerimeter()return(2*Math.PI*r);public Cricle(double x,double y,double width,double height) super(x, y, width, height);r=(double)width/2.0;Overridedouble getPeimeter() return 0;public cl

11、ass hfw1 extends AppletSquare Box=new Square(20,200,120,80);Cricle Oval=new Cricle(30,70,100,100);double b=Math.sqrt(Math.pow(150-200),2)+Math.pow(350-200),2);double c=Math.sqrt(Math.pow(150-300),2)+Math.pow(350-400),2);Triangle tri=new Triangle(0,0,b,c);public void paint(Graphics g) /输出长方形的信息g.draw

12、Rect(20,200,120,80); g.drawString (Box Area:+Box.getArea(),20,300);g.drawString (Box Perimeter:+Box.getPerimeter(),20,320);/输出圆的信息g.drawOval(30,70,100,100);g.drawString (Oval Area:+Oval.getArea(),150,100);g.drawString (Oval Perimeter:+Oval.getPerimeter(),150,120);/输出三角形的信息Graphics2D g2=(Graphics2D)g

13、; int x1=150,200,300,150; int y1=350,200,400,350;GeneralPath polygon=new GeneralPath(0,x1.length+y1.length);polygon.moveTo(x10,y10 );for(int index=1;indexx1.length;index+) polygon.lineTo(x1index, y1index);polygon.closePath(); g2.draw(polygon);g.drawString ( Triangle Area:+tri.getArea(),280,280);g.dr

14、awString (Triangle Perimeter:+tri.getPerimeter(),290,300); 52.接口import java.applet.Applet;import java.awt.*;import java.awt.geom.*;interface Shape abstract double getArea();double getPerimeter();class Circler implements Shape public double x,y;public double width,height;public double r;Overridepubli

15、c double getArea() return (r*r*Math.PI);Overridepublic double getPerimeter() return (2*r*Math.PI);public Circler(double x,double y,double width,double height)this.x=x;this.y=y;this.width=width;this.height=height;r=(double)width/2.0;class Squarer implements Shapepublic double x,y;public double width,height;Overridepublic double getArea() return (width*height);Overridepublic double getPerimeter() return (2*width+2*height);public Squarer(double x,double y,double width,double height)this.x=x;this.y=y;this.width=width;this.height=height;class Triangler imple

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

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

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