7-10 week OOP assignment summary

I. Introduction:

In the process of writing these three topic sets, I almost did research while writing, I learned a lot, and at the same time I found that I still have a lot to learn. The difficulty of the three questions gradually increased, from the first inspection of the basic loop structure, control structure, data input and output (which can be solved by c coding), the second week to the advanced String format judgment and content extraction, the third After three weeks of geometric point and line calculation (starting to investigate the understanding and application of object-oriented thinking), I gradually realized some differences between process-oriented thinking and object-oriented thinking, and had a deeper understanding of object-oriented programming.

1.PTA topic set

    Compared with the previous weeks, the number of questions is reduced, the difficulty is different, and the knowledge points are more scattered. For example, [7-1 sdut-String-2 Manned Deep Dive of the Jiaolong, Li Science and Technology Report National Records (II)] is regular. Expression, the last two questions are the point-line calculation and class design of practice geometry. This blog analyzes the very troublesome [7-2 point-line series 4-calculation of convex quadrilateral].

2. Midterm exam

    The difficulty of the three questions is gradual, and the knowledge points are inspected from class design to inheritance and polymorphism to container classes. The code can be iterated layer by layer. This blog mainly analyzes [7-1 Points and Lines (Class Design)] [7-2 Point-line-surface problem reconstruction (inheritance and polymorphism)] [7-3 Point-line-surface problem reconstruction (container class)].

2. Design and analysis:

1.PTA topic set four [7-2 point line series 4-calculation of convex quadrilateral]

Input format:

Basic format: option+":"+coordinate x+","+coordinate y+" "+coordinate x+","+coordinate y. The x and y coordinates of the points are separated by English ",", and the points are separated by an English space.

(The string array is used to receive the input value, the split function is used to divide the coordinates, and then stored in the point object for operation)

Output format:

The basic output format is described in the description of each option.
Exception output:
If it does not conform to the basic format, output "Wrong Format".
If the basic format is met, but the number of input points does not meet the requirements, output "wrong number of points".
Note: If the output data exceeds 3 digits after the decimal point, only 3 digits after the decimal point are reserved, and the excess part is rounded to the lowest digit. If there are less than 3 digits after the decimal point, it will be displayed according to the original number of digits, and there is no need to make up. For example: 1/3 of the result is formatted as 0.333, 1.0 is formatted as 1.0

(The indexOf method of the string is used to record the number of digits after the decimal point of each data and use the ternary operator to judge)

  String str_d = String.valueOf(s);
    str_d = str_d.substring(str_d.indexOf(".") + 1);
    int len = str_d.length();
    len = len > 3 ? 3 : len;
    String out = String.format("%."+len+"f", s);

In options 1, 2, and 3, if there are coincident points among the four points of the quadrilateral, output "points coincide".
In option 4, if the points of the first two input lines coincide, output "points coincide".

(The equals method of point is customized to judge the coincidence of points)

SourceMonitor analysis diagram:

 

Full code:

import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
  Scanner inp = new Scanner(System.in);
  String s=inp.nextLine();
  String[] a=s.split("[:]");
  String[] num=a[1].split("[, ]");
  int i=Integer.parseInt(a[0]);
  switch(i) {
     case 1:
{
        if(num.length==8) {
          point p1=new point(Double.parseDouble(num[0]),Double.parseDouble(num[1]));
          point p2=new point(Double.parseDouble(num[2]),Double.parseDouble(num[3]));
          point p3=new point(Double.parseDouble(num[4]),Double.parseDouble(num[5]));
          point p4=new point(Double.parseDouble(num[6]),Double.parseDouble(num[7]));
          Quad quad=new Quad(p1,p2,p3,p4);
          if(quad.ischonghe()) {
          if(quad.isQuad())
          System.out.print(quad.isQuad()+" ");
          System.out.print(quad.isprQuad());

                }
                else
                    System.out.println("points coincide");
            }
            else
            System.out.println("wrong number of points");
            return;
        }
        case 2:
        {
            if(num.length==8) {
                point p1=new point(Double.parseDouble(num[0]),Double.parseDouble(num[1]));
                point p2=new point(Double.parseDouble(num[2]),Double.parseDouble(num[3]));
                point p3=new point(Double.parseDouble(num[4]),Double.parseDouble(num[5]));
                point p4=new point(Double.parseDouble(num[6]),Double.parseDouble(num[7]));
                Quad quad=new Quad(p1,p2,p3,p4);
                if(quad.ischonghe()) {
                System.out.print(quad.isdiaQuad()+" ");
                System.out.print(quad.isrecQuad()+" ");
                System.out.print(quad.issqQuad());
                }
                else
                    System.out.println("points coincide");
            }
            else
            System.out.println("wrong number of points");
            return;
        }
        case 3:
        {
            if(num.length==8) {
                point p1=new point(Double.parseDouble(num[0]),Double.parseDouble(num[1]));
                point p2=new point(Double.parseDouble(num[2]),Double.parseDouble(num[3]));
                point p3=new point(Double.parseDouble(num[4]),Double.parseDouble(num[5]));
                point p4=new point(Double.parseDouble(num[6]),Double.parseDouble(num[7]));
                Quad quad=new Quad(p1,p2,p3,p4);
                if(quad.ischonghe()) {
                System.out.print(quad.iscvQuad()+" ");
                System.out.print(quad.c()+" ");
                System.out.print(quad.s());
                }
                else
                    System.out.println("points coincide");
            }
            else
            System.out.println("Wrong Format");
            return;
        }
        case 4:
        {
            if(!(num.length==12)) {
                System.out.print("Wrong Format");
                return;
            }
             
             
        }
        case 5:
        {
            if(num.length==10) {
                point p=new point(Double.parseDouble(num[0]),Double.parseDouble(num[1]));
                point p1=new point(Double.parseDouble(num[2]),Double.parseDouble(num[3]));
                point p2=new point(Double.parseDouble(num[4]),Double.parseDouble(num[5]));
                point p3=new point(Double.parseDouble(num[6]),Double.parseDouble(num[7]));
                point p4=new point(Double.parseDouble(num[8]),Double.parseDouble(num[9]));
                Quad quad=new Quad(p1,p2,p3,p4);
                if(quad.ischonghe()) {
                if(quad.isSanjiao()) {
                    if(p.isin(quad)) {
                        if(p.ison(quad)) {
                        System.out.print("on the triangle");
                        }
                        else 
                        System.out.print("in the triangle");}
                        else 
                            System.out.print("outof the triangle");
                }
                    else if(quad.isQuad()) {
                        if(p.isin(quad)) {
                            if(p.ison(quad)) {
                            System.out.print("on the quadrilateral");
                            }
                            else 
                            System.out.print("in the quadrilateral");}
                            else 
                                System.out.print("outof the quadrilateral");}
                    else 
                        System.out.print("not a quadrilateral or triangle");}
                else
                    System.out.println("points coincide");
                
            }
            else
            System.out.println("Wrong Format");
            return;
            }
       
         }
        }
}

