第二次博客作业

1.前言

 本次PTA题目集的题目中有关多边形的判断、计算等的题目涉及到了类的应用,如果能先构思好类再进行写代码的话写起来会很容易一些,本次多边形的题目总体的题量还是蛮多的,而且题目的难度比较大,写代码的时候很容易遗漏一些情况,导致一些测试点过不了,由于个人能力不足,本次题目集5我只拿了40分,后面那几个选项实在是写不出来。
img
而此次的期中考试的话涉及到了多态以及链表Arrarylist<>的应用,总体来说题量不多而且不算难,多注意一下题目中的要求就行了。

2.设计与分析

(1)题目集4 7-2 点线形系列4-凸四边形的计算

题目:
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出”not a quadrilateral”
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出”not a quadrilateral”
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出”The line is coincide with one of the lines”。若后四个点不符合四边形或三角形的输入,输出”not a quadrilateral or triangle”。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出”on the triangle或者on the quadrilateral”。若后四个点不符合四边形或三角形,输出”not a quadrilateral or triangle”。

输入格式:
基本格式:选项+”:”+坐标x+”,”+坐标y+” “+坐标x+”,”+坐标y。点的x、y坐标之间以英文”,”分隔,点与点之间以一个英文空格分隔。

输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出”Wrong Format”。
如果符合基本格式,但输入点的数量不符合要求,输出”wrong number of points”。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

选项1、2、3中,若四边形四个点中有重合点,输出”points coincide”。
选项4中,若前两个输入线的点重合,输出”points coincide”。

试题分析:
 本题一共有五种选项对应五种不同的功能,在判断过程中,前三个选项得判断能否构成四边形以及判断四边形的类型,而后两个选项不仅得判断是否是四边形,还得判断是否为三角形,然后再判断直线与多边形的交点的个数,最后再输出交点个数并且依次从小到大输出多边形被分割成的两部分的面积。

源代码展示:

