正则表达式的使用,几何问题等总结

一、前言

1.知识点总结

(蓝色为重点考察)

<1>第四次大作业

题型总结:正则表达式的使用、凸四边形的计算、银行业务

涉及知识点:正则表达式、正则表达式多次匹配、Scanner类的使用、while循环、for循环、Math类的使用、类的创建和初始化、类的设计、条件判断语句、 构造方法、final修饰符、访问对象属性和行为,对象的创建、this关键字的使用、Pattern的使用、Matcher的使用

<2>期中考试

题型总结:点和面、点线面、点线面

涉及知识点:Scanner类的使用、类的创建和初始化、条件判断语句、 构造方法、final修饰符、访问对象属性和行为,对象的创建、this关键字的使用、继承和多态、抽象方法、类的重构、ArrayList的使用、容器的使用

<3>第一次实验作业

题型总结:学生成绩计算最高分和平均分

设计知识点:数据的提取处理、Scanner类的使用、if语句、类的创建和初始化、类的设计、for语句、访问类的属性和方法

2.题量

<1>第四次大作业:适中

<2>期中考试:适中

<2>第一次实验:较少

3.难度评估

<1>第四次大作业:适中、困难、适中

<2>期中考试:适中、适中、适中

<3>第一次实验:适中

二、设计与分析

1.算法设计以及思路

<1>第四次作业第二题
  • 用户输入一组选项和数据,进行与四边形有关的计算。

以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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"。

 

 类图:

 大概思路:

根据选项来调用方法,根据点的个数分成三种方法,处理四个点、五个点、六个点的情况,每种情况都要先对数据进行整理拆分disPose(),获得点坐标,线的长度,并判断点不足和输入不合法的情况。

  • fourPoints(String x,int no)://传入输入内容和选项

首先先对点重合的点进行判断,根据传来的选项选择四个点情况中的某一种:

选项1:输出quad()和prall()的判断结果;

选项2:如果quad()为真,输出diamond()(菱形)和rect()(矩形)和square()(正方形)结果,否则输出"not a quadrilateral"

选项3:如果quad()为真,输出bump()(判断凸四边形)和周长和面积(调用quad_s()方法),否则输出"not a quadrilateral"

这里有巧妙的一点,quad() - parall() - diamond() - square() 这几个函数的判断要在前面的函数成立的情况下 ,这里考察数学的图形定义知识

quad()算法:四点的任意三点可以构成三角形

parall()算法:四边形的对边平行且相等(一定要是对边)

diamond()算法:平行四边形的对角线垂直,即两个对角线的斜率相乘得-1

square()算法:菱形的对角线长度相等

  • fivePoints():

  首先先对点重合的点进行判断,判断是否构成凸四边形,再判断点是否在四边形内isPointInRect(),输出结果

  bump()算法:向量每个点相连接,得到AB,BC,CD,DA四个向量,ABxBC,BCxCD,CDxDA,DAxAB四个叉乘的乘积如果>0,则说明同号,证明图形是凸四边形。

    quad_s()算法:分成两个三角形分别运算,需要用到点到点距离公式和点到线的距离公式

  isPointInRect()算法:同样是用向量积的运算

        

   如果有凸四边形为ABCD,且ABCD顺时针,待判断的点为N,则需要满足:

      AB × AN>0,BC × BN>0, CD × CN>0,DA × DN>0或者AB × AN<0,BC × BN<0, CD × CN<0,DA × DN<0,可证点N在凸四边形内部。

  如果 AB × AN,BC × BN, CD × CN,DA × DN,有1个为0,则说明点在四边形的边上

如果AB × AN,BC × BN, CD × CN,DA × DN,符号不一致,则说明点在四边形外

源码:

package test;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String x = s.nextLine();
        boolean a = false;
        Choice z = new Choice();
        for (int i = 0; i < x.length(); i++)
            if (x.charAt(i) == ' ')
                a = true;
        if (x.charAt(1) != ':' || !a)
            System.out.print("Wrong Format");
        else {
            if (x.charAt(0) == '1' || x.charAt(0) == '2' || x.charAt(0) == '3')
                z.fourPoints(x.substring(2), Integer.parseInt(x.substring(0, 1)));
            if (x.charAt(0) == '4')
                z.sixPoints(x.substring(2));
            if (x.charAt(0) == '5')
                z.fivePoints(x.substring(2));
        }
    }
}

