• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

第二次 java 学习 blog

第二次 java 学习 blog

前言

1.知识点

2.题量与难度

设计与分析

题目集四

题目集五

题目集六

链表作业

期中考试

采坑心得

改进建议

总结

学习到的内容

继承

多态

抽象类与接口

抽象类

接口 Interface

Java泛型

1.java 中泛型标记符

课程改进建议及意见

前言

1.知识点

1.基础图形运算知识

2.浮点数有效位数的保留

3.正则表达式

4.字符串转浮点型和整型

5.类与结构的设计

6.继承,多态,接口

2.题量与难度

1.题量不多,但是题量基本与难度成反比

2.题不多的时候,题目难度较大,情况复杂,判断很多,边界问题较多,花费大量的时间

设计与分析

题目集四

1.计算两点之间的距离

源码:

导入Java。util。扫描仪;

//错误的格式错误的点数

公共类主要{

公共静态void main(String[] args) {

扫描仪输入=新扫描仪(系统。在);

字符串a=输入。下一行();

double[]num=new double[4];

//a=a.replace(',',' ');

String[] arr=a.split('[,]');

for(int I=0;长度;i ) {

如果(i 4) {

if(法官(arr){

num=double。parse double(arr);

}否则{

System.out.println("格式错误");

系统。退出(0);

}

}否则{

System.out.println('点数错误');

系统。退出(0);

打破;

}

}

双ans=数学。sqrt(数学。pow(num[0]-num[2],2) Math.pow(nu

m[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;// 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; } }

类图
image

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

  • 2.线的计算

源码:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//1:-3,01 -1,+1
//Wrong Format  wrong number of points
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 = "[:, ]+";// ^\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);
        }
        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

类图:

image

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

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;
    }
}

类图:

image

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

题目集五

  • 前四题都为简单的正则表达式问题,只要熟悉了正则表达式后可以很快解决问题
5.ATM 机类结构设计

源码:

import java.util.Objects;
import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.ArrayList;
//Arrays.sort ()
public class Main {
    public static void main(String[] args) {
        ArrayList<ATM> atms = new ArrayList<>();
        ArrayList<Person> persons = new ArrayList<>();
        atms.add(new ATM("01", "6217"));
        atms.add(new ATM("02", "6217"));
        atms.add(new ATM("03", "6217"));
        atms.add(new ATM("04", "6217"));
        atms.add(new ATM("05", "6222"));
        atms.add(new ATM("06", "6222"));
        persons.add(new Person("杨过"));
        persons.get(0).addAccount("3217000010041315709", 10000.00);
        persons.get(0).getAccounts().get(0).addCard("6217000010041315709", "88888888");
        persons.get(0).getAccounts().get(0).addCard("6217000010041315715", "88888888");
        persons.get(0).addAccount("3217000010041315715", 10000.00);
        persons.get(0).getAccounts().get(1).addCard("6217000010041315718", "88888888");
        persons.add(new Person("郭靖"));
        persons.get(1).addAccount("3217000010051320007", 10000.00);
        persons.get(1).getAccounts().get(0).addCard("6217000010051320007", "88888888");
        persons.add(new Person("张无忌"));
        persons.get(2).addAccount("3222081502001312389", 10000.00);
        persons.get(2).getAccounts().get(0).addCard("6222081502001312389", "88888888");
        // 222081502001312390 10000.00 6222081502001312390
        persons.get(2).addAccount("6222081502001312389", 10000.00);
        persons.get(2).getAccounts().get(1).addCard("6222081502001312390", "88888888");
        // 3222081502001312399 6222081502001312399 6222081502001312400
        persons.get(2).addAccount("3222081502001312399", 10000.00);
        persons.get(2).getAccounts().get(2).addCard("6222081502001312399", "88888888");
        persons.get(2).getAccounts().get(2).addCard("6222081502001312400", "88888888");
        persons.add(new Person("韦小宝"));
        persons.get(3).addAccount("3222081502051320785", 10000.00);
        persons.get(3).getAccounts().get(0).addCard("6222081502051320785", "88888888");
        persons.get(3).addAccount("3222081502051320786", 10000.00);
        persons.get(3).getAccounts().get(1).addCard("6222081502051320786", "88888888");
        Scanner input = new Scanner(System.in);
        DecimalFormat DF = new DecimalFormat("0.00");
        while (true) {
            String s = input.nextLine();
            if (s.equals("#")) {
                break;
            } else {
                boolean getcardid = false;
                boolean getamtid = false;
                String arr[] = s.split("\\s+");
                String carddid = arr[0];
                if (arr.length == 1) {
                    for (int i = 0; i < persons.size(); i++) {
                        for (int j = 0; j < persons.get(i).getAccounts().size(); j++) {
                            for (int k = 0; k < persons.get(i).getAccounts().get(j).getCards().size(); k++) {
                                if (Objects.equals(persons.get(i).getAccounts().get(j).getCards().get(k).getCardid(),
                                        carddid)) {
                                    getcardid = true;
                                    System.out
                                            .println("?" + DF.format(persons.get(i).getAccounts().get(j).getBalance()));
                                    break;
                                }
                            }
                        }
                    }
                    if (!getcardid) {
                        System.out.println("Sorry,this card does not exist.");
                    }
                } else {
                    String cardid = arr[0];
                    String password = arr[1];
                    String atmid = arr[2];
                    double charge = Double.parseDouble(arr[3]);
                    for (int i = 0; i < persons.size(); i++) {
                        for (int j = 0; j < persons.get(i).getAccounts().size(); j++) {
                            for (int k = 0; k < persons.get(i).getAccounts().get(j).getCards().size(); k++) {
                                if (Objects.equals(persons.get(i).getAccounts().get(j).getCards().get(k).getCardid(),
                                        cardid)) {
                                    getcardid = true;
                                    for (int t = 0; t < atms.size(); t++) {
                                        if (atms.get(t).getId().equals(atmid)) {
                                            getamtid = true;
                                            if (!password.equals(persons.get(i).getAccounts().get(j).getCards().get(k)
                                                    .getPassword())) {
                                                System.out.println("Sorry,your password is wrong.");
                                                break;
                                            }
                                            if (charge > persons.get(i).getAccounts().get(j).getBalance()) {
                                                System.out.println("Sorry,your account balance is insufficient.");
                                                break;
                                            } else {
                                                persons.get(i).getAccounts().get(j).changebalance(charge);
                                            }
                                            if (Objects.equals(atms.get(t).getBelong(), cardid.substring(0, 4))) {
                                                if (charge > 0) {// 取走
                                                    if (t < 4) {// 杨过在中国建设银行的 02 号 ATM 机上取款?3500.00
                                                        System.out.println(persons.get(i).getName() + "在中国建设银行的" + atmid
                                                                + "号ATM机上取款?" + DF.format(charge));
                                                        System.out.println("当前余额为?" + DF.format(
                                                                persons.get(i).getAccounts().get(j).getBalance()));
                                                    } else {
                                                        System.out.println(persons.get(i).getName() + "在中国工商银行的" + atmid
                                                                + "号ATM机上取款?" + DF.format(charge));
                                                        System.out.println("当前余额为?" + DF.format(
                                                                persons.get(i).getAccounts().get(j).getBalance()));
                                                    }
                                                } else {// 存储
                                                    if (charge != 0) {
                                                        charge = charge * (-1);
                                                    }
                                                    if (t < 4) {
                                                        System.out.println(persons.get(i).getName() + "在中国建设银行的" + atmid
                                                                + "号ATM机上存款?" + DF.format(charge));
                                                        System.out.println("当前余额为?" + DF.format(
                                                                persons.get(i).getAccounts().get(j).getBalance()));
                                                    } else {
                                                        System.out.println(persons.get(i).getName() + "在中国工商银行的" + atmid
                                                                + "号ATM机上存款?" + DF.format(charge));
                                                        System.out.println("当前余额为?" + DF.format(
                                                                persons.get(i).getAccounts().get(j).getBalance()));
                                                    }
                                                }
                                            } else {
                                                System.out.println("Sorry,cross-bank withdrawal is not supported.");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (!getcardid) {
                        System.out.println("Sorry,this card does not exist.");
                        continue;
                    }
                    if (!getamtid) {
                        System.out.println("Sorry,the ATM's id is wrong.");
                    }
                }
            }
        }
    }
}
class ATM {// 做成一个数组,便于便利
    private String id;
    private String belong;// 存银行卡号前四位;
    public ATM(String id, String belong) {
        this.id = id;
        this.belong = belong;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getBelong() {
        return belong;
    }
    public void setBelong(String belong) {
        this.belong = belong;
    }
}
class Person {
    private ArrayList<Account> accounts = new ArrayList<>();// 用户账户
    private String name;
    public Person(String name) {
        this.name = name;
    }
    public void addAccount(String accid, double balance) {
        accounts.add(new Account(accid, balance));
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public ArrayList<Account> getAccounts() {
        return accounts;
    }
    public void setAccounts(ArrayList<Account> accounts) {
        this.accounts = accounts;
    }
}
class Account {
    private double balance;// 账户余额
    private ArrayList<Card> cards = new ArrayList<>();// 账户卡号
    private String accid;
    public Account(String accid) {
        this.accid = accid;
    }
    public Account(String accid, double balance) {
        this.accid = accid;
        this.balance = balance;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public ArrayList<Card> getCards() {
        return cards;
    }
    public void setCards(ArrayList<Card> cards) {
        this.cards = cards;
    }
    public void changebalance(double a) {// 题意为负数为存款,正数为取款
        this.balance = balance - a;
    }
    public void addCard(String cardid, String password) {
        cards.add(new Card(cardid, password));
    }
}
class Card {
    private String cardid;
    private String password;// 密码
    public Card(String cardid, String password) {
        this.cardid = cardid;
        this.password = password;
    }
    public String getCardid() {
        return cardid;
    }
    public void setCardid(String cardid) {
        this.cardid = cardid;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

类图:

image

该题难度不大,但是在类的设计上需要提前花费较大的功夫,但类之间的调用关系设计好了之后,只需要按照题目的要求进行输入输出即可

题目集六

  • 1.sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式)

源码:

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);
        while (true) {
            int ans = 0;
            String arr = input.nextLine();
            if (arr.equals("end")) {
                System.exit(0);
            }
            String regex = "\\d+";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(arr);
            while (matcher.find()) {
                ans = ans + Integer.parseInt(matcher.group(0));
            }
            System.out.println(ans);
        }
    }
}

类图:

image

该题难度不大,使用最基本的正则表达式即可解决

  • 2.点线形系列4-凸四边形的计算

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) {
        try (Scanner input = new Scanner(System.in)) {
            int t[] = { 0, 8, 8, 8, 12, 10 };
            String str = input.nextLine();
            String[] arr = str.split("[: ,]");
            double[] num = new double[12];
            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);
            } // 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:
                    judgequadrilateral(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
                    break;
                case 2:
                    judgespecialqua(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
                    break;
                case 3:
                    jdugequas(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
                    break;
                case 4:
                    getans(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7],
                            num[8], num[9], num[10], num[11]);
                    break;
                case 5:
                    judgein(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7], num[8], num[9]);
                    break;
                default:
                    System.out.println("Wrong Format");
            }
        }
    }
    public static void getans(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
            double x4, double y4, double x5, double y5, double x6, double y6) {
        System.out.println("not a quadrilateral or triangle");
    }
    // 5:2,2 +0,-0.0 -10,10 +0.0,20 10,10
    public static void judgein(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
            double x4, double y4, double x5, double y5) {// on the triangle outof the triangle
        if (quadrilateral(num, x5, y5, x2, y2, x3, y3, x4, y4)) {// 四边形 in the triangle
            for (int i = 2; i < 10; i += 2) {
                if (num[0] == num[i]) {
                    if (num[1] == num[i + 1]) {// 点重合
                        System.out.println("on the quadrilateral");
                        System.exit(0);
                    }
                }
            }
            if (gett(num, x1, y1, x2, y2, x3, y3, x4, y4).equals("on")
                    && gett(num, x1, y1, x2, y2, x4, y4, x5, y5).equals("on")) {
                System.out.println("on the quadrilateral");
            } else if (gett(num, x1, y1, x2, y2, x3, y3, x4, y4).equals("on")
                    || gett(num, x1, y1, x2, y2, x4, y4, x5, y5).equals("on")) {
                System.out.println("on the quadrilateral");
            } else if (gett(num, x1, y1, x2, y2, x3, y3, x4, y4).equals("in")
                    || gett(num, x1, y1, x2, y2, x4, y4, x5, y5).equals("in")) {
                System.out.println("in the quadrilateral");
            } else {
                System.out.println("outof the quadrilateral");
            }
        } else {// 非四边形
            boolean st[] = new boolean[10];
            double x[] = new double[3];
            double y[] = new double[3];
            int cnt = 0;
            for (int i = 2; i < 10; i += 2) {
                for (int j = i + 2; j < 10; j += 2) {
                    if (num[i] == num[j] && !st[i] && !st[j]) {
                        if (num[i + 1] == num[j + 1]) {
                            cnt++;
                            st[j] = true;
                        }
                    }
                }
            }
            if (cnt >= 2) {
                System.out.println("not a quadrilateral or triangle");
            } else {
                int ttt = 0;// 计数作用
                for (int i = 2; i < 10; i += 2) {
                    if (!st[i]) {
                        x[ttt++] = num[i];
                        y[ttt++] = num[i + 1];
                    }
                }
                if (judgesanjiao(x[0], y[0], x[1], y[1], x[2], y[2])) {
                    String sss = gett(num, x1, y1, x[0], y[0], x[1], y[1], x[2], y[2]);
                    if (sss.equals("on")) {// on the triangle outof the triangle
                        System.out.println("on the triangle");
                    } else if (sss.equals("in")) {
                        System.out.println("in the triangle");
                    } else {
                        System.out.println("outof the triangle");
                    }
                } else {
                    System.out.println("not a quadrilateral or triangle");
                }
            }
        }
    }
    public static boolean judgesanjiao(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) {
            return false;
        } else {
            return true;
        } // 判断是否可以构成三角形
    }
    public static String gett(double num[], double x4, double y4, double x1, double y1, double x2, double y2, double x3,
            double y3) {
        for (int j = 2; j < 8; j += 2) {// 判断该点是否在顶点
            if (num[0] == num[j]) {
                if (num[1] == num[j + 1]) {
                    return "on";// 在顶点即为在边上
                }
            }
        }
        double x[] = new double[3];
        double y[] = new double[3];
        String s[] = new String[3];
        s[0] = getPoints(x4, y4, x1, y1, x2, y2, x3, y3);
        s[1] = getPoints(x4, y4, x2, y2, x1, y1, x3, y3);
        s[2] = getPoints(x4, y4, x3, y3, x1, y1, x2, y2);
        for (int i = 0; i < 3; i++) {
            if (s[i] == null) {
                return "out";
            }
        }
        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) {
                    return "on";// 在边上
                }
            }
        }
        if (x[0] < Math.min(x2, x3) || x[0] > Math.max(x2, x3)) {
            return "out";
        }
        if (x[1] < Math.min(x1, x3) || x[1] > Math.max(x1, x3)) {
            return "out";
        }
        if (x[2] < Math.min(x1, x2) || x[2] > Math.max(x1, x2)) {
            return "out";
        }
        return "in";// 在三角形内部
    }
    static void jdugequas(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
            double x4, double y4) {
        DecimalFormat df = new DecimalFormat("0.0##");
        if (quadrilateral(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
            if (judgeut(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
                System.out.println("true " + df.format(getc(x1, y1, x2, y2, x3, y3, x4, y4)) + " "
                        + df.format(getmianji(num, x1, y1, x2, y2, x3, y3, x4, y4)));
            } else {
                System.out.println("false " + df.format(getc(x1, y1, x2, y2, x3, y3, x4, y4)) + " "
                        + df.format(getmianji(num, x1, y1, x2, y2, x3, y3, x4, y4)));
            }
        } else {
            System.out.println("not a quadrilateral");
        }
    }
    public static double getmianji(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
            double x4, double y4) {// 凸为true;
        double a = gets(x1, y1, x2, y2, x3, y3);
        double c = gets(x1, y1, x4, y4, x3, y3);
        double b = gets(x4, y4, x2, y2, x3, y3);
        double d = gets(x1, y1, x4, y4, x2, y2);
        if (Math.abs(a + c - b - d) < 1e-6) {
            return a + c;
        } else {
            return Math.min(a + c, b + d);
        }
    }
    public static boolean judgeut(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
            double x4, double y4) {// 凹为true
        double a = gets(x1, y1, x2, y2, x3, y3);
        double c = gets(x1, y1, x4, y4, x3, y3);
        double b = gets(x4, y4, x2, y2, x3, y3);
        double d = gets(x1, y1, x4, y4, x2, y2);
        if (Math.abs(a + c - b - d) < 1e-6) {
            return true;
        } else {
            return false;
        }
    }
    public static void judgespecialqua(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
            double x4, double y4) {
        if (quadrilateral(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
            if (parallel(x1, y1, x2, y2, x3, y3, x4, y4)) {
                if (judgelinxin(x1, y1, x2, y2, x3, y3)) {
                    if (vertical(x1, y1, x2, y2, x2, y2, x3, y3)) {
                        System.out.println("true true true");
                    } else {
                        System.out.println("true false false");
                    }
                } else {
                    if (vertical(x1, y1, x2, y2, x2, y2, x3, y3)) {
                        System.out.println("false true false");
                    } else {
                        System.out.println("false false false");
                    }
                }
            } else {
                System.out.println("false false false");
            }
        } else {
            System.out.println("not a quadrilateral");
        }
    }
    public static double gets(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 double getc(double x1, double y1, double x2, double y2, double x3, double y3,
            double x4,
            double y4) {
        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 - x4, 2) + Math.pow(y3 - y4, 2));
        double l4 = Math.sqrt(Math.pow(x4 - x1, 2) + Math.pow(y4 - y1, 2));
        return (l1 + l2 + l3 + l4);
    }
    public static boolean vertical(double x1, double y1, double x2, double y2, double x3, double y3,
            double x4,
            double y4) {
        double a1 = x1 - x2;
        double b1 = y1 - y2;
        double a2 = x3 - x4;
        double b2 = y3 - y4;
        if (Math.abs(a1 * a2 - b1 * b2) < 1e-6) {
            return true;
        } else {
            return false;
        }
    }
    public static boolean judgelinxin(double x1, double y1, double x2, double y2, double x3, double y3) {
        double a = Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2);
        double b = Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2);
        if (Math.abs(a - b) < 1e-6) {
            return true;
        } else {
            return false;
        }
    }
    public static void judgequadrilateral(double num[], double x1, double y1, double x2, double y2, double x3,
            double y3, double x4, double y4) {
        if (coincide(num, 8)) {
            System.out.println("points coincide");
        } else {
            if (quadrilateral(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
                if (Parallelogram(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
                    System.out.println("true true");
                } else {
                    System.out.println("true false");
                }
            } else {
                System.out.println("false false");
            }
        }
    }
    public static boolean quadrilateral(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
            double x4,
            double y4) {// 判断四边形
        if (parallel(x1, y1, x2, y2, x2, y2, x3, y3)) {
            return false;
        } else if (parallel(x2, y2, x3, y3, x3, y3, x4, y4)) {
            return false;
        } else if (parallel(x1, y1, x4, y4, x4, y4, x3, y3)) {
            return false;
        } else {
            return true;
        }
    }
    public static boolean coincide(double num[], int n) {
        for (int i = 0; i < n; i++) {// 点重合
            for (int j = i + 2; j < n; j++) {
                if (num[i] == num[j]) {
                    if (num[i + 1] == num[j + 1]) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    public static boolean Parallelogram(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
            double x4,
            double y4) {// 判断平行四边形
        boolean flag = false;
        if (parallel(x1, y1, x2, y2, x3, y3, x4, y4) && parallel(x1, y1, x3, y3, x2, y2, x4, y4)) {
            flag = true;
        } else {
            flag = false;
        }
        return flag;
    }
    public static boolean parallel(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
            double y4) {// 平行返回true
        double A1 = y1 - y2;
        double B1 = x2 - x1;
        double A2 = y3 - y4;
        double B2 = x4 - x3;
        double det = A1 * B2 - A2 * B1;
        if (Math.abs(det) < 1e-6) {
            return true;
        } else {
            return false;
        }
    }
    public static String getPoints(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;// 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;
    }
    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;
    }
}

类图:

image

+该题的难度较大,在凹凸四边形判断时涉及了多种情况,并且在输入时格式错误问题非常多,导致该题花费时间极多,并且获取满分的难度很大,在第四点时,当两点切割凹四边形时,情况十分复杂,难度很大,导致花费时间很多,并且满分极少,从而导致该次作业满分人数很少

链表作业

  • 单向链表

源码:

package list;
public interface LinearListInterface<E> {
    public boolean isEmpty();
    public int size();
    public E get(int index);
    public void remove(int index);
    public void add(int index, E theElement);
    public void add(E element);
    public void printList();
}
package list;
public class LList<E> implements LinearListInterface<E> {
    private Node<E> head, curr, tail;
    private int size;
    LList() {
        this.head = null;
        this.curr = null;
        this.tail = null;
        this.size = 0;
    }
    public boolean isEmpty() {
        return head == null;
    }
    public int size() {
        return this.size;
    }
    public E get(int index) {
        curr = head;
        boolean flag = false;
        for (int i = 1; i <= size; i++) {
            if (i == index) {
                flag = true;
                break;
            }
            curr = curr.getNext();
        }
        if (flag) {
            return curr.getData();
        } else {
            return null;
        }
    }
    public void remove(int index) {
        if (index > this.size) {
            System.out.println("δ????????");
        } else {
            this.curr = this.head;
            boolean flag = false;
            Node<E> it = this.head.getNext();
            if (index == 1) {
                this.head = this.head.getNext();
                this.size--;
            } else {
                for (int i = 2; i <= size; i++) {
                    if (i == index) {
                        curr.setNext(it.getNext());
                        flag = true;
                        this.size--;
                    } else {
                        curr = curr.getNext();
                        it = it.getNext();
                    }
                }
            }
            if (index > this.size || !flag) {
                System.out.println("δ????????");
            }
        }
    }
    public void add(int index, E theElement) {
        curr = head;
        Node<E> it = head;
        Node<E> newnode = new Node<E>();
        newnode.setData(theElement);
        if (index == 1) {
            newnode.setNext(head);
            head = newnode;
            this.size++;
        }
        for (int i = 2; i <= size; i++) {
            if (index == i) {
                curr.setNext(newnode);
                newnode.setNext(it);
                this.size++;
                break;
            } else {
                curr = curr.getNext();
                it = it.getNext();
            }
        }
        if (index == this.size + 1) {
            it.setNext(newnode);
            newnode.setNext(null);
            this.size++;
        }
    }
    public void add(E element) {
        this.size++;
        Node<E> newnode = new Node<E>();
        newnode.setData(element);
        if (head == null) {
            head = newnode;
        } else {
            tail.setNext(newnode);
        }
        tail = newnode;
        tail.setNext(null);
    }
    public void printList() {
        curr = head;
        for (int i = 0; i < size; i++) {
            System.out.print(curr.getData() + " ");
            curr = curr.getNext();
        }
        System.out.println();
    }
}
package list;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        LList<Integer> list = new LList<Integer>();
        while (true) {
            System.out.println("1.?????? 2.???λ???????? 3.?ж????????? 4.???????????");
            System.out.println("5.???????? 6.???????λ?????? 7.??????λ?????? 0.???????");
            int choice = -1;
            Scanner input = new Scanner(System.in);
            choice = input.nextInt();
            if (choice == 0) {
                break;
            }
            switch (choice) {
                case 1 -> list.add(input.nextInt());
                case 2 -> list.add(input.nextInt(), input.nextInt());
                case 3 -> System.out.println(!list.isEmpty());
                case 4 -> System.out.println(list.size());
                case 5 -> list.printList();
                case 6 -> System.out.println(list.get(input.nextInt()));
                case 7 -> list.remove(input.nextInt());
                default -> System.out.println("??????????????");
            }
        }
    }
}
package list;
public class Node<E> {
    private E data;
    private Node<E> next;
    public E getData() {
        return data;
    }
    public void setData(E data) {
        this.data = data;
    }
    public Node<E> getNext() {
        return next;
    }
    public void setNext(Node<E> next) {
        this.next = next;
    }
}

类图:

image

利用了C语言的知识,实现起来较为容易,只需要搞懂每个数据域的作用,以及

  • 双向链表

源码:

package list;
public interface DoubleLinkedListImpl<E> {
	public boolean isEmpty();
	public int getSize();
	public E getDate(int index);
	public void remove();// ɾ������һ���ڵ�
	public void remove(int index);
	public void add(int index, E theElement);
	public void add(E element);
	public void printList();
	public E getFirst();
	public E getLast();
}
package list;
public class LList<E> implements DoubleLinkedListImpl<E> {
	private Node<E> head;// ͷ���㣨�ǵ�һ���ڵ㣩����ǰ�ڵ㣬β�ڵ�
	private Node<E> curr;
	private Node<E> tail;
	private int size = 0;
	public LList() {
		super();
		// TODO Auto-generated constructor stub
		head = new Node<E>();
		// tail = new Node<E>();
		head.setPrevious(null);
		head.setNext(null);
		// tail.setPrevious(head);
		// tail.setNext(null);
		curr = tail = null;
		this.size = 0;
	}
	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return this.size == 0;
	}
	@Override
	public int getSize() {
		// TODO Auto-generated method stub
		return this.size;
	}
	@Override
	public void remove() {
		if (this.size == 0) {
			return;
		} else {
			tail = tail.getPrevious();
			tail.setNext(null);
			this.size--;
		}
	}
	@Override
	public E getFirst() {
		if (this.size == 0) {
			return null;
		} else {
			return head.getNext().getData();
		}
	}
	@Override
	public E getDate(int index) {
		// TODO Auto-generated method stub
		if (index < 1 || index > this.size) {
			return null;
		}
		curr = head;
		for (int i = 0; i < index; i++) {
			curr = curr.getNext();
		}
		return curr.getData();
	}
	@Override
	public void remove(int index) {
		// TODO Auto-generated method stub
		if (index < 1 || index > this.size) {
			return;
		}
		curr = head;
		if (index == this.size) {// ����ɾ����������һ���ڵ�
			tail = tail.getPrevious();
			tail.setNext(null);
		}
		else if (index == 1) {
			curr = head.getNext();
			head.setNext(curr.getNext());
			head.setPrevious(null);
		} else {// �ҵ���index - 1���ڵ㸳��curr
			for (int i = 1; i <= index; i++) {
				curr = curr.getNext();
			}
			curr.getPrevious().setNext(curr.getNext());
			curr.getNext().setPrevious(curr.getPrevious());
		}
		this.size--;// ���������Ľڵ�����-1
	}
	@Override
	public void add(int index, E theElement) {
		// TODO Auto-generated method stub
		if (index < 1 || index > this.size + 1) {
			return;
		}
		Node<E> curr = new Node<>();
		curr.setData(theElement);
		curr.setNext(null);
		if (this.size == 0) {
			head.setNext(curr);
			tail = curr;
			tail.setPrevious(head);
		} else if (index == this.size + 1) {
			this.tail.setNext(curr);
			curr.setPrevious(tail);
			tail = curr;
		} else {
			Node<E> tempNode = head;
			for (int i = 1; i <= index; i++) {
				tempNode = tempNode.getNext();
			}
			curr.setNext(tempNode);
			curr.setPrevious(tempNode.getPrevious());
			tempNode.getPrevious().setNext(curr);
			tempNode.setPrevious(curr);
		}
		this.size++;
	}
	@Override
	public void add(E element) {
		// TODO Auto-generated method stub
		this.add(this.size + 1, element);
	}
	@Override
	public void printList() {
		// TODO Auto-generated method stub
		curr = head.getNext();
		for (int i = 1; i <= this.size; i++) {
			System.out.print(curr.getData() + " ");
			curr = curr.getNext();
		}
		System.out.println("");
	}
	public E getLast() {
		if (this.size != 0) {
			return tail.getData();
		}
		return null;
	}
}
package list;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LList<Integer> list = new LList<Integer>();
		while (true) {
			System.out.println("1.����Ԫ�� 2.ָ��λ������Ԫ�� 3.�ж��Ƿ�����Ԫ�� 4.����Ԫ�صĸ��� 5.�������� ");
			System.out.println("6.����ָ��λ�õ�Ԫ�� 7.ɾ��ָ��λ�õ�Ԫ�� 8.ɾ��ĩβԪ�� 9.������Ԫ�� 10.����ĩβԪ�� 0.�˳�����");
			int choice = -1;
			Scanner input = new Scanner(System.in);
			choice = input.nextInt();
			if (choice == 0) {
				break;
			}
			switch (choice) {
				case 1 -> list.add(input.nextInt());
				case 2 -> list.add(input.nextInt(), input.nextInt());
				case 3 -> System.out.println(!list.isEmpty());
				case 4 -> System.out.println(list.getSize());
				case 5 -> list.printList();
				case 6 -> System.out.println(list.getDate(input.nextInt()));
				case 7 -> list.remove(input.nextInt());
				case 8 -> list.remove();
				case 9 -> System.out.println(list.getFirst());
				case 10 -> System.out.println(list.getLast());
				default -> System.out.println("��������ȷ��ѡ��");
			}
		}
	}
}
package list;
public class Node<E> {
	private E data;
	private Node<E> next;
	private Node<E> previous;
	public Node() {
	}
	
	public Node<E> getPrevious() {
		return previous;
	}
	public void setPrevious(Node<E> previous) {
		this.previous = previous;
	}
	public Node(E data, Node<E> next) {
		super();
		this.data = data;
		this.next = next;
	}
	public E getData() {
		return data;
	}
	public void setData(E data) {
		this.data = data;
	}
	public Node<E> getNext() {
		return next;
	}
	public void setNext(Node<E> next) {
		this.next = next;
	}
	
}

类图:

image

双向链表与单向链表的区别就在于双向链表多了一个指针域指向之前的节点,让其可以双向遍历,可以进行双向查找和链表的反转,难度简单,注意边界问题即可

期中考试

  • 1.点与线(类设计)

源码:


import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double num[] = new double[4];
        String color = "";
        int cnt = 0;
        String arr;
        while (true) {
            if (cnt == 5) {
                break;
            }
            arr = input.next();
            if (!arr.equals(" ")) {
                if (cnt < 4) {
                    num[cnt] = Double.parseDouble(arr);
                    cnt++;
                } else {
                    color = arr;
                    cnt++;
                }
            }
        }
        Points a1 = new Points(num[0], num[1]);
        Points a2 = new Points(num[2], num[3]);
        Line line = new Line(a1, a2, color);
        line.display();
    }
}
class Points {
    private double x;// 0<x,y<=200
    private double y;// Wrong Format
    public Points() {
    }
    public Points(double x, double y) {
        this.x = x;
        this.y = y;
        if (this.x < 0 || this.x > 200) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        if (this.y < 0 || this.y > 200) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
    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", this.x) + "," + String.format("%.2f", this.y) + ")");
    }
}
class Line {
    private Points point1;
    private Points point2;
    private String color;
    public Line() {
    }
    public Line(Points point1, Points point2, String color) {
        this.point1 = point1;
        this.point2 = point2;
        this.color = color;
    }
    public Points getPoint1() {
        return point1;
    }
    public void setPoint1(Points point1) {
        this.point1 = point1;
    }
    public Points getPoint2() {
        return point2;
    }
    public void setPoint2(Points point2) {
        this.point2 = point2;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public double getDistance() {
        double ans = Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));
        return ans;
    }
    public void display() {
        System.out.println("The line's color is:" + this.color);
        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()));
    }
}

