前三次OO作业总结

一、前言

知识点

  • 第一次作业
    主要以Java基础语法为主,基本上都类似于C,比如if else等逻辑语句,包括程序的三种执行逻辑(顺序,循环和分支)。
  • 第二次作业
    开始逐步接触面向对象的知识,学习到对象、字符串等知识。但是此时做题仍然是面向过程的思维为主。
  • 第三次作业
    这次的作业主要涉及到类、对象等知识,以及正则表达式的应用。

题量和难度

这三次实验,我认为第三次实验的难度是最大的,我认为最难的地方难在正则表达式的书写和应用上。
三次实验的题量,第一次虽然题目最多,但基本上都是较为容易的题目,简单的运算和输入输出就可以解决。第三次实验的三个题目是循序渐进的关系。
我认为第三次实验才是真正应用到了面向对象。

二、第一次实验

做实验一的时候,我基本上还停留在面向过程的思维状态下,做题也基本上都是在Main方法中完成。

设计与分析

这题是对BMI进行简单的运算,从下面的代码可以看出。我基本上还是在用C语言的方式去解决问题,并没有面向对象的封装。
对于下面的代码,我完成了一个基本的输入,然后通过输入的数据进行一些判断,从而得到希望的输出值。

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double kg, m, bmi;
        kg = input.nextDouble();
        m = input.nextDouble();
        if (kg <= 0.0 || kg > 727.0 || m > 2.72 || m <= 0.0)
            System.out.println("input out of range");
        else {
            bmi = kg/m/m;
            if (bmi > 28)
                System.out.println("fat");
            else if (bmi > 24 && bmi <= 28)
                System.out.println("overweight");
            else if (bmi >= 18.5 && bmi <= 24)
                System.out.println("fit");
            else
                System.out.println("thin");
        }
    }
}

该次实验中的其他代码我也是类似的写法,不再过多阐述。

踩坑心得

在Java中,如果想要控制输出的位数,需要使用System.out.printf进行,常用的System.out.println不行,如果想要不输出空格,就需要System.out.print或者System.out.printf

由于试验时探索的代码已经找不到了,这里另写代码进行测试。

public class Main {
    public static void main(String[] args) {
        double a = 1.8;
        System.out.println("println" + a);
        System.out.println("-------------");
        System.out.printf("printf%.5f", a);
        System.out.println("-------------");
        System.out.print("print" + a);
    }
}

上面这个代码的运行结果
在这里插入图片描述
此时,如果给printf中加上\n即可换行,得到
在这里插入图片描述

改进建议

我认为这里面的代码,可以将其专门封装成类。
例如第一题可以将其专门封装为一个人体BMI类,其中包含体重和身高两个成员。拥有一个方法返回BMI,一个方法检测BMI是否符合。
改进后的代码如下

class Bmi{
    double weight;
    double height;

    public Double getBMI(){
        return weight/(height*height);
    }
    
    //肥胖返回2,超重返回1,标准返回0,体重不足返回-1
    public int judgeBMI(){
        double x = getBMI();
        if(x < 18.5){
            return -1;
        }
        if(x >= 28){
            return 2;
        }
        if(x >= 24){
            return 1;
        }
        return 0;
    }
}

三、第二次实验

