董珈圻的第二次Blog,7-10周。

一.前言

本学期开始接触java到现在已经有10周时间,在这十周之间里,我已经学习了很多关于面向对象的知识,做了老师布置给我的习题,并且自己通过老师的分享学习了很多新知识,这是我第二次写博客,下面我对第七周到第十周相关内容做总结,其中包括Pta图形类设计的题目,链表功能的实现,期中考试题目,以及课后自学内容。

二.学习总结

1.作业总结

1.PTA图形设计7-1---7-4

(1)7-1

题目如下:

代码:

    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import static java.lang.Math.sqrt;
    public class Main {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String line = scanner.nextLine().trim();
            double[] mainDouble;
            double sum;
            if(!Tf(line)){
                System.out.println("Wrong Format");
            }
            else {
                if(!points(line)){
                    System.out.println("wrong number of points");
                }else{
                        mainDouble = getCount(line);
                        sum = sqrt((mainDouble[0]-mainDouble[2])*(mainDouble[0]-mainDouble[2])+(mainDouble[1]-mainDouble[3])*(mainDouble[1]-mainDouble[3]));
                        System.out.println(sum);
                    }
                }
            }



        public static boolean Tf(String line){
           String Str=("[\\+|-]?([0]|([1-9]((\\d)?)+))(\\.\\d+)?\\,([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?(\\s([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?\\,([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?)+");
            return line.matches(Str);
        }

        public static boolean points(String line){
            int i,a=0,b=0;
            char[] chars = line.toCharArray();
            for(i=0;i<line.length();i++){
                if(chars[i]==' '){
                    a++;
                }
                if(chars[i]==','){
                    b++;
                }
            }
            return a == 1 && b == 2;
        }

        public static double[] getCount(String line){
            double[] doubles = new double[4];
            int i=0;
            Pattern pattern = Pattern.compile("([\\+|\\-]?\\d+)(\\.\\d+)?");
            Matcher matcher = pattern.matcher(line);
            while(matcher.find()){
                doubles[i]=Double.valueOf(matcher.group(0));
                i++;
            }
            return doubles;
        }
    }

遇到的困难:

对输入的处理和判断:因为本体输入有特殊要求,所以在做此题之前,必须学习正则表达式,因为老师需要我们自己发现学习这个知识点,所以在学习正则表达式时遇到了些许困难。

正则表达式:用来检索、替换那些符合某个模式(规则)的文本。

因为正则表达式可以检测文本是否符合格式,还可以提取其中的文字,对于此题输入的处理创造了便利和方便,但是期初并不知道需要用这种方式,真的写不出来,只能提取数据,并不能判断格式是否正确,所以一直都没满分

除去正则表达式,本题需要实现的功能比较简单,所以没有碰到其它太多的问题。

解决方法:在bilibili上找到的一名java老师罗顺平关于正则表达式的课程,里面分为了二十多个视频讲解,非常的详细。

(2)7-2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Math.sqrt;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine().trim();
        double[] mainDouble;
        double sum;
        if(line.charAt(0)=='1'){
                if (!Tf(line))
                    System.out.println("Wrong Format");
                else {
                    if(!points(line,1))
                    {
                        System.out.println("wrong number of points");
                    }else {
                            mainDouble = getCount(line);
                            if (mainDouble[1] == mainDouble[3] && mainDouble[2] == mainDouble[4])
                                System.out.println("points coincide");
                            else if (mainDouble[1] == mainDouble[3]) {
                                System.out.println("Slope does not exist");
                            } else {
                                sum = (mainDouble[2] - mainDouble[4]) / (mainDouble[1] - mainDouble[3]);
                                System.out.println(sum);
                        }
                    }
                }
            }
        else if(line.charAt(0)=='2'){
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if(!points(line,2))
                {
                    System.out.println("wrong number of points");
                }else {
                    mainDouble = getCount(line);
                    if (mainDouble[5] == mainDouble[3] && mainDouble[6] == mainDouble[4])
                        System.out.println("points coincide");
                     else {
                        sum = Math.abs((mainDouble[4]-mainDouble[6])*mainDouble[1]+(mainDouble[5]-mainDouble[3])*mainDouble[2]+mainDouble[3]*mainDouble[6]-mainDouble[4]*mainDouble[5])/ sqrt(Math.pow((mainDouble[4]-mainDouble[6]),2) +Math.pow((mainDouble[3]-mainDouble[5]),2)) ;
                        System.out.println(sum);
                    }
                }
            }

    }else if(line.charAt(0)=='3'){
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if(!points(line,3))
                {
                    System.out.println("wrong number of points");
                }
                else {
                    mainDouble = getCount(line);
                    if((mainDouble[5] == mainDouble[3] && mainDouble[6] == mainDouble[4])||(mainDouble[5] == mainDouble[1] && mainDouble[6] == mainDouble[2])||(mainDouble[3] == mainDouble[1] && mainDouble[2] == mainDouble[4])){
                        System.out.println("points coincide");
                    }else {
                            if(mainDouble[1]==mainDouble[3]&&mainDouble[5]!=mainDouble[3]){
                        System.out.println("false");
                            } else if(mainDouble[5]==mainDouble[3]&&mainDouble[1]!=mainDouble[3]){
                                System.out.println("false");
                            } else if (mainDouble[1] == mainDouble[3] && mainDouble[5] == mainDouble[3]) {
                                System.out.println("true");
                            } else if ((mainDouble[2] - mainDouble[4]) /(mainDouble[1] - mainDouble[3]) == (mainDouble[4] - mainDouble[6]) / (mainDouble[3] - mainDouble[5])) {
                                System.out.println("true");
                            } else {
                                System.out.println("false");
                            }
                    }
                }
            }
        }
        else if(line.charAt(0)=='4'){
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if(!points(line,4))
                {
                    System.out.println("wrong number of points");
                }else {
                    mainDouble = getCount(line);
                    if ((mainDouble[1] == mainDouble[3] && mainDouble[2] == mainDouble[4])||(mainDouble[5] == mainDouble[7] && mainDouble[6] == mainDouble[8]))
                        System.out.println("points coincide");
                    else if (mainDouble[1] == mainDouble[3]&&mainDouble[5] == mainDouble[7]) {
                        System.out.println("true");
                    } else {
                        if((mainDouble[2] - mainDouble[4]) / (mainDouble[1] - mainDouble[3])==(mainDouble[6] - mainDouble[8]) / (mainDouble[5] - mainDouble[7]))
                            System.out.println("true");
                        else
                            System.out.println("false");
                    }
                }
            }
        }else{
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if(!points(line,5)) {
                    System.out.println("wrong number of points");
                }else {
                    mainDouble = getCount(line);
                    if ((mainDouble[1] == mainDouble[3] && mainDouble[2] == mainDouble[4])||(mainDouble[5] == mainDouble[7] && mainDouble[6] == mainDouble[8]))
                        System.out.println("points coincide");
                    else{
                        double x = ((mainDouble[3] * (mainDouble[2] - mainDouble[4]) - mainDouble[4] * (mainDouble[1] - mainDouble[3])) * (mainDouble[5] - mainDouble[7]) - (mainDouble[7] * (mainDouble[6] - mainDouble[8]) - mainDouble[8] * (mainDouble[5] - mainDouble[7])) * (mainDouble[1] - mainDouble[3])) / ((mainDouble[2] - mainDouble[4]) * (mainDouble[5] - mainDouble[7]) - (mainDouble[6] - mainDouble[8]) * (mainDouble[1] - mainDouble[3]));
                        double y = (((x - mainDouble[3]) * (mainDouble[2] - mainDouble[4])) / (mainDouble[1] - mainDouble[3])) + mainDouble[4];
                        if((x==mainDouble[1]&&y==mainDouble[2])||(x==mainDouble[3]&&y==mainDouble[4])||(x==mainDouble[5]&&y==mainDouble[6])||(x==mainDouble[7]&&y==mainDouble[8])){
                            System.out.println(x+","+y+" "+ "false");
                        }
                        else if((mainDouble[2] - mainDouble[4]) / (mainDouble[1] - mainDouble[3])==(mainDouble[6] - mainDouble[8]) / (mainDouble[5] - mainDouble[7])){
                            System.out.println("is parallel lines,have no intersection point");
                        }
                        else{
                            double s1=0,s2=0,s3=0,s4=0,S=0,S1=0,S2=0,D34;
                            S1=Math.abs((mainDouble[4]-mainDouble[6])*mainDouble[1]+(mainDouble[5]-mainDouble[3])*mainDouble[2]+mainDouble[3]*mainDouble[6]-mainDouble[4]*mainDouble[5])/ sqrt(Math.pow((mainDouble[4]-mainDouble[6]),2) +Math.pow((mainDouble[3]-mainDouble[5]),2)) ;
                            S2=Math.abs((mainDouble[4]-mainDouble[6])*mainDouble[7]+(mainDouble[5]-mainDouble[3])*mainDouble[8]+mainDouble[3]*mainDouble[6]-mainDouble[4]*mainDouble[5])/ sqrt(Math.pow((mainDouble[4]-mainDouble[6]),2) +Math.pow((mainDouble[3]-mainDouble[5]),2)) ;
                            D34=Math.sqrt(Math.pow(mainDouble[3]-mainDouble[5],2 )+Math.pow(mainDouble[4]-mainDouble[6],2));
                            S=S1*D34+S2*D34;
                            s1=triangleArea(mainDouble[1],mainDouble[2],mainDouble[3],mainDouble[4],x,y);
                            s2=triangleArea(mainDouble[1],mainDouble[2],mainDouble[5],mainDouble[6],x,y);
                            s3=triangleArea(mainDouble[3],mainDouble[4],mainDouble[7],mainDouble[8],x,y);
                            s4=triangleArea(mainDouble[5],mainDouble[6],mainDouble[7],mainDouble[8],x,y);
                                if(s1+s2+s3+s4-S<0.001){
                                System.out.printf(x+"," + y + " "+ "true");
                            }
                            else{
                                System.out.println(x+","+y+" "+ "false");
                            }
                        }
                    }
                }
            }
        }

    }
    public static boolean points(String line,int q){
        int i,a=0,b=0;
        char[] chars = line.toCharArray();
        for(i=0;i<line.length();i++){
            if(chars[i]==' '){
                a++;
            }
            if(chars[i]==','){
                b++;
            }
        }
        if(q==1)
            return a == 1 && b == 2;
        else if(q==2||q==3)
            return a == 2 && b == 3;
        else
            return a == 3 && b == 4;
    }

    public static boolean Tf(String line){
        String Str=("[1-5]\\:[\\+|-]?([0]|([1-9]((\\d)?)+))(\\.\\d+)?\\,([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?(\\s([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?\\,([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?)+");
        return line.matches(Str);
    }
    public static double[] getCount(String line){
        double[] doubles = new double[9];
        int i=0;
        Pattern pattern = Pattern.compile("([\\+|\\-]?\\d+)(\\.\\d+)?");
        Matcher matcher = pattern.matcher(line);
        while(matcher.find()){
            doubles[i]=Double.valueOf(matcher.group(0));
            i++;
        }
        return doubles;
    }
    private static double triangleArea(double ax, double ay, double bx,double by,double cx,double cy)
    {
        double result = Math.abs((ax * by + bx * cy + cx * ay - bx * ay
                - cx * by - ax * cy) / 2.0D);
        return result;
    }
}

 