类图:
image

  • 主要难度在于类的设计,以及输入时的判断,但是利用我的读取方式让其可以正常读取,并且让其读取没有问题,只需要将其使用next进行读取并且利用一个数进行计数即可
  • 2.点线面问题重构(继承与多态)

源码:


import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double num[] = new double[4];
        String color = "";
        int cnt = 0;
        String arr;
        while (true) {
            if (cnt == 5) {
                break;
            }
            arr = input.next();
            if (!arr.equals(" ")) {
                if (cnt < 4) {
                    num[cnt] = Double.parseDouble(arr);
                    cnt++;
                } else {
                    color = arr;
                    cnt++;
                }
            }
        }
        Element a1 = new Point(num[0], num[1]);
        Element a2 = new Point(num[2], num[3]);
        Element line = new Line((Point) a1, (Point) a2, color);
        Element plane = new Plane(color);
        a1.display();
        a2.display();
        line.display();
        plane.display();
    }
}
class Point extends Element {
    private double x;// 0<x,y<=200
    private double y;// Wrong Format
    public Point() {
    }
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
        if (this.x < 0 || this.x > 200) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        if (this.y < 0 || this.y > 200) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
    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;
    }
    @Override
    public void display() {
        System.out.println("(" + String.format("%.2f", this.x) + "," + String.format("%.2f", this.y) + ")");
    }
}
class Line extends Element {
    private Point point1;
    private Point point2;
    private String color;
    public Line() {
    }
    public Line(Point point1, Point point2, String color) {
        super();
        this.point1 = point1;
        this.point2 = point2;
        this.color = 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() {
        double ans = Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));
        return ans;
    }
    @Override
    public void display() {
        System.out.println("The line's color is:" + this.color);
        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()));
    }
    // The line's color is:颜色值
    // The line's begin point's Coordinate is:
    // (x1,y1)
    // The line's end point's Coordinate is:
    // (x2,y2)
    // The line's length is:长度值
}
class Plane extends Element {
    private String color;
    public Plane() {
    }
    public Plane(String color) {
        this.color = color;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    @Override
    public void display() {
        System.out.println("The Plane's color is:" + this.color);
    }
}
abstract class Element {
    public void display() {
        System.out.println("output their status");
    }
}

类图:

image

在第一题的基础上利用继承和多态让代码有更强的复用性和可读性,所以难度很小