设计与分析

  • 第一题
    这里主要就是通过字符串,将每一个字符提取出来,然后进行±运算,转换成数字。

  • 第二题
    这题主要涉及到一个奇校验位。采用奇校验,发送端发送的一个字符编码(含校验位)中,“1”的个数一定为奇数个,在 接收端对接收字符二进制位中的“1”的个数进行统计,若统计出“1”的个数为偶数个,则意味着传输过程中有1位(或奇数位)发生差错。
    第二点仍然就是对字符串进行一些处理判断。

  • 第三题
    通过对字符串的判断,通过charAt提取出对应位上的内容,然后进行判断输出。
    对代码的分析在代码的注释当中

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i, j, k;
        char b;
        int book2, book3 = 0, book4, book5, book6;
        int count = 1, count2;
        String a = sc.nextLine();

        /*
        * 通过题目分析
        * 有效数据8位,起始码一位,奇校验位一位,结束位一位,
        * 所以最少需要十一位才可能存在有效信息
        * */
        if (a.length() < 11)
        {
            System.out.println("null data");
            return;
        }

        /*
        * 这里开始对读入的字符串进行操作
        * */
        for (i = 0; i < a.length(); i++) {
            
            /*
            * 出现0的时候,开始进行判断数据
            * */
            if (a.charAt(i) == '0') {
                
                /*
                * count2用于对1出现的次数进行统计
                * 最后以此进行奇校验的判断
                * */
                count2 = 0;
                //统计出现的1的个数
                for (k = i + 1; k <= i + 8; k++) {
                    if (a.charAt(k) == '1')
                        count2 = count2 + 1;
                }
                /*
                * 标记应该出现的奇校验位
                * */
                if (count2 % 2 == 1)
                    b = '0';
                else
                    b = '1';
                
                /*
                * 对多种情况分别定义booki进行标记
                * */
                //book3进行有无数据判断
                book3 = 1;
                //完全正确就是book2为1的时候
                book2 = 0;
                //奇校验正确,但终止错误,book4为1
                book4 = 0;
                //奇校验位错误,终止正确,book5为1
                book5 = 0;
                //其他情况book6为1,即格式错误
                book6 = 0;
                if (a.charAt(i + 9) == b && a.charAt(i + 10) == '1')
                    book2 = 1;
                else if (a.charAt(i + 9) == b && a.charAt(i + 10) == '0')
                    book4 = 1;
                else if (a.charAt(i + 9) != b && a.charAt(i + 10) == '1')
                    book5 = 1;
                else
                    book6 = 1;
                
                /*
                * 根据判断的结果进行输出
                * */
                if (book6 == 1) {
                    System.out.println(count + ":" + "validate error");
                    count = count + 1;
                    i = i + 9;
                }
                if (book5 == 1) {
                    System.out.println(count + ":" + "parity check error");
                    count = count + 1;
                    i = i + 9;
                }
                if (book4 == 1) {
                    System.out.println(count + ":" + "validate error");
                    count = count + 1;
                    i = i + 9;
                }
                
                /*
                * 当book2为1时,说明格式正确,存在数据
                * 所以进行数据的输出
                * 这里通过for循环进行
                * 还可以通过substring进行切割输出
                * */
                if (book2 == 1) {
                    System.out.print(count + ":");
                    for (j = i + 1; j <= i + 7; j++) {
                        System.out.print(a.charAt(j));
                    }
                    System.out.println(a.charAt(i + 8));
                    count = count + 1;
                    i = i + 9;
                }
            }
        }
        if (book3 == 0)
            System.out.println("null data");
    }
}

踩坑心得

第二题对奇校验理解出错,认为奇校验就是校验位为1,导致题目的部分正确
在这里插入图片描述

改进建议

  • 同样,这里我认为都是对字符串的基本处理,并没有非常严格的对类的划分。所以我觉得这里可以对函数进行分解,但可以不用分出多个类。例如第二题中,所有的代码都在main中,导致了一个函数中代码有七十多行,阅读性很差。
  • 其次,在第二题中,输出字符串采用了循环的方式,通过charAt输出,效率较低。可以采用substring切割字符串后输出
String s = a.substring(i + 1, i + 8);
System.out.println(s);

可以达到与循环相同的效果

for (j = i + 1; j <= i + 7; j++) {
    System.out.print(a.charAt(j));
}
System.out.println(a.charAt(i + 8));

在这里插入图片描述

  • 在后面学习了正则表达式之后,我认为还可以使用正则表达式进行前面的判断,由于时间原因,目前还未完成。

四、第三次实验

这次实验是我认为最难的一次实验。

设计与分析

7-1

这题题目要求输入两个点,然后输出距离。通过正则进行判断输入的字符串是否符合规范,然后通过正则提取出需要的内容。

在这里插入图片描述

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//输入
public class Main {

    public static void main(String[] args) {
        String a;
        Scanner scanner = new Scanner(System.in);
        a = scanner.nextLine();
        if (judge(a) == false) {
            System.out.print("Wrong Format");
            return;
        } else {
            Point point = new Point();
            Double m =point.xy(a);
            System.out.print(m);
        }
    }