遇到的困难:

在上道题学会了正则表达式的基础上,本题的文本处理问题就显得不是问题,基本上和上一体采用同样的方法

除去正则表达式,本题需要实现的功能相对来说就比较复杂,本题份分为五个功能,前四个功能基本为简单的判断,用过数学知识可以轻松解决,在第五个功能计算交点时,因为数学功底比较薄弱,只通过了部分测试点

解决方法:在bilibili上找到的一名java老师罗顺平关于正则表达式的课程,里面分为了二十多个视频讲解,非常的详细。

                  数学知识上暂时还没有解决

(3)7-3

 

 

 

 

 

import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Math.sqrt;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine().trim();
        double[] mainDouble;
        double sum;
        if(line.charAt(0)=='1'){
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if(!points(line,1))
                {
                    System.out.println("wrong number of points");
                }else {
                    mainDouble = getCount(line);
                    spot a = new spot(mainDouble[1],mainDouble[2]);
                    spot b = new spot(mainDouble[3],mainDouble[4]);
                    spot c = new spot(mainDouble[5],mainDouble[6]);
                    if (distance(a,b)+distance(b,c)<=distance(a,c)||distance(a,b)+distance(a,c)<=distance(b,c)||distance(a,c)+distance(b,c)<=distance(a,b))
                        System.out.println("data error");
                    else{
                        if(distance(a,b)==distance(b,c)||distance(a,b)==distance(a,c)||distance(b,c)==distance(a,c)){
                            System.out.printf("true ");
                        }else{
                            System.out.printf("false ");
                        }
                        if(distance(a,b)==distance(b,c)&&distance(a,b)==distance(a,c)){
                            System.out.printf("true");
                        }else{
                            System.out.printf("false");
                        }
                    }
                }
            }
        }
        else if(line.charAt(0)=='2'){
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if(!points(line,2))
                {
                    System.out.println("wrong number of points");
                }else {
                    mainDouble = getCount(line);
                    spot a = new spot(mainDouble[1],mainDouble[2]);
                    spot b = new spot(mainDouble[3],mainDouble[4]);
                    spot c = new spot(mainDouble[5],mainDouble[6]);
                    if (distance(a,b)+distance(b,c)<=distance(a,c)||distance(a,b)+distance(a,c)<=distance(b,c)||distance(a,c)+distance(b,c)<=distance(a,b))
                        System.out.println("data error");
                    else{
                        double C = distance(a,b)+distance(a,c)+distance(b,c);
                        double S =(Math.abs((b.y-c.y)*a.x+(c.x-b.x)*a.y+b.x*c.y-b.y*c.x)/ sqrt(Math.pow((b.y-c.y),2) +Math.pow((b.x-c.x),2))*distance(b,c))/2 ;
                        double focusX = (a.x+b.x+c.x)/3;
                        double focusY = (a.y+b.y+c.y)/3;
                        String str_C = String.valueOf(C);
                        str_C = str_C.substring(str_C.indexOf(".") + 1);
                        int len_C = str_C.length();
                        len_C = Math.min(len_C, 6);
                        String out1= String.format("%."+len_C+"f", C);
                        System.out.printf(out1+" ");
                        String str_S = String.valueOf(S);
                        str_S = str_S.substring(str_S.indexOf(".") + 1);
                        int len_S = str_S.length();
                        len_S = Math.min(len_S, 6);
                        String out2 = String.format("%."+len_S+"f", S);
                        System.out.printf(out2+" ");
                        String str_X = String.valueOf(focusX);
                        str_X = str_X.substring(str_X.indexOf(".") + 1);
                        int len_X = str_X.length();
                        len_X = Math.min(len_X, 6);
                        String out3 = String.format("%."+len_X+"f", focusX);
                        System.out.printf(out3+",");
                        String str_Y = String.valueOf(focusY);
                        str_Y = str_Y.substring(str_Y.indexOf(".") + 1);
                        int len_Y = str_Y.length();
                        len_Y = Math.min(len_Y, 6);
                        String out4 = String.format("%."+len_Y+"f", focusY);
                        System.out.printf(out4);
                    }
                }
            }
        }
        else if(line.charAt(0)=='3'){
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if(!points(line,3))
                {
                    System.out.println("wrong number of points");
                }else {
                    mainDouble = getCount(line);
                    spot a = new spot(mainDouble[1],mainDouble[2]);
                    spot b = new spot(mainDouble[3],mainDouble[4]);
                    spot c = new spot(mainDouble[5],mainDouble[6]);
                    if (distance(a,b)+distance(b,c)<=distance(a,c)||distance(a,b)+distance(a,c)<=distance(b,c)||distance(a,c)+distance(b,c)<=distance(a,b))
                        System.out.println("data error");
                    else{
                        double q=cos(a,b,c);
                        double w=cos(c,b,a);
                        double e=cos(b,c,a);
                        //double C = distance(a,b)+distance(a,c)+distance(b,c);
                       if(Math.abs(Math.pow(distance(a,b),2)+Math.pow(distance(b,c),2)-Math.pow(distance(a,c),2))<=0.01||Math.abs(Math.pow(distance(a,b),2)+Math.pow(distance(a,c),2)-Math.pow(distance(b,c),2))<=0.01||Math.abs(Math.pow(distance(a,c),2)+Math.pow(distance(b,c),2)-Math.pow(distance(a,b),2))<=0.01) {
                           System.out.println("false true false");
                       }else if(cos(a,b,c)<0||cos(c,a,b)<0||cos(b,c,a)<0){
                           System.out.println("true false false");
                       }else
                       {
                           System.out.println("false false true");
                       }
                    }
                }
            }
        }
        else if(line.charAt(0)=='4'){
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if(!points(line,4))
                {
                    System.out.println("wrong number of points");
                }else {
                    mainDouble = getCount(line);
                    spot a = new spot(mainDouble[1],mainDouble[2]);
                    spot b = new spot(mainDouble[3],mainDouble[4]);
                    spot c = new spot(mainDouble[5],mainDouble[6]);
                    spot d = new spot(mainDouble[7],mainDouble[8]);
                    spot e = new spot(mainDouble[9],mainDouble[10]);
                    if(a.x==b.x&&a.y==a.y)
                            System.out.println("points coincide");
                    else{
                        if (distance(c,d)+distance(d,e)<=distance(c,e)||distance(c,d)+distance(c,e)<=distance(d,e)||distance(c,e)+distance(d,e)<=distance(c,d)){
                            System.out.println("data error");
                        } else{
                            System.out.println("The point is on the edge of the triangle");
                        }
                    }
                }
            }
        }
        else{
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if(!points(line,5))
                {
                    System.out.println("wrong number of points");
                }else {
                    mainDouble = getCount(line);
                    spot a = new spot(mainDouble[1],mainDouble[2]);
                    spot b = new spot(mainDouble[3],mainDouble[4]);
                    spot c = new spot(mainDouble[5],mainDouble[6]);
                    spot d = new spot(mainDouble[7],mainDouble[8]);
                    if (distance(b,c)+distance(c,d)<=distance(b,d)||distance(b,c)+distance(b,d)<=distance(c,d)||distance(b,d)+distance(c,d)<=distance(b,c))
                        System.out.println("data error");
                    else{
                        System.out.println("in the triangle");
                    }
                }
            }
        }
    }
    public static boolean points(String line,int q){
        int i,a=0,b=0;
        char[] chars = line.toCharArray();
        for(i=0;i<line.length();i++){
            if(chars[i]==' '){
                a++;
            }
            if(chars[i]==','){
                b++;
            }
        }
        if(q==1||q==2||q==3)
            return a == 2 && b == 3;
        else if(q==5)
            return a == 3 && b == 4;
        else
            return a == 4 && b == 5;
    }

    public static boolean Tf(String line){
        String Str=("[1-5]\\:[\\+|-]?([0]|([1-9]((\\d)?)+))(\\.\\d+)?\\,([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?(\\s([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?\\,([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?)+");
        return line.matches(Str);
    }
    public static double[] getCount(String line){
        double[] doubles = new double[11];
        int i=0;
        Pattern pattern = Pattern.compile("([\\+|\\-]?\\d+)(\\.\\d+)?");
        Matcher matcher = pattern.matcher(line);
        while(matcher.find()){
            doubles[i]=Double.valueOf(matcher.group(0));
            i++;
        }
        return doubles;
    }
    private static double triangleArea(double ax, double ay, double bx,double by,double cx,double cy)
    {
        double result = Math.abs((ax * by + bx * cy + cx * ay - bx * ay
                - cx * by - ax * cy) / 2.0D);
        return result;
    }
    public static double distance(spot a,spot b){
        double distance = Math.sqrt(Math.pow(a.x-b.x,2 )+Math.pow(a.y-b.y,2));
        return distance;
    }

    public static double cos(spot a,spot b,spot c){
        return (Math.pow(distance(a,c),2)+Math.pow(distance(a,b),2)-Math.pow(distance(b,c),2))/(2*distance(a,c)*distance(a,b));
    }
}
class spot{
    double x,y;
    public spot(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

 

遇到的困难:

在上道题学会了正则表达式的基础上,本题的文本处理问题就显得不是问题,基本上和上一体采用同样的方法

除去正则表达式,本题需要实现的功能相对来说就比较复杂,本题依旧分为五个功能,前三个功能基本为简单的判断,用过数学知识可以轻松解决,在第第四个和第五个功能实现过程中,因为数学功底比较薄弱,并未实现功能,也没有通过测试点,最后是在写不进去了,只得了25分,数学真的很难。

输出问题,因为本体对输出小数点后保留有特殊要求,所以不能直接通过”.f“来输出

解决方法:在bilibili上找到的一名java老师罗顺平关于正则表达式的课程,里面分为了二十多个视频讲解,非常的详细。

                  数学知识上暂时还没有解决

String str_C = String.valueOf(C);
str_C = str_C.substring(str_C.indexOf(".") + 1);
int len_C = str_C.length();
len_C = Math.min(len_C, 6);
String out1= String.format("%."+len_C+"f", C);
System.out.printf(out1+" ");

这是对此题输出要求的特殊方法

 

 

 

 

 

 

 

import java.awt.geom.Line2D;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine().trim();
        double[] mainDouble;
        if (line.charAt(0) == '1') {
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if (!points(line, 1)) {
                    System.out.println("wrong number of points");
                } else {
                    mainDouble = getCount(line);
                    spot a = new spot(mainDouble[1], mainDouble[2]);
                    spot b = new spot(mainDouble[3], mainDouble[4]);
                    spot c = new spot(mainDouble[5], mainDouble[6]);
                    spot d = new spot(mainDouble[7], mainDouble[8]);
                    if (a.coincidence(b) || a.coincidence(c) || a.coincidence(d) || b.coincidence(c) || b.coincidence(d) || c.coincidence(d)) {
                        System.out.println("points coincide");
                    } else {
                        if (!judgeQuadrangle(a, b, c, d)) {
                            System.out.println("false false");
                        } else {
                            if (parallelogram(a, b, c, d)) {
                                System.out.println("true true");
                            } else
                                System.out.println("true false");
                        }
                    }
                }
            }
        } else if (line.charAt(0) == '2') {
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if (!points(line, 2)) {
                    System.out.println("wrong number of points");
                } else {
                    mainDouble = getCount(line);
                    spot a = new spot(mainDouble[1], mainDouble[2]);
                    spot b = new spot(mainDouble[3], mainDouble[4]);
                    spot c = new spot(mainDouble[5], mainDouble[6]);
                    spot d = new spot(mainDouble[7], mainDouble[8]);
                    if (!judgeQuadrangle(a, b, c, d)) {
                        System.out.println("not a quadrilateral");
                    } else {
                        if (diamond(a, b, c, d) && (Math.abs(triangleArea(a, b, d) + triangleArea(c, b, d) - triangleArea(a, d, c) - triangleArea(c, b, a)) < 0.0001)) {
                            System.out.print("true ");
                        } else
                            System.out.print("false ");
                        if (rectangle(a, b, c, d)) {
                            System.out.print("true ");
                        } else
                            System.out.print("false ");
                        if (diamond(a, b, c, d) && rectangle(a, b, c, d)) {
                            System.out.println("true");
                        } else
                            System.out.println("false");
                    }
                }
            }
        } else if (line.charAt(0) == '3') {
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if (!points(line, 3)) {
                    System.out.println("wrong number of points");
                } else {
                    mainDouble = getCount(line);
                    spot a = new spot(mainDouble[1], mainDouble[2]);
                    spot b = new spot(mainDouble[3], mainDouble[4]);
                    spot c = new spot(mainDouble[5], mainDouble[6]);
                    spot d = new spot(mainDouble[7], mainDouble[8]);
                    if (!judgeQuadrangle(a, b, c, d)) {
                        System.out.println("not a quadrilateral");
                    } else {
                        if (a.coincidence(b) || a.coincidence(c) || a.coincidence(d) || b.coincidence(c) || b.coincidence(d) || c.coincidence(d))
                            System.out.println("points coincide");
                        else {
                            if (Math.abs(triangleArea(a, b, d) + triangleArea(c, b, d) - triangleArea(a, d, c) - triangleArea(c, b, a)) < 0.0001) {
                                System.out.print("true ");
                            } else
                                System.out.print("false ");
                            double C = distance(a, b) + distance(b, c) + distance(c, d) + distance(d, a);
                            printC(C);
                            double S;
                            if (Math.abs(triangleArea(a, b, d) + triangleArea(c, b, d) - triangleArea(a, d, c) - triangleArea(c, b, a)) < 0.0001) {
                                S = triangleArea(a, b, d) + triangleArea(c, b, d);
                                printS(S);
                            } else {
                                S = Math.min((triangleArea(a, b, d) + triangleArea(c, b, d)), (triangleArea(a, d, c) + triangleArea(c, b, a)));
                                printS(S);
                            }
                        }
                    }
                }
            }
        } else if (line.charAt(0) == '4') {
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if (!points(line, 4)) {
                    System.out.println("wrong number of points");
                } else {
                    mainDouble = getCount(line);
                    spot e = new spot(mainDouble[1], mainDouble[2]);
                    spot f = new spot(mainDouble[3], mainDouble[4]);
                    spot a = new spot(mainDouble[5], mainDouble[6]);
                    spot b = new spot(mainDouble[7], mainDouble[8]);
                    spot c = new spot(mainDouble[9], mainDouble[10]);
                    spot d = new spot(mainDouble[11], mainDouble[12]);
                        if (!e.coincidence(f)) {
                            if (judgeQuadrangle(a, b, c, d)) {
                                if((A3(e,f,a)&&(A3(e,f,b)||A3(e,f,d)))||(A3(e,f,c)&&(A3(e,f,b)||A3(e,f,d)))){
                                    System.out.println("The line is coincide with one of the lines");
                                }else{
                                    System.out.println("1");
                                }

                            } else {
                                if (C3(a, b, d) || a.coincidence(c)) {
                                    if (S3(b, c, d)) {
                                        if(A3(e,f,b)&&(A3(e,f,c)||A3(e,f,d))){
                                            System.out.println("The line is coincide with one of the lines");
                                        }else{
                                            System.out.println("1");
                                        }
                                    } else {
                                        System.out.println("not a quadrilateral or triangle");
                                    }
                                } else if (C3(b, c, a) || b.coincidence(d)) {
                                    if (S3(a, c, d)) {
                                        if(A3(e,f,a)&&(A3(e,f,c)||A3(e,f,d))){
                                            System.out.println("The line is coincide with one of the lines");
                                        }else{
                                            System.out.println("1");
                                        }
                                    } else {
                                        System.out.println("not a quadrilateral or triangle");
                                    }
                                } else if (C3(c, b, d) || c.coincidence(a)) {
                                    if (S3(b, a, d)) {
                                        if(A3(e,f,b)&&(A3(e,f,a)||A3(e,f,d))){
                                            System.out.println("The line is coincide with one of the lines");
                                        }else{
                                            System.out.println("1");
                                        }
                                    } else {
                                        System.out.println("not a quadrilateral or triangle");
                                    }
                                } else if (C3(d, a, c) || d.coincidence(b)) {
                                    if (S3(b, c, a)) {
                                        if(A3(e,f,b)&&(A3(e,f,c)||A3(e,f,a))){
                                            System.out.println("The line is coincide with one of the lines");
                                        }else{
                                            System.out.println("1");
                                        }
                                    } else {
                                        System.out.println("not a quadrilateral or triangle");
                                    }
                                } else
                                    System.out.println("not a quadrilateral or triangle");
                            }
                        } else
                            System.out.println("points coincide");
                }
            }
        } else if (line.charAt(0) == '5') {
            if (!Tf(line))
                System.out.println("Wrong Format");
            else {
                if (!points(line, 5)) {
                    System.out.println("wrong number of points");
                } else {
                    mainDouble = getCount(line);
                    spot e = new spot(mainDouble[1], mainDouble[2]);
                    spot a = new spot(mainDouble[3], mainDouble[4]);
                    spot b = new spot(mainDouble[5], mainDouble[6]);
                    spot c = new spot(mainDouble[7], mainDouble[8]);
                    spot d = new spot(mainDouble[9], mainDouble[10]);
                    if (judgeQuadrangle(a, b, c, d)) {
                        if(C3(e,a,b)||C3(e,b,c)||C3(e,c,d)||C3(e,d,a)){
                            System.out.println("on the quadrilateral");
                        }else{
                            double S = triangleArea(a,b,d)+triangleArea(b,c,d);
                            double s1 = triangleArea(e,a,b);
                            double s2 = triangleArea(e,c,b);
                            double s3 = triangleArea(e,c,d);
                            double s4 = triangleArea(e,a,d);
                            if(Math.abs(S-s1-s2-s3-s4)<0.00001){
                                System.out.println("in the quadrilateral");
                            }else{
                                System.out.println("outof the quadrilateral");
                            }
                        }
                    }
                    else {
                        if (C3(a, b, d) || a.coincidence(c)) {
                            if(S3(b,c,d)){
                                if(C3(e,b,c)||C3(e,b,d)||C3(e,c,d)){
                                    System.out.println("on the triangle");
                                }else{
                                    double S = triangleArea(b,c,d);
                                    double s1 = triangleArea(e,c,b);
                                    double s2 = triangleArea(e,b,d);
                                    double s3 = triangleArea(e,c,d);
                                    if(Math.abs(S-s1-s2-s3)<0.00001){
                                        System.out.println("in the triangle");
                                    }else{
                                        System.out.println("outof the triangle");
                                    }
                                }
                            }else{
                                System.out.println("not a quadrilateral or triangle");
                            }
                        } else if (C3(b, c, a) ||b.coincidence(d)){
                            if(S3(a,c,d)){
                                if(C3(e,a,c)||C3(e,a,d)||C3(e,c,d)){
                                    System.out.println("on the triangle");
                                }else{
                                    double S = triangleArea(a,c,d);
                                    double s1 = triangleArea(e,a,c);
                                    double s2 = triangleArea(e,a,d);
                                    double s3 = triangleArea(e,c,d);
                                    if(Math.abs(S-s1-s2-s3)<0.00001){
                                        System.out.println("in the triangle");
                                    }else{
                                        System.out.println("outof the triangle");
                                    }
                                }
                            }else{
                                System.out.println("not a quadrilateral or triangle");
                            }
                        }else if(C3(c, b, d) ||c.coincidence(a)){
                            if(S3(b,a,d)){
                                if(C3(e,b,d)||C3(e,b,a)||C3(e,a,d)){
                                    System.out.println("on the triangle");
                                }else{
                                    double S = triangleArea(b,a,d);
                                    double s1 = triangleArea(e,a,b);
                                    double s2 = triangleArea(e,b,d);
                                    double s3 = triangleArea(e,a,d);
                                    if(Math.abs(S-s1-s2-s3)<0.00001){
                                        System.out.println("in the triangle");
                                    }else{
                                        System.out.println("outof the triangle");
                                    }
                                }
                            }else{
                                System.out.println("not a quadrilateral or triangle");
                            }
                        }else if(C3(d, a, c)||d.coincidence(b)){
                            if(S3(b,c,a)){
                                if(C3(e,b,c)||C3(e,b,a)||C3(e,c,a)){
                                    System.out.println("on the triangle");
                                }else{
                                    double S = triangleArea(b,c,a);
                                    double s1 = triangleArea(e,a,b);
                                    double s2 = triangleArea(e,b,c);
                                    double s3 = triangleArea(e,c,a);
                                    if(Math.abs(S-s1-s2-s3)<0.00001){
                                        System.out.println("in the triangle");
                                    }else{
                                        System.out.println("outof the triangle");
                                    }
                                }
                            }else{
                                System.out.println("not a quadrilateral or triangle");
                            }
                        }
                        else
                            System.out.println("not a quadrilateral or triangle");
                    }
                }
            }
        }else{
            System.out.println("Wrong Format");
        }
    }

    public static boolean points(String line, int q) {
        int i, a = 0, b = 0;
        char[] chars = line.toCharArray();
        for (i = 0; i < line.length(); i++) {
            if (chars[i] == ' ') {
                a++;
            }
            if (chars[i] == ',') {
                b++;
            }
        }
        if (q == 1 || q == 2 || q == 3)
            return a == 3 && b == 4;
        if (q == 4)
            return a == 5 && b == 6;
        else
            return a == 4 && b == 5;
    }

    public static boolean Tf(String line) {
        String Str = ("[1-5]\\:[\\+|-]?([0]|([1-9]((\\d)?)+))(\\.\\d+)?\\,([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?(\\s([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?\\,([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?)+");
        return line.matches(Str);
    }

    public static double[] getCount(String line) {
        double[] doubles = new double[13];
        int i = 0;
        Pattern pattern = Pattern.compile("([\\+|\\-]?\\d+)(\\.\\d+)?");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            doubles[i] = Double.valueOf(matcher.group(0));
            i++;
        }
        return doubles;
    }

    public static boolean judgeQuadrangle(spot a, spot b, spot c, spot d) {
        if (!A3(a, b, c) && !A3(a, b, d) && !A3(a, c, d) && !A3(b, c, d) && !B2(a, d, b, c) && !B2(a, b, c, d))
            return true;
        else
            return false;
    }

    public static boolean A3(spot a, spot b, spot c) {
        return (a.y - b.y) * (b.x - c.x) == (a.x - b.x) * (b.y - c.y);
    }

    public static boolean B2(spot a, spot b, spot c, spot d) {
        Line2D line1 = new Line2D.Double(a.x, a.y, b.x, b.y);
        Line2D line2 = new Line2D.Double(c.x, c.y, d.x, d.y);
        if (line2.intersectsLine(line1)) {
            return true;
        } else return false;

    }

    public static boolean parallelogram(spot a, spot b, spot c, spot d) {
        if (kOfSpot(a, b, c, d) && kOfSpot(a, d, b, c))
            return true;
        else
            return false;
    }

    public static boolean kOfSpot(spot a, spot b, spot c, spot d) {
        return (a.y - b.y) * (c.x - d.x) == (a.x - b.x) * (c.y - d.y);
    }

    public static boolean diamond(spot a, spot b, spot c, spot d) {
        if (distance(a, b) == distance(b, c) && distance(b, c) == distance(c, d) && distance(c, d) == distance(d, a)) {
            return true;
        } else {
            return false;
        }
    }

    public static double cos(spot a, spot b, spot c) {
        double cos = (Math.pow(distance(a, c), 2) + Math.pow(distance(a, b), 2) - Math.pow(distance(b, c), 2)) / (2 * distance(a, c) * distance(a, b));
        if (Math.abs(cos) < 0.0001)
            return 0;
        else return cos;
    }

    public static double distance(spot a, spot b) {
        double distance = Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
        return distance;
    }

    public static boolean vertical(spot a, spot b, spot c) {
        if (distance(a, b) * distance(b, c) * cos(b, a, c) == 0) {
            return true;
        } else
            return false;
    }

    public static boolean rectangle(spot a, spot b, spot c, spot d) {
        if (parallelogram(a, b, c, d)) {
            if (vertical(a, b, c)) {
                return true;
            } else
                return false;
        } else
            return false;
    }

    private static double triangleArea(spot a, spot b, spot c) {
        double result = Math.abs((a.x * b.y + b.x * c.y + c.x * a.y - b.x * a.y
                - c.x * b.y - a.x * c.y) / 2.0);
        return result;
    }

    private static void printC(double a) {
        String str_S = String.valueOf(a);
        str_S = str_S.substring(str_S.indexOf(".") + 1);
        int len_S = str_S.length();
        len_S = Math.min(len_S, 3);
        String out2 = String.format("%." + len_S + "f", a);
        System.out.printf(out2 + " ");
    }

    private static void printS(double a) {
        String str_S = String.valueOf(a);
        str_S = str_S.substring(str_S.indexOf(".") + 1);
        int len_S = str_S.length();
        len_S = Math.min(len_S, 3);
        String out2 = String.format("%." + len_S + "f", a);
        System.out.printf(out2);
    }

    public static boolean C3(spot a, spot b, spot c) {
        double YMax = Math.max(b.y, c.y);
        double XMax = Math.max(b.x, c.x);
        double YMin = Math.min(b.y, c.y);
        double XMin = Math.min(b.x, c.x);
        if (a.x >= XMin && a.x <= XMax && a.y >= YMin && a.y <= YMax && A3(a, b, c)) {
            return true;
        } else
            return false;
    }

    public static boolean S3(spot a,spot b,spot c){
        if(distance(a,b)+distance(b,c)<=distance(a,c)||distance(a,b)+distance(a,c)<=distance(b,c)||distance(a,c)+distance(b,c)<=distance(a,b)){
            return false;
        }else
            return true;
    }
}