class Choice {
    private double x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, p, p1, p2, p3, p4, p13, p24;
    private String[] dj, point1, point2, point3, point4, point5, point6;

    boolean quad() {// 判断是否能组成四边形
        p13 = Math.sqrt(Math.pow(x3 - x1, 2.0) + Math.pow(y3 - y1, 2.0));
        p24 = Math.sqrt(Math.pow(x4 - x2, 2.0) + Math.pow(y4 - y2, 2.0));
        if (p1 + p2 > p13 && p1 + p13 > p2 && p13 + p2 > p1 && p24 + p2 > p3 && p24 + p3 > p2 && p2 + p3 > p24
                && p13 + p3 > p4 && p13 + p4 > p3 && p4 + p3 > p13 && p24 + p1 > p4 && p24 + p4 > p1 && p4 + p1 > p24) {
            return true;
        }
        return false;
    }

    double quad_s() {
        if (x1 == x3 && y2 == y4) {
            double s = (y3 - y1) * (x4 - x2) * 0.5;
            return s;
        }
        double s = (x1 * y2 - x1 * y3 + x2 * y3 - x2 * y1 + x3 * y1 - x2 * y2)
                + (x1 * y4 - x1 * y3 + x4 * y3 - x4 * y1 + x3 * y1 - x4 * y4);
        return s;
    }

    boolean parall() {
        if (p1 == p3 && (y2 - y1) / (x2 - x1) == (y4 - y3) / (x4 - x3)) {
            return true;
        }
        return false;
    }

    boolean diamond() {
        if (((y3 - y1) / (x3 - x1)) * ((y4 - y2) / (x4 - x2)) == -1) {
            return true;
        } else
            return false;
    }

    boolean rect() {
        if (diamond()) {
            if (p13 == p24) {
                return true;
            } else {
                return false;
            }
        } else
            return false;
    }