    //判断是否格式正确
    public static Boolean judge(String a) {
        String pattern = "^((-?)||(\\+?))(\\d+)(\\.\\d+)?[\\,]((-?)||(\\+?))(\\d+)(\\.\\d+)?[\\s]((-?)||(\\+?))(\\d+)(\\.\\d+)?[,]((-?)||(\\+?))(\\d+)(\\.\\d+)?$";
        Pattern compile = Pattern.compile(pattern);
        Matcher match = compile.matcher(a);
        boolean matches = match.matches();
        return matches;
    }
}
class Point{
    //点的坐标
    double x,y;
    //将字符串拿进来分出点,并得到距离
    public Double xy(String a){
        int i;
        String tq="((-?)||(\\+?))(\\d+)(\\.\\d+)?";
        Pattern compile = Pattern.compile(tq);
        Matcher matcher = compile.matcher(a);

		//通过matcher.find将符合正则的提取出来
		//通过Double中的valueOf方法转换出数字
        matcher.find();
        String group1 = matcher.group();
        Double x1 = Double.valueOf(group1);

        matcher.find();
        String group2 = matcher.group();
        Double y1 = Double.valueOf(group2);

        matcher.find();
        String group3 = matcher.group();
        Double x2 = Double.valueOf(group3);

        matcher.find();
        String group4 = matcher.group();
        Double y2 = Double.valueOf(group4);

		//返回点之间的距离
        return (Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
    }
}

7-2

这题是关于点线的运算,创建的类如下。
代码分析在下面的类中
在这里插入图片描述
在这里插入图片描述

//执行线的操作
class line {
    /*
    * 两个点的情况
    * */
    public static void liang(Double x1, Double y1, Double x2, Double y2)//n=1时
    {
        double m;
        /*
        * 如果x1==x2且y1==y2,输出points coincide
        * */
        if (Math.abs(x1 - x2) < 1e-6 && Math.abs(y1 - y2) < 1e-6) {
            System.out.println("points coincide");
        }
        /*
        * 如果x1==x2,y1!=y2,输出Slope does not exist
        * */
        else if (Math.abs(x1 - x2) < 1e-6 && Math.abs(y1 - y2) >= 1e-6)
            System.out.println("Slope does not exist");
        /*
        * 其余情况就是存在
        * */
        else {
            m = (y2 - y1) / (x2 - x1);
            System.out.println(m);
        }
    }

    /*
    * 三个点的第一种情况
    * */
    public static void sany(Double x1, Double y1, Double x2, Double y2, Double x3, Double y3)//n=2时
    {
        double p;
        /*
        * 对三个点进行判断,确保存在距离
        * */
        if ((Math.abs(x1 - x2) < 1e-6 && Math.abs(y1 - y2) < 1e-6) || (Math.abs(x3 - x2) < 1e-6 && Math.abs(y3 - y2) < 1e-6) || (Math.abs(x1 - x3) < 1e-6 && Math.abs(y1 - y3) < 1e-6))
            System.out.println("points coincide");
        else {
            /*
            * 根据点到直线的距离公式计算距离然后输出距离
            * */
            p = Math.abs((-y1 + (y3 - y2) / (x3 - x2) * (x1 - x3) + y3) / (Math.sqrt(x1 * x1 + y1 * y1)));
            System.out.println(p);
        }
    }

    /*
    * 三个点的第二种情况
    * */
    public static void sane(Double x1, Double y1, Double x2, Double y2, Double x3, Double y3)//n=3时
    {
        double p;
        /*
        * 确保三个点没有重复
        * */
        if ((x1 == x2 && y1 == y2) || (x3 == x2 && y3 == y2) || (x1 == x3 && y1 == y3))
            System.out.println("points coincide");
        else {
            /*
            * 通过斜率判断是否在一条直线上
            * */
            if ((y2 - y1) / (x2 - x1) == (y3 - y2) / (x3 - x2))
                System.out.println("true");
            else
                System.out.println("false");
        }
    }

    /*
    * 四个点的第一种情况
    * */
    public static void siy(Double x1, Double y1, Double x2, Double y2, Double x3, Double y3, Double x4, Double y4)//n=4时
    {
        double p;
        /*
        * 确保四个点没有重复的点
        * */
        if ((x1 == x2 && y1 == y2) || (x3 == x2 && y3 == y2) || (x1 == x3 && y1 == y3) || (x4 == x2 && y4 == y2) || (x4 == x2 && y4 == y2) || (x4 == x1 && y4 == y1))
            System.out.println("points coincide");
        else {
            /*
            * 通过斜率判断直线是否平行
            * */
            if ((y2 - y1) / (x2 - x1) == (y4 - y3) / (x4 - x3))
                System.out.println("true");
            else
                System.out.println("false");
        }
    }