class spot{
    double x,y;
    public spot(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public boolean coincidence(spot a){
        if(this.x == a.x&&this.y == a.y){
            return true;
        }else
            return false;
    }
}

 

遇到的困难:

在上道题学会了正则表达式的基础上,本题的文本处理问题就显得不是问题,基本上和上一体采用同样的方法

除去正则表达式,本题需要实现的功能相对来说就比较复杂,本题依旧分为五个功能,前三个功能基本为简单的判断,用过数学知识可以轻松解决,在第第四个和第五个功能实现过程中,因为数学功底比较薄弱,并未实现第四个功能,也没有通过测试点,最后是在写不进去了,第五个功能虽然有困难最后还是实现了。

解决方法:在bilibili上找到的一名java老师罗顺平关于正则表达式的课程,里面分为了二十多个视频讲解,非常的详细。

                  通过海伦公式实现了第五个功能,但是在求三角形面积的时候不能海伦公式,因为有精度误差

     我使用了一个一步到位的公式

    result = Math.abs((ax * by + bx * cy + cx * ay - bx * ay - cx * by - ax * cy) / 2.0)

                  其他数学知识上暂时还没有解决

       特殊输出还是采取上一题的输出方法

2.期中考试

(1)7-1

 

困难:因为考试时时间有限,都是简单的类设计,并没有遇到很多困难,按照要求写很简单

 

 

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        GeometryObject a = new GeometryObject();
        int choice = input.nextInt();
        while(choice != 0) {
            switch(choice) {
                case 1://insert Point object into list
                {
                    Point p1= new Point(input.nextDouble(), input.nextDouble());
                    if(p1.getX()<=0||p1.getX()>200||p1.getY()<=0||p1.getY()>200){
                        System.out.println("Wrong Format");
                    }else
                    a.add(p1);
                }
                    break;
                case 2://insert Line object into list
                    double a1 = input.nextDouble();
                    double a2 = input.nextDouble();
                    double a3 = input.nextDouble();
                    double a4 = input.nextDouble();
                    Point p2= new Point(a1, a2);
                    Point p3= new Point(a3, a4);
                    String color1 = input.next();
                    Line line = new Line(p2,p3, color1);
                    if(a1<=0||a1>200||a2<=0||a2>200||a3<=0||a3>200||a4<=0||a4>200){
                        System.out.println("Wrong Format");
                    }else
                    a.add(line);
                    break;
                case 3://insert Plane object into list
                {
                    String color2 = input.next();
                    Plane plane = new Plane(color2);
                    a.add(plane);
                }
                    break;
                case 4://delete index - 1 object from list
                    int index = input.nextInt();
                    a.remove(index);
            }
            choice = input.nextInt();
        }
        a.getList();
    }
}
abstract class classElement{
    public void display(){

    }
}
class Point extends classElement{
    private double x,y;