    boolean square() {
        if (rect()) {
            if (p1 == p2 || p2 == p3 || p3 == p4 || p24 == p1) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    boolean bump() {
        double t1 = (x4 - x1) * (y2 - y1) - (y4 - y1) * (x2 - x1);
        double t2 = (x1 - x2) * (y3 - y2) - (y1 - y2) * (x3 - x2);
        double t3 = (x2 - x3) * (y4 - y3) - (y2 - y3) * (x4 - x3);
        double t4 = (x3 - x4) * (y1 - y4) - (y3 - y4) * (x1 - x4);
        if (t1 * t2 * t3 * t4 < 0)
            return false;
        else
            return true;

    }

    boolean isPointInRect() {
        final double a = (x3 - x2) * (y1 - y2) - (y3 - y2) * (x1 - x2);
        final double b = (x4 - x2) * (y1 - y3) - (y4 - y3) * (x1 - x3);
        final double c = (x5 - x3) * (y1 - y4) - (y5 - y4) * (x1 - x4);
        final double d = (x2 - x4) * (y1 - y5) - (y2 - y5) * (x1 - x5);
        if ((a > 0 && b > 0 && c > 0 && d > 0) || (a < 0 && b < 0 && c < 0 && d < 0)) {
            return true;
        }
        return false;
    }

    String tran(double x) {
        String str_d = String.valueOf(x);
        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", x);
        return out;
    }

    void disPose(String x, int num) {//
        dj = x.split(" ");
        point1 = dj[0].split(",");
        point2 = dj[1].split(",");
        if (dj.length < num) {
            System.out.print("wrong number of points");
            System.exit(0);
        }
        point3 = dj[2].split(",");
        point4 = dj[3].split(",");
        if (legal(point1[0]) && legal(point1[1]) && legal(point2[0]) && legal(point2[1]) && legal(point3[0])
                && legal(point3[1]) && legal(point4[0]) && legal(point4[1])) {
            x1 = Double.parseDouble(point1[0]);
            y1 = Double.parseDouble(point1[1]);
            x2 = Double.parseDouble(point2[0]);
            y2 = Double.parseDouble(point2[1]);
            x3 = Double.parseDouble(point3[0]);
            y3 = Double.parseDouble(point3[1]);
            x4 = Double.parseDouble(point4[0]);
            y4 = Double.parseDouble(point4[1]);
            p1 = Math.sqrt(Math.pow(x2 - x1, 2.0) + Math.pow(y2 - y1, 2.0));
            p2 = Math.sqrt(Math.pow(x3 - x2, 2.0) + Math.pow(y3 - y2, 2.0));
            p3 = Math.sqrt(Math.pow(x4 - x3, 2.0) + Math.pow(y4 - y3, 2.0));
            p4 = Math.sqrt(Math.pow(x1 - x4, 2.0) + Math.pow(y1 - y4, 2.0));

        } else {
            System.out.print("Wrong Format");
            System.exit(0);
        }

        if (num == 5) {
            point5 = dj[4].split(",");
            if (legal(point5[0]) && legal(point5[1])) {

                x5 = Double.parseDouble(point5[0]);
                y5 = Double.parseDouble(point5[1]);
                p1 = Math.sqrt(Math.pow(x3 - x2, 2.0) + Math.pow(y3 - y2, 2.0));
                p2 = Math.sqrt(Math.pow(x4 - x3, 2.0) + Math.pow(y4 - y3, 2.0));
                p3 = Math.sqrt(Math.pow(x5 - x4, 2.0) + Math.pow(y5 - y4, 2.0));
                p4 = Math.sqrt(Math.pow(x2 - x5, 2.0) + Math.pow(y2 - y5, 2.0));
            } else {
                System.out.print("Wrong Format");
                System.exit(0);
            }

        }
        if (num == 6) {
            point5 = dj[4].split(",");
            point6 = dj[5].split(",");
            if (legal(point5[0]) && legal(point5[1]) && legal(point6[0]) && legal(point6[1])) {

                x5 = Double.parseDouble(point5[0]);
                y5 = Double.parseDouble(point5[1]);
                x6 = Double.parseDouble(point6[0]);
                y6 = Double.parseDouble(point6[1]);
                p = Math.sqrt(Math.pow(x2 - x1, 2.0) + Math.pow(y2 - y1, 2.0));
                p1 = Math.sqrt(Math.pow(x4 - x3, 2.0) + Math.pow(y4 - y3, 2.0));
                p2 = Math.sqrt(Math.pow(x5 - x4, 2.0) + Math.pow(y5 - y4, 2.0));
                p3 = Math.sqrt(Math.pow(x6 - x5, 2.0) + Math.pow(y6 - y5, 2.0));
                p4 = Math.sqrt(Math.pow(x3 - x6, 2.0) + Math.pow(y3 - y6, 2.0));
            } else {
                System.out.print("Wrong Format");
                System.exit(0);
            }
        }
    }

    boolean legal(String x) {
        String w = "[+-]?(\\d+)(\\.\\d+)?$";
        for (int i = 1; i < x.length(); i++) {
            if (x.charAt(0) == '0' && x.length() > 1 && x.charAt(1) != '.')
                return false;
        }
        if (x.matches(w))
            return true;
        else
            return false;
    }

    void fourPoints(String x, int no) {
        disPose(x, 4);
        if (!dj[0].equals(dj[1]) && !dj[1].equals(dj[2]) && !dj[2].equals(dj[3]) && !dj[3].equals(dj[0])) {
            switch (no) {
            case 1:
                System.out.print(quad() + " " + parall());
                // 大于要求个数情况???
                break;
            case 2:
                if (!quad())
                    System.out.print("not a quadrilateral");
                else
                    System.out.print(diamond() + " " + rect() + " " + square());
                break;
            case 3:
                if (!quad())
                    System.out.print("not a quadrilateral");
                else
                    System.out.print(bump() + " " + tran(p1 + p2 + p3 + p4) + " " + tran(quad_s()));
                break;
            }
        } else
            System.out.print("points coincide");
    }

    void sixPoints(String x) {
        
    }

    void fivePoints(String x) {
        disPose(x, 5);
        if (!dj[0].equals(dj[1])) {
            if (!quad())
                System.out.print("not a quadrilateral or triangle");
            else {
                if (isPointInRect()) {
                    if (((y3 - y2) / (x1 - x2) == (y1 - y2) / (x3 - x2)
                            || (y1 - y2) / (x3 - x2) == (y3 - y2) / (x1 - x2)) || x1 == 2)
                        System.out.print("on the quadrilateral");
                    else
                        System.out.print("in the quadrilateral");
                } else
                    System.out.print("outof the quadrilateral");
            }

        } else
            System.out.print("points coincide");
    }
}

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

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

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

 类图:

 大概思路:

在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的display()方法进行输出

源码:

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);
        double x1, y1, x2, y2;
        String color;
        x1 = s.nextDouble();
        y1 = s.nextDouble();
        x2 = s.nextDouble();
        y2 = s.nextDouble();
        color = s.nextLine();
        if (x1 < 0 || x1 > 200 || x2 < 0 || x2 > 200 || y1 < 0 || y1 > 200 || y2 < 0 || y2 > 200) {
            System.out.println("Wrong Format");
            System.exit(0);
        }

