****第一次Java学习blog

  • 前言
    • 关于java学习
    • 1.知识点:
    • 2.题量
    • 3.难度
  • 设计分析
    • 题目集二7.2
      • 1.源代码
      • 2.源代码分析
      • 3.心得
    • 题目集三 7.1
      • 1.源代码
      • 2.类图
      • 3.源代码分析
      • 4.心得
    • 题目集三 7.2
      • 1.源代码
      • 2.类图
      • 3.源代码分析
      • 4.心得
    • 题目集三7.3
      • 1.源代码
      • 2.类图
      • 3.源代码分析
      • 4.心得
  • 踩坑心得
  • 改进建议
  • 总结

前言

关于java学习

1.知识点:

  • 1.java基本语法
  • 2.String类的使用
  • 3.RS232串口通信协议
  • 4.方法的定义和调用
  • 5.定义类和创建对象

2.题量
作业题量相较于之前C语言课程减少了很多,但是代码量在第三次大作业急剧上升,并且单题花费的时间更多了。

3.难度
对比C语言作业,本学期的java难度提高了很多,无论是在程序的设计上面还是在边界的判断方面都有很大的提升,
尤其是对于程序的边界判断有很大的难度提升 。

设计分析

一、题目集二7.2

  • 1.源代码
点击查看代码
mport java.util.Scanner;
public class Main {
    public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            String arr;
            int count = 0;
            arr = input.nextLine();
            int len = arr.length();
            boolean flag = false;// 判断是否为无效数据
            for (int i = 0; i < arr.length(); i++) {
                if (arr.charAt(i) == '0') {
                    flag = true;
                    break;
                }
            }
            if (len < 11) {
                System.out.println("null data");
            } else {
                if (!flag) {
                    System.out.println("null data");
                } else {
                    int i = 0;
                    for (; i < arr.length(); i++) {
                        int cnt = 0;
                        if (arr.charAt(i) == '0') {
                            i++;
                            boolean judge1 = true;
                            boolean judge2 = true;
                            String ans = "";
                            for (; i < arr.length() && ans.length() != 8; i++) {
                                if (arr.charAt(i) == '1') {
                                    cnt++;
                                }
                                ans = ans + arr.charAt(i);
                            } // 检测边界问题 正常跳出时 已经为校验为
                            if (i >= arr.length()) {
                                break;
                            }
                            if (cnt % 2 == 1) {
                                if (arr.charAt(i) != '0') {
                                    judge1 = false;
                                }
                            } else {
                                if (arr.charAt(i) != '1') {
                                    judge1 = false;
                                }
                            }
                            i++;
                            if (i >= arr.length()) {
                                break;
                            }
                            if (arr.charAt(i) != '1') {
                                judge2 = false;
                            }
                            if (judge1 && judge2) {// 1:11101011
                                System.out.println((++count) + ":" + ans);
                            } else if (judge1 == false && judge2 == true) {
                                System.out.println((++count) + ":" + "parity check error");
                            } else if (judge2 == false) {
                                System.out.println((++count) + ":" + "validate error");
                            }
                        }
                    }
                }
            }
    }
}
  • 2.源代码分析
    • 1).利用String类对代码进行逐个字符的便利与拆封,从而判断该段字符的合法性

    • 2).设置两个boolean类型变量对字符串进行相应的输出判定

  • 3.心得
    ● 该题目使用的RS232是串口通信协议的奇偶校验是计算机上一种非常通用的设备通信协议,其中的奇偶校验是该题的解题关键,奇/偶校验(ECC)是数据传送时采用的一种校正数据错误的一种方式,分为奇校验和偶校验两种。如果是采用奇校验,在传送每一个字节的时候另外附加一位作为校验位,当实际数据中“1”的个数为偶数的时候,这个校验位就是“1”,否则这个校验位就是“0”,这样就可以保证传送数据满足奇校验的要求。在接收方收到数据时,将按照奇校验的要求检测数据中“1”的个数,如果是奇数,表示传送正确,否则表示传送错误。同理偶校验的过程和奇校验的过程一样,只是检测数据中“1”的个数为偶数。如0100101偶校验码就是10100101推理偶校验: 当实际数据中“1”的个数为偶数的时候,这个校验位就是“0”,否则这个校验位就是“1”,这样就可以保证传送数据满足偶校验的要求。在接收方收到数据时,将按照偶校验的要求检测数据中“1”的个数,如果是偶数个“1”,表示传送正确,否则表示传送错误。Rs232奇偶校验校的都是“1”的个数 。

具体RS232串口通信协议链接为:https://www.cnblogs.com/ybqjymy/p/12175994.html

二、题目集三 7.1计算两点之间的距离
源代码