    public Point() {
    }

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

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

    public double getY() {
        return y;
    }

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

    public void display(){
        System.out.printf("(%.2f,%.2f)\n",this.x,this.y);
    }
}
class Line extends classElement{
    private Point point1;
    private Point point2;
    private String color;

    public Line() {
    }

    public Line(Point point1, Point point2, String color) {
        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 void display(){
        System.out.println("The line's color is:" +this.color+
                "\nThe line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.printf("The line's length is:");
        System.out.printf("%.2f\n",distance(point1,point2));
    }

    public double distance(Point a, Point b) {
        double distance = Math.sqrt(Math.pow(a.getX() - b.getX(), 2) + Math.pow(a.getY() - b.getY(), 2));
        return distance;
    }
}
class Plane extends classElement{
    private String color;

    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);
    }
}
class GeometryObject{
    private ArrayList<classElement> a= new ArrayList<>();

    public void add(classElement b){
        this.a.add(b);
    }

    public void remove(int index){
        if(index<=a.size())
            this.a.remove(index-1);
        else
            return;
    }

    public void getList(){
        for(classElement e:a){
            e.display();
        }
    }
}

(2)7-2

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Point p1= new Point(scanner.nextDouble(), scanner.nextDouble());
        Point p2= new Point(scanner.nextDouble(), scanner.nextDouble());
        String color = scanner.next();
        Line line = new Line(p1,p2, color);
        Plane plane = new Plane(color);
        classElement element;
        if(p1.getX()<=0||p1.getX()>200||p1.getY()<=0||p1.getY()>200||p2.getX()<=0||p2.getX()>200||p2.getY()<=0||p2.getY()>200){
            System.out.println("Wrong Format");
        }else{
            element = p1;//起点Point
            element.display();

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

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

            element = plane;//
            element.display();

        }
    }
}
abstract class classElement{
    public void display(){

    }
}
class Point extends classElement{
    private double x,y;