        color = s.nextLine();
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        Line l1 = new Line(p1, p2, color);
        Plane c1 = new Plane(color);
        Element element = new Element();
        element = p1;// 起点Point
        element.display();

        element = p2;// 终点Point
        element.display();

        element = l1;// 线段
        element.display();

        element = c1;// 面
        element.display();

    }
}

class Point extends Element {
    private double x;
    private double y;

    Point() {

    }

    Point(double x, double y) {
        setX(x);
        setY(y);
    }

    public double getX() {
        return x;
    }

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

    public double getY() {
        return y;
    }

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

    public void display() {// 只打印点坐标
        System.out.println("(" + String.format("%.2f", getX()) + "," + String.format("%.2f", getY()) + ")");
    }
}

class Line extends Element {
    Point point1;
    Point point2;
    String color;

    Line() {

    }

    Line(Point p1, Point p2, String color) {
        setPoint1(p1);
        setPoint2(p2);
        setColor(color);
    }

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

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

    public double getDistance() {
        return Math.sqrt(Math.pow((getPoint2().getY() - getPoint1().getY()), 2)
                + Math.pow((getPoint2().getX() - getPoint1().getX()), 2));
    }

    public void display() {
        System.out.println("The line's color is:" + getColor());
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.println("The line's length is:" + String.format("%.2f", getDistance()));
    }
}

class Element {
    public void display() {

    }
}

class Plane extends Element {
    private String color;

    Plane() {

    }

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

    public String getColor() {
        return color;
    }

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

    public void display() {
        System.out.println("The Plane's color is:" + getColor());
    }
}
<3>期中考试第二题

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

  • 对题目中的点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()方法,从而实现多态特性。

 类图:

 大概思路:

数据输入的判断同前一题一样,但难度相对上一题进一步升级

主函数需要定义一条线段对象,和一个点对象

需要用到抽象类,子类点和线类继承父类tem,按照要求最后display()出来

 源码:

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);
        double x1, y1, x2, y2;
        String color;
        x1 = s.nextDouble();
        y1 = s.nextDouble();
        x2 = s.nextDouble();
        y2 = s.nextDouble();
        color = s.nextLine();
        if (x1 < 0 || x1 > 200 || x2 < 0 || x2 > 200 || y1 < 0 || y1 > 200 || y2 < 0 || y2 > 200) {
            System.out.println("Wrong Format");
            System.exit(0);
        }

        color = s.nextLine();
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        Line l1 = new Line(p1, p2, color);
        Plane c1 = new Plane(color);
        Element element = new Element();
        element = p1;// 起点Point
        element.display();

        element = p2;// 终点Point
        element.display();

        element = l1;// 线段
        element.display();

        element = c1;// 面
        element.display();

    }
}

class Point extends Element {
    private double x;
    private double y;

    Point() {

    }

    Point(double x, double y) {
        setX(x);
        setY(y);
    }

    public double getX() {
        return x;
    }

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

    public double getY() {
        return y;
    }

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

    public void display() {// 只打印点坐标
        System.out.println("(" + String.format("%.2f", getX()) + "," + String.format("%.2f", getY()) + ")");
    }
}

class Line extends Element {
    Point point1;
    Point point2;
    String color;

    Line() {

    }

    Line(Point p1, Point p2, String color) {
        setPoint1(p1);
        setPoint2(p2);
        setColor(color);
    }

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

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

    public double getDistance() {
        return Math.sqrt(Math.pow((getPoint2().getY() - getPoint1().getY()), 2)
                + Math.pow((getPoint2().getX() - getPoint1().getX()), 2));
    }

    public void display() {
        System.out.println("The line's color is:" + getColor());
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.println("The line's length is:" + String.format("%.2f", getDistance()));
    }
}

class Element {
    public void display() {

    }
}

class Plane extends Element {
    private String color;

    Plane() {

    }

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

    public String getColor() {
        return color;
    }

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

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

<4>期中考试第三题

在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。