点击展开
import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        if (s.length() <= 2) {
            System.out.println("Wrong Format");
            return;
        }
        String str = "^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$";
        String sRem = s.substring(2);
        int end = sRem.length() - 1;
        if (sRem.charAt(end) == '-' || sRem.charAt(end) == '+' || sRem.charAt(end) == '.' || sRem.charAt(end) == ',' || sRem.charAt(end) == ' ' || s.charAt(0) == ' ') {
            System.out.println("Wrong Format");
            return;
        }
        if (sRem.contains(" -,") || sRem.contains(" +,") || sRem.contains("- ") || sRem.contains("+ ") || sRem.contains(" :") || sRem.contains(": ") || sRem.contains("  ") || sRem.contains("..") || sRem.contains(".,") || sRem.contains(",.") || sRem.contains(". ") || sRem.contains(" .") || sRem.contains(", ") || sRem.contains(" ,")) {
            System.out.println("Wrong Format");
            return;
        }
        int count = 0,count1 = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ',') {
                count++;
            }
            if (s.charAt(i) == ' '){
                count1++;
            }
        }
        double[] num = new double[1000];
        int no = 0;
        String[] sArr1 = s.substring(2).split(" ");
        for (int i = 0; i < sArr1.length; i++) {
            String[] sArr2 = sArr1[i].split(",");
            for (int j = 0; j < sArr2.length; j++) {
                if (!sArr2[j].matches(str)) {
                    System.out.println("Wrong Format");
                    return;
                } else {
                    num[no++] = Double.parseDouble(sArr2[j]);
                }
            }
        }
        double[] douArr = new double[no];
        for (int i = 0; i < no; i++) {
            douArr[i] = num[i];
        }
        String top_2 = s.substring(0, 2);
        switch (top_2) {
            case "1:":
                if (no != 8 || no % count != 0) {
                    if (count == 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    if (no % 2 != 0 || no % count != 0 || count1 >= count) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    JudgmentType1(douArr);
                break;
            case "2:":
                if (no != 8 || no % count != 0) {
                    if (count == 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    if (no % 2 != 0 || no % count != 0 || count1 >= count) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    JudgmentType2(douArr);
                break;
            case "3:":
                if (no != 8 || no % count != 0) {
                    if (count == 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    if (no % 2 != 0 || no % count != 0 || count1 >= count) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    JudgmentType3(douArr);
                break;
            case "4:":
                if (no != 12 || no % count != 0) {
                    if (count == 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    if (no % 2 != 0 || no % count != 0 || count1 >= count) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                }
                    JudgmentType4(douArr);
                break;
            case "5:":
                if (no != 10 || no % count != 0) {
                    if (count == 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    if (no % 2 != 0 || no % count != 0 || count1 >= count) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    JudgmentType5(douArr);
                break;
            default:
                System.out.println("Wrong Format");
                break;
        }
    }

    public static void JudgmentType1(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = douArr[6], y4 = douArr[7];
        if (IsCoordinatesCoincide(douArr)) {
            System.out.println("points coincide");
            return;
        }
        if (IsDataError(douArr) || Collinear(x1, y1, x2, y2, x3, y3) || Collinear(x1, y1, x2, y2, x4, y4) || Collinear(x1, y1, x3, y3, x4, y4) || Collinear(x2, y2, x3, y3, x4, y4)) {
            System.out.println("false false");
            return;
        }
        double A1 = GetA(y1, y2), B1 = GetB(x1, x2);
        double A2 = GetA(y3, y4), B2 = GetB(x3, x4);
        double A3 = GetA(y1, y4), B3 = GetB(x1, x4);
        double A4 = GetA(y2, y3), B4 = GetB(x2, x3);
        if ((A1 * B2 == A2 * B1) && (A3 * B4 == A4 * B3)) {
            System.out.print("true true");
        } else {
            System.out.print("true false");
        }

    }

    public static void JudgmentType2(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = douArr[6], y4 = douArr[7];
        if (IsDataError(douArr) || IsCoordinatesCoincide(douArr) || Collinear(x1, y1, x2, y2, x3, y3) || Collinear(x1, y1, x2, y2, x4, y4) || Collinear(x1, y1, x3, y3, x4, y4) || Collinear(x2, y2, x3, y3, x4, y4)) {
            System.out.println("not a quadrilateral");
            return;
        }
        boolean t1 = Parallel(x1, y1, x2, y2, x3, y3, x4, y4) && Parallel(x2, y2, x3, y3, x1, y1, x4, y4);
        boolean t2 = Equal(x1, y1, x2, y2, x3, y3, x4, y4) && Equal(x2, y2, x3, y3, x1, y1, x4, y4);
        boolean t3 = Equal(x1, y1, x2, y2, x2, y2, x3, y3) && t2;
        boolean t4 = Vertical(x1, y1, x3, y3, x2, y2, x4, y4);
        boolean t5 = Vertical(x1, y1, x2, y2, x2, y2, x3, y3) && Vertical(x1, y1, x2, y2, x1, y1, x4, y4) && Vertical(x2, y2, x3, y3, x3, y3, x4, y4);
        if (t1 && t3 && t4) {
            System.out.print("true ");
        } else {
            System.out.print("false ");
        }
        if (t1 && t2 && t5) {
            System.out.print("true ");
        } else {
            System.out.print("false ");
        }
        if (t1 && t3 && t4 && t5) {
            System.out.print("true");
        } else {
            System.out.print("false");
        }
    }

    public static void JudgmentType3(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = douArr[6], y4 = douArr[7];

        if (IsCoordinatesCoincide(douArr)) {
            System.out.println("points coincide");
            return;
        }
        if (IsDataError(douArr) || Collinear(x1, y1, x2, y2, x3, y3) || Collinear(x1, y1, x2, y2, x4, y4) || Collinear(x1, y1, x3, y3, x4, y4) || Collinear(x2, y2, x3, y3, x4, y4)) {
            System.out.println("not a quadrilateral");
            return;
        }
        double a = SideLength(x1, y1, x2, y2), b = SideLength(x2, y2, x3, y3), c = SideLength(x3, y3, x4, y4), d = SideLength(x1, y1, x4, y4);
        double area1 = Area(new double[]{x1, y1, x2, y2, x3, y3});
        double area2 = Area(new double[]{x1, y1, x2, y2, x4, y4});
        double area3 = Area(new double[]{x1, y1, x3, y3, x4, y4});
        double area4 = Area(new double[]{x2, y2, x3, y3, x4, y4});
        double perimeter = Double.parseDouble(String.format("%.3f", a + b + c + d));
        double s1 = Double.parseDouble(String.format("%.3f", area1 + area3));
        double s2 = Double.parseDouble(String.format("%.3f", area2 + area4));
        double area;
        if (s1 < s2) {
            area = s1;
        } else {
            area = s2;
        }
        if (Math.abs(s1 - s2) > 0) {
            System.out.print("false ");
        } else {
            System.out.print("true ");
        }
        area = Double.parseDouble(String.format("%.3f", area));
        System.out.print(perimeter + " " + area);
    }

    public static void JudgmentType4(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = douArr[6], y4 = douArr[7];
        double x5 = douArr[8], y5 = douArr[9];
        double x6 = douArr[10], y6 = douArr[11];
        double A = GetA(y1, y2), B = GetB(x1, x2), C = GetC(x1, y1, x2, y2);
        double[] polygon = {x3, y3, x4, y4, x5, y5, x6, y6};
        double[] notTriangle = {Double.POSITIVE_INFINITY};
        double[] triangle = IsTriangle(polygon);
        boolean isQuadrilateral = false;
        boolean isTriangle = false;
        if (x1 == x2 && y1 == y2) {
            System.out.println("points coincide");
            return;
        }
        if (IsDataError(polygon) || Collinear(x3, y3, x4, y4, x5, y5) || Collinear(x3, y3, x4, y4, x6, y6) || Collinear(x3, y3, x5, y5, x6, y6) || Collinear(x4, y4, x5, y5, x6, y6)) {
            isQuadrilateral = false;
        } else {
            isQuadrilateral = true;
        }
        if (!isQuadrilateral && (triangle[0] == notTriangle[0])) {
            isTriangle = false;
        } else if (!isQuadrilateral && (triangle[0] != notTriangle[0])) {
            isTriangle = true;
        }

        if (!isQuadrilateral && !isTriangle) {
            System.out.println("not a quadrilateral or triangle");
            return;
        }
        if (isQuadrilateral && !isTriangle) {
            double A1 = GetA(y3, y4), B1 = GetB(x3, x4), C1 = GetC(x3, y3, x4, y4);
            double A2 = GetA(y4, y5), B2 = GetB(x4, x5), C2 = GetC(x4, y4, x5, y5);
            double A3 = GetA(y5, y6), B3 = GetB(x5, x6), C3 = GetC(x5, y5, x6, y6);
            double A4 = GetA(y6, y3), B4 = GetB(x6, x3), C4 = GetC(x6, y6, x3, y3);
            if ((A1 * x1 + B1 * y1 + C1 == 0 && A1 * x2 + B1 * y2 + C1 == 0)
                    || (A2 * x1 + B2 * y1 + C2 == 0 && A2 * x2 + B2 * y2 + C2 == 0)
                    || (A3 * x1 + B3 * y1 + C3 == 0 && A3 * x2 + B3 * y2 + C3 == 0)
                    || (A4 * x1 + B4 * y1 + C4 == 0 && A4 * x2 + B4 * y2 + C4 == 0)) {
                System.out.println("The line is coincide with one of the lines");
            } else {
                double[] point_quadrilateral = GetPoints_Quadrilateral(A, B, C, A1, B1, C1, A2, B2, C2, A3, B3, C3, A4, B4, C4, douArr);
                if (point_quadrilateral[0] == 0 || point_quadrilateral[0] == 1) {
                    System.out.println((int) point_quadrilateral[0]);
                } else {
                    if (point_quadrilateral[1] <= point_quadrilateral[2])
                        System.out.println((int) point_quadrilateral[0] + " " + point_quadrilateral[1] + " " + point_quadrilateral[2]);
                    else
                        System.out.println((int) point_quadrilateral[0] + " " + point_quadrilateral[2] + " " + point_quadrilateral[1]);
                }
            }
        } else if (!isQuadrilateral && isTriangle) {
            double A1 = GetA(triangle[1], triangle[3]), B1 = GetB(triangle[0], triangle[2]), C1 = GetC(triangle[0], triangle[1], triangle[2], triangle[3]);
            double A2 = GetA(triangle[3], triangle[5]), B2 = GetB(triangle[2], triangle[4]), C2 = GetC(triangle[2], triangle[3], triangle[4], triangle[5]);
            double A3 = GetA(triangle[5], triangle[1]), B3 = GetB(triangle[4], triangle[0]), C3 = GetC(triangle[4], triangle[5], triangle[0], triangle[1]);
            if ((A1 * x1 + B1 * y1 + C1 == 0 && A1 * x2 + B1 * y2 + C1 == 0)
                    || (A2 * x1 + B2 * y1 + C2 == 0 && A2 * x2 + B2 * y2 + C2 == 0)
                    || (A3 * x1 + B3 * y1 + C3 == 0 && A3 * x2 + B3 * y2 + C3 == 0)) {
                System.out.println("The line is coincide with one of the lines");
            } else {
                double A5 = GetA(triangle[1], triangle[3]), B5 = GetB(triangle[0], triangle[2]), C5 = GetC(triangle[0], triangle[1], triangle[2], triangle[3]);
                double A6 = GetA(triangle[3], triangle[5]), B6 = GetB(triangle[2], triangle[4]), C6 = GetC(triangle[2], triangle[3], triangle[4], triangle[5]);
                double A7 = GetA(triangle[5], triangle[1]), B7 = GetB(triangle[4], triangle[0]), C7 = GetC(triangle[4], triangle[5], triangle[0], triangle[1]);
                double[] point_triangle = GetPoints_Triangle(A, B, C, A5, B5, C5, A6, B6, C6, A7, B7, C7, triangle, new double[]{x1, y1, x2, y2});
                if (point_triangle[0] == 0 || point_triangle[0] == 1) {
                    System.out.println((int) point_triangle[0]);
                } else {
                    if (point_triangle[1] <= point_triangle[2])
                        System.out.println((int) point_triangle[0] + " " + point_triangle[1] + " " + point_triangle[2]);
                    else
                        System.out.println((int) point_triangle[0] + " " + point_triangle[2] + " " + point_triangle[1]);
                }
            }
        }

    }

    public static void JudgmentType5(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = douArr[6], y4 = douArr[7];
        double x5 = douArr[8], y5 = douArr[9];
        double[] polygon = {x2, y2, x3, y3, x4, y4, x5, y5};
        double[] notTriangle = {Double.POSITIVE_INFINITY};
        double[] triangle = IsTriangle(polygon);
        boolean isQuadrilateral = false;
        boolean isTriangle = false;
        if (IsDataError(polygon) || Collinear(x2, y2, x3, y3, x4, y4) || Collinear(x2, y2, x3, y3, x5, y5) || Collinear(x2, y2, x4, y4, x5, y5) || Collinear(x3, y3, x4, y4, x5, y5)) {
            isQuadrilateral = false;
        } else {
            isQuadrilateral = true;
        }
        if (!isQuadrilateral && (triangle[0] == notTriangle[0])) {
            isTriangle = false;
        } else if (!isQuadrilateral && (triangle[0] != notTriangle[0])) {
            isTriangle = true;
        }
        if (!isQuadrilateral && !isTriangle) {
            System.out.println("not a quadrilateral or triangle");
            return;
        }
        if (isQuadrilateral && !isTriangle) {
            if ((IsIntermediatePoint(x2, y2, x1, y1, x3, y3)) || (IsIntermediatePoint(x3, y3, x1, y1, x4, y4)) || (IsIntermediatePoint(x4, y4, x1, y1, x5, y5)) || (IsIntermediatePoint(x5, y5, x1, y1, x2, y2))) {
                System.out.println("on the quadrilateral");
            } else {
                double area = Area(new double[]{x2, y2, x3, y3, x5, y5}) + Area(new double[]{x3, y3, x4, y4, x5, y5});
                double area1 = Area(new double[]{x1, y1, x2, y2, x3, y3});
                double area2 = Area(new double[]{x1, y1, x3, y3, x4, y4});
                double area3 = Area(new double[]{x1, y1, x4, y4, x5, y5});
                double area4 = Area(new double[]{x1, y1, x5, y5, x2, y2});
                if (Math.abs(area - (area1 + area2 + area3 + area4)) < 1e-4) {
                    System.out.println("in the quadrilateral");
                } else {
                    System.out.println("outof the quadrilateral");
                }
            }
        } else if (!isQuadrilateral && isTriangle) {
            if ((IsIntermediatePoint(triangle[0], triangle[1], x1, y1, triangle[2], triangle[3]))
                    || (IsIntermediatePoint(triangle[2], triangle[3], x1, y1, triangle[4], triangle[5]))
                    || (IsIntermediatePoint(triangle[4], triangle[5], x1, y1, triangle[0], triangle[1]))) {
                System.out.println("on the triangle");
            } else {
                double area = Area(triangle);
                double area1 = Area(new double[]{x1, y1, triangle[0], triangle[1], triangle[2], triangle[3]});
                double area2 = Area(new double[]{x1, y1, triangle[2], triangle[3], triangle[4], triangle[5]});
                double area3 = Area(new double[]{x1, y1, triangle[4], triangle[5], triangle[0], triangle[1]});
                if (Math.abs(area - (area1 + area2 + area3)) < 1e-4) {
                    System.out.println("in the triangle");
                } else {
                    System.out.println("outof the triangle");
                }
            }
        }
    }

    /*----------------------------------------------------------------------------------*/
    public static double[] GetPoints_Quadrilateral(double A, double B, double C, double A1, double B1, double C1, double A2, double B2, double C2, double A3, double B3, double C3, double A4, double B4, double C4, double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = douArr[6], y4 = douArr[7];
        double x5 = douArr[8], y5 = douArr[9];
        double x6 = douArr[10], y6 = douArr[11];
        double[] count = {-1, -1, -1};
        double area = Area(new double[]{x3, y3, x4, y4, x5, y5}) + Area(new double[]{x3, y3, x5, y5, x6, y6});
        double[] d1 = GetPointCount_Line(A, B, C, A1, B1, C1, new double[]{x1, y1, x2, y2, x3, y3, x4, y4});
        double[] d2 = GetPointCount_Line(A, B, C, A2, B2, C2, new double[]{x1, y1, x2, y2, x4, y4, x5, y5});
        double[] d3 = GetPointCount_Line(A, B, C, A3, B3, C3, new double[]{x1, y1, x2, y2, x5, y5, x6, y6});
        double[] d4 = GetPointCount_Line(A, B, C, A4, B4, C4, new double[]{x1, y1, x2, y2, x6, y6, x3, y3});
        boolean t1 = (d1[0] == 1);
        boolean t2 = (d2[0] == 1);
        boolean t3 = (d3[0] == 1);
        boolean t4 = (d4[0] == 1);
        if (((d1[1] == d2[1] && d1[2] == d2[2]) || (d1[1] == d3[1] && d1[2] == d3[2]) || (d1[1] == d4[1] && d1[2] == d4[2]))
                && d1[1] != Double.POSITIVE_INFINITY && d1[2] != Double.POSITIVE_INFINITY) {
            t1 = !t1;
        }
        if (((d2[1] == d3[1] && d2[2] == d3[2]) || (d2[1] == d4[1] && d2[2] == d4[2]))
                && d2[1] != Double.POSITIVE_INFINITY && d2[2] != Double.POSITIVE_INFINITY) {
            t2 = !t2;
        }
        if (d3[1] == d4[1] && d3[2] == d4[2] && d3[1] != Double.POSITIVE_INFINITY && d3[2] != Double.POSITIVE_INFINITY) {
            t3 = !t3;
        }
        if (((A * B1 == A1 * B) && !t2 && !t4) || ((A * B2 == A2 * B) && !t1 && !t3) || ((A * B3 == A3 * B) && !t2 && !t4) || ((A * B4 == A4 * B) && !t1 && !t3)) {
            count[0] = 0;
            return count;
        }
        if (t1) {
            if (t2) {
                double[] newXY1 = GetXY(A, B, C, A1, B1, C1);
                BigDecimal new_x1 = BigDecimal.valueOf(newXY1[0]), new_y1 = BigDecimal.valueOf(newXY1[1]);
                double[] newXY2 = GetXY(A, B, C, A2, B2, C2);
                BigDecimal new_x2 = BigDecimal.valueOf(newXY2[0]), new_y2 = BigDecimal.valueOf(newXY2[1]);
                if (new_x1 == new_x2 && new_y1 == new_y2) {
                    count[0] = 1;
                } else {
                    double area1 = Area(new double[]{x4, y4, new_x1.doubleValue(), new_y1.doubleValue(), new_x2.doubleValue(), new_y2.doubleValue()});
                    double area2 = area - area1;
                    count[0] = 2;
                    count[1] = Double.parseDouble(String.format("%.3f", area1));
                    count[2] = Double.parseDouble(String.format("%.3f", area2));
                }
                return count;
            } else if (t3) {
                double[] newXY1 = GetXY(A, B, C, A1, B1, C1);
                BigDecimal new_x1 = BigDecimal.valueOf(newXY1[0]), new_y1 = BigDecimal.valueOf(newXY1[1]);
                double[] newXY2 = GetXY(A, B, C, A3, B3, C3);
                BigDecimal new_x2 = BigDecimal.valueOf(newXY2[0]), new_y2 = BigDecimal.valueOf(newXY2[1]);
                double area1 = Area(new double[]{x4, y4, new_x1.doubleValue(), new_y1.doubleValue(), new_x2.doubleValue(), new_y2.doubleValue()})
                        + Area(new double[]{x4, y4, x5, y5, new_x2.doubleValue(), new_y2.doubleValue()});
                double area2 = area - area1;
                count[0] = 2;
                count[1] = Double.parseDouble(String.format("%.3f", area1));
                count[2] = Double.parseDouble(String.format("%.3f", area2));
                return count;
            } else if (t4) {
                double[] newXY1 = GetXY(A, B, C, A1, B1, C1);
                BigDecimal new_x1 = BigDecimal.valueOf(newXY1[0]), new_y1 = BigDecimal.valueOf(newXY1[1]);
                double[] newXY2 = GetXY(A, B, C, A4, B4, C4);
                BigDecimal new_x2 = BigDecimal.valueOf(newXY2[0]), new_y2 = BigDecimal.valueOf(newXY2[1]);
                if (new_x1 == new_x2 && new_y1 == new_y2) {
                    count[0] = 1;
                } else {
                    double area1 = Area(new double[]{x3, y3, new_x1.doubleValue(), new_y1.doubleValue(), new_x2.doubleValue(), new_y2.doubleValue()});
                    double area2 = area - area1;
                    count[0] = 2;
                    count[1] = Double.parseDouble(String.format("%.3f", area1));
                    count[2] = Double.parseDouble(String.format("%.3f", area2));
                }
                return count;
            }
        }
        if (t2) {
            if (t3) {
                double[] newXY1 = GetXY(A, B, C, A2, B2, C2);
                BigDecimal new_x1 = BigDecimal.valueOf(newXY1[0]), new_y1 = BigDecimal.valueOf(newXY1[1]);
                double[] newXY2 = GetXY(A, B, C, A3, B3, C3);
                BigDecimal new_x2 = BigDecimal.valueOf(newXY2[0]), new_y2 = BigDecimal.valueOf(newXY2[1]);
                if (new_x1 == new_x2 && new_y1 == new_y2) {
                    count[0] = 1;
                } else {
                    double area1 = Area(new double[]{x4, y4, new_x1.doubleValue(), new_y1.doubleValue(), new_x2.doubleValue(), new_y2.doubleValue()});
                    double area2 = area - area1;
                    count[0] = 2;
                    count[1] = Double.parseDouble(String.format("%.3f", area1));
                    count[2] = Double.parseDouble(String.format("%.3f", area2));
                }
                return count;
            } else if (t4) {
                double[] newXY1 = GetXY(A, B, C, A2, B2, C2);
                BigDecimal new_x1 = BigDecimal.valueOf(newXY1[0]), new_y1 = BigDecimal.valueOf(newXY1[1]);
                double[] newXY2 = GetXY(A, B, C, A4, B4, C4);
                BigDecimal new_x2 = BigDecimal.valueOf(newXY2[0]), new_y2 = BigDecimal.valueOf(newXY2[1]);
                double area1 = Area(new double[]{x5, y5, new_x1.doubleValue(), new_y1.doubleValue(), new_x2.doubleValue(), new_y2.doubleValue()})
                        + Area(new double[]{x5, y5, x6, y6, new_x2.doubleValue(), new_y2.doubleValue()});
                double area2 = area - area1;
                count[0] = 2;
                count[1] = Double.parseDouble(String.format("%.3f", area1));
                count[2] = Double.parseDouble(String.format("%.3f", area2));
                return count;
            }
        }
        if (t3) {
            if (t4) {
                double[] newXY1 = GetXY(A, B, C, A3, B3, C3);
                BigDecimal new_x1 = BigDecimal.valueOf(newXY1[0]), new_y1 = BigDecimal.valueOf(newXY1[1]);
                double[] newXY2 = GetXY(A, B, C, A4, B4, C4);
                BigDecimal new_x2 = BigDecimal.valueOf(newXY2[0]), new_y2 = BigDecimal.valueOf(newXY2[1]);
                if (new_x1 == new_x2 && new_y1 == new_y2) {
                    count[0] = 1;
                } else {
                    double area1 = Area(new double[]{x6, y6, new_x1.doubleValue(), new_y1.doubleValue(), new_x2.doubleValue(), new_y2.doubleValue()});
                    double area2 = area - area1;
                    count[0] = 2;
                    count[1] = Double.parseDouble(String.format("%.3f", area1));
                    count[2] = Double.parseDouble(String.format("%.3f", area2));
                }
                return count;
            }
        }
        if (t4) {
            count[0] = 1;
            return count;
        }
        count[0] = 0;
        return count;
    }

    public static double[] GetPoints_Triangle(double A, double B, double C, double A1, double B1, double C1, double A2, double B2, double C2, double A3, double B3, double C3, double[] douArr, double[] line) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = line[0], y4 = line[1];
        double x5 = line[2], y5 = line[3];
        double[] count = {-1, -1, -1};
        double area = Area(new double[]{x1, y1, x2, y2, x3, y3});
        double[] d1 = GetPointCount_Line(A, B, C, A1, B1, C1, new double[]{x4, y4, x5, y5, x1, y1, x2, y2});
        double[] d2 = GetPointCount_Line(A, B, C, A2, B2, C2, new double[]{x4, y4, x5, y5, x2, y2, x3, y3});
        double[] d3 = GetPointCount_Line(A, B, C, A3, B3, C3, new double[]{x4, y4, x5, y5, x1, y1, x3, y3});
        boolean t1 = (d1[0] == 1);
        boolean t2 = (d2[0] == 1);
        boolean t3 = (d3[0] == 1);
        if (((d1[1] == d2[1] && d1[2] == d2[2]) || (d1[1] == d3[1] && d1[2] == d3[2]))
                && d1[1] != Double.POSITIVE_INFINITY && d1[2] != Double.POSITIVE_INFINITY) {
            t1 = !t1;
        }
        if (d2[1] == d3[1] && d2[2] == d3[2] && d2[1] != Double.POSITIVE_INFINITY && d2[2] != Double.POSITIVE_INFINITY) {
            t2 = !t2;
        }
        if (t1) {
            if (t2) {
                double[] newXY1 = GetXY(A, B, C, A1, B1, C1);
                BigDecimal new_x1 = BigDecimal.valueOf(newXY1[0]), new_y1 = BigDecimal.valueOf(newXY1[1]);
                double[] newXY2 = GetXY(A, B, C, A2, B2, C2);
                BigDecimal new_x2 = BigDecimal.valueOf(newXY2[0]), new_y2 = BigDecimal.valueOf(newXY2[1]);
                if (new_x1 == new_x2 && new_y1 == new_y2) {
                    count[0] = 1;
                } else {
                    double area1 = Area(new double[]{x2, y2, new_x1.doubleValue(), new_y1.doubleValue(), new_x2.doubleValue(), new_y2.doubleValue()});
                    double area2 = area - area1;
                    count[0] = 2;
                    count[1] = Double.parseDouble(String.format("%.3f", area1));
                    count[2] = Double.parseDouble(String.format("%.3f", area2));
                }
                return count;
            }
            if (t3) {
                double[] newXY1 = GetXY(A, B, C, A1, B1, C1);
                BigDecimal new_x1 = BigDecimal.valueOf(newXY1[0]), new_y1 = BigDecimal.valueOf(newXY1[1]);
                double[] newXY2 = GetXY(A, B, C, A3, B3, C3);
                BigDecimal new_x2 = BigDecimal.valueOf(newXY2[0]), new_y2 = BigDecimal.valueOf(newXY2[1]);
                if (new_x1 == new_x2 && new_y1 == new_y2) {
                    count[0] = 1;
                } else {
                    double area1 = Area(new double[]{x1, y1, new_x1.doubleValue(), new_y1.doubleValue(), new_x2.doubleValue(), new_y2.doubleValue()});
                    double area2 = area - area1;
                    count[0] = 2;
                    count[1] = Double.parseDouble(String.format("%.3f", area1));
                    count[2] = Double.parseDouble(String.format("%.3f", area2));
                }
                return count;
            }
        }
        if (t2) {
            if (t3) {
                double[] newXY1 = GetXY(A, B, C, A2, B2, C2);
                BigDecimal new_x1 = BigDecimal.valueOf(newXY1[0]), new_y1 = BigDecimal.valueOf(newXY1[1]);
                double[] newXY2 = GetXY(A, B, C, A3, B3, C3);
                BigDecimal new_x2 = BigDecimal.valueOf(newXY2[0]), new_y2 = BigDecimal.valueOf(newXY2[1]);
                if (new_x1 == new_x2 && new_y1 == new_y2) {
                    count[0] = 1;
                } else {
                    double area1 = Area(new double[]{x3, y3, new_x1.doubleValue(), new_y1.doubleValue(), new_x2.doubleValue(), new_y2.doubleValue()});
                    double area2 = area - area1;
                    count[0] = 2;
                    count[1] = Double.parseDouble(String.format("%.3f", area1));
                    count[2] = Double.parseDouble(String.format("%.3f", area2));
                }
                return count;
            }
        }
        if (t3) {
            count[0] = 1;
            return count;
        }
        count[0] = 0;
        return count;
    }

    public static double[] IsTriangle(double[] douArr) {
        double[] triangle;
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = douArr[6], y4 = douArr[7];
        if ((x1 == x2 && y1 == y2) || (x1 == x3 && y1 == y3)) {
            if (!Collinear(x2, y2, x3, y3, x4, y4)) {
                triangle = new double[]{x2, y2, x3, y3, x4, y4};
                return triangle;
            }
        }
        if ((x3 == x4 && y3 == y4) || (x2 == x4 && y2 == y4) || (x1 == x4 && y1 == y4)) {
            if (!Collinear(x1, y1, x2, y2, x3, y3)) {
                triangle = new double[]{x1, y1, x2, y2, x3, y3};
                return triangle;
            }
        }
        if (x2 == x3 && y2 == y3) {
            if (!Collinear(x1, y1, x2, y2, x4, y4)) {
                triangle = new double[]{x1, y1, x2, y2, x4, y4};
                return triangle;
            }
        }
        if (IsIntermediatePoint(x1, y1, x2, y2, x3, y3)) {
            if (!Collinear(x1, y1, x3, y3, x4, y4)) {
                triangle = new double[]{x1, y1, x3, y3, x4, y4};
                return triangle;
            }
        }
        if (IsIntermediatePoint(x2, y2, x3, y3, x4, y4)) {
            if (!Collinear(x1, y1, x2, y2, x4, y4)) {
                triangle = new double[]{x1, y1, x2, y2, x4, y4};
                return triangle;
            }
        }
        if (IsIntermediatePoint(x4, y4, x1, y1, x2, y2)) {
            if (!Collinear(x2, y2, x3, y3, x4, y4)) {
                triangle = new double[]{x2, y2, x3, y3, x4, y4};
                return triangle;
            }
        }
        if (IsIntermediatePoint(x3, y3, x4, y4, x1, y1)) {
            if (!Collinear(x1, y1, x2, y2, x3, y3)) {
                triangle = new double[]{x1, y1, x2, y2, x3, y3};
                return triangle;
            }
        }

        return new double[]{Double.POSITIVE_INFINITY};
    }

    public static boolean IsIntermediatePoint(double x1, double y1, double x2, double y2, double x3, double y3) {
        double A1 = GetA(y1, y3), B1 = GetB(x1, x3), C1 = GetC(x1, y1, x3, y3);
        boolean t1 = (A1 * x2 + B1 * y2 + C1 == 0);
        boolean t2 = ((x2 - x1) * (x2 - x3) <= 0 && (y2 - y1) * (y2 - y3) <= 0);
        if (t1 && t2) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean Parallel(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
        double A1 = GetA(y1, y2), B1 = GetB(x1, x2);
        double A2 = GetA(y3, y4), B2 = GetB(x3, x4);
        if (A1 * B2 == A2 * B1) {
            return true;
        }
        return false;
    }

    public static boolean Equal(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
        double a = Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
        double b = Math.sqrt(Math.pow(y4 - y3, 2) + Math.pow(x4 - x3, 2));
        if (a == b) {
            return true;
        }
        return false;
    }

    public static boolean Vertical(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
        double A1 = GetA(y2, y1), B1 = GetB(x2, x1);
        double A2 = GetA(y4, y3), B2 = GetB(x4, x3);
        if ((y2 == y1 && x4 == x3) || (x2 == x1 && y4 == y3)) {
            return true;
        }
        if (Math.abs((A1 * A2) / (B1 * B2) + 1) < 1e-6) {
            return true;
        }
        return false;
    }

    public static boolean IsDataError(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double x4 = douArr[6], y4 = douArr[7];
        double A1 = GetA(y1, y2), B1 = GetB(x1, x2), C1 = GetC(x1, y1, x2, y2);
        double A2 = GetA(y3, y4), B2 = GetB(x3, x4), C2 = GetC(x3, y3, x4, y4);
        double A3 = GetA(y2, y3), B3 = GetB(x2, x3), C3 = GetC(x2, y2, x3, y3);
        double A4 = GetA(y1, y4), B4 = GetB(x1, x4), C4 = GetC(x1, y1, x4, y4);
        if (GetPointCount(A1, B1, C1, A2, B2, C2, douArr) == 1 || GetPointCount(A3, B3, C3, A4, B4, C4, new double[]{x2, y2, x3, y3, x1, y1, x4, y4}) == 1) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean IsCoordinatesCoincide(double[] douArr) {
        boolean flag = false;
        for (int i = 0, j = 1; j < douArr.length - 2; i += 2, j += 2) {
            for (int k = i + 2, l = j + 2; l < douArr.length; k += 2, l += 2) {
                if (douArr[i] == douArr[k] && douArr[j] == douArr[l]) {
                    flag = true;
                }
            }
        }
        return flag;
    }

    public static boolean Collinear(double x1, double y1, double x2, double y2, double x3, double y3) {
        double A1 = GetA(y1, y2), B1 = GetB(x1, x2), C1 = GetC(x1, y1, x2, y2);
        if (A1 * x3 + B1 * y3 + C1 == 0) {
            return true;
        }
        return false;
    }

    public static double GetA(double y1, double y2) {
        double A;
        A = y2 - y1;
        return A;
    }

    public static double GetB(double x1, double x2) {
        double B;
        B = x1 - x2;
        return B;
    }

    public static double GetC(double x1, double y1, double x2, double y2) {
        double C;
        C = x2 * y1 - x1 * y2;
        return C;
    }

    public static double SideLength(double x1, double y1, double x2, double y2) {
        double d = Math.sqrt((Math.pow(y2 - y1, 2.0)) + Math.pow(x2 - x1, 2.0));
        return d;
    }

    public static double Area(double[] douArr) {
        double x1 = douArr[0], y1 = douArr[1];
        double x2 = douArr[2], y2 = douArr[3];
        double x3 = douArr[4], y3 = douArr[5];
        double a = SideLength(x1, y1, x2, y2);
        double b = SideLength(x1, y1, x3, y3);
        double c = SideLength(x2, y2, x3, y3);
        double p = (a + b + c) / 2.0;
        double area = Math.sqrt(p * (p - a) * (p - b) * (p - c));
        return Double.parseDouble(String.format("%.3f", area));
    }

    public static double[] GetXY(double A1, double B1, double C1, double A2, double B2, double C2) {
        double[] XY = new double[2];
        XY[0] = (C2 * B1 - C1 * B2) / (A1 * B2 - A2 * B1);
        XY[1] = (C2 * A1 - C1 * A2) / (B1 * A2 - B2 * A1);
        return XY;
    }

    public static int GetPointCount(double A1, double B1, double C1, double A2, double B2, double C2, double[] douArr) {
        int count = 0;
        if (A1 * B2 == A2 * B1) {
            return count;
        }
        double[] newXY1 = GetXY(A1, B1, C1, A2, B2, C2);
        BigDecimal x1 = BigDecimal.valueOf(newXY1[0]), y1 = BigDecimal.valueOf(newXY1[1]);
        if ((x1.doubleValue() == douArr[0] && y1.doubleValue() == douArr[1]) || (x1.doubleValue() == douArr[2] && y1.doubleValue() == douArr[3]) || (x1.doubleValue() == douArr[4] && y1.doubleValue() == douArr[5]) || (x1.doubleValue() == douArr[6] && y1.doubleValue() == douArr[7])) {
            return count;
        }
        boolean t1 = ((x1.doubleValue() - douArr[0]) * (x1.doubleValue() - douArr[2]) <= 0 && (y1.doubleValue() - douArr[1]) * (y1.doubleValue() - douArr[3]) <= 0);
        boolean t2 = ((x1.doubleValue() - douArr[4]) * (x1.doubleValue() - douArr[6]) <= 0 && (y1.doubleValue() - douArr[5]) * (y1.doubleValue() - douArr[7]) <= 0);
        if (t1 && t2) {
            count = 1;
        }
        return count;
    }

    public static double[] GetPointCount_Line(double A1, double B1, double C1, double A2, double B2, double C2, double[] douArr) {
        double[] count = {0, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY};
        if (A1 * B2 == A2 * B1) {
            return count;
        }
        double[] newXY1 = GetXY(A1, B1, C1, A2, B2, C2);
        BigDecimal x1 = BigDecimal.valueOf(newXY1[0]), y1 = BigDecimal.valueOf(newXY1[1]);
        boolean t1 = ((x1.doubleValue() - douArr[4]) * (x1.doubleValue() - douArr[6]) <= 0 && (y1.doubleValue() - douArr[5]) * (y1.doubleValue() - douArr[7]) <= 0);
        if (t1) {
            count[0] = 1;
            count[1] = x1.doubleValue();
            count[2] = y1.doubleValue();
            return count;
        }
        return count;
    }

    /*----------------------------------------------------------------------------------*/
}

SourceMonitor生成的报表内容:
img
img
img
类图:
img

代码分析总结:
 从上述的SourceMonitor生成的报表内容和类图来看,我这题的代码还有很大的改进空间,开始写这题的时候还没有形成写类的习惯,总是习惯性地构造函数,这样使得代码的冗余度很高,复用性不好,并且不美观。从类图来看,有些函数具有相似的功能并且含有大部分重复的代码,如GetPoints_Quadrilateral()、GetPoints_Triangle()和GetPointCount()这三个函数,虽然具体功能不一样,但核心都是获取交点或交点数量,其中包含了许多重复的代码。如果改用类的方法来写,可以避免写许多重复的代码,降低冗余度。

(2)题目集5 7-1 点线形系列5-凸五边形的计算-1

题目:
用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出”not a pentagon”
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出”The line is coincide with one of the lines”。若后五个点不符合五边形输入,若前两点重合,输出”points coincide”。

以上3选项中,若输入的点无法构成多边形,则输出”not a polygon”。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:
基本格式:选项+”:”+坐标x+”,”+坐标y+” “+坐标x+”,”+坐标y。点的x、y坐标之间以英文”,”分隔,点与点之间以一个英文空格分隔。

输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出”Wrong Format”。
如果符合基本格式,但输入点的数量不符合要求,输出”wrong number of points”。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0.

试题分析:
 本题选项一需要判断出多边形是否是五边形;选项二需要判断五边形的类型,是凸五边形还是凹五边形;选项三需要判断多边形的类型,是五边形、四边形还是三角形,并且计算出由直线切割后的面积,并将两部分的面积从小到大依次输出。

源代码展示:

Main类
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        DataCheck dataCheck = new DataCheck();
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        dataCheck.Data(s);

    }
}

DataCheck类
import java.util.ArrayList;

public class DataCheck {
    public static ArrayList<Double> doubleArrayList = new ArrayList<>();

    public void Data(String s) {
        if (s.length() <= 2) {
            System.out.println("Wrong Format");
            return;
        }
        String str = "^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$";
        String sRem = s.substring(2);
        int end = sRem.length() - 1;
        if (sRem.charAt(end) == '-' || sRem.charAt(end) == '+' || sRem.charAt(end) == '.' || sRem.charAt(end) == ',' || sRem.charAt(end) == ' ' || s.charAt(0) == ' ') {
            System.out.println("Wrong Format");
            return;
        }
        if (sRem.contains(" -,") || sRem.contains(" +,") || sRem.contains("- ") || sRem.contains("+ ") || sRem.contains(" :") || sRem.contains(": ") || sRem.contains("  ") || sRem.contains("..") || sRem.contains(".,") || sRem.contains(",.") || sRem.contains(". ") || sRem.contains(" .") || sRem.contains(", ") || sRem.contains(" ,")) {
            System.out.println("Wrong Format");
            return;
        }
        int count = 0, count1 = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ',') {
                count++;
            }
            if (s.charAt(i) == ' ') {
                count1++;
            }
        }
        int no = 0;
        String[] sArr1 = s.substring(2).split(" ");
        for (int i = 0; i < sArr1.length; i++) {
            String[] sArr2 = sArr1[i].split(",");
            for (int j = 0; j < sArr2.length; j++) {
                if (!sArr2[j].matches(str)) {
                    System.out.println("Wrong Format");
                    return;
                } else {
                    doubleArrayList.add(Double.parseDouble(sArr2[j]));
                    no++;
                }
            }
        }
        Point p = new Point();
        ArrayList<Point> points = p.XY_TO_POINT(doubleArrayList);
        String top_2 = s.substring(0, 2);
        boolean b = no % 2 != 0 || no % count != 0 || count1 >= count;
        switch (top_2) {
            case "1:":
                if (no != 10 || no % count != 0) {
                    if (b) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    JudgmentType1(points);
                break;
            case "2:":
                if (no != 10 || no % count != 0) {
                    if (b) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else {
                    Polygon polygon = new Polygon();
                    if (polygon.IsPentagon(points)) {
                        JudgmentType2(points);
                    } else {
                        System.out.println("not a pentagon");
                    }
                }
                break;
            case "3:":
                if (no != 14 || no % count != 0) {
                    if (b) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    JudgmentType3(points);
                break;
            case "4:":
                if (no != 12 || no % count != 0) {
                    if (b) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    System.out.println(0);
                break;
            case "5:":
                if (no != 10 || no % count != 0) {
                    if (b) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    System.out.println("wrong number of points");
                    return;
                } else
                    System.out.println(0);
                break;
            default:
                System.out.println("Wrong Format");
                break;
        }
    }

    public static void JudgmentType1(ArrayList<Point> doubleArrayList) {
        Polygon polygon = new Polygon();
        if (polygon.IsPentagon(doubleArrayList)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }

    public static void JudgmentType2(ArrayList<Point> doubleArrayList) {
        Polygon polygon = new Polygon();
        double[] arr = polygon.PentagonType(doubleArrayList);
        if (arr[0] == 1) {
            System.out.println("true " + Double.valueOf(String.format("%.3f", arr[1])) + " " + Double.valueOf(String.format("%.3f", arr[2])));
        } else {
            System.out.println("false");
        }
    }

    public static void JudgmentType3(ArrayList<Point> doubleArrayList) {
        Point p1 = doubleArrayList.get(0), p2 = doubleArrayList.get(1);
        Line line = new Line();
        line.p1 = p1;
        line.p2 = p2;
        line.setABC();
        if (p1.IsSame(p1, p2)) {
            System.out.println("points coincide");
            return;
        }
        Polygon polygon = new Polygon();
        ArrayList<Point> polygonPoints = new ArrayList<>();
        for (int i = 2, count = 0; i < doubleArrayList.size(); i++, count++) {
            Point d = doubleArrayList.get(i);
            polygonPoints.add(count, d);
        }
        ArrayList<Point> new_polygon = polygon.DeletePoint(polygonPoints);
        boolean t = false, q = false, p = false;
        if (new_polygon.size() == 3) {
            t = polygon.IsTriangle(new_polygon);
            if (t) {
                Point p3 = new_polygon.get(0);
                Point p4 = new_polygon.get(1);
                Point p5 = new_polygon.get(2);
                Line l1 = new Line();
                l1.p1 = p3;
                l1.p2 = p4;
                l1.SetLength();
                l1.setABC();
                Line l2 = new Line();
                l2.p1 = p4;
                l2.p2 = p5;
                l2.SetLength();
                l2.setABC();
                Line l3 = new Line();
                l3.p1 = p5;
                l3.p2 = p3;
                l3.SetLength();
                l3.setABC();
                if ((Line.Collinear(l1, p1) && Line.Collinear(l1, p2)) || (Line.Collinear(l2, p1) && Line.Collinear(l2, p2)) || (Line.Collinear(l3, p1) && Line.Collinear(l3, p2))) {
                    System.out.println("The line is coincide with one of the lines");
                    return;
                }
                Point errorPoint = new Point();
                errorPoint.setXY(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
                Point point1 = Line.GetPointCount_Line(line, l1);
                Point point2 = Line.GetPointCount_Line(line, l2);
                Point point3 = Line.GetPointCount_Line(line, l3);
                boolean t1 = (point1.IsSame(point1, errorPoint));
                boolean t2 = (point2.IsSame(point2, errorPoint));
                boolean t3 = (point3.IsSame(point3, errorPoint));
                if (point1.IsSame(point1, point2)) {
                    t1 = !t1;
                }
                if (point2.IsSame(point2, point3)) {
                    t2 = !t2;
                }
                if (point3.IsSame(point3, point1)) {
                    t3 = !t3;
                }
                ArrayList<Point> arrayList = new ArrayList<>();
                arrayList.add(0, point1);
                arrayList.add(1, point2);
                arrayList.add(2, point3);
                for (int i = 0; i < arrayList.size() - 1; i++) {
                    for (int j = i + 1; j < arrayList.size(); j++) {
                        if (arrayList.get(j).IsSame(arrayList.get(j), errorPoint)
                                || arrayList.get(j).IsSame(arrayList.get(j), arrayList.get(i))) {
                            arrayList.remove(j);
                            j--;
                        }
                    }

                }
                if (arrayList.get(0).IsSame(arrayList.get(0), errorPoint)) {
                    arrayList.remove(0);
                }

                if (arrayList.size() == 0) {
                    System.out.println(0);
                }
                if (arrayList.size() == 1) {
                    System.out.println(1);
                }
                if (arrayList.size() == 2) {
                    double area = Double.parseDouble(String.format("%.3f", Area.CalculatingArea(p3, p4, p5)));
                    if (!t1) {
                        if (!t2 && t3) {
                            double area1 = Double.parseDouble(String.format("%.3f", Area.CalculatingArea(p4, point1, point2)));
                            double area2 = Double.parseDouble(String.format("%.3f", area - area1));
                            if (area1 <= area2) {
                                System.out.println(arrayList.size() + " " + area1 + " " + area2);
                            } else {
                                System.out.println(arrayList.size() + " " + area2 + " " + area1);
                            }
                        }
                        if (t2 && !t3) {
                            double area1 = Double.parseDouble(String.format("%.3f", Area.CalculatingArea(p3, point1, point3)));
                            double area2 = Double.parseDouble(String.format("%.3f", area - area1));
                            if (area1 <= area2) {
                                System.out.println(arrayList.size() + " " + area1 + " " + area2);
                            } else {
                                System.out.println(arrayList.size() + " " + area2 + " " + area1);
                            }
                        }
                    }
                    if (!t2) {
                        if (!t3 && t1) {
                            double area1 = Double.parseDouble(String.format("%.3f", Area.CalculatingArea(p5, point2, point3)));
                            double area2 = Double.parseDouble(String.format("%.3f", area - area1));
                            if (area1 <= area2) {
                                System.out.println(arrayList.size() + " " + area1 + " " + area2);
                            } else {
                                System.out.println(arrayList.size() + " " + area2 + " " + area1);
                            }
                        }
                    }
                }
            }
        }
        if (new_polygon.size() == 4) {
            q = polygon.IsQuadrilateral(new_polygon);
            if (q) {

            }
        }
        if (new_polygon.size() == 5) {
            p = polygon.IsPentagon(new_polygon);

        }
        if (!t && !q && !p) {
            System.out.println("not a polygon");
            return;
        }

    }
}


Point类
import java.util.ArrayList;

public class Point {
    private double x, y;     //点坐标

    public void setXY(double x, double y) {      //设置点坐标
        this.x = x;
        this.y = y;
    }

    public double getX() {      //获得坐标x
        return x;
    }

    public double getY() {      //获得坐标y
        return y;
    }

    public boolean IsSame(Point p1, Point p2) {       //判断点是否重合
        if (p1.getX() == p2.getX() && p1.getY() == p2.getY()) {
            return true;
        }
        return false;
    }

    public ArrayList<Point> XY_TO_POINT(ArrayList<Double> doubleArrayList) {
        ArrayList<Point> points = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < doubleArrayList.size() / 2; i++, count += 2) {
            Point p = new Point();
            p.setXY(doubleArrayList.get(count), doubleArrayList.get(count + 1));
            points.add(p);
        }
        return points;
    }
}


Line类
import java.math.BigDecimal;

public class Line {
    Point p1 = new Point(), p2 = new Point();       //线段两端点
    private double length;      //线段长度
    double A, B, C;     //直线的一般式

    public void SetLength() {        //计算线段的长度
        this.length = Math.sqrt((Math.pow(p2.getY() - p1.getY(), 2.0)) + Math.pow(p2.getX() - p1.getX(), 2.0));
    }

    public double GetLength() {        //计算线段的长度
        return this.length;
    }

    public static double GetA(Point p1, Point p2) {
        double A;
        A = p2.getY() - p1.getY();
        return A;
    }

    public static double GetB(Point p1, Point p2) {
        double B;
        B = p1.getX() - p2.getX();
        return B;
    }

    public static double GetC(Point p1, Point p2) {
        double C;
        C = p2.getX() * p1.getY() - p1.getX() * p2.getY();
        return C;
    }

    public void setABC() {      //获取直线的一般式
        this.A = GetA(this.p1, this.p2);
        this.B = GetB(this.p1, this.p2);
        this.C = GetC(this.p1, this.p2);
    }

    public static boolean Collinear(Line line, Point p) {
        double A = line.A, B = line.B, C = line.C;
        return A * p.getX() + B * p.getY() + C == 0;
    }

    public static boolean IsIntermediatePoint(Point p1, Point p2, Point p) {
        Line line = new Line();
        line.p1 = p1;
        line.p2 = p2;
        line.setABC();
        double A = line.A, B = line.B, C = line.C;
        boolean t1 = (A * p.getX() + B * p.getY() + C == 0);
        boolean t2 = ((p.getX() - line.p1.getX()) * (p.getX() - line.p2.getX()) <= 0 && (p.getY() - line.p1.getY()) * (p.getY() - line.p2.getY()) <= 0);
        return (t1 && t2);
    }

    public static boolean GetPointCount_Segment(Line l1, Line l2) {
        if (l1.A * l2.B == l2.A * l1.B) {
            return false;
        }
        Point point = new Point();
        point.setXY((l2.C * l1.B - l1.C * l2.B) / (l1.A * l2.B - l2.A * l1.B), (l2.C * l1.A - l1.C * l2.A) / (l1.B * l2.A - l2.B * l1.A));
        BigDecimal x1 = BigDecimal.valueOf(point.getX()), y1 = BigDecimal.valueOf(point.getY());
        boolean t1 = ((x1.doubleValue() - l1.p1.getX()) * (x1.doubleValue() - l1.p2.getX()) < 0
                || (y1.doubleValue() - l1.p1.getY()) * (y1.doubleValue() - l1.p2.getY()) < 0);
        boolean t2 = ((x1.doubleValue() - l2.p1.getX()) * (x1.doubleValue() - l2.p2.getX()) < 0
                || (y1.doubleValue() - l2.p1.getY()) * (y1.doubleValue() - l2.p2.getY()) < 0);
        boolean t3 = ((x1.doubleValue() - l1.p1.getX()) * (x1.doubleValue() - l1.p2.getX()) <= 0
                && (y1.doubleValue() - l1.p1.getY()) * (y1.doubleValue() - l1.p2.getY()) <= 0);
        boolean t4 = ((x1.doubleValue() - l2.p1.getX()) * (x1.doubleValue() - l2.p2.getX()) <= 0
                && (y1.doubleValue() - l2.p1.getY()) * (y1.doubleValue() - l2.p2.getY()) <= 0);
        boolean t5 = ((point.getX() == l1.p1.getX() && point.getY() == l1.p1.getY())
                || (point.getX() == l1.p2.getX() && point.getY() == l1.p2.getY())
                || (point.getX() == l2.p1.getX() && point.getY() == l2.p1.getY())
                || (point.getX() == l2.p2.getX() && point.getY() == l2.p2.getY()));
        if (t3 && t4 && t5) {
            return true;
        }
        if (t1 && t2) {
            return true;
        }
        return false;
    }

    public static Point GetPointCount_Line(Line l1, Line l2) {
        Point point = new Point();
        point.setXY(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
        if (l1.A * l2.B == l2.A * l1.B) {
            return point;
        }
        Point p = new Point();
        p.setXY((l2.C * l1.B - l1.C * l2.B) / (l1.A * l2.B - l2.A * l1.B), (l2.C * l1.A - l1.C * l2.A) / (l1.B * l2.A - l2.B * l1.A));
        BigDecimal x1 = BigDecimal.valueOf(p.getX()), y1 = BigDecimal.valueOf(p.getY());
        boolean t1 = ((x1.doubleValue() - l2.p1.getX()) * (x1.doubleValue() - l2.p2.getX()) <= 0
                && (y1.doubleValue() - l2.p1.getY()) * (y1.doubleValue() - l2.p2.getY()) <= 0);
        boolean t2 = (t1 && ((point.getX() == l1.p1.getX() && point.getY() == l1.p1.getY())
                || (point.getX() == l1.p2.getX() && point.getY() == l1.p2.getY())
                || (point.getX() == l2.p1.getX() && point.getY() == l2.p1.getY())
                || (point.getX() == l2.p2.getX() && point.getY() == l2.p2.getY())));
        if (t1 || t2) {
            return p;
        }
        return point;
    }
}


Area类
public class Area {
    public static double CalculatingArea(Point p1, Point p2, Point p3) {
        Line l1 = new Line();
        l1.p1 = p1;
        l1.p2 = p2;
        l1.SetLength();
        Line l2 = new Line();
        l2.p1 = p2;
        l2.p2 = p3;
        l2.SetLength();
        Line l3 = new Line();
        l3.p1 = p3;
        l3.p2 = p1;
        l3.SetLength();
        double p = (l1.GetLength() + l2.GetLength() + l3.GetLength()) / 2.0;
        return Math.sqrt(p * (p - l1.GetLength()) * (p - l2.GetLength()) * (p - l3.GetLength()));
    }
}


Polygon类
import java.util.ArrayList;

public class Polygon {

    public boolean IsTriangle(ArrayList<Point> doubleArrayList) {
        boolean isTriangle = false;
        Point p1 = doubleArrayList.get(0);
        Point p2 = doubleArrayList.get(1);
        Point p3 = doubleArrayList.get(2);
        Line l1 = new Line();
        l1.p1 = p1;
        l1.p2 = p2;
        l1.SetLength();
        Line l2 = new Line();
        l2.p1 = p2;
        l2.p2 = p3;
        l2.SetLength();
        Line l3 = new Line();
        l3.p1 = p3;
        l3.p2 = p1;
        l3.SetLength();
        if (Line.Collinear(l1, p3) && (l1.GetLength() + l2.GetLength() > l3.GetLength())
                && (l1.GetLength() + l3.GetLength() > l2.GetLength())
                && (l2.GetLength() + l3.GetLength() > l1.GetLength())) {
            isTriangle = true;
        }
        return isTriangle;
    }

    public boolean IsQuadrilateral(ArrayList<Point> doubleArrayList) {
        boolean isQuadrilateral = false;
        Point p1 = doubleArrayList.get(0);
        Point p2 = doubleArrayList.get(1);
        Point p3 = doubleArrayList.get(2);
        Point p4 = doubleArrayList.get(3);
        Point p5 = doubleArrayList.get(4);
        Line l1 = new Line();
        l1.p1 = p1;
        l1.p2 = p2;
        Line l2 = new Line();
        l2.p1 = p2;
        l2.p2 = p3;
        Line l3 = new Line();
        l3.p1 = p3;
        l3.p2 = p4;
        Line l4 = new Line();
        l4.p1 = p4;
        l4.p2 = p5;
        Line l5 = new Line();
        l5.p1 = p5;
        l5.p2 = p1;


        return isQuadrilateral;
    }

    public boolean IsPentagon(ArrayList<Point> doubleArrayList) {
        Point p1 = doubleArrayList.get(0);
        Point p2 = doubleArrayList.get(1);
        Point p3 = doubleArrayList.get(2);
        Point p4 = doubleArrayList.get(3);
        Point p5 = doubleArrayList.get(4);
        Line l1 = new Line();
        l1.p1 = p1;
        l1.p2 = p2;
        l1.setABC();
        Line l2 = new Line();
        l2.p1 = p2;
        l2.p2 = p3;
        l2.setABC();
        Line l3 = new Line();
        l3.p1 = p3;
        l3.p2 = p4;
        l3.setABC();
        Line l4 = new Line();
        l4.p1 = p4;
        l4.p2 = p5;
        l4.setABC();
        Line l5 = new Line();
        l5.p1 = p5;
        l5.p2 = p1;
        l5.setABC();
        if (Line.GetPointCount_Segment(l1, l3) || Line.GetPointCount_Segment(l2, l4) || Line.GetPointCount_Segment(l3, l5)) {
            return false;
        }
        if (Line.Collinear(l1, p3) || Line.Collinear(l2, p4) || Line.Collinear(l3, p5) || Line.Collinear(l4, p1) || Line.Collinear(l5, p2)) {
            return false;
        }
        return true;
    }

    public double[] PentagonType(ArrayList<Point> doubleArrayList) {
        //arr数组数据含义     [0]:五边形类型  [1]:周长  [2]:面积
        double[] arr = {-1, 0, 0};
        //五边形的五个顶点
        Point p1 = doubleArrayList.get(0);
        Point p2 = doubleArrayList.get(1);
        Point p3 = doubleArrayList.get(2);
        Point p4 = doubleArrayList.get(3);
        Point p5 = doubleArrayList.get(4);
        //五条边的向量
        Point n1 = new Point();
        n1.setXY(p2.getX() - p1.getX(), p2.getY() - p1.getY());
        Point n2 = new Point();
        n2.setXY(p3.getX() - p2.getX(), p3.getY() - p2.getY());
        Point n3 = new Point();
        n3.setXY(p4.getX() - p3.getX(), p4.getY() - p3.getY());
        Point n4 = new Point();
        n4.setXY(p5.getX() - p4.getX(), p5.getY() - p4.getY());
        Point n5 = new Point();
        n5.setXY(p1.getX() - p5.getX(), p1.getY() - p5.getY());
        //获得五条边的长度
        Line l1 = new Line();
        l1.p1 = p1;
        l1.p2 = p2;
        l1.SetLength();
        Line l2 = new Line();
        l2.p1 = p2;
        l2.p2 = p3;
        l2.SetLength();
        Line l3 = new Line();
        l3.p1 = p3;
        l3.p2 = p4;
        l3.SetLength();
        Line l4 = new Line();
        l4.p1 = p4;
        l4.p2 = p5;
        l4.SetLength();
        Line l5 = new Line();
        l5.p1 = p5;
        l5.p2 = p1;
        l5.SetLength();
        double d1 = n1.getX() * n2.getY() - n1.getY() * n2.getX();
        double d2 = n2.getX() * n3.getY() - n2.getY() * n3.getX();
        double d3 = n3.getX() * n4.getY() - n3.getY() * n4.getX();
        double d4 = n4.getX() * n5.getY() - n4.getY() * n5.getX();
        double d5 = n5.getX() * n1.getY() - n5.getY() * n1.getX();
        boolean t1 = d1 * d2 >= 0;
        boolean t2 = d2 * d3 >= 0;
        boolean t3 = d3 * d4 >= 0;
        boolean t4 = d4 * d5 >= 0;
        boolean t5 = d5 * d1 >= 0;
        if ((t1 && t2 && t3 && t4 && t5) || (!t1 && !t2 && !t3 && !t4 && !t5)) {
            arr[0] = 1;
            arr[1] = l1.GetLength() + l2.GetLength() + l3.GetLength() + l4.GetLength() + l5.GetLength();
            arr[2] = Area.CalculatingArea(p1, p2, p3) + Area.CalculatingArea(p1, p3, p4) + Area.CalculatingArea(p1, p4, p5);
        }

        return arr;
    }

    public ArrayList<Point> DeletePoint(ArrayList<Point> doubleArrayList) {
        ArrayList<Point> polygon = new ArrayList<>();
        Point p1 = doubleArrayList.get(0);
        Point p2 = doubleArrayList.get(1);
        Point p3 = doubleArrayList.get(2);
        Point p4 = doubleArrayList.get(3);
        Point p5 = doubleArrayList.get(4);
        polygon.add(p1);
        polygon.add(p2);
        polygon.add(p3);
        polygon.add(p4);
        polygon.add(p5);
        for (int i = 0; i < polygon.size() - 1; i++) {
            for (int j = i + 1; j < polygon.size(); j++) {
                if (polygon.get(i).IsSame(polygon.get(i), polygon.get(j))) {
                    polygon.remove(j);
                }
            }
            if (i > 0 && Line.IsIntermediatePoint(polygon.get(i - 1), polygon.get(i + 1), polygon.get(i))) {
                polygon.remove(i);
            }
        }
        if (polygon.get(0).IsSame(polygon.get(0), polygon.get(polygon.size() - 1))) {
            polygon.remove(polygon.get(polygon.size() - 1));
        }
        if (Line.IsIntermediatePoint(polygon.get(polygon.size() - 2), polygon.get(0), polygon.get(polygon.size() - 1))) {
            polygon.remove(polygon.get(polygon.size() - 1));
        }
        return polygon;
    }
}


SourceMonitor生成的报表内容:
img
img
img
img
img
img

类图:
img

代码分析总结:
 这一题的代码将许多不同的功能写入了不同的类中,使得代码的复用性较好。但是通过观察上述的报表图知道,我本题的代码指标仍然有几项未达到被测量维度的期望值,因此还有待改进。

(3)题目集5 7-2 点线形系列5-凸五边形的计算-2

题目:
用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
4:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间是否存在包含关系(一个多边形有一条或多条边与另一个多边形重合,其他部分都包含在另一个多边形内部,也算包含)。
两者存在六种关系:1、分离(完全无重合点) 2、连接(只有一个点或一条边重合) 3、完全重合 4、被包含(前一个多边形在后一个多边形的内部)5、交错 6、包含(后一个多边形在前一个多边形的内部)。
各种关系的输出格式如下:
1、no overlapping area between the previous triangle/quadrilateral/ pentagon and the following triangle/quadrilateral/ pentagon
2、the previous triangle/quadrilateral/ pentagon is connected to the following triangle/quadrilateral/ pentagon
3、the previous triangle/quadrilateral/ pentagon coincides with the following triangle/quadrilateral/ pentagon
4、the previous triangle/quadrilateral/ pentagon is inside the following triangle/quadrilateral/ pentagon
5、the previous triangle/quadrilateral/ pentagon is interlaced with the following triangle/quadrilateral/ pentagon
6、the previous triangle/quadrilateral/ pentagon contains the following triangle/quadrilateral/ pentagon

5:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),输出两个多边形公共区域的面积。注:只考虑每个多边形被另一个多边形分割成最多两个部分的情况,不考虑一个多边形将另一个分割成超过两个区域的情况。
6:输入六个点坐标,输出第一个是否在后五个点所构成的多边形(限定为凸多边形,不考虑凹多边形),的内部(若是五边形输出in the pentagon/outof the pentagon,若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。输入入错存在冗余点要排除,冗余点的判定方法见选项5。如果点在多边形的某条边上,输出”on the triangle/on the quadrilateral/on the pentagon”。
以上4、5、6选项输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:
基本格式:选项+”:”+坐标x+”,”+坐标y+” “+坐标x+”,”+坐标y。点的x、y坐标之间以英文”,”分隔,点与点之间以一个英文空格分隔。

输出格式:
输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

这一题没写出来
img

(4)期中考试 7-1 点与线(类设计)

题目:
设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:(x,y),数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]。若输入有误,系统则直接输出Wrong Format

设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:


Copy
    The line's color is:颜色值
    The line's begin point's Coordinate is:
    (x1,y1)
    The line's end point's Coordinate is:
    (x2,y2)
    The line's length is:长度值

其中,所有数值均保留两位小数,建议可用String.format(“%.2f”, data)方法。

设计类图如下图所示。
img
题目要求:在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的display()方法进行输出。

以下情况为无效作业
无法运行
设计不符合所给类图要求
未通过任何测试点测试
判定为抄袭

输入格式:
分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:
The line’s color is:颜色值
The line’s begin point’s Coordinate is:
(x1,y1)
The line’s end point’s Coordinate is:
(x2,y2)
The line’s length is:长度值

试题分析:
 这题相对来说比较简单,只需要构建Main类、Point类和Line类,然后实现display()方法即可。

源代码展示:

查看代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Point p1 = new Point();
        Point p2 = new Point();
        Line l = new Line();
        Scanner sc = new Scanner(System.in);
        p1.setX(sc.nextFloat());
        p1.setY(sc.nextFloat());
        p2.setX(sc.nextFloat());
        p2.setY(sc.nextFloat());
        if (((p1.getX() <= 0 || p1.getX() > 200) || (p1.getY() <= 0 || p1.getY() > 200))
                || ((p2.getX() <= 0 || p2.getX() > 200) || (p2.getY() <= 0 || p2.getY() > 200))) {
            System.out.println("Wrong Format");
            return;
        }
        l.setP1(p1);
        l.setP2(p2);
        l.setColor(sc.next());
        l.disPlay();
    }
}

class Line {
    Point p1 = new Point(), p2 = new Point();       //线段两端点
    private String color;

    public void Line() {
    }

    public void Line(Point p1, Point p2, String color) {
        setP1(p1);
        setP2(p2);
        setColor(color);
    }

    public Point getP1() {
        return p1;
    }

    public void setP1(Point p1) {
        this.p1 = p1;
    }

    public Point getP2() {
        return p2;
    }

    public void setP2(Point p2) {
        this.p2 = p2;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getDistance() {
        double distance = Math.sqrt(Math.pow(p2.getX() - p1.getX(), 2) + Math.pow(p2.getY() - p1.getY(), 2));
        return Double.parseDouble(String.format("%.2f", distance));
    }

    public void disPlay() {
        System.out.println("The line's color is:" + getColor());
        System.out.println("The line's begin point's Coordinate is:");
        getP1().disPlay();
        System.out.println("The line's end point's Coordinate is:");
        getP2().disPlay();
        System.out.println("The line's length is:" + getDistance());
    }
}

class Point {
    private double x, y;     //点坐标

    public void Point() {
    }

    public void Point(double x, double y) {      //设置点坐标
        setX(x);
        setY(y);
    }

    public double getX() {      //获得坐标x
        return x;
    }

    public double getY() {      //获得坐标y
        return y;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public void disPlay() {
        System.out.println("(" + Double.parseDouble(String.format("%.2f", x)) + "," + Double.parseDouble(String.format("%.2f", y)) + ")");
    }
}

SourceMonitor生成的报表内容:
img

(5)期中考试 7-2 点线面问题重构(继承与多态)

题目:
在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。

对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane’s color is:颜色
在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:


element = p1;//起点Point
element.display();

Copy
  element = p2;//终点Point
  element.display();
   
  element = line;//线段
  element.display();
   
  element = plane;//面
  element.display();

类结构如下图所示。
img
其中,所有数值均保留两位小数,建议可用String.format(“%.2f”, data)方法。

以下情况为无效作业
无法运行
设计不符合所给类图要求
未通过任何测试点测试
判定为抄袭

输入格式:
分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:
(x1,y1)
(x2,y2)
The line’s color is:颜色值
The line’s begin point’s Coordinate is:
(x1,y1)
The line’s end point’s Coordinate is:
(x2,y2)
The line’s length is:长度值
The Plane’s color is:颜色值

试题分析:
 这一题相较于上一题多加了Plane类和抽象类Element,Plane类中包含了私有属性color,抽象类Element中拥有一个抽象方法display()。

源代码展示:

查看代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Point p1 = new Point();
        Point p2 = new Point();
        Line l = new Line();
        Plane plane = new Plane();
        Scanner sc = new Scanner(System.in);
        p1.setX(sc.nextFloat());
        p1.setY(sc.nextFloat());
        p2.setX(sc.nextFloat());
        p2.setY(sc.nextFloat());
        if (((p1.getX() <= 0 || p1.getX() > 200) || (p1.getY() <= 0 || p1.getY() > 200))
                || ((p2.getX() <= 0 || p2.getX() > 200) || (p2.getY() <= 0 || p2.getY() > 200))) {
            System.out.println("Wrong Format");
            return;
        }
        l.setP1(p1);
        l.setP2(p2);
        l.setColor(sc.next());
        plane.setColor(l.getColor());
        Element element1 = p1;
        element1.disPlay();
        Element element2 = p2;
        element2.disPlay();
        Element element3 = l;
        element3.disPlay();
        Element element4 = plane;
        element4.disPlay();
    }
}

class Line extends Element {
    Point p1 = new Point(), p2 = new Point();       //线段两端点
    private String color;

    public void Line() {
    }

    public void Line(Point p1, Point p2, String color) {
        setP1(p1);
        setP2(p2);
        setColor(color);
    }

    public Point getP1() {
        return p1;
    }

    public void setP1(Point p1) {
        this.p1 = p1;
    }

    public Point getP2() {
        return p2;
    }

    public void setP2(Point p2) {
        this.p2 = p2;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getDistance() {
        double distance = Math.sqrt(Math.pow(p2.getX() - p1.getX(), 2) + Math.pow(p2.getY() - p1.getY(), 2));
        return String.format("%.2f", distance);
    }

    public void disPlay() {
        System.out.println("The line's color is:" + getColor());
        System.out.println("The line's begin point's Coordinate is:");
        getP1().disPlay();
        System.out.println("The line's end point's Coordinate is:");
        getP2().disPlay();
        System.out.println("The line's length is:" + getDistance());
    }
}

class Point extends Element {
    private double x, y;     //点坐标

    public void Point() {
    }

    public void Point(double x, double y) {      //设置点坐标
        setX(x);
        setY(y);
    }

    public double getX() {      //获得坐标x
        return x;
    }

    public double getY() {      //获得坐标y
        return y;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public void disPlay() {
        System.out.println("(" + String.format("%.2f", x) + "," + String.format("%.2f", y) + ")");
    }
}

class Plane extends Element {
    private String color;

    public void Plane() {
    }

    public void Plane(String color) {
        setColor(color);
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }


    @Override
    public void disPlay() {
        System.out.println("The Plane's color is:" + getColor());
    }
}

abstract class Element {
    public abstract void disPlay();
}


SourceMonitor生成的报表内容:
img

(6)期中考试 7-3 点线面问题再重构(容器类)

题目:
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList类型的对象(若不了解泛型,可以不使用)
增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index – 1(ArrayList中index>=0)个对象
在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
1:向容器中增加Point对象
2:向容器中增加Line对象
3:向容器中增加Plane对象
4:删除容器中第index – 1个数据,若index数据非法,则无视此操作
0:输入结束
示例代码如下:


Copy
   choice = input.nextInt();
    while(choice != 0) {
        switch(choice) {
        case 1://insert Point object into list
          ...
            break;
        case 2://insert Line object into list
            ...
            break;
        case 3://insert Plane object into list
            ...
            break;
        case 4://delete index - 1 object from list
            int index = input.nextInt();
            ...
        }
        choice = input.nextInt();
    }

输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。
类图如下所示:
img
以下情况为无效作业
无法运行
设计不符合所给类图要求
未通过任何测试点测试
判定为抄袭

输入格式:

switch(choice) {
            case 1:insert Point object into list
              //输入“点”对象的x,y值
                break;
            case 2:insert Line object into list
                //输入“线”对象两个端点的x,y值
                break;
            case 3:insert Plane object into list
                //输入“面”对象的颜色值
                break;
            case 4:delete index - 1 object from list
                //输入要删除的对象位置(从1开始)
            }

输出格式:
Point、Line、Plane的输出参考题目2
删除对象时,若输入的index超出合法范围,程序自动忽略该操作

试题分析:
本题主要考察对象的容器以及其遍历,需要新建一个GeometryObject容器类,其包含的属性为ArrayList
,该类中包含ArrayList中元素的增加方法和减少方法以及获取List表的方法。

源代码展示:

查看代码
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        GeometryObject geometryObject = new GeometryObject();
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        while (choice != 0) {
            switch (choice) {
                case 1://insert Point object into list
                    Point p = new Point();
                    p.setX(sc.nextFloat());
                    p.setY(sc.nextFloat());
                    if ((p.getX() <= 0 || p.getX() > 200) || (p.getY() <= 0 || p.getY() > 200)) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    geometryObject.add(p);
                    break;
                case 2://insert Line object into list
                    Line line = new Line();
                    Point p1 = new Point();
                    Point p2 = new Point();
                    p1.setX(sc.nextFloat());
                    p1.setY(sc.nextFloat());
                    p2.setX(sc.nextFloat());
                    p2.setY(sc.nextFloat());
                    if (((p1.getX() <= 0 || p1.getX() > 200) || (p1.getY() <= 0 || p1.getY() > 200))
                            || ((p2.getX() <= 0 || p2.getX() > 200) || (p2.getY() <= 0 || p2.getY() > 200))) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    line.setP1(p1);
                    line.setP2(p2);
                    line.setColor(sc.next());
                    geometryObject.add(line);
                    break;
                case 3:
                    Plane plane = new Plane();
                    plane.setColor(sc.next());
                    geometryObject.add(plane);
                    break;
                case 4://delete index - 1 object from list
                    int index = sc.nextInt();
                    if (index < 1 || index > geometryObject.arrayList.size()) break;
                    else geometryObject.remove(index - 1);
                    break;
            }
            choice = sc.nextInt();
        }
        geometryObject.getList();
    }
}

class Line extends Element {
    Point p1 = new Point(), p2 = new Point();       //线段两端点
    private String color;

    public void Line() {
    }

    public void Line(Point p1, Point p2, String color) {
        setP1(p1);
        setP2(p2);
        setColor(color);
    }

    public Point getP1() {
        return p1;
    }

    public void setP1(Point p1) {
        this.p1 = p1;
    }

    public Point getP2() {
        return p2;
    }

    public void setP2(Point p2) {
        this.p2 = p2;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getDistance() {
        double distance = Math.sqrt(Math.pow(p2.getX() - p1.getX(), 2) + Math.pow(p2.getY() - p1.getY(), 2));
        return String.format("%.2f", distance);
    }

    public void disPlay() {
        System.out.println("The line's color is:" + getColor());
        System.out.println("The line's begin point's Coordinate is:");
        getP1().disPlay();
        System.out.println("The line's end point's Coordinate is:");
        getP2().disPlay();
        System.out.println("The line's length is:" + getDistance());
    }
}

class Point extends Element {
    private double x, y;     //点坐标

    public void Point() {
    }

    public void Point(double x, double y) {      //设置点坐标
        setX(x);
        setY(y);
    }

    public double getX() {      //获得坐标x
        return x;
    }

    public double getY() {      //获得坐标y
        return y;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public void disPlay() {
        System.out.println("(" + String.format("%.2f", x) + "," + String.format("%.2f", y) + ")");
    }
}

class Plane extends Element {
    private String color;

    public void Plane() {
    }

    public void Plane(String color) {
        setColor(color);
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }


    @Override
    public void disPlay() {
        System.out.println("The Plane's color is:" + getColor());
    }
}

abstract class Element {
    public abstract void disPlay();
}

class GeometryObject {
    ArrayList<Element> arrayList = new ArrayList<>();

    public void GeometryObject() {
    }

    public void add(Element element) {
        arrayList.add(element);
    }

    public void remove(int index) {
        arrayList.remove(index);
    }

    public void getList() {
        for (Element i : arrayList) {
            i.disPlay();
        }
    }
}

SourceMonitor生成的报表内容:
img

3.采坑心得

 1.题目集四中7-2计算面积的时候使用的是double类型的数据,但是有时候会出现精度丢失的情况,后续的判断可能会出错,上网查了一下之后发现BigDemical类型的数据可以有效防止精度丢失,于是将交点改用了BigDemical类型,运算结束后再转回double类型,这样再次提交代码后成功通过。
 2.由于题目集五没完成,所以也不知道有啥坑。
 3.对于期中考试的题目的话,要注意的就是仔细看题目!仔细看题目!还是仔细看题目!比如7-3中当choice == 4时,题目的要求为“4:删除容器中第index – 1个数据,若index数据非法,则无视此操作”,但是我写代码时没看到这个要求,于是有一个点始终过不去。
img
再仔细看一遍题目后将case4换成以下的代码后就通过了。

case 4://delete index - 1 object from list
    int index = sc.nextInt();
    if (index < 1 || index > geometryObject.arrayList.size()) break;
    else geometryObject.remove(index - 1);
    break;

img

4.改进建议

 题目集4、5中的多边形的题目如果在代码开头部分先用正则表达式来筛选的话应该会更简单些,目前我的代码相对来说更加复杂。题目集4、5中的多边形的题目有些不同的选项可能需要用到相同的功能,因此我认为我的代码应该尽可能多的将多次用到的相同的功能写成一个类中的方法,需要此功能的时候直接调用该方法即可。而且应该尽可能地运用写类的方法,不要把函数全塞到Main里。

5.总结

通过这三次题目集的练习:

  1. 让我明白了写Java的代码时,不应该只将目光放在实现结果上,而是应该关注代码的总体结构,结构清晰分工明确,实现代码也就又快又准;
  2. 封装能够在实现类的功能时保证类的独立性类在独立时,能够在任何情况下被快速引用,所以写代码应该尽可能地使代码模块化,避免大量重复的代码段,让自己的代码有可观的复用性;
  3. 我掌握了正则表达式的基本用法,并且让我明白了正则表达式对于字符处理类的题目的便利;

建议:希望老师在有空的时候能够讲解一下这些pta上的题目的解题思路,供大家参考一下。




最后附上Source Monitor的使用说明链接:https://blog.csdn.net/u012075442/article/details/115653071

原文地址:http://www.cnblogs.com/nchu202071/p/16835850.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性