    public Point() {
    }

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

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

    public double getY() {
        return y;
    }

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

    public void display(){
        System.out.printf("(%.2f,%.2f)\n",this.x,this.y);
    }
}
class Line extends classElement{
    private Point point1;
    private Point point2;
    private String color;

    public Line() {
    }

    public Line(Point point1, Point point2, String color) {
        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 void display(){
        System.out.println("The line's color is:" +this.color+
                "\nThe line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.printf("The line's length is:");
        System.out.printf("%.2f\n",distance(point1,point2));
    }

    public double distance(Point a, Point b) {
        double distance = Math.sqrt(Math.pow(a.getX() - b.getX(), 2) + Math.pow(a.getY() - b.getY(), 2));
        return distance;
    }
}
class Plane extends classElement{
    private String color;

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

 

困难:也是考试中的试题,没有遇见太大困难,还是按照要求,在7-1的要求上更改,而且示例代码非常贴心,最主要的还是注意多态的使用

(3)7-3

 

 

 困难:因为是第一次实际操作使用容器,并且时间比较紧急,所以在使用容器时遇到一些小困难,其它还是再上两道题的基础上更改,示例代码还是很贴心

解决方法:参考了书中关于容器的一些内容按要求完成了题目内容

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        GeometryObject a = new GeometryObject();
        int choice = input.nextInt();
        while(choice != 0) {
            switch(choice) {
                case 1://insert Point object into list
                {
                    Point p1= new Point(input.nextDouble(), input.nextDouble());
                    if(p1.getX()<=0||p1.getX()>200||p1.getY()<=0||p1.getY()>200){
                        System.out.println("Wrong Format");
                    }else
                    a.add(p1);
                }
                    break;
                case 2://insert Line object into list
                    double a1 = input.nextDouble();
                    double a2 = input.nextDouble();
                    double a3 = input.nextDouble();
                    double a4 = input.nextDouble();
                    Point p2= new Point(a1, a2);
                    Point p3= new Point(a3, a4);
                    String color1 = input.next();
                    Line line = new Line(p2,p3, color1);
                    if(a1<=0||a1>200||a2<=0||a2>200||a3<=0||a3>200||a4<=0||a4>200){
                        System.out.println("Wrong Format");
                    }else
                    a.add(line);
                    break;
                case 3://insert Plane object into list
                {
                    String color2 = input.next();
                    Plane plane = new Plane(color2);
                    a.add(plane);
                }
                    break;
                case 4://delete index - 1 object from list
                    int index = input.nextInt();
                    a.remove(index);
            }
            choice = input.nextInt();
        }
        a.getList();
    }
}
abstract class classElement{
    public void display(){

    }
}
class Point extends classElement{
    private double x,y;