  • 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>
  • 增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
  • 在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
    • 1:向容器中增加Point对象
    • 2:向容器中增加Line对象
    • 3:向容器中增加Plane对象
    • 4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
    • 0:输入结束

 类图:

大概思路:

增加一个面对象的设计,对题目的类设计进行重构

添加子类面,编辑对List的操作方法,按照输入的要求输出

 源码:

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);
        double x1, y1, x2, y2;
        String color;
        x1 = s.nextDouble();
        y1 = s.nextDouble();
        x2 = s.nextDouble();
        y2 = s.nextDouble();
        color = s.nextLine();
        if (x1 < 0 || x1 > 200 || x2 < 0 || x2 > 200 || y1 < 0 || y1 > 200 || y2 < 0 || y2 > 200) {
            System.out.println("Wrong Format");
            System.exit(0);
        }

        color = s.nextLine();
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        Line l1 = new Line(p1, p2, color);
        Plane c1 = new Plane(color);
        Element element = new Element();
        element = p1;// 起点Point
        element.display();

        element = p2;// 终点Point
        element.display();

        element = l1;// 线段
        element.display();

        element = c1;// 面
        element.display();

    }
}

class Point extends Element {
    private double x;
    private double y;

    Point() {

    }

    Point(double x, double y) {
        setX(x);
        setY(y);
    }

    public double getX() {
        return x;
    }

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

    public double getY() {
        return y;
    }

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

    public void display() {// 只打印点坐标
        System.out.println("(" + String.format("%.2f", getX()) + "," + String.format("%.2f", getY()) + ")");
    }
}

class Line extends Element {
    Point point1;
    Point point2;
    String color;

    Line() {

    }

    Line(Point p1, Point p2, String color) {
        setPoint1(p1);
        setPoint2(p2);
        setColor(color);
    }

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

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

    public double getDistance() {
        return Math.sqrt(Math.pow((getPoint2().getY() - getPoint1().getY()), 2)
                + Math.pow((getPoint2().getX() - getPoint1().getX()), 2));
    }

    public void display() {
        System.out.println("The line's color is:" + getColor());
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.println("The line's length is:" + String.format("%.2f", getDistance()));
    }
}

class Element {
    public void display() {

    }
}

class Plane extends Element {
    private String color;

    Plane() {

    }

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

    public String getColor() {
        return color;
    }

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

    public void display() {
        System.out.println("The Plane's color is:" + getColor());
    }
}
<5>第一次实验
  • 题目描述:某班有30名学生,每个学生有学号、姓名和4门成绩(学生信息随机产生,存入一个对象数组),现新转来一名学生,请输入这位学生的学号、姓名和成绩(四门课的成绩小于100分,从键盘输入,以空格为区分),然后显示这名学生平均分、最高分,全班的最高平均分学生信息、新同学在班级中的成绩水平

 类图:

大概思路:

设计一个学生类,属性有id,名字,成绩,平均分,最高分,申请一个数组放每一个对象

对数组遍历,找到所有学生成绩中的最高平均分,再与新生进行比较

随机数的产生会用到Math.random()

 源码:

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);
        double x1, y1, x2, y2;
        String color;
        x1 = s.nextDouble();
        y1 = s.nextDouble();
        x2 = s.nextDouble();
        y2 = s.nextDouble();
        color = s.nextLine();
        if (x1 < 0 || x1 > 200 || x2 < 0 || x2 > 200 || y1 < 0 || y1 > 200 || y2 < 0 || y2 > 200) {
            System.out.println("Wrong Format");
            System.exit(0);
        }

        color = s.nextLine();
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        Line l1 = new Line(p1, p2, color);
        Plane c1 = new Plane(color);
        Element element = new Element();
        element = p1;// 起点Point
        element.display();

        element = p2;// 终点Point
        element.display();

        element = l1;// 线段
        element.display();

        element = c1;// 面
        element.display();

    }
}

class Point extends Element {
    private double x;
    private double y;

    Point() {

    }

    Point(double x, double y) {
        setX(x);
        setY(y);
    }

    public double getX() {
        return x;
    }

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

    public double getY() {
        return y;
    }

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

    public void display() {// 只打印点坐标
        System.out.println("(" + String.format("%.2f", getX()) + "," + String.format("%.2f", getY()) + ")");
    }
}