    /*
    * 四个点的第二种情况
    * */
    public static void sie(Double x1, Double y1, Double x2, Double y2, Double x3, Double y3, Double x4, Double y4) {//n=5时
        double p1, p2;
        /*
        * 确保四个点没有重复
        * */
        if ((Math.abs(x1 - x2) < 1e-6 && Math.abs(y1 - y2) < 1e-6) || (Math.abs(x3 - x2) < 1e-6 && Math.abs(y3 - y2) < 1e-6) || (Math.abs(x1 - x3) < 1e-6 && Math.abs(y1 - y3) < 1e-6) || (Math.abs(x4 - x2) < 1e-6 && Math.abs(y4 - y2) < 1e-6) || (Math.abs(x4 - x3) < 1e-6 && Math.abs(y4 - y3) < 1e-6) || (Math.abs(x4 - x1) < 1e-6 && Math.abs(y4 - y1) < 1e-6))
            System.out.println("points coincide");
        /*
        * 如果斜率相同,说明平行,没有交点
        * */
        else if ((y2 - y1) / (x2 - x1) == (y4 - y3) / (x4 - x3)) {
            System.out.println("is parallel lines,have no intersection point");
        } else {
            /*
            * 通过数学推导的公式推算出交点
            * */
            p1 = (y3*x4*x2-y4*x3*x2-y3*x4*x1+y4*x3*x1-y1*x2*x4+y2*x1*x4+y1*x2*x3-y2*x1*x3)/(x4*y2-x4*y1-x3*y2+x3*y1-x2*y4+x2*y3+x1*y4-x1*y3);
            p2 = (-y3*x4*y2+y4*x3*y2+y3*x4*y1-y4*x3*y1+y1*x2*y4-y1*x2*y3-y2*x1*y4+y2*x1*y3)/(y4*x2-y4*x1-y3*x2+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3);
            System.out.print(p1 + "," + p2);
            if (p1 <= max(max(x1, x2), max(x3, x4)) && p1 >= min(min(x1, x2), min(x3, x4)))
                System.out.println(" true");
            else
                System.out.println(" false");
        }
    }

    /*
    * 获得最大值
    * */
    public static double max(double x1, double x2) {
        if (x1 > x2) {
            return x2;
        } else {
            return x1;
        }
    }

    /*
    * 获得最小值
    * */
    public static double min(double x1, double x2) {
        if (x2 > x1) {
            return x1;
        } else {
            return x2;
        }
    }
}

7-3

创建的类如下
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//输入
public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        char first = s.charAt(0);
        if (s.charAt(1) != ':') {
            System.out.println("Wrong Format");
        }
        switch (first) {
            case '1':
                Triangle.judgeEqual(s);
                break;
            case '2':
                Triangle.getThree(s);
                break;
            case '3':
                Triangle.judgeThree(s);
                break;
            case '4':
                break;
            case '5':
                break;
            default:
                System.out.println("Wrong Format");
                return;
        }
    }

}

//三角形类
class Triangle {
    private Line l1;
    private Line l2;
    private Line l3;

    private static String pattern = "((-?)||(\\+?))(\\d+)(\\.\\d+)?[\\,]((-?)||(\\+?))(\\d+)(\\.\\d+)?";

    /*
    * 判断是否是等腰三角形、等边三角形
    * */
    public static void judgeEqual(String s) {

        /*
        * 截取第三个字符开始的字符串
        * */
        String need = s.substring(2);

        /*
        * 一个pattern代表一个点坐标
        * */
        String needPattern = pattern + "[\\s]" + pattern + "[\\s]" + pattern;

        /*
        * 判断格式是否合法
        * */
        Pattern compile = Pattern.compile(needPattern);
        Matcher match = compile.matcher(need);
        boolean matches = match.matches();
        if (matches == false) {
            System.out.println("Wrong Format");
            return;
        }

        /*
        * 将其按照空格进行拆分,即拆分为一个点一个点
        * 并判断,点的数量是否为3
        * */
        String[] s1 = need.split("[ ]");
        if (s1.length > 3) {
            System.out.println("wrong number of points");
            return;
        }

        /*
        * 调用getTriangle方法获得一个三角行类
        * */
        Triangle triangle = getTriangle(s1);

        /*
        * 调用judgeTriangleEqual判断三角形是的类型
        * */
        judgeTriangleEqual(triangle);
    }