  • 3.点线面问题再重构(容器类)

源码:


import java.util.ArrayList;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        GeometryObject list = new GeometryObject();
        Scanner input = new Scanner(System.in);
        while (true) {
            switch (input.nextInt()) {
                case 1:// insert Point object into list
                       // 输入“点”对象的x,y值
                    double num1[] = new double[2];
                    String arr1;
                    int cnt1 = 0;
                    while (true) {
                        if (cnt1 == 2) {
                            break;
                        }
                        arr1 = input.next();
                        if (!arr1.equals(" ")) {
                            num1[cnt1] = Double.parseDouble(arr1);
                            cnt1++;
                        }
                    }
                    Points a = new Points(num1[0], num1[1]);
                    list.add(a);
                    break;
                case 2:// insert Line object into list
                       // 输入“线”对象两个端点的x,y值
                    double num[] = new double[4];
                    String color = "";
                    int cnt = 0;
                    String arr;
                    while (true) {
                        if (cnt == 5) {
                            break;
                        }
                        arr = input.next();
                        if (!arr.equals(" ")) {
                            if (cnt < 4) {
                                num[cnt] = Double.parseDouble(arr);
                                cnt++;
                            } else {
                                color = arr;
                                cnt++;
                            }
                        }
                    }
                    Points a1 = new Points(num[0], num[1]);
                    Points a2 = new Points(num[2], num[3]);
                    Line l = new Line((Points) a1, (Points) a2, color);
                    list.add(l);
                    break;
                case 3:// insert Plane object into list
                       // 输入“面”对象的颜色值
                    String arr2 = new String();
                    while (true) {
                        arr2 = input.next();
                        if (!arr2.equals(" ")) {
                            break;
                        }
                    }
                    Element plane = new Plane(arr2);
                    list.add(plane);
                    break;
                case 4:// delete index - 1 object from list
                       // 输入要删除的对象位置(从1开始)
                    int index;
                    while (true) {
                        String arrr = input.next();
                        if (!arrr.equals(" ")) {
                            index = Integer.parseInt(arrr);
                            break;
                        }
                    }
                    list.remove(index - 1);
                    break;
                case 0:
                    ArrayList<Element> arrayList = list.getList();
                    for (Element aa : arrayList) {
                        aa.display();
                    }
                    System.exit(0);
                default:
                    System.out.println("Wrong Format");
            }
        }
    }
}
class Points extends Element {
    private double x;// 0<x,y<=200
    private double y;// Wrong Format
    public Points() {
    }
    public Points(double x, double y) {
        this.x = x;
        this.y = y;
        if (this.x < 0 || this.x > 200) {
            System.out.println("Wrong Format");
            // System.exit(0);
        }
        if (this.y < 0 || this.y > 200) {
            System.out.println("Wrong Format");
            // System.exit(0);
        }
    }
    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", this.x) + "," + String.format("%.2f", this.y) + ")");
    }
}
class Line extends Element {
    private Points point1;
    private Points point2;
    private String color;
    public Line() {
    }
    public Line(Points point1, Points point2, String color) {
        super();
        this.point1 = point1;
        this.point2 = point2;
        this.color = color;
    }
    public Points getPoint1() {
        return point1;
    }
    public void setPoint1(Points point1) {
        this.point1 = point1;
    }
    public Points getPoint2() {
        return point2;
    }
    public void setPoint2(Points point2) {
        this.point2 = point2;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public double getDistance() {
        double ans = Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));
        return ans;
    }
    public void display() {
        System.out.println("The line's color is:" + this.color);
        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()));
    }
    // The line's color is:颜色值
    // The line's begin point's Coordinate is:
    // (x1,y1)
    // The line's end point's Coordinate is:
    // (x2,y2)
    // The line's length is:长度值
}
class Plane extends Element {
    private String color;
    public Plane() {
    }
    public Plane(String color) {
        this.color = 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:" + this.color);
    }
}
abstract class Element {
    public void display() {
        System.out.println("output their status");
    }
}
class GeometryObject {
    ArrayList<Element> list = new ArrayList<>();
    public GeometryObject() {
    }
    public void add(Element a) {
        list.add(a);
    }
    public void remove(int index) {
        if (index >= 0 && index <= list.size() - 1) {
            list.remove(index);
        } else {
            return;
        }
    }
    public ArrayList<Element> getList() {
        return list;
    }
}