点击查看代码
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String message = input.nextLine();
        double[] num = new double[4];
        String[] arr = message.split("[, ]");
        for (int i = 0; i < arr.length; i++) {
            if (i < 4) {
                if (judge(arr[i])) {
                    num[i] = Double.parseDouble(arr[i]);
                }
                else {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
            }
            else {
                System.out.println("wrong number of points");
                System.exit(0);
                break;
            }
        }
        double ans = Math.sqrt(Math.pow(num[0] - num[2], 2) + Math.pow(num[1] - num[3], 2));
        System.out.println(ans);
    }

    public static boolean judge(String arr) {
        boolean flag = true;
        if (arr.charAt(0) == '+' || arr.charAt(0) == '-') {
            for (int i = 1; i < arr.length(); i++) {
                if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
                    continue;
                }
                else if (arr.charAt(i) == '.') {
                    flag = false;
                    for (int j = i + 1; j < arr.length(); j++) {
                        if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
                            flag = true;
                            continue;
                        }
                    }
                    break;
                }
                else {
                    flag = false;
                }
                break;
            }
        }
        else if (arr.charAt(0) >= '0' && arr.charAt(0) <= '9') {
            if (arr.charAt(0) == '0') {
                if (arr.length() == 1) {
                    flag = true;
                }
                else {
                    if(arr.charAt(1)!='.'){
                        flag = false; 
                    }
                    else{
                        flag=false;
                        for(int i=2;i<arr.length();i++){
                            if(arr.charAt(i)>='0'&&arr.charAt(i)<='9'){
                                flag = true;
                                break;
                            }
                        }
                    }
                }
            }
            for (int i = 1; i < arr.length(); i++) {
                if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
                    continue;
                }
                else if (arr.charAt(i) == '.') {
                    flag = false;
                    for (int j = i + 1; j < arr.length(); j++) {
                        if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
                            flag = true;
                            continue;
                        }
                    }
                    break;
                }
                else {
                    flag = false;
                }
                break;
            }
        }
        else {
            flag = false;
        }
        return flag;
    }
}

类图

该题难度不大,最主要是在数据的读取上有问题,在输入的时候要先将其转换为字符串然后再进行浮点和整型的转换,从
而才可以开始计算

三、题目三7.2线的计算
源码:

点击查看代码
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int t[] = { 0, 4, 6, 6, 8, 8 };
        String str = input.nextLine();
        String[] arr = str.split("[: ,]");
        double[] num = new double[8];
        int max = 0;
        int choice = -1;

        String content = str;
        if(str.charAt(1)!=':'){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        String regStr = "[:, ]+";
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        String ss = "";
        while (matcher.find()) {
            ss = ss + matcher.group(0);
        }
        if (ss.charAt(0) != ':') {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        for (int i = 1; i < ss.length(); i++) {
            if (i % 2 == 1) {
                if (ss.charAt(i) == ',') {
                    continue;
                }
                else {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
            }
            else{
                if (ss.charAt(i) == ' ') {
                    continue;
                }
                else{
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
            }
        }

        if (arr[0] == "") {
            // if (arr[0].charAt(0) >= '0' && arr[0].charAt(0) <= '9') {
            System.out.println("Wrong Format");
            System.exit(0);
        } 
        else {
            if (judge(arr[0])) {
                if (arr[0].charAt(0) < '1' || arr[0].charAt(0) > '5') {// 6.0
                    choice = 0;
                } 
                else {
                    if (arr[0].length() == 1) {
                        choice = Integer.parseInt(arr[0]);
                    }
                    else {
                        choice = 0;
                    }
                }
            }
            else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        if (choice > 5 || choice < 1) {
            System.out.println("Wrong Format");
            System.exit(0);
        } // 1:-2.5,3 -2,+5.3
        for (int i = 1; i < arr.length; i++) {
            if (max >= t[choice]) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
            if (judge(arr[i])) {
                num[max++] = Double.parseDouble(arr[i]);
            } 
            else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        if (max != t[choice]) {
            System.out.println("wrong number of points");
            System.exit(0);
        }
        for (int i = 0; i < max; i += 2) {
            for (int j = i + 2; j < max; j += 2) {
                if (num[i] == num[j]) {
                    if (num[i + 1] == num[j + 1]) {
                        System.out.println("points coincide");
                        System.exit(0);
                    }
                }
            }
        }

        switch (choice) {
            case 1:
                getK(num[0], num[1], num[2], num[3]);
                break;
            case 2:
                getLength(num[0], num[1], num[2], num[3], num[4], num[5]);
                break;
            case 3:
                getLine(num[0], num[1], num[2], num[3], num[4], num[5]);
                break;
            case 4:
                getpingx(num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
                break;
            case 5:
                getpoint(num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
                break;
            default:
                System.out.println("Wrong Format");
        }
    }

    public static void getK(double x1, double y1, double x2, double y2) {
        double ans;
        if (x1 == x2) {
            System.out.println("Slope does not exist");
        } else {
            ans = (y1 - y2) / (x1 - x2);
            System.out.println(ans);
        } // 1:12,3 -2,+5
    }

    public static void getLength(double x1, double y1, double x2, double y2, double x3, double y3) {
        if (x2 == x3) {
            System.out.println(Math.abs(x1 - x2));
        } else if (y2 == y3) {
            System.out.println(Math.abs(y1 - y2));
        } else {
            double k = (y3 - y2) / (x3 - x2);
            double b = -k * x2 + y2;
            double ans = Math.abs(k * x1 - y1 + b) / Math.sqrt(1 + k * k);
            if (ans <= 1e-6) {
                ans = 0;
            }
            System.out.println(ans);
        }
    }

    public static void getLine(double x1, double y1, double x2, double y2, double x3, double y3) {
        if (x1 == x2 && x1 == x3) {
            System.out.println("true");
        } else if (y1 == y2 && y1 == y3) {
            System.out.println("true");
        } else {
            if (x1 == x2 || x2 == x3) {
                System.out.println("false");
            } else {
                double k1 = (y3 - y2) / (x3 - x2);
                double k2 = (y1 - y2) / (x1 - x2);
                if (Math.abs(k1 - k2) <= 1e-6) {
                    System.out.println("true");
                } else {
                    System.out.println("false");
                }
            }
        }
    }

    public static void getpingx(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
                                double y4) {
        double ax = x2 - x1, ay = y2 - y1;
        double bx = x4 - x3, by = y4 - y3;
        if (Math.abs(ax * by - bx * ay) <= 1e-6) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }

    public static void getpoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
                                double y4) {
        // is parallel lines,have no intersection point
        double ax = x2 - x1, ay = y2 - y1;
        double bx = x4 - x3, by = y4 - y3;
        double ansx, ansy;
        if (Math.abs(ax * by - bx * ay) <= 1e-6) {
            System.out.println("is parallel lines,have no intersection point");
        } else {// 以下为不平行
            if (x1 == x2) {
                double k1 = (y3 - y4) / (x3 - x4);
                double b1 = (float) -1 * k1 * x4 + y4;
                ansx = (float) x1;
                ansy = (float) k1 * ansx + b1;
            } else if (x4 == x3) {
                double k2 = (y1 - y2) / (x1 - x2);
                double b2 = (float) -1 * k2 * x2 + y2;
                ansx = x3;
                ansy = k2 * ansx + b2;
            } else {
                double k1 = (y3 - y4) / (x3 - x4);
                double k2 = (y1 - y2) / (x1 - x2);
                double b1 = -1 * k1 * x4 + y4;
                double b2 = -1 * k2 * x2 + y2;
                ansx = (b2 - b1) / (k1 - k2);
                ansy = k2 * ansx + b2;
            }
            // System.out.println((float) ansx + "," + (float) ansy + " " + "false");
            // System.out.println((float) ansx + "," + (float) ansy + " " + "true");
            if (((ansx > Math.min(x1, x2) && ansx < Math.max(x1, x2) && ansy > Math.min(y1, y2))
                    && ansy < Math.max(y1, y2))
                    || (ansx > Math.min(x3, x4) && ansx < Math.max(x3, x4) && ansy > Math.min(y3, y4)
                    && ansy < Math.max(y3, y4))) {
                System.out.println((float) ansx + "," + (float) ansy + " " + "true");
            } else {
                System.out.println((float) ansx + "," + (float) ansy + " " + "false");
            }
        }
    }

    public static boolean judge(String arr) {
        for (int i = 0; i < arr.length(); i++) {
            if (arr.charAt(i) == '.') {
                if (arr.length() > i + 1) {
                    if (arr.charAt(i + 1) == '.') {
                        return false;
                    }
                }
            }
        }
        boolean flag = true;
        if (arr.charAt(0) == '+' || arr.charAt(0) == '-') {
            if (arr.length() == 1) {
                return false;
            }
            if (arr.length() >= 3) {
                if (arr.charAt(1) == '0' && arr.charAt(2) != '.') {
                    return false;
                }
                if (arr.charAt(1) == '.') {
                    return false;
                }
            }
            if (arr.charAt(1) < '0' || arr.charAt(1) > '9') {
                return false;
            }
            for (int i = 1; i < arr.length(); i++) {
                if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
                    continue;// yi di zheng que
                } else if (arr.charAt(i) == '.') {
                    flag = false;
                    for (int j = i + 1; j < arr.length(); j++) {
                        if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
                            flag = true;
                            continue;
                        }
                    }
                    break;
                } else {
                    flag = false;
                }
                break;
            }
        } else if (arr.charAt(0) >= '0' && arr.charAt(0) <= '9') {
            if (arr.charAt(0) == '0') {
                if (arr.length() == 1) {
                    flag = true;
                } else {
                    if (arr.charAt(1) != '.') {
                        flag = false;
                    } else {
                        flag = false;
                        for (int i = 2; i < arr.length(); i++) {
                            if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
                                flag = true;
                                break;
                            }
                        }
                    }
                }
            }
            for (int i = 1; i < arr.length(); i++) {
                if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
                    continue;// yi di zheng que
                } else if (arr.charAt(i) == '.') {
                    flag = false;
                    for (int j = i + 1; j < arr.length(); j++) {
                        if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
                            flag = true;
                            continue;
                        }
                    }
                    break;
                } else {
                    flag = false;
                }
                break;
            }
        } else {
            flag = false;
        }
        return flag;
    }
}// :1,2 2,3