    /*
    * 输入三个点坐标,输出周长、面积、重心坐标
    * */
    public static void getThree(String s){
        String need = s.substring(2);

        /*
        * 拼接正则进行判断
        * */
        String needPattern = pattern + "[\\s]" + pattern + "[\\s]" + pattern;
        Pattern compile = Pattern.compile(needPattern);
        Matcher match = compile.matcher(need);
        boolean matches = match.matches();
        if (matches == false) {
            System.out.println("Wrong Format");
            return;
        }

        /*
        * 通过正则利用空格拆分
        * */
        String[] s1 = need.split("[ ]");

        /*
        * 由于只有三个点,所以大于3就是错误
        * */
        if (s1.length > 3) {
            System.out.println("wrong number of points");
            return;
        }

        /*
        * 通过getTriangle获得三角形对象
        * */
        Triangle triangle = getTriangle(s1);

        /*
        * 通过printf控制输出格式
        * 通过getC获得周长,getS获得面积
        * */
        System.out.printf("%.6f %.1f ",getC(triangle),getS(triangle));

        /*
        * 通过toString获得坐标字符串
        * */
        System.out.println(getMidHert(triangle).toString());
    }

    /*
    * 计算获得三角形的周长
    * */
    public static Double getC(Triangle triangle) {
        Double l1Length = triangle.l1.getLength();
        Double l2Length = triangle.l2.getLength();
        Double l3Length = triangle.l3.getLength();
        return l1Length + l2Length + l3Length;
    }

    /*
    * 根据三角形面积计算公式获得三角形的面积
    * */
    public static Double getS(Triangle triangle) {
//        S=(1/2)* | (x1y2+x2y3+x3y1-x2y1-x3y2-x1y3) |

        /*
        * 获得各个点
        * */
        Point p1 = triangle.l1.getP1();
        Point p2 = triangle.l1.getP2();
        Point p3;
        if (p1.equals(triangle.l2.getP1())) {
            p3 = triangle.l2.getP1();
        } else {
            p3 = triangle.l2.getP2();
        }

        /*
        * 获得各个点的坐标
        * */
        Double x1 = p1.getX();
        Double y1 = p1.getY();
        Double x2 = p2.getX();
        Double y2 = p2.getY();
        Double x3 = p3.getX();
        Double y3 = p3.getY();

        /*
        * 计算获得对应的数值
        * */
        Double s = (1.0 / 2) * Math.abs(x1 * y2 + x2 * y3 + x3 * y1 - x2 * y1 - x3 * y2 - x1 * y3);
        return s;
    }

    /*
    * 计算重心
    * */
    public static Point getMidHert(Triangle triangle) {
        /*
        * 通过线获得三角形三个点的坐标
        * 三角形三个坐标的中心就是重心
        * */
        Point p1 = triangle.l1.getP1();
        Point p2 = triangle.l1.getP2();
        Point p3;
        if (p1.equals(triangle.l2.getP1())) {
            p3 = triangle.l2.getP1();
        } else {
            p3 = triangle.l2.getP2();
        }
        return new Point((p1.getX() + p2.getX() + p3.getX()) / 3, (p1.getY() + p2.getY() + p3.getY()) / 3);
    }

    /*
    * 根据字符串,获得三角形对象
    * */
    public static Triangle getTriangle(String[] s1) {

        /*
        * 创建点的集合
        * */
        List<Point> pointList = new ArrayList<Point>();
//        一个点
        for (String s2 : s1) {
            String[] split = s2.split(",");
            Double[] a = new Double[2];
            int k = 0;
            for (String s3 : split) {
                a[k++] = Double.valueOf(s3);
            }

            /*
            * 将点加入到集合当中
            * */
            pointList.add(new Point(a[0], a[1]));
        }

        /*
        * 根据点创建线,从而通过线创建三角形对象,返回对象
        * */
        Triangle triangle = new Triangle();
        triangle.l1 = new Line(pointList.get(0), pointList.get(1));
        triangle.l2 = new Line(pointList.get(1), pointList.get(2));
        triangle.l3 = new Line(pointList.get(0), pointList.get(2));
        return triangle;
    }

    /*
    * 根据传入的三角形对象,判断三角形的类型(第一种情况)
    * */
    public static void judgeTriangleEqual(Triangle triangle) {

        /*
        * 获得三角形对象中线的长度
        * */
        Double l1Length = triangle.l1.getLength();
        Double l2Length = triangle.l2.getLength();
        Double l3Length = triangle.l3.getLength();

        /*
        * 将长度加入集合
        * */
        List<Double> doubleList = new ArrayList<Double>();
        doubleList.add(l1Length);
        doubleList.add(l2Length);
        doubleList.add(l3Length);

        /*
        * 在集合中进行排序
        * */
        Collections.sort(doubleList);

        /*
        * 获得小中大
        * */
        Double shortL = doubleList.get(0);
        Double middleL = doubleList.get(1);
        Double largeL = doubleList.get(2);

        /*
        * 进行判断
        * 先判断等腰,再判断等边
        * 等腰是两个小的相等
        * 等边是等腰之后,大的和小的相等
        * */
        if (Math.abs(shortL - middleL)<1e-6) {
            System.out.print(true);
            if (Math.abs(largeL - middleL)<1e-6) {
                System.out.println(" " + true);
            } else {
                System.out.println(" " + false);
            }
        } else {
            System.out.println(false + " " + false);
        }
    }