类图:

image

增加容器类,利用泛型可以同时存储更多的对象,难度不大,只需要在之前的两题上进行更改即可

采坑心得

  • 在期中考试的第三题中,在容器的删除元素时,没有理解清楚容器的size是什么意思,从而导致在删除出现问题

    public void remove(int index) {
        if (index >= 0 && index <= list.size() ) {
            list.remove(index);
        } else {
            return;
        }
    }

在该代码中,index <= list.size(),list.size()表示的是中存储元素的个数,而不是其最后一个元素的,从而导致在删除元素时出错,从而出现越界问题,代码应该修为:


    public void remove(int index) {
        if (index >= 0 && index <= list.size() - 1) {
            list.remove(index);
        } else {
            return;
        }
    }

改进建议

  • 在期中考试第三题中,输入的过程不需要通过字符串转换为数字类型,只需要直接读取即可

修改前源码:

public class Main {
    public static void main(String[] args) {
        GeometryObject list = new GeometryObject();
        Scanner input = new Scanner(System.in);
        while (true) {
            switch (input.nextInt()) {
                case 1:// insert Point object into list
                       // 输入“点”对象的x,y值
                    double num1[] = new double[2];
                    String arr1;
                    int cnt1 = 0;
                    while (true) {
                        if (cnt1 == 2) {
                            break;
                        }
                        arr1 = input.next();
                        if (!arr1.equals(" ")) {
                            num1[cnt1] = Double.parseDouble(arr1);
                            cnt1++;
                        }
                    }
                    Points a = new Points(num1[0], num1[1]);
                    list.add(a);
                    break;
                case 2:// insert Line object into list
                       // 输入“线”对象两个端点的x,y值
                    double num[] = new double[4];
                    String color = "";
                    int cnt = 0;
                    String arr;
                    while (true) {
                        if (cnt == 5) {
                            break;
                        }
                        arr = input.next();
                        if (!arr.equals(" ")) {
                            if (cnt < 4) {
                                num[cnt] = Double.parseDouble(arr);
                                cnt++;
                            } else {
                                color = arr;
                                cnt++;
                            }
                        }
                    }
                    Points a1 = new Points(num[0], num[1]);
                    Points a2 = new Points(num[2], num[3]);
                    Line l = new Line((Points) a1, (Points) a2, color);
                    list.add(l);
                    break;
                case 3:// insert Plane object into list
                       // 输入“面”对象的颜色值
                    String arr2 = new String();
                    while (true) {
                        arr2 = input.next();
                        if (!arr2.equals(" ")) {
                            break;
                        }
                    }
                    Element plane = new Plane(arr2);
                    list.add(plane);
                    break;
                case 4:// delete index - 1 object from list
                       // 输入要删除的对象位置(从1开始)
                    int index;
                    while (true) {
                        String arrr = input.next();
                        if (!arrr.equals(" ")) {
                            index = Integer.parseInt(arrr);
                            break;
                        }
                    }
                    list.remove(index - 1);
                    break;
                case 0:
                    ArrayList<Element> arrayList = list.getList();
                    for (Element aa : arrayList) {
                        aa.display();
                    }
                    System.exit(0);
                default:
                    System.out.println("Wrong Format");
            }
        }
    }
}