class Line extends Element {
    Point point1;
    Point point2;
    String color;

    Line() {

    }

    Line(Point p1, Point p2, String color) {
        setPoint1(p1);
        setPoint2(p2);
        setColor(color);
    }

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

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

    public double getDistance() {
        return Math.sqrt(Math.pow((getPoint2().getY() - getPoint1().getY()), 2)
                + Math.pow((getPoint2().getX() - getPoint1().getX()), 2));
    }

    public void display() {
        System.out.println("The line's color is:" + getColor());
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.println("The line's length is:" + String.format("%.2f", getDistance()));
    }
}

class Element {
    public void display() {

    }
}

class Plane extends Element {
    private String color;

    Plane() {

    }

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

    public String getColor() {
        return color;
    }

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

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

三、踩坑心得

1.第四次大作业

<1>通过图表分析,发现代码复杂度太高,不够抽象,写的嵌套有些严重,方法设计的非常糟糕,

后续要多花时间在代码质量和程序设计上,减少模块之间的牵连,注重细节,学习复杂度相关知识,以及如何写出高质代码

2.期中考试

<1>考试时候没有写上abstract,这里不同于接口,不可以默认为abstract。

<2>String.format("%.2f", data)方法比我自己写的保留小数的方法好用多了。

3.第一次实验

<1>过于复杂,嵌套严重,可读性不高,没有做到功能模块化

<2>代码书写习惯不好,一行写了太长的代码,阅读难度增加

四、改进建议

一、第四次大作业  

2.

  • 学会提炼方法,将复杂的方法进一步细化
  • 学习算法,来简洁代码
  • 学会用多态取代条件式
  • 简化条件判断

3.

  •   对于Bank类里方法的判断以及输出可以进一步包装成方法
二、期中考试
  • 注意代码的规范,修饰符的正确使用
  • 容器类的使用不够熟练,包装里的一些函数也不熟悉
  • 以及,PowerDesigner不仅可以将代码自动生成类图,也可以根据类图生成一个框架,但前提是你得自己画好类图
三、第一次实验
  • 减少代码的嵌套,将具体功能和判断写成详细的方法,包装起来,不要一个函数写一长串

五、总结

一个优秀的程序的设计过程和经验可以是艺术,对比自己的代码和老师给的类图写出的代码分析出的数据,明白了差距和前进方向。

认识到软件的核心在于降低复杂度,以及编程不应该只为解决问题,解决BUG,东补一下西补一下,而是应该每次修复BUG都像是在重新设计系统。

往后写代码,我会开始锻炼自己,实施“战略编程”而非“战术编程”,我想这才是老师发三道题却让我们做很久的原因。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Delphi是一种面向对象的编程语言,它支持正则表达式使用正则表达式是一种强大的文本匹配工具,可以用于在字符串中查找、替换和提取特定模式的文本。 在Delphi中,可以使用TRegEx类来进行正则表达式的操作。下面是使用正则表达式的基本步骤: 1. 引入正则表达式单元: 在Delphi中,需要使用System.RegularExpressions单元来引入正则表达式相关的类和函数。 2. 创建TRegEx对象: 使用TRegEx类的Create方法创建一个TRegEx对象,可以指定一些选项来控制正则表达式的匹配行为。 3. 编写正则表达式模式: 正则表达式模式是用来描述要匹配的文本模式的字符串。可以使用各种元字符和特殊字符来构建模式,例如通配符、字符类、重复次数等。 4. 进行匹配操作: 使用TRegEx对象的IsMatch方法可以判断一个字符串是否与正则表达式模式匹配。使用Match方法可以获取匹配到的结果。 5. 处理匹配结果: 匹配结果是一个TMatch对象,可以通过该对象的Value属性获取匹配到的字符串。还可以通过Groups属性获取捕获组的内容。 下面是一个简单的示例代码,演示了如何在Delphi中使用正则表达式: ```delphi uses System.RegularExpressions; var regex: TRegEx; input: string; pattern: string; match: TMatch; begin input := 'Hello, World!'; pattern := '\b\w+\b'; // 匹配单词 regex := TRegEx.Create(pattern); if regex.IsMatch(input) then begin match := regex.Match(input); while match.Success do begin Writeln(match.Value); match := match.NextMatch; end; end; end; ``` 上述代码中,使用正则表达式模式`\b\w+\b`来匹配字符串中的单词,并输出匹配到的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值