    /*
    * 判断钝角、直角还是锐角三角形
    * */
    public static void judgeThreeT(Triangle triangle){
        /*
        * 获得三角形三边的长度
        * */
        Double l1Length = triangle.l1.getLength();
        Double l2Length = triangle.l2.getLength();
        Double l3Length = triangle.l3.getLength();
        /*
        * 放入集合然后排序
        * */
        List<Double> doubleList = new ArrayList<Double>();
        doubleList.add(l1Length);
        doubleList.add(l2Length);
        doubleList.add(l3Length);
        Collections.sort(doubleList);
        Double shortL = doubleList.get(0);
        Double middleL = doubleList.get(1);
        Double largeL = doubleList.get(2);

        /*
        * 通过勾股定理进行计算
        * 如果小于则是钝角
        * 大于则是锐角
        * 等于则是直角
        * */
        if (shortL*shortL + middleL*middleL < largeL*largeL){
            System.out.print(true);
        }else{
            System.out.println(false);
        }
        System.out.print(" ");
        if (shortL*shortL + middleL*middleL == largeL*largeL){
            System.out.print(true);
        }else{
            System.out.print(false);
        }
        System.out.print(" ");
        if (shortL*shortL + middleL*middleL > largeL*largeL){
            System.out.print(true);
        }else{
            System.out.println(false);
        }
    }

    /*
    * 输入三个点坐标,输出是钝角、直角还是锐角三角形
    * */
    public static void judgeThree(String s) {
        String need = s.substring(2);

        /*
        * 拼接正则进行判断
        * */
        String needPattern = pattern + "[\\s]" + pattern + "[\\s]" + pattern;
        Pattern compile = Pattern.compile(needPattern);
        Matcher match = compile.matcher(need);
        boolean matches = match.matches();
        if (matches == false) {
            System.out.println("Wrong Format");
            return;
        }

        /*
        * 通过正则和空格进行拆分
        * 然后判断数量
        * */
        String[] s1 = need.split("[ ]");
        if (s1.length > 3) {
            System.out.println("wrong number of points");
            return;
        }

        /*
        * 通过getTriangle获得三角形对象
        * */
        Triangle triangle = getTriangle(s1);

        /*
        * 判断钝角、直角还是锐角三角形
        * */
        judgeThreeT(triangle);

    }
}

//线类
class Line {
    private Point p1;
    private Point p2;


    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 Line(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public Double getLength() {
        Double length = Math.sqrt((p1.getX() - p2.getX()) * (p1.getX() - p2.getX()) + (p1.getY() - p2.getY()) * (p1.getY() - p2.getY()));
        return length;
    }
}

//点类
class Point {
    private Double x;
    private Double y;

    public Boolean equals(Point p) {
        if (Math.abs(x-p.getX())<1e-6 && Math.abs(y-p.getY())<1e-6) {
            return true;
        }
        return false;
    }

    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 Point(Double x, Double y) {
        this.x = x;
        this.y = y;
    }

    /*
    * 控制格式返回字符串
    * */
    @Override
    public String toString() {
        return String.format("%.1f,%.6f",x,y);
    }
}

踩坑心得

对正则表达式使用不熟练,屡屡出现问题
题目的数学公式推导复杂

改进建议

我认为我的正则表示式写的不够好,出现了很多问题,是可以优化的地方。但是碍于目前正则表达式我学的不够好,还没有写出更好的方案。

五、总结

  1. 在这三次实验中,从Java零基础开始,到现在接触了很多新东西。
    也第一次切身体会到了面向对象这个思想。满足了以往听到面向对象这个词的好奇,揭开了面向对象的神秘。
  2. 可以慢慢可以接受和理解面向对象。理解了封装的概念。
  3. 学习了正则表达式,发现可以通过这种简单的方式去处理字符串,与实验二循环对比,非常的简便。
  4. 对类的划分逻辑越来越清晰。
  5. 学会了字符串的一些使用操作。
  6. 代码的质量得到了很大的提高。
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值