修改后源码:

public class Main {
    public static void main(String[] args) {
        GeometryObject list = new GeometryObject();
        Scanner input = new Scanner(System.in);
        while (true) {
            switch (input.nextInt()) {
                case 1:// insert Point object into list
                       // 输入“点”对象的x,y值
                    double num1[] = new double[2];
                    num1[0] = input.nextDouble();
                    num1[1] = input.nextDouble();
                    Points a = new Points(num1[0], num1[1]);
                    list.add(a);
                    break;
                case 2:// insert Line object into list
                       // 输入“线”对象两个端点的x,y值
                    double num[] = new double[4];
                    String color = "";
                    num[0] = input.nextDouble();
                    num[1] = input.nextDouble();
                    num[2] = input.nextDouble();
                    num[3] = input.nextDouble();
                    color = input.next();
                    Points a1 = new Points(num[0], num[1]);
                    Points a2 = new Points(num[2], num[3]);
                    Line l = new Line((Points) a1, (Points) a2, color);
                    list.add(l);
                    break;
                case 3:// insert Plane object into list
                       // 输入“面”对象的颜色值
                    String arr2 = new String();
                    arr2 = input.next();
                    Element plane = new Plane(arr2);
                    list.add(plane);
                    break;
                case 4:// delete index - 1 object from list
                       // 输入要删除的对象位置(从1开始)
                    int index = input.nextInt();
                    list.remove(index - 1);
                    break;
                case 0:
                    ArrayList<Element> arrayList = list.getList();
                    for (Element aa : arrayList) {
                        aa.display();
                    }
                    System.exit(0);
                default:
                    System.out.println("Wrong Format");
            }
        }
    }
}