类图:

SourceMonitor的生成报表

  • 该题难度较大,虽然情况不多,但是想要获得满分很困难,错误格式判断难度较大,非常多的边界问题。
  • 格式的判断中有很多坑,并且再过程中没有将格式判断成功就会影响之后的结果,导致题目整体花费时间巨大,该题难度较高。

四、题目集三7.3三角形的计算
源码:

点击查看代码
import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//Wrong Format  wrong number of points
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int t[] = { 0, 6, 6, 6, 10, 8 };
        String str = input.nextLine();
        String[] arr = str.split("[: ,]");
        double[] num = new double[10];
        int max = 0;
        int choice = -1;

        String content = str;
        if (str.charAt(1) != ':') {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        String regStr = "[:, ]+";// ^\d+(\.\d+)?
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        String ss = "";
        while (matcher.find()) {
            ss = ss + matcher.group(0);
        } // 1:-3,01 -1,+1
        if (ss.charAt(0) != ':') {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        for (int i = 1; i < ss.length(); i++) {
            if (i % 2 == 1) {
                if (ss.charAt(i) == ',') {
                    continue;
                } else {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
            } else {
                if (ss.charAt(i) == ' ') {
                    continue;
                } else {
                    System.out.println("Wrong Format");
                    System.exit(0);
                }
            }
        }

        if (arr[0] == "") {
            // if (arr[0].charAt(0) >= '0' && arr[0].charAt(0) <= '9') {
            System.out.println("Wrong Format");
            System.exit(0);
        } else {
            if (judge(arr[0])) {
                if (arr[0].charAt(0) < '1' || arr[0].charAt(0) > '5') {// 6.0
                    choice = 0;
                } else {
                    if (arr[0].length() == 1) {
                        choice = Integer.parseInt(arr[0]);
                    } else {
                        choice = 0;
                    }
                }
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        if (choice > 5 || choice < 1) {
            System.out.println("Wrong Format");
            System.exit(0);
        } // 1:-2.5,3 -2,+5.3
        for (int i = 1; i < arr.length; i++) {
            if (max >= t[choice]) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
            if (judge(arr[i])) {
                num[max++] = Double.parseDouble(arr[i]);
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        if (max != t[choice]) {
            System.out.println("wrong number of points");
            System.exit(0);
        }

        switch (choice) {
            case 1:
                getK(num, num[0], num[1], num[2], num[3], num[4], num[5]);
                break;
            case 2:
                getscp(num, num[0], num[1], num[2], num[3], num[4], num[5]);
                break;
            case 3:
                getsan(num, num[0], num[1], num[2], num[3], num[4], num[5]);
                break;
            case 4:
                gets(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7], num[8], num[9]);
                break;
            case 5:
                gett(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
                break;
            default:
                System.out.println("Wrong Format");
        }

    } // ++2,-2.3,1.9,-3.2

    private static void gets(double[] num, double x4, double y4, double x5, double y5, double x1, double y1, double x2,
            double y2, double x3, double y3) {
        if (x4 == x5 && y4 == y5) {// 直线可以构成
            System.out.println("points coincide");
            System.exit(0);
        }
        for (int i = 4; i < 10; i += 2) {
            for (int j = i + 2; j < 10; j += 2) {
                if (num[i] == num[j]) {
                    if (num[i + 1] == num[j + 1]) {
                        System.out.println("data error");
                        System.exit(0);
                    }
                }
            }
        }
        double ax = x2 - x1, ay = y2 - y1;
        double bx = x2 - x3, by = y2 - y3;
        if (Math.abs(ax * by - bx * ay) <= 1e-6) {
            System.out.println("data error");
            System.exit(0);
        } // 三角形可以构成

        // 三角形与直线都可以构成
        double a[][] = new double[3][2];// 顶点
        double b[][] = new double[3][2];// 交点
        boolean flag1 = false;// 交点是否为顶点
        a[0][0] = x1;
        a[0][1] = y1;
        a[1][0] = x2;
        a[1][1] = y2;
        a[2][0] = x3;
        a[2][1] = y3;
        int cnt = 0;
        String s = Points(x4, y4, x5, y5, x1, y1, x2, y2);
        if (s == "no") {
            System.out.println("The point is on the edge of the triangle");
            System.exit(0);
        } else if (s == null) {// 无交点

        } else {
            String arr[] = s.split("[ ]");
            double x, y;
            x = Double.parseDouble(arr[0]);
            y = Double.parseDouble(arr[1]);
            if (x >= Math.min(x1, x2) && x <= Math.max(x1, x2)&&y>=Math.min(y1,y2)&&y<=Math.max(y1, y2)) {
                b[cnt][0] = x;
                b[cnt][1] = y;
                cnt++;
            }
        }

        s = Points(x4, y4, x5, y5, x2, y2, x3, y3);
        if (s == "no") {
            System.out.println("The point is on the edge of the triangle");
            System.exit(0);
        } else if (s == null) {// 无交点

        } else {
            String arr[] = s.split("[ ]");
            double x, y;
            x = Double.parseDouble(arr[0]);
            y = Double.parseDouble(arr[1]);
            if (x >= Math.min(x3, x2) && x <= Math.max(x3, x2)&&y>=Math.min(y3,y2)&&y<=Math.max(y3, y2)) {
                b[cnt][0] = x;
                b[cnt][1] = y;
                cnt++;
            }
        }

        s = Points(x4, y4, x5, y5, x1, y1, x3, y3);
        if (s == "no") {
            System.out.println("The point is on the edge of the triangle");
            System.exit(0);
        } else if (s == null) {// 无交点

        } else {
            String arr[] = s.split("[ ]");
            double x, y;
            x = Double.parseDouble(arr[0]);
            y = Double.parseDouble(arr[1]);
            if (x >= Math.min(x3, x1) && x <= Math.max(x3, x1)&&y>=Math.min(y1,y3)&&y<=Math.max(y1, y3)) {
                b[cnt][0] = x;
                b[cnt][1] = y;
                cnt++;
            }
        }
        // 数据存储完毕
        if (cnt == 0) {
            System.out.println("0");
            System.exit(0);
        }
        int k = 0, aaa = -1, bbb = -1;
        for (int i = 0; i < cnt; i++) {
            for (int j = i + 1; j < cnt; j++) {
                if (b[i][0] == b[j][0] && b[i][1] == b[j][1]) {
                    k++;// 有k组重复点
                    aaa = i;
                    bbb = j;
                }
            }
        } // 判断交点有无重复点。

        if (k != 0) {
            flag1 = true;
            double t1 = 0, t2 = 0, t3 = 0, t4 = 0;
            for (int i = 0; i < cnt; i++) {
                if (i == aaa || i == bbb) {
                    t1 = b[i][0];
                    t2 = b[i][1];
                } else {
                    t3 = b[i][0];
                    t4 = b[i][1];
                }
            }
            b[0][0] = t1;
            b[0][1] = t2;
            b[1][0] = t3;
            b[1][1] = t4;
            cnt = cnt - k;
        }
        if (cnt == 1) {
            System.out.println("1");
        } else {
            if (flag1) {
                int t = 0;// 找到重合点
                for (int i = 0; i < cnt; i++) {
                    for (int j = 0; j < 3; j++) {
                        if (a[j][0] == b[i][0] && a[j][1] == b[i][1]) {
                            t = j;
                        }
                    }
                }
                double ans[] = new double[2];
                int sss = 0;// 计数
                for (int i = 0; i < 3; i++) {
                    if (i != t) {
                        ans[sss++] = getmianji(b[0][0], b[0][1], b[1][0], b[1][1], a[i][0], a[i][1]);
                    }
                }
                if (ans[0] > ans[1]) {
                    double ttt = ans[1];
                    ans[1] = ans[0];
                    ans[0] = ttt;
                }
                System.out.println("2" + " " + new DecimalFormat("0.0#####").format(ans[0]) + " "
                        + new DecimalFormat("0.0#####").format(ans[1]));
            } else {
                double ans[] = new double[2];
                if (getLine(a[0][0], a[0][1], a[1][0], a[1][1], b[0][0], b[0][1])) {
                    if (getLine(a[2][0], a[2][1], a[1][0], a[1][1], b[1][0], b[1][1])) {
                        ans[0] = getmianji(b[0][0], b[0][1], b[1][0], b[1][1], a[1][0], a[1][1]);
                        ans[1] = getmianji(b[0][0], b[0][1], b[1][0], b[1][1], a[0][0], a[0][1])
                                + getmianji(b[1][0], b[1][1], a[2][0], a[2][1], a[0][0], a[0][1]);
                    } else {
                        ans[0] = getmianji(b[0][0], b[0][1], b[1][0], b[1][1], a[0][0], a[0][1]);
                        ans[1] = getmianji(b[0][0], b[0][1], b[1][0], b[1][1], a[2][0], a[2][1])
                                + getmianji(b[0][0], b[0][1], a[2][0], a[2][1], a[1][0], a[1][1]);
                    }
                } else {
                    ans[0] = getmianji(b[0][0], b[0][1], b[1][0], b[1][1], a[2][0], a[2][1]);
                    ans[1] = getmianji(b[0][0], b[0][1], b[1][0], b[1][1], a[0][0], a[0][1])
                            + getmianji(b[0][0], b[0][1], a[0][0], a[0][1], a[1][0], a[1][1]);
                }
                if (ans[0] > ans[1]) {
                    double ttt = ans[1];
                    ans[1] = ans[0];
                    ans[0] = ttt;
                }
                System.out.println("2" + " " + new DecimalFormat("0.0#####").format(ans[0]) + " "
                        + new DecimalFormat("0.0#####").format(ans[1]));
            }
        }

    }

    public static boolean getLine(double x1, double y1, double x2, double y2, double x3, double y3) {
        if (x1 == x2 && x1 == x3) {
            return true;
        } else if (y1 == y2 && y1 == y3) {
            return true;
            // System.out.println("true");
        } else {
            if (x1 == x2 || x2 == x3) {
                return false;
                // System.out.println("false");
            } else {
                double k1 = (y3 - y2) / (x3 - x2);
                double k2 = (y1 - y2) / (x1 - x2);
                if (Math.abs(k1 - k2) <= 1e-6) {
                    return true;
                    // System.out.println("true");
                } else {
                    return false;
                    // System.out.println("false");
                }
            }
        }
    }

    public static void gett(double num[], double x4, double y4, double x1, double y1, double x2, double y2, double x3,
            double y3) {
        double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
        double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
        double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
        if (l1 > l3) {// l3>l1
            double t = l1;
            l1 = l3;
            l3 = t;
        }
        if (l2 > l3) {// l3>l2
            double t = l2;
            l2 = l3;
            l3 = t;
        }
        if ((l1 + l2 - l3) <= 1e-6) {
            System.out.println("data error");
            System.exit(0);
        } // 判断是否可以构成三角形
        for (int j = 2; j < 8; j += 2) {// 判断该点是否在顶点
            if (num[0] == num[j]) {
                if (num[1] == num[j + 1]) {
                    System.out.println("on the triangle");
                    System.exit(0);
                }
            }
        }
        double x[] = new double[3];
        double y[] = new double[3];
        String s[] = new String[3];
        s[0] = Points(x1, y1, x4, y4, x2, y2, x3, y3);
        s[1] = Points(x2, y2, x4, y4, x1, y1, x3, y3);
        s[2] = Points(x3, y3, x4, y4, x1, y1, x2, y2);
        for (int i = 0; i < 3; i++) {
            String arr[] = s[i].split("[ ]");
            x[i] = Double.parseDouble(arr[0]);
            y[i] = Double.parseDouble(arr[1]);
        } // 读取所有坐标位置
        for (int i = 0; i < 3; i++) {// 判断该点是否在边上
            if (x[i] == x4) {
                if (y[i] == y4) {
                    System.out.println("on the triangle");
                    System.exit(0);
                }
            }
        }
        if (x[0] < Math.min(x2, x3) || x[0] > Math.max(x2, x3)) {
            System.out.println("outof the triangle");
            System.exit(0);
        }
        if (x[1] < Math.min(x1, x3) || x[1] > Math.max(x1, x3)) {
            System.out.println("outof the triangle");
            System.exit(0);
        }
        if (x[2] < Math.min(x1, x2) || x[2] > Math.max(x1, x2)) {
            System.out.println("outof the triangle");
            System.exit(0);
        }
        System.out.println("in the triangle");
        System.exit(0);
    }

    public static double getmianji(double x1, double y1, double x2, double y2, double x3, double y3) {// 求三角形面积
        double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
        double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
        double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
        double c = l1 + l2 + l3;
        double t = c / 2;
        double s = Math.sqrt(t * (t - l1) * (t - l2) * (t - l3));
        return s;
    }

    public static void getK(double num[], double x1, double y1, double x2, double y2, double x3, double y3) {

        judges(6, num, x1, y1, x2, y2, x3, y3);
        double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
        double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
        double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
        if (Math.abs(l1 - l2) < 1e-6) {
            if (Math.abs(l1 - l3) < 1e-6) {
                System.out.println("true" + " " + "true");
            }
        } else {
            if (Math.abs(l3 - l2) < 1e-6 || Math.abs(l1 - l3) < 1e-6) {
                System.out.println("true" + " " + "false");
            } else {
                System.out.println("false" + " " + "false");
            }
        }
    }

    public static void getscp(double num[], double x1, double y1, double x2, double y2, double x3, double y3) {
        judges(6, num, x1, y1, x2, y2, x3, y3);
        double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
        double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
        double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
        double c = l1 + l2 + l3;
        double t = c / 2;
        double s = Math.sqrt(t * (t - l1) * (t - l2) * (t - l3));
        double x4 = (x1 + x2 + x3) / 3, y4 = (y1 + y2 + y3) / 3;
        System.out.println(new DecimalFormat("0.0#####").format(c) + " " + new DecimalFormat("0.0#####").format(s) + " "
                + new DecimalFormat("0.0#####").format(x4) + "," + new DecimalFormat("0.0#####").format(y4));
    }

    public static void getsan(double num[], double x1, double y1, double x2, double y2, double x3, double y3) {
        judges(6, num, x1, y1, x2, y2, x3, y3);
        double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
        double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
        double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
        if (l1 > l3) {// l3>l1
            double t = l1;
            l1 = l3;
            l3 = t;
        }
        if (l2 > l3) {// l3>l2
            double t = l2;
            l2 = l3;
            l3 = t;
        }
        if (Math.abs(l3 * l3 - l1 * l1 - l2 * l2) <= 1e-6) {
            System.out.println("false true false");
        } else if (l3 * l3 > l1 * l1 + l2 * l2) {
            System.out.println("true false false");
        } else {
            System.out.println("false false true");
        }
    }

    public static void judges(int n, double num[], double x1, double y1, double x2, double y2, double x3, double y3) {
        for (int i = 0; i < n; i += 2) {
            for (int j = i + 2; j < n; j += 2) {
                if (num[i] == num[j]) {
                    if (num[i + 1] == num[j + 1]) {
                        System.out.println("data error");
                        System.exit(0);
                    }
                }
            }
        }
        double ax = x2 - x1, ay = y2 - y1;
        double bx = x2 - x3, by = y2 - y3;
        if (Math.abs(ax * by - bx * ay) <= 1e-6) {
            System.out.println("data error");
            System.exit(0);
        }
    }

    public static boolean judge(String arr) {
        for (int i = 0; i < arr.length(); i++) {
            if (arr.charAt(i) == '.') {
                if (arr.length() > i + 1) {
                    if (arr.charAt(i + 1) == '.') {
                        return false;
                    }
                }
            }
        }
        boolean flag = true;
        if (arr.charAt(0) == '+' || arr.charAt(0) == '-') {
            if (arr.length() == 1) {
                return false;
            }
            if (arr.length() >= 3) {
                if (arr.charAt(1) == '0' && arr.charAt(2) != '.') {
                    return false;
                }
                if (arr.charAt(1) == '.') {
                    return false;
                }
            }
            if (arr.charAt(1) < '0' || arr.charAt(1) > '9') {
                return false;
            }
            for (int i = 1; i < arr.length(); i++) {
                if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
                    continue;// yi di zheng que
                } else if (arr.charAt(i) == '.') {
                    flag = false;
                    for (int j = i + 1; j < arr.length(); j++) {
                        if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
                            flag = true;
                            continue;
                        }
                    }
                    break;
                } else {
                    flag = false;
                }
                break;
            }
        } else if (arr.charAt(0) >= '0' && arr.charAt(0) <= '9') {
            if (arr.charAt(0) == '0') {
                if (arr.length() == 1) {
                    flag = true;
                } else {
                    if (arr.charAt(1) != '.') {
                        flag = false;
                    } else {
                        flag = false;
                        for (int i = 2; i < arr.length(); i++) {
                            if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
                                flag = true;
                                break;
                            }
                        }
                    }
                }
            }
            for (int i = 1; i < arr.length(); i++) {
                if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
                    continue;// yi di zheng que
                } else if (arr.charAt(i) == '.') {
                    flag = false;
                    for (int j = i + 1; j < arr.length(); j++) {
                        if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
                            flag = true;
                            continue;
                        }
                    }
                    break;
                } else {
                    flag = false;
                }
                break;
            }
        } else {
            flag = false;
        }
        return flag;
    }

    public static String Points(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
            double y4) {// 求交点坐标,用空格分开的字符串
        double A1 = y1 - y2;
        double B1 = x2 - x1;
        double C1 = -(A1 * x1 + B1 * y1);

        double A2 = y3 - y4;
        double B2 = x4 - x3;
        double C2 = -(A2 * x3 + B2 * y3);

        double det = A1 * B2 - A2 * B1;
        if (Math.abs(det) < 1e-6) {// 判断是否平行
            if (C1 != C2) {// 判断是否重合
                return null;
            } else {
                return "no";// 重合返回"no"
            }
        }
        Double x = (B1 * C2 - B2 * C1) / (A1 * B2 - A2 * B1);
        Double y = (A1 * C2 - A2 * C1) / (A2 * B1 - A1 * B2);
        String s = x.toString() + " " + y.toString();
        return s;
    }
}