    public Point() {
    }

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

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

    public double getY() {
        return y;
    }

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

    public void display(){
        System.out.printf("(%.2f,%.2f)\n",this.x,this.y);
    }
}
class Line extends classElement{
    private Point point1;
    private Point point2;
    private String color;

    public Line() {
    }

    public Line(Point point1, Point point2, String color) {
        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 void display(){
        System.out.println("The line's color is:" +this.color+
                "\nThe line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.printf("The line's length is:");
        System.out.printf("%.2f\n",distance(point1,point2));
    }

    public double distance(Point a, Point b) {
        double distance = Math.sqrt(Math.pow(a.getX() - b.getX(), 2) + Math.pow(a.getY() - b.getY(), 2));
        return distance;
    }
}
class Plane extends classElement{
    private String color;

    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);
    }
}
class GeometryObject{
    private ArrayList<classElement> a= new ArrayList<>();

    public void add(classElement b){
        this.a.add(b);
    }

    public void remove(int index){
        if(index<=a.size())
            this.a.remove(index-1);
        else
            return;
    }

    public void getList(){
        for(classElement e:a){
            e.display();
        }
    }
}

链表实现:

要求:

 

 

 遇到的困难:

通过老师上课的讲解,并没有遇到太多的困难,顺利的按要求完成了要求的功能,最主要的困难是接口的使用

解决方法:

通过网课学习接口的使用方法

类图:

 

 