总结

学习到的内容

继承

  • 子类继承父类

多态

  • 父类变量调用子类方法

抽象类与接口

抽象类
  • 1.在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。
2.由于抽象类不能实例化对象,所以抽象类必须被继承,才能被使用。 3.父类包含了子类集合的常见的方法,但是由于父类本身是抽象的,所以不能使用这些方法。 4.在 Java 中抽象类表示的是一种继承关系,一个类只能继承一个抽象类,而一个类却可以实现多个接口。

在 Java 语言中使用 abstract class 来定义抽象类

/* 文件名 : Employee.java */
public abstract class Employee
{
   private String name;
   private String address;
   private int number;
   public Employee(String name, String address, int number)
   {
      System.out.println("Constructing an Employee");
      this.name = name;
      this.address = address;
      this.number = number;
   }
   public double computePay()
   {
     System.out.println("Inside Employee computePay");
     return 0.0;
   }
   public void mailCheck()
   {
      System.out.println("Mailing a check to " + this.name
       + " " + this.address);
   }
   public String toString()
   {
      return name + " " + address + " " + number;
   }
   public String getName()
   {
      return name;
   }
   public String getAddress()
   {
      return address;
   }
   public void setAddress(String newAddress)
   {
      address = newAddress;
   }
   public int getNumber()
   {
     return number;
   }
}
通过以下代码来使用
```java
/* 文件名 : Salary.java */
public class Salary extends Employee
{
   private double salary; //Annual salary
   public Salary(String name, String address, int number, double
      salary)
   {
       super(name, address, number);
       setSalary(salary);
   }
   public void mailCheck()
   {
       System.out.println("Within mailCheck of Salary class ");
       System.out.println("Mailing check to " + getName()
       + " with salary " + salary);
   }
   public double getSalary()
   {
       return salary;
   }
   public void setSalary(double newSalary)
   {
       if(newSalary >= 0.0)
       {
          salary = newSalary;
       }
   }
   public double computePay()
   {
      System.out.println("Computing salary pay for " + getName());
      return salary/52;
   }
}
接口 Interface
  • 1.在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明。一个类通过继承接口的方式,从而来继承接口的抽象方法。
2.接口并不是类,编写接口的方式和类很相似,但是它们属于不同的概念。类描述对象的属性和方法。接口则包含类要实现的方法。 3.除非实现接口的类是抽象类,否则该类要定义接口中的所有方法。 4.接口无法被实例化,但是可以被实现。一个实现接口的类,必须实现接口内所描述的所有方法,否则就必须声明为抽象类。另外,在 Java 中,接口类型可用来声明一个变量,他们可以成为一个空指针,或是被绑定在一个以此接口实现的对象。 5.一个接口可以有多个方法。 6.接口文件保存在 .java 结尾的文件中,文件名使用接口名。 7.接口的字节码文件保存在 .class 结尾的文件中。 8.接口相应的字节码文件必须在与包名称相匹配的目录结构中

Java泛型

1.java 中泛型标记符
  • E - Element (在集合中使用,因为集合中存放的是元素)
T - Type(Java 类) K - Key(键) V - Value(值) N - Number(数值类型) ? - 表示不确定的 java 类型

课程改进建议及意见

  • 1.我觉得上课的时候可以晚点交课堂作业,一个小时的时间对于知识的学习还是较为仓促的。
2.希望老师能够在每次实验的重难点部分作一定的讲解说明
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now