class point{
    double x;
    double y;
public point(double x,double y) {
        this.x=x;
        this.y=y;
    }
public boolean equals(point p) {
    if(p.x==x&&p.y==y)
    {
        return true;
    }
    else
        return false;
}
public boolean isin(Quad quad) {
    double s1=sanjiao(this,quad.p1,quad.p2);
    double s2=sanjiao(this,quad.p3,quad.p2);
    double s3=sanjiao(this,quad.p3,quad.p4);
    double s4=sanjiao(this,quad.p1,quad.p4);
    if(quad.s==s1+s2+s3+s4) {
        return true;
    }
    else
        return false;
}
public boolean ison(Quad quad) {
    line l1=new line(quad.p1,quad.p2);
    line l2=new line(quad.p2,quad.p3);
    line l3=new line(quad.p3,quad.p4);
    line l4=new line(quad.p4,quad.p1);
    if(l1.ison(this)||l2.ison(this)||l3.ison(this)||l4.ison(this)){
        return true;
    }
    else
        return false;
}
public double sanjiao(point p1,point p2,point p3) {
    double s=(p1.x*p2.y+p2.x*p3.y+p3.x*p1.y+p1.x*p3.y+p2.x*p1.y-p3.x*p2.y)/2;
    return s;
}
}
class line{   
    double a;
    double b;
    double c;
    point p1;
    point p2;
    public boolean equals(line l) {
        if((a/b==l.a/l.b&&a/c==l.a/l.c)||(a/b==-l.a/l.b&&a/c==-l.a/l.c))
        {
            return true;
        }
        else
            return false;
    }
    public line(point p1,point p2) {
        this.p1=p1;
        this.p2=p2;
        this.a=p2.y-p1.y;
        this.b=p1.x-p2.x;
        this.c=p1.y*p2.x-p1.x*p2.y;
    }
    public double d(point p) {
        return a*p.x-b*p.y+c;
    }
    public boolean ishere(point p) {
        if(d(p)==0) {
            return true;
        }
        else
            return false;
    }
    public boolean ison(point p) {
        if(this.ishere(p)&&p.x<=Math.max(p1.x, p2.x)&&p.x>=Math.min(p1.x, p2.x)&&p.y<=Math.max(p1.y, p2.y)&&p.x>=Math.min(p1.y, p2.y)) {
            return true;
        }
        else
        return false;
    }
    
}
class Quad{
    point p1;
    point p2;
    point p3;
    point p4;
    double s;
public Quad(point p1,point p2,point p3,point p4) {
    this.p1=p1;
    this.p2=p2;
    this.p3=p3;
    this.p4=p4;
    point a=new point(p1.x-p3.x,p1.y-p3.y);
    point b=new point(p2.x-p4.x,p2.y-p4.y);
    this.s=a.x*b.y-a.y*b.x;
}
public String s() {
    String str_d = String.valueOf(s);
    str_d = str_d.substring(str_d.indexOf(".") + 1);
    int len = str_d.length();
    len = len > 3 ? 3 : len;
    String out = String.format("%."+len+"f", s);
    return out;
}
public String c() {
    double c=Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))+Math.sqrt((p3.x-p2.x)*(p3.x-p2.x)+(p3.y-p2.y)*(p3.y-p2.y))+Math.sqrt((p3.x-p4.x)*(p3.x-p4.x)+(p3.y-p4.y)*(p3.y-p4.y))+Math.sqrt((p1.x-p4.x)*(p1.x-p4.x)+(p1.y-p4.y)*(p1.y-p4.y));
    String str_d = String.valueOf(c);
    str_d = str_d.substring(str_d.indexOf(".") + 1);
    int len = str_d.length();
    len = len > 3 ? 3 : len;
    String out = String.format("%."+len+"f", c);
    return out;
}
public boolean ischonghe() {
    if(p1.equals(p2)||p1.equals(p3)||p1.equals(p4)||p2.equals(p3)||p2.equals(p4)||p3.equals(p4)) {
        return false;
        }
    else
        return true;
}
public boolean isQuad() {
        line l1=new line(p1,p2);
        line l2=new line(p2,p3);
        line l3=new line(p3,p4);
        line l4=new line(p4,p1);
        if(l1.equals(l2)||l1.equals(l3)||l1.equals(l4)||l2.equals(l3)||l2.equals(l4)||l3.equals(l4)) {
            return false;
        }
        else
            return true;
    }
public boolean isSanjiao() {
    line l1=new line(p1,p2);
    line l2=new line(p2,p3);
    line l3=new line(p3,p4);
    line l4=new line(p4,p1);
    if((l1.equals(l2)&&(!l1.ishere(p4)))||(l1.equals(l4)&&(!l1.ishere(p3)))||(l2.equals(l3)&&(!l2.ishere(p1)))||(l3.equals(l4)&&(!l3.ishere(p2)))||!this.ischonghe()) {
        return true;
    }
    else
        return false;
}
public boolean isprQuad() {
        if(!this.isQuad()) {
            return false;
        }
        double a=(p2.y-p1.y)*(p4.x-p3.x);
        double b=(p4.y-p3.y)*(p2.x-p1.x);
        if(a!=b) {
            return false;
        }
        a=(p4.y-p1.y)*(p3.x-p2.x);
        b=(p3.y-p2.y)*(p4.x-p1.x);
            return (a==b);
    }
