Java-SSD3-实验4《面向对象——继承》

一,实验目的

  1. 理解面向对象编程,尤其是继承的思想,学习通过继承,在父类的基础上创建子类
  2. 使用关键字super调用父类的构造方法和方法
  3. 在子类中覆盖方法

二、实验内容

   1. (P380, 11.1)【三角形类Triangle】设计一个名为Triangle的类来扩展GeometricObject类。该类包括:

(1)三个名为side1、side2和side3的double数据域表示这个三角形的三条边,它们的默认值是1.0。

(2)一个无参构造方法创建默认的三角形。

(3)一个能创建带指定side1、side2和side3的三角形的构造方法。

(4)所有三个数据域的访问器方法。

(5)一个名为getArea()的方法返回这个三角形的面积。计算三角形面积的公式参见实验1练习题2.19。

(6)一个名为getPerimeter()的方法返回这个三角形的周长。

(7)一个名为toString()的方法返回这个三角形的字符串描述,显示三角形的三边长、周长及面积。

 

目标输出任务:

  • 画出Triangle类和GeometricObject类的UML图。

  • 实现这些类。

  • 编写一个测试程序,创建边长为1、1.5和1,颜色为yellow,filled为true的Triangle对象,然后显示它的三边长、周长、面积、颜色以及是否被填充。

 

1.1画出UML类图如下图所示

 

1.2运行结果分析

 1)输入题中所给数据,程序运行结果如下图所示:

2)输入另一组测试数据,程序运行结果如下图所示:

由上可知该程序符合实验要求。

 

1.3心得体会

 

   面向对象相比面向过程虽然方便却也多了体力活。但IDEA中含有各种插件,以后要多多研究,可以方便编程。这个题是最基本的继承,并未遇到难坎。总的来说,细心为上。

 

1.4源代码展示

  1. public class Program1 {  
  2.     public static void main(String[] args) {  
  3.         Triangle t = new Triangle(1,1,1);  
  4.         t.setColor("yellow");  
  5.         t.setFilled(true);  
  6.         System.out.println(t.toString());  
  7.   
  8.     }  
  9. }  
  10.   
  11. class GeometricObject{  
  12.     private String color ;  
  13.     private boolean filled;  
  14.   
  15.   
  16.     public GeometricObject() {  
  17.     }  
  18.   
  19.     public GeometricObject(String color, boolean filled) {  
  20.         this.color = color;  
  21.         this.filled = filled;  
  22.     }  
  23.   
  24.     public String getColor() {  
  25.         return color;  
  26.     }  
  27.   
  28.     public void setColor(String color) {  
  29.         this.color = color;  
  30.     }  
  31.   
  32.     public boolean isFilled() {  
  33.         return filled;  
  34.     }  
  35.   
  36.     public void setFilled(boolean filled) {  
  37.         this.filled = filled;  
  38.     }  
  39.   
  40.     @Override  
  41.     public String toString() {  
  42.         return "GeometricObject{" +  
  43.                 "color='" + color + '\'' +  
  44.                 ", filled=" + filled +  
  45.                 '}';  
  46.     }  
  47.   
  48.   
  49. }  
  50. class Triangle extends  GeometricObject{  
  51.   
  52.     double side1 = 1;  
  53.     double side2 = 1;  
  54.     double side3 = 1;  
  55.   
  56.   
  57.   
  58.   
  59.     public Triangle() {  
  60.         this.side1 = 1;  
  61.         this.side2 = 1;  
  62.         this.side3 = 1;  
  63.     }  
  64.     public Triangle(double side1, double side2, double side3) {  
  65.         this.side1 = side1;  
  66.         this.side2 = side2;  
  67.         this.side3 = side3;  
  68.     }  
  69.   
  70.   
  71.     public double getSide1() {  
  72.         return side1;  
  73.     }  
  74.   
  75.     public double getSide2() {  
  76.         return side2;  
  77.     }  
  78.   
  79.     public double getSide3() {  
  80.         return side3;  
  81.     }  
  82.     public double getArea(){  
  83.         double s = (side1 + side2 + side3)/2;  
  84.         return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));  
  85.   
  86.     }  
  87.     public  double getPerimeter(){  
  88.         return side1 + side2 +side3 ;  
  89.     }  
  90.   
  91.     @Override  
  92.     public String toString() {  
  93.         return "Triangle{" +  
  94.                 "\nside1=" + side1 +  
  95.                 ", side2=" + side2 +  
  96.                 ", side3=" + side3 +  
  97.                 "\ncolor :"+getColor()+  
  98.                 "\nfilled:"+isFilled()+  
  99.                 "\nArea :"+ getArea()+  
  100.                 "\nPerimeter:"+getPerimeter()+  
  101.   
  102.                 "\n}";  
  103.   
  104.     }  
  105. }  

2. (P342, 10.11)【Circle2D类】定义Circle2D类,包括:

(1)两个带有get方法的名为x和y的double型数据域,表明圆的中心点。

(2)一个带get方法的数据域radius。

(3)一个无参构造方法,该方法创建一个(x,y)值为(0, 0)且radius为1的默认圆。