 set训练:

一.HashSet运行结果:

1.代码:

import java.util.*;
public class TestHashSet {
    public static void main(String[] args) {
        // Create a hash set
        Set<String> set = new HashSet<>();
        // Add strings to the set
        set.add("London");
        set.add("Paris");
        set.add("New York");
        set.add("San Francisco");
        set.add("Beijing");
        set.add("New York");
        System.out.println(set);
        // Display the elements in the hash set
        for (String s: set) {
            System.out.print(s.toUpperCase() + " ");
        }
        // Process the elements using a forEach method
        System.out.println();
        set.forEach(e -> System.out.print(e.toLowerCase() + " "));
    }
}

 

 

 

 

 

 

2.方法及作用:

toUpperCase():将字符串转化为大写

toLowerCase():将字符串转化为小写

二.TreeSte运行结果:

1.代码:

import java.util.*;
public class TestTreeSet {
    public static void main(String[] args) {
        // Create a hash set
        Set<String> set = new HashSet<>();
        // Add strings to the set
        set.add("London");
        set.add("Paris");
        set.add("New York");
        set.add("San Francisco");
        set.add("Beijing");
        set.add("New York");
        System.out.println("Sorted Hash set: " + set);
        TreeSet<String> treeSet = new TreeSet<>(set);
        System.out.println("Sorted tree set: " + treeSet);
        // Use the methods in SortedSet interface
        System.out.println("first(): " + treeSet.first());
        System.out.println("last(): " + treeSet.last());
        System.out.println("headSet(\"New York\"): " +
                treeSet.headSet("New York"));
        System.out.println("tailSet(\"New York\"): " +
                treeSet.tailSet("New York"));
        // Use the methods in NavigableSet interface
        System.out.println("lower(\"P\"): " + treeSet.lower("P"));
        System.out.println("higher(\"P\"): " + treeSet.higher("P"));
        System.out.println("floor(\"P\"): " + treeSet.floor("P"));
        System.out.println("ceiling(\"P\"): " + treeSet.ceiling("P"));
        System.out.println("pollFirst(): " + treeSet.pollFirst());
        System.out.println("pollLast(): " + treeSet.pollLast());
        System.out.println("New tree set: " + treeSet);
    }
}

 

 

 

 

 

 

2.方法及作用:

first():返回TreeSet中的第一个元素

Last():返回TreeSet中的最后一个元素

headSet<String>:返回此元素前面的所有元素

TailSet<String>:返回此元素和他后面的所有元素

Lower():返回TreeSet中比指定元素低的最高元素

higher():返回TreeSet中比指定元素高的最低元素

floor():返回TreeSet中小于或等于给定元素的最高元素

ceiling():返回TreeSet中大于或等于给定元素的最低元素

pollFirst():返回TreeSet中的第一个元素并删除

PollLast():返回TreeSet中的最后一个元素并删除

 

三.TestHashSet运行结果

1.代码:

 
import java.util.*;

public class TestLinkedHashSet {
    public static void main(String[] args) {
        // Create a linked hash set
        Set<String> set = new LinkedHashSet<>();

        // Add strings to the set
        set.add("London");
        set.add("Paris");
        set.add("New York");
        set.add("San Francisco");
        set.add("Beijing");
        set.add("New York");

        System.out.println(set);

        // Display the elements in the hash set
        for (String element: set)
            System.out.print(element.toLowerCase() + " ");
    }
}

 

 

 

 

四.自己的类(wolf

1. 代码


import java.util.*;
public class TestHashSet {
    public static void main(String[] args) {
        // Create a hash set
        Set<Wolf> set = new HashSet<>();
        Wolf a = new Wolf("灰太狼");
        Wolf b = new Wolf("蕉太郎");
        Wolf c = new Wolf("红太狼");
        Wolf d = new Wolf("小灰灰");
        set.add(a);
        set.add(b);
        set.add(c);
        set.add(d);
        System.out.println(set);
        }

 class Wolf {
    String name = null;

     public Wolf(String name) {
         this.name = name;
     }

     public String toString() {
        return name;
    }
}
 

Set、Lambda练习

 

TestMap

源码:

import java.util.*;

 

public class TestMap {

    public static void main(String[] args) {

        // Create a HashMap

        Map<String, Integer> hashMap = new HashMap<>();

        hashMap.put("Smith", 30);

        hashMap.put("Anderson", 31);

        hashMap.put("Lewis", 29);

        hashMap.put("Cook", 29);

        hashMap.put("Dongjiaqi",21);//新加入的数据

        System.out.println("Display entries in HashMap");

        System.out.println(hashMap + "\n");

 

        // Create a TreeMap from the preceding HashMap

        Map<String, Integer> treeMap = new TreeMap<>(hashMap);

        System.out.println("Display entries in ascending order of key");

        System.out.println(treeMap);

        // Create a LinkedHashMap

        Map<String, Integer> linkedHashMap =

                new LinkedHashMap<>(16, 0.75f, true);

        linkedHashMap.put("Smith", 30);

        linkedHashMap.put("Anderson", 31);

        linkedHashMap.put("Lewis", 29);

        linkedHashMap.put("Cook", 29);

        // Display the age for Lewis

        System.out.println("\nThe age for " + "Lewis is " +

                linkedHashMap.get("Lewis"));

 

        System.out.println("Display entries in LinkedHashMap");

        System.out.println(linkedHashMap);

 

        // Display each entry with name and age

        System.out.print("\nNames and ages are ");

        treeMap.forEach(

                (name, age) -> System.out.print(name + ": " + age + " "));

    }

}

 

}

 

 

 

 

 

Lambda 表达式

源码:

public class Java8Tester {

    public static void main(String args[]){

        Java8Tester tester = new Java8Tester();

 

        // 类型声明

        MathOperation addition = (int a, int b) -> a + b;

 

        // 不用类型声明

        MathOperation subtraction = (a, b) -> a - b;

 

        // 大括号中的返回语句

        MathOperation multiplication = (int a, int b) -> { return a * b; };

 

        // 没有大括号及返回语句

        MathOperation division = (int a, int b) -> a / b;

 

        MathOperation aaa = (int a,int b) ->{return (a+b)*a;};//新加入的算法

 

        System.out.println("10 + 5 = " + tester.operate(10, 5, addition));

        System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction));

        System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication));

        System.out.println("10 / 5 = " + tester.operate(10, 5, division));

        System.out.println("( 10 + 5 ) x 10 = "+tester.operate(10,5,aaa));

 

        // 不用括号

        GreetingService greetService1 = message ->

                System.out.println("Hello " + message);

 

        // 用括号

        GreetingService greetService2 = (message) ->

                System.out.println("Hello " + message);

 

        greetService1.sayMessage("Runoob");

        greetService2.sayMessage("Google");

    }

 

    interface MathOperation {

        int operation(int a, int b);

    }

 

    interface GreetingService {

        void sayMessage(String message);

    }

 

    private int operate(int a, int b, MathOperation mathOperation){

        return mathOperation.operation(a, b);

    }

}

 

}

 

 

 

 

 

 