public boolean isdiaQuad() {
        if(!this.isprQuad()) {
        return false;
        }
        double a=(p1.y-p3.y)*(p2.y-p4.y);
        double b=(p1.x-p3.x)*(p2.x-p4.x);
            return (a==-b);
    }
public boolean isrecQuad() {
    if(!this.isprQuad()) {
    return false;
    }
    double a=(p1.x-p3.x)*(p1.x-p3.x)+(p1.y-p3.y)*(p1.y-p3.y);
    double b=(p2.x-p4.x)*(p2.x-p4.x)+(p2.y-p4.y)*(p2.y-p4.y);
        return (a==b);
}
public boolean issqQuad() {
    if(this.isrecQuad()&&this.isdiaQuad()) {
        return true;
    }
    else
        return false;
}
public boolean iscvQuad() {//convex quadrilateral, concave quadrilateral
    double t1=(p4.x-p1.x)*(p2.y-p1.y)-(p4.y-p1.y)*(p2.x-p1.x);
    double t2=(p1.x-p2.x)*(p3.y-p2.y)-(p1.y-p2.y)*(p3.x-p2.x);
    double t3=(p2.x-p3.x)*(p4.y-p3.y)-(p2.y-p3.y)*(p4.x-p3.x);
    double t4=(p3.x-p4.x)*(p1.y-p4.y)-(p3.y-p4.y)*(p1.x-p4.x);
    if(t1*t2*t3*t4>0) {
        return true;
    }
    else
        return false;
}
}

 

 The same as the last point and line coding, it took two whole days to write. In contrast, in this coding, point, line, and quadrilateral classes were set up, and the corresponding shape judgment and area calculation methods were written, but finally found Because the basic quadrilateral legal judgment missed the collinear situation of the three points, the test points were wrong by more than half, and the temporary changes at eleven o'clock did not catch up with the ddl, which made the hero full of tears.

2.21 software engineering mid-term exam (OOP) [7-1 points and lines (class design)]

 

 

 

5. Summary:

 

  1. In this topic concentration, I deepened the use and understanding of regular expressions, and realized that I am not proficient in the use of regular expressions. Once the strings are complicated, I don’t know how to use regular expressions to process them. The use of expressions also needs to be strengthened.

 

2. Understand the inheritance and polymorphism relationship in Java, and understand the advantages of polymorphism in object-oriented.

 

3. Learned how subclasses override the methods of parent classes in Java, and why subclasses override the methods of parent classes.

 

4. Understand polymorphism, as well as the meaning of polymorphism and method overriding.

 

5. Learned the use of the Arraylist class and learned to use some ready-made data structures in Java.

 

6. The understanding of the use of container classes is not enough, and the understanding of the structure of container classes will be strengthened in the follow-up study.

 

7. The use of the Arraylist class is not proficient enough, and the understanding is not deep enough. In the follow-up study, strengthen the understanding and application of the Arraylist class.

 

8. I am not familiar with the code implementation method of point-line geometric calculation in Java, and the coding of important self-defined methods is not perfect. It is necessary to strengthen the follow-up study of code conversion of mathematical operations.

 

9. The understanding of the aggregation relationship between classes is not enough, and the understanding of the aggregation relationship between classes needs to be strengthened in the follow-up study.

 

Improvement suggestions and comments:

I hope that there will be more explanations of the difficult points in the homework (such as the geometric calculation method of point and line) in the class, as well as explanations of the topics that have been completed before, let us learn the correct coding ideas and coding skills. Wrong questions do not know the correct way of writing, which not only hits the learning confidence but also fails to learn something, and cannot achieve the purpose of improving learning efficiency.

In addition, I also hope that some hints about the required methods (such as the previous ray method) can be added to the homework to reduce the pitfalls in coding.

 

Posted by uancletus on Mon, 09 May 2022 17:57:28 +0300