类图:

SourceMonitor的生成报表

  • 该题难度较大,在类的设计方面有较大的难度,但是在第二题的基础上解决了原本就有的格式错误判断问题,从而解决了该题的很大一部分难度.
  • 在该题中分为了多个方法去解决问题,让代码的变得更易读.

踩坑心得

  • 1.java double精度问题
    在java中double存在精度丢失的问题,如下所示:
点击查看代码
java
public class Main {
public static void main(String[] args) {
System.out.println(0.06 + 0.01);
System.out.println(1.0 - 0.42);
System.out.println(4.014 * 100);
System.out.println(303.1 / 1000);
}
}
● 该段代码应该输出为 0.07 0.58 401.4 0.3031

● 实际输出为:

与所需要输出有误差,不可用于作比较,会出现很大的误差
总结:在利用java进行高精度运算时,应该使用BigInteger或者BigDecimal类,使用double类极其容易出现误差,从而影响计算的
结果

  • 2.RS232串口通信协议
    ● 未理解什么是奇偶校验,但是未查其概念,从而导致自己调试但是仍然无法通过代码的运行,从而浪费大量的时间用来进行
    代码调试 >具体RS232串口通信协议链接为:https://www.cnblogs.com/ybqjymy/p/12175994.html
  • 3.起初未用正则表达式进行判断,导致很多格式错误,走了很多弯路。其次三角形判断的方式没有选择好
  • 4.在类中很多地方的属性没有进行初始化,导致出现很多错误

