Java-SSD3-Experiment 4 "Object Orientation-Inheritance"

1. The purpose of the experiment

  1. Understand object-oriented programming, especially the idea of ​​inheritance, and learn to use inheritance to create subclasses on the basis of parent classes
  2. Use the keyword super to call the construction method and method of the parent class
  3. Override methods in subclasses

2. Experimental content

   1. (P380, 11.1) [Triangle class Triangle] Design a class named Triangle to extend the GeometricObject class. This category includes:

(1) Three double data fields named side1, side2 and side3 represent the three sides of this triangle, and their default value is 1.0.

(2) A parameterless construction method creates the default triangle.

(3) A construction method that can create a triangle with specified side1, side2, and side3.

(4) Accessor methods for all three data fields.

(5) A method named getArea() returns the area of ​​this triangle. For the formula for calculating the area of ​​a triangle, see Exercise 2.19 in Experiment 1.

(6) A method named getPerimeter() returns the perimeter of this triangle.

(7) A method named toString() returns the string description of the triangle, displaying the length, perimeter and area of ​​the triangle.

 

Target output tasks:

  • Draw the UML diagrams of the Triangle and GeometricObject classes.

  • Implement these classes.

  • Write a test program to create a Triangle object with side lengths of 1, 1.5 and 1, color yellow and filled to true, and then display its three side length, perimeter, area, color and whether it is filled.

 

1.1 Draw the UML class diagram as shown below :

 

1.2 Analysis of operating results

 1) Enter the data given in the question, and the program operation result is shown in the figure below:

2) Enter another set of test data, the program running result is shown in the figure below:

It can be seen from the above that the program meets the experimental requirements.

 

1.3 Experience

 

   Although object-oriented is more convenient than process-oriented, it also requires more physical effort. But IDEA contains a variety of plug-ins, you need to study more in the future to facilitate programming. This question is the most basic inheritance and has not encountered difficulties. In general, care is the best.

 

1.4 Source code display

  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 class】Define the Circle2D class, including:

(1) Two double data fields named x and y with get methods indicate the center point of the circle.

(2) A data domain radius with a get method.

(3) A parameterless construction method that creates a default circle whose (x, y) value is (0, 0) and radius is 1.

(4) A construction method to create a circle with specified x, y and radius.

(5) A method getArea() that returns the area of ​​a circle.

(6) A method getPerimeter() that returns the circumference of a circle.

(7) If the given point (x, y) is inside the circle, then the method contains(double x, double y) returns true. As shown in Figure a.

(8) If the given circle is inside this circle, the method contains(Circle2D circle) returns true. As shown in Figure b.

(9) If the given circle overlaps this circle, the method overlaps(Circle2D circle) returns true. As shown in Figure c.

 

 

 

s

Target output tasks:

  • Draw the UML diagram of this type.
  • Implement this class.
  • Write a test program, create a Circle2D object c1(new Circle2D(2, 2, 5.5)), display its area and perimeter, and display c1.contains(3, 3), c1.contains(new Circle2D(4, 5, 10.5)) and c1.overlaps(new Circle2D(3, 5, 2.3)).

 

 

2.1 Draw the UML class diagram as shown below:

 

2.2 Analysis of running results

 

Test the data required by the experiment, and the program running results are shown in the figure below:

It can be seen from the program running results that the program meets the experimental requirements.

 

2.3 Experience

   There is no particular difficulty in implementing this program, and it is relatively smooth. What is defined here is a plane circle, a classic problem of analytic geometry solved with the help of programs. Similarly, with the help of programming, we can solve more mathematical problems.

 

2.4 Program source code:

  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 ) If the given point ( x, y ) is inside the circle, then the method contains(double x, double y) returns true . As shown in Figure a . 
  38.            w     ( 8 ) If the given circle is in this circle, the method contains(Circle2D circle) returns true . As shown in Figure b . 
  39.            w     ( 9 ) If the given circle overlaps this circle, the method overlaps(Circle2D circle) returns true . As shown in Figure 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. }  

Guess you like

Origin blog.csdn.net/qq_45768060/article/details/106056446