(4)一个构造方法,创建带指定的x、y和radius的圆。

(5)一个返回圆面积的方法getArea()。

(6)一个返回圆周长的方法getPerimeter()。

(7)如果给定的点(x, y)在圆内,那么方法contains(double x, double y)返回true。如图a所示。

(8)如果给定的圆在这个圆内,那么方法contains(Circle2D circle)返回true。如图b所示。

(9)如果给定的圆和这个圆重叠,那么方法overlaps(Circle2D circle)返回true。如图c所示。

 

 

 

s

目标输出任务:

  • 画出该类的UML图。
  • 实现这个类。
  • 编写测试程序,创建一个Circle2D对象c1(new Circle2D(2, 2, 5.5)),显示它的面积和周长,还要显示c1.contains(3, 3)、c1.contains(new Circle2D(4, 5, 10.5))和c1.overlaps(new Circle2D(3, 5, 2.3))。

 

 

2.1画出UML类图如下图所示:

 

2.2运行结果分析

 

对实验所要求数据进行测试,程序运行结果如下图所示:

由程序运行结果可知,该程序符合实验要求。

 

2.3心得体会

   这个程序实现并无特别难的地方,比较顺利。这里定义的是平面的圆类,借助程序解决的解析几何的一个经典问题。同样的,借助编程,我们可以解决更多的数学问题。

 

2.4程序源代码:

  1. public class Program2 {  
  2.     public static void main(String[] args) {  
  3.         Circle2D c1 = new Circle2D(2,2,5.5);  
  4.         System.out.println(c1.toString());  
  5.         System.out.println("The Area is :"+c1.getArea()+"\nThe Perimetrt is :"+c1.getPerimeter());  
  6.         System.out.println("The result of c1.contains(3, 3) is :"+c1.contains(3, 3));  
  7.         System.out.println("The result of c1.contains(new Circle2D(4, 5, 10.5)) is :"+  
  8.                                c1.contains(new Circle2D(4, 5, 10.5)));  
  9.         System.out.println("The result of c1.overlap(new Circle2D(3, 5, 2.3)) is :"  
  10.                             +c1.overlaps(new Circle2D(3, 5, 2.3)));  
  11.   
  12.     }  
  13. }  
  14. class Circle2D{  
  15.     double x;  
  16.     double y;  
  17.     double radius;  
  18.   
  19.   
  20.     public Circle2D() {  
  21.         this.x = 1;  
  22.         this.y = 1;  
  23.         this.radius = 1;  
  24.     }  
  25.   
  26.     public Circle2D(double x, double y, double radius) {  
  27.         this.x = x;  
  28.         this.y = y;  
  29.         this.radius = radius;  
  30.     }  
  31.     public  double getArea(){  
  32.         return Math.PI * getRadius() * getRadius();  
  33.     }  
  34.     public  double getPerimeter(){  
  35.         return  2 * Math.PI * getRadius();  
  36.     }  
  37.    /*      7)如果给定的点(x, y)在圆内,那么方法contains(double x, double y)返回true。如图a所示。 
  38.            w    8)如果给定的圆在这个圆内,那么方法contains(Circle2D circle)返回true。如图b所示。 
  39.            w    9)如果给定的圆和这个圆重叠,那么方法overlaps(Circle2D circle)返回true。如图c所示。 
  40.             * 
  41.     */  
  42.     public boolean contains(double x,double y){  
  43.         if((x-this.x) *(x-this.x) + (y-this.y)*(y-this.y) <= radius * radius)  
  44.             return  true;  
  45.         return false;  
  46.     }  
  47.     public boolean contains(Circle2D circle){  
  48.         double s =  (circle.getY() - this.getY())*(circle.getY() - this.getY())  
  49.                     + (circle.getX() - this.getX()) *(circle.getX() - this.getX());  
  50.         s = Math.sqrt(s);  
  51.         if(s <= this.getRadius() -circle.getRadius())  
  52.             return true;  
  53.         return false;  
  54.     }  
  55.     public boolean overlaps(Circle2D circle){  
  56.         double s =  (circle.getY() - this.getY())*(circle.getY() - this.getY())  
  57.                     + (circle.getX() - this.getX()) *(circle.getX() - this.getX());  
  58.         s = Math.sqrt(s);  
  59.         if(s <= this.getRadius() + circle.getRadius() && s >= this.getRadius() -circle.getRadius())  
  60.             return true;  
  61.         return false;  
  62.     }  
  63.     public double getX() {  
  64.         return x;  
  65.     }  
  66.   
  67.     public double getY() {  
  68.         return y;  
  69.     }  
  70.   
  71.     public double getRadius() {  
  72.         return radius;  
  73.     }  
  74.   
  75.     @Override  
  76.     public String toString() {  
  77.         return "Circle2D{" +  
  78.                 "x=" + x +  
  79.                 ", y=" + y +  
  80.                 ", radius=" + radius +  
  81.                 '}';  
  82.     }  
  83. }  

猜你喜欢

转载自blog.csdn.net/qq_45768060/article/details/106056446