改进意见

  • 根据SourceMonitor的生成报表显示,最高复杂度、平均复杂度和平均深度超出应有范围。说明7.2与7.3的代码均写的有些复杂。虽然题目所要求的功能都能实现,但是占用了许多不应有的资源,程序健壮性欠佳。初步估计与类的划分与方法的划分有关,又或者用了太多的for循环,使得复杂度与深度均超标。会在以后的学习中格外注意,侧重学习如何科学有效的规划代码,将类科学的进行划分,一个类只完成一个功能,对于方法的运营也要合理的当,以此作为目标对未来的学习指明方向。

总结

  • 1.学习成果

    • 1.面向对象:在日常生活或编程中,简单的问题可以用面向过程的思路来解决,直接有效,但是当问题的规模变得更大时,用面向过程的思想是远远不够的。所以慢慢就出现了面向对象的编程思想。世界上有很多人和事物,每一个都可以看做一个对象,而每个对象都有自己的属性和行为,对象与对象之间通过方法来交互。面向对象是一种以“对象”为中心的编程思想,把要解决的问题分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个对象在整个解决问题的步骤中的属性和行为。

    • 2.面向过程:面向过程是一种以事件为中心的编程思想,编程的时候把解决问题的步骤分析出来,然后用函数把这些步骤实现,在一步一步的具体步骤中再按顺序调用函数。

    • 3.经过这三次的练习和这段时间的java学习,我对java有了更加深入的了解,并且对java语言的面向对象特性也有了更加深入的理解,对程序的边界输入判断也有了更多的经验和想法,在代码的规范上也有了很大的提升,并且会对每个类进行封装,从而增强其复用性。

    • 4.对于方法的使用已经渐入佳境、逐渐掌握,但是对于类的规划存在严重不足。第三次大作业都只用了一个类,这一个类中包含了许多方法,以后的学习要在类上多下功夫。同时,在java的边界判断上还有很大的不足,应该增强自身对代码的数据边界问题的判断能力。

    • 5.正则表达式

      • (1)replace检测空格换成空的在java里就是//s为空格,替换成“”

      • (2)"([+-]?[1-9][0-9])?" + "(\?[+-]?x(\^([+-]?[1-9][0-9]))?)?"这个就是匹配单个的表达式,是不是直接懵B,我先来直接讲解一下吧,第一个是判断+-号应该通俗吧,后面有?是什么意思呢就是后面这一个数有没有都无所谓,有就是两位数没有就是一位数,[1-9][0-9]就是两位数字十位上【1-9】个位上【0-9】对没错,这个只能搞两位数,后面这个相当于一个无所谓的,没有他就是错的,?前面说了是后面有没有都无所谓所以这里相当于一个结束符,java里的+就是连接大家都知道吧,后面的话大家应该知道了吧可以自己去翻译一下,那下面再来讲一下正则表达式先来这个代码讲解。好了后面就是匹配了,把express跟匹配wholeexpress,然后在返回,错了就直接输出错误,对了就进行计算。

  • 2.改进和意见

  • 1.对于课程的作业,希望老师的考察点更加明确,比如第一次作业的浮点问题,用float才可以正常通过,用double则不行,如果该次作业的题目考察的是精度问题,那么我用BigDecimal进行计算时应该是可以过的,但还是WA了,所以我觉得应该对知识点有更加明确的考察,若考察浮点数则可以给出提示该题测试点的设计是用double还是float,不应该让我们去猜测这种问题,对于出现这种的调试十分困难,非常浪费我们的时间

  • 2.个人意见,在上课结束之后,老师可以课程计划发给我们,便于我们去进行复习。个人觉得看文档复习上课内容比看课外补习视频花费的时间要少,并且也可以锻炼我们的自学能力,由于老师讲课的时候知识点跳跃性很大,所以我觉得课后将记事本上的内容拷贝一份给我们是有必要的,上课的时候好好听,下课后再进行笔记的记录,可以做到提高上课的效率,同时可以有课后复习的效果。

posted on 2022-10-01 23:06  wuzx。  阅读(48)  评论(0)    收藏  举报