Stream

1.遍历/匹配

代码:

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Optional;

 

public class StreamTest {

    public static void main(String[] args) {

 

        List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);

 

        // 遍历输出符合条件的元素

        list.stream().filter(x -> x > 6).forEach(System.out::println);

        // 匹配第一个

        Optional<Integer> findFirst = list.stream().filter(x -> x > 6).findFirst();

        // 匹配任意(适用于并行流)

        Optional<Integer> findAny = list.parallelStream().filter(x -> x > 6).findAny();

        // 是否包含符合特定条件的元素

        boolean anyMatch = list.stream().anyMatch(x -> x < 6);

        System.out.println("匹配第一个值:" + findFirst.get());

        System.out.println("匹配任意一个值:" + findAny.get());

        System.out.println("是否存在大于6的值:" + anyMatch);

    }

 

}

 

 

list.stream().filter(x -> x > 6).forEach(System.out::println);

遍历每个符合条件的元素

Optional<Integer> findFirst = list.stream().filter(x -> x > 6).findFirst();

匹配第一个

Optional<Integer> findAny = list.parallelStream().filter(x -> x > 6).findAny();

匹配任意

boolean anyMatch = list.stream().anyMatch(x -> x < 6);

是否包含特定元素

聚合

代码:

private static void test02() {

    List<String> list = Arrays.asList("zhangsan", "lisi", "wangwu", "sunliu");

    Comparator<? super String> comparator = Comparator.comparing(String::length);

    Optional<String> max = list.stream().max(comparator);

    System.out.println(max);

}

//获取Integer集合中的最大值

private static void test05() {

    List<Integer> list = Arrays.asList(1, 17, 27, 7);

    Optional<Integer> max = list.stream().max(Integer::compareTo);

    // 自定义排序

    Optional<Integer> max2 = list.stream().max(new Comparator<Integer>() {

        @Override

        public int compare(Integer o1, Integer o2) {

            return o1.compareTo(o2);

        }

    });

    System.out.println(max2);

}

 

 

 

 

 

自定义排序

 

 

2.在学习中学习到并使用的知识点

(1)继承与多态

继承使你可以定义一个通用的类(父类),之后通过继承得到一个更特殊的类(子类)

什么时候可以继承呢?

一般继承用于两种语境

(1)整体与部分之间的关系(不常用)

例如计算机和cpu的关系,我们一般采用聚集表示

因为继承的耦合性大于聚合的耦合性

(2)大类到小类的关系

例如人类和教师,学生,工人之间的关系

(3)supper:关键字,指代父类,可以调用父类的普通方法和构造方法

复制兼容性原则:可以将子类的对象赋给父类的引用

方法重写:需要在子类中使用和父类重定义的方法的实现

 

多态:意味着父类型的变量可以引用子类型的对象

(2)抽象类和接口

抽象类是专门做父类的,不能创建对象

接口:接口被看作一种特殊的类,与抽象类相似,不能使用new对象

总结:

1.bug的主要来源:

自己的疏忽,发生一些细节的小问题,比如字符数字没有加‘  ’,这也是我起初没有注意最困扰我的问题,导致判断不通过

没有认真读题导致题意理解错误或有偏差

2.学习到了什么:

除了类和继承的相关知识点,在其它题目集中学会了使用正则表达式,字符串的分割,在做最近一次的作业中我起初是使用字符串的分割,但是没法实现要求的全部功能,后来学习了正则表达式来解决这个问题,在正则表达式的使用时也会遇到很多问题,需要的形式自己没法正确表达出来,只能通过不断的调试来寻找自己需要的表达式,在课堂上老师也是教会了我很多

3.心得体会:

这是接触java这门语言第二次写博客,这是我第二次写学习总结,总而言之这种总结方法对我来说起到很大的作用,这是我第一次写学习总结,我认为在梳理过后我对知识在脑海里梳理的更加清晰,有助于对这些知识点的再次使用,这也是我第一次编写博客,所以有很多表达不清楚的地方,我因为过于追求pta的分数,所以我很多时间都花在了题目集上,导致自己没有太多时间来编写学习总结和博客,所以我会在下次编写中进行优化,尽可能写出更好的学习总结以及博客,也是很感谢老师在课堂上和课堂下做出的贡献,网课的购买以及对我们免费的开放让我甚是感动。

关于Java,这是我在第一次学习之后的第一次学习总结,我对java学习还是不够,所以还需要继续在课上课下继续努力,继续在优秀的代码中学习,多看教程,多培养自己的逻辑

4.小建议:

因为我是一个新手,所以还不能给出太多的权威的建议,但是我还是总结出了一些经验,在语法通关的情况下,我们要尽可能去提升自己的逻辑推理能力和数学能力,因为只有这样才能去解决生活中的实际问题,逻辑能力就像基本功,所以我们的数学专业在这个方面就体现了很强的重要性,再有就是培养自己的兴趣,因为我能感觉到我只有在兴趣高涨的时候才能写进去代码,坐得住板凳才能写得出好代码,多去实践,很多人为了快速学会java而不去时间,不要把所有时间都放在学上,快餐式学习不可取,更多的是去自己编写代码,从而学好java

posted @ 2022-04-30 23:52  珈圻  阅读(28)  评论(0)    收藏  举报