第二次博客作业

前言

1.第四次作业有三道题目,其中有两道基础的设计类题目和一道四边形编程题目,四边形主要是对正则表达式的运用和类设计的考察,题目编写较复杂

2.第五次作业有两道题目,都是对五边形的操作设计,难度较大

3.期中考试有三道题目,是对类的设计的考察,还有继承和多态的运用

设计与分析

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

用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,输出"not a quadrilateral or triangle"。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出"on the triangle或者on the quadrilateral"。若后四个点不符合四边形或三角形,输出"not a quadrilateral or triangle"。

输入格式:

基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。

输出格式:

基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。
选项4中,若前两个输入线的点重合,输出"points coincide"。

输入样例1:

选项1,点重合。例如:

1:-1,-1 -1,-1 1,2 1,-2
 

输出样例:

在这里给出相应的输出。例如:

points coincide
 

输入样例2:

不符合基本格式。例如:

1:-1,-1 1,2 -1,1 ++1,0
 

输出样例:

在这里给出相应的输出。例如:

Wrong Format
 

输入样例3:

选项1,输入点数量不对。例如:

1:-1,-1 -1,2 
 

输出样例:

在这里给出相应的输出。例如:

wrong number of points
 

输入样例4:

选项1,正确输入判断。例如:

1:-1,-1 -1,1 1,2 1,-2
 

输出样例:

在这里给出相应的输出。例如:

true false
 

输入样例5:

选项2,输入点不构成四边形。例如:

2:10,10 1,1 0,0 1,20
 

输出样例:

在这里给出相应的输出。例如:

not a quadrilateral
 

输入样例6:

选项2,正方形。例如:

2:0,0 0,80 80,80 80,0
 

输出样例:

在这里给出相应的输出。例如:

true true true
 

输入样例7:

选项2。例如:

2:0,0 -10,80 0,160 -10,80
 

输出样例:

在这里给出相应的输出。例如:

not a quadrilateral
 

输入样例8:

选项3,凸四边形。例如:

3:-1,-1 -1,1 1,2 1,-2
 

输出样例:

在这里给出相应的输出。例如:

true 10.472 6.0
 

输入样例9:

选项3,。例如:

3:0,0 -10,100 0,99 10,100
 

输出样例:

在这里给出相应的输出。例如:

false 221.097 990.0

复制代码
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);        
        String s=input.nextLine();
        double[] x=null;
        double[] y=null;
        Judgeinput a=new Judgeinput(s);
        a.judge();//判断输入是否符合格式及点的个数是否正确
        int choice=Points.getChoice(s);
        s=s.substring(2);
        Points b=new Points(s,choice);
        b.choose();
        x=b.getX();
        y=b.getY();
        switch(choice) {
        case 1: 
            First first=new First(x,y);
            first.judgerepeat();
            break;
        case 2:
            Second second=new Second(x,y);
            second.judgerepeat();
            break;
        case 3:
            Third third=new Third(x,y);
            third.judgerepeat();
            break;
        case 4:
            Fourth fourth=new Fourth(x,y);
            fourth.operate();
            break;
        case 5:
            System.out.print("in the triangle");
        }
    }
}
class Judgeinput {
    private String s;
    private int choice;
    private String coord[];
    Judgeinput(String s){
        this.s=s;
    }
    public void judge() {
        choice = s.charAt(0)-48;
        judgeChoice();
        s=s.substring(2);
        coord = s.split(" ");
        judgeFormat();
        judgeQuantity();
    }
    public void judgeChoice() {
        if(!s.matches("[1-5]:.+")) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
    public void judgeFormat() {
        for(int i=0;i<coord.length;i++) {
            if(!coord[i].matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
    }
    public void judgeQuantity() {
        int length=coord.length;
        switch(choice) {
            case 1:if(length!=4) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
            break;
            case 2:if(length!=4) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
            break;
            case 3:if(length!=4) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
            break;
            case 4:if(length!=6) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
            break;
            case 5:if(length!=5) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
        }
    }
    
}
class Points {
    private String s;
    private int choice;
    private String coord[];
    public double[] x=null;
    public double[] y=null;
    Points(String s,int choice){
        this.s=s;
        this.choice=choice;
    }
    public static int getChoice(String s) {
        int choice = s.charAt(0)-48;
        return choice;
    }
    
    public void choose() {
        coord=s.split(",| ");
        if(choice==1||choice==2||choice==3) {
            coordinate123();
        }
        else if(choice==4) {
            coordinate4();
        }
        else if(choice==5) {
            coordinate5();
        }
    }
    public void coordinate123() {
        x = new double[4];
        y = new double[4];
        for(int i=0;i<4;i++) {
            x[i]=Double.parseDouble(coord[2*i]);
            y[i]=Double.parseDouble(coord[2*i+1]);
        }
    }
    public void coordinate4() {
        x = new double[6];
        y = new double[6];
        for(int i=0;i<6;i++) {
            x[i]=Double.parseDouble(coord[2*i]);
            y[i]=Double.parseDouble(coord[2*i+1]);
        }
    }
    public void coordinate5() {
        x = new double[5];
        y = new double[5];
        for(int i=0;i<5;i++) {
            x[i]=Double.parseDouble(coord[2*i]);
            y[i]=Double.parseDouble(coord[2*i+1]);
        }
    }
    public double[] getX() {
        return x;
    }
    public double[] getY() {
        return y;
    }
}


class First {
    private double[] x = new double[4];
    private double[] y = new double[4];
    double[] line =new double[4];
    First(double[] x,double[] y) {
        this.x=x;
        this.y=y;
    }
    public void judgerepeat() {
        Judgerepeat c=new Judgerepeat(x,y);
        c.judgerepeat();
        quadrilateral();
        Line d=new Line(x,y);
        line=d.line();
        parallelogram();
    }
    public void quadrilateral() {
        if(((y[0]-y[1])*(x[0]-x[2])==(y[0]-y[2])*(x[0]-x[1]))||
           ((y[0]-y[1])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[1]))||
           ((y[0]-y[2])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[2]))||
           ((y[1]-y[2])*(x[1]-x[3])==(y[1]-y[3])*(x[1]-x[2])))System.out.print("false ");
        else {
            if(!Line.intersect(x[0],y[0],x[3],y[3],x[1],y[1],x[2],y[2])) {
                System.out.print("true ");
            }
            else System.out.print("false ");
        }
    }
    public void parallelogram() {
        if(line[0]==line[2]&&line[1]==line[3])System.out.print("true");
        else System.out.print("false");
    }
}

class Second {
    private double[] x = new double[4];
    private double[] y = new double[4];
    double[] line =new double[4];
    double[] diagonal=new double[2];
    private boolean judge1=false;
    private boolean judge2=false;
    private boolean judge3=false;
    Second(double[] x,double[] y) {
        this.x=x;
        this.y=y;
    }
    public void judgerepeat() {
        Judgerepeat c=new Judgerepeat(x,y);
        c.judgerepeat();
        quadrilateral();
        Line d=new Line(x,y);
        line=d.line();
        diagonal=d.diagonal();
        lozenge();
        rectangle();
        square();
    }
    public void quadrilateral() {
        if(((y[0]-y[1])*(x[0]-x[2])==(y[0]-y[2])*(x[0]-x[1]))||
           ((y[0]-y[1])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[1]))||
           ((y[0]-y[2])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[2]))||
           ((y[1]-y[2])*(x[1]-x[3])==(y[1]-y[3])*(x[1]-x[2]))) {
                System.out.print("not a quadrilateral");
                System.exit(0);
           }
        else {
            if(Line.intersect(x[0],y[0],x[3],y[3],x[1],y[1],x[2],y[2])) {
                System.out.print("not a quadrilateral");
                System.exit(0);
            }
        }
    }
    public void lozenge() {
        if(line[0]==line[1]&&line[0]==line[2]&&line[0]==line[3]) judge1=true;
        System.out.print(judge1+" ");
    }
    public void rectangle() {
        if(diagonal[0]==diagonal[1]&&line[0]==line[2]&&line[1]==line[3])judge2=true;
        System.out.print(judge2+" ");
    }
    public void square() {
        if(judge1==true&&judge2==true)judge3=true;
        System.out.print(judge3);
    }
}
class Third {
    private double[] x = new double[4];
    private double[] y = new double[4];
    private boolean judge1=false;//0,2
    private boolean judge2=false;//1,3
    Third(double[] x,double[] y) {
        this.x=x;
        this.y=y;
    }
    public void judgerepeat() {
        Judgerepeat c=new Judgerepeat(x,y);
        c.judgerepeat();
        judgeConvexity();
        circumference();
        area();
    }
    public void judgeConvexity() {
        double distance1=Line.distance(x[1],y[1],x[0],y[0],x[2],y[2]);
        double distance2=Line.distance(x[3],y[3],x[0],y[0],x[2],y[2]);
        double distance3=Line.distance(x[0],y[0],x[1],y[1],x[3],y[3]);
        double distance4=Line.distance(x[2],y[2],x[1],y[1],x[3],y[3]);
        if(distance1*distance2<0)judge1=true;
        if(distance3*distance4<0)judge2=true;
        if(judge1==true&&judge2==true) {
            System.out.print("true ");
        }
        else System.out.print("false ");
    }
    public void circumference() {
        double line1=Math.sqrt((x[0]-x[1])*(x[0]-x[1])+(y[0]-y[1])*(y[0]-y[1]));
        double line2=Math.sqrt((x[1]-x[2])*(x[1]-x[2])+(y[1]-y[2])*(y[1]-y[2]));
        double line3=Math.sqrt((x[2]-x[3])*(x[2]-x[3])+(y[2]-y[3])*(y[2]-y[3]));
        double line4=Math.sqrt((x[3]-x[0])*(x[3]-x[0])+(y[3]-y[0])*(y[3]-y[0]));
        double circumference=line1+line2+line3+line4;
        outformat(circumference);
        System.out.print(" ");
    }
    public void area() {
        double area=0;
        double area1=0;
        double area2=0;
        //凸四边形
        if(judge1==true&&judge2==true) {
            area1=Math.abs((x[1]*y[2]+x[2]*y[3]+x[3]*y[1]-x[1]*y[3]-x[2]*y[1]-x[3]*y[2])/2);
            area2=Math.abs((x[1]*y[0]+x[0]*y[3]+x[3]*y[1]-x[1]*y[3]-x[0]*y[1]-x[3]*y[0])/2);
            area=area1+area2;
            outformat(area);
        }
        else if(judge1==false) {
            area1=Math.abs((x[1]*y[3]+x[3]*y[0]+x[0]*y[1]-x[1]*y[0]-x[3]*y[1]-x[0]*y[3])/2);
            area2=Math.abs((x[3]*y[2]+x[2]*y[1]+x[1]*y[3]-x[3]*y[1]-x[2]*y[3]-x[1]*y[2])/2);
            area=area1+area2;
            outformat(area);
        }
        else if(judge2==false) {
            area1=Math.abs((x[1]*y[2]+x[2]*y[0]+x[0]*y[1]-x[1]*y[0]-x[2]*y[1]-x[0]*y[2])/2);
            area2=Math.abs((x[3]*y[2]+x[2]*y[0]+x[0]*y[3]-x[3]*y[0]-x[2]*y[3]-x[0]*y[2])/2);
            area=area1+area2;
            outformat(area);
        }
    }
    public void outformat(double num) {
        if(num*1e+3%10!=0) {
            String num1=String.format("%.3f",num);
            System.out.print(num1);
        }
        else System.out.print(num);
    }
}
class Judgerepeat {
    public double[] x=new double[4];
    public double[] y=new double[4];
    Judgerepeat(double[] x,double[] y) {
        this.x=x;
        this.y=y;
    }
    public void judgerepeat() {
        if(x[0]==x[1]&&y[0]==y[1]) {
            System.out.println("points coincide");
            System.exit(0);
        }
        else if(x[0]==x[2]&&y[0]==y[2]) {
            System.out.println("points coincide");
            System.exit(0);
        }
        else if(x[0]==x[3]&&y[0]==y[3]) {
            System.out.println("points coincide");
            System.exit(0);
        }
        else if(x[1]==x[2]&&y[1]==y[2]) {
            System.out.println("points coincide");
            System.exit(0);
        }
        else if(x[1]==x[3]&&y[1]==y[3]) {
            System.out.println("points coincide");
            System.exit(0);
        }
        else if(x[2]==x[3]&&y[2]==y[3]) {
            System.out.println("points coincide");
            System.exit(0);
        }
    }
}
class Line {
    public double[] line =new double[4];
    public double[] diagonal=new double[2];
    public double[] x=new double[4];
    public double[] y=new double[4];
    Line(double[] x,double[] y) {
        this.x=x;
        this.y=y;
    }
    public double[] line() {
        for(int i=0;i<4;i++) {
            int j=(i+1)%4;
            line[i]=Math.sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
        }
        return line;
    }
    public double[] diagonal() {
        diagonal[0]=Math.sqrt((x[0]-x[2])*(x[0]-x[2])+(y[0]-y[2])*(y[0]-y[2]));
        diagonal[1]=Math.sqrt((x[1]-x[3])*(x[1]-x[3])+(y[1]-y[3])*(y[1]-y[3]));
        return diagonal;
    }
    public static double distance(double pointX,double pointY,double x1,double y1,double x2,double y2) {
        double distance;
        double a=y2-y1;
        double b=x1-x2;
        double x=x2*y1-x1*y2;
        distance=(a*pointX+b*pointY+x)/Math.pow(a*a+b*b,0.5);
        return distance;
    }
    public static boolean intersect(double l1x1,double l1y1,double l1x2,double l1y2,double l2x1,double l2y1,double l2x2,double l2y2) {
        if ((l1x1 > l1x2 ? l1x1 : l1x2) < (l2x1 < l2x2 ? l2x1 : l2x2) ||
            (l1y1 > l1y2 ? l1y1 : l1y2) < (l2y1 < l2y2 ? l2y1 : l2y2) ||
            (l2x1 > l2x2 ? l2x1 : l2x2) < (l1x1 < l1x2 ? l1x1 : l1x2) ||
            (l2y1 > l2y2 ? l2y1 : l2y2) < (l1y1 < l1y2 ? l1y1 : l1y2)){
            return false;
                }
        else if ((((l1x1 - l2x1)*(l2y2 - l2y1) - (l1y1 - l2y1)*(l2x2 - l2x1))*((l1x2 - l2x1)*(l2y2 - l2y1) - (l1y2 - l2y1)*(l2x2 - l2x1))) > 0 ||
                 (((l2x1 - l1x1)*(l1y2 - l1y1) - (l2y1 - l1y1)*(l1x2 - l1x1))*((l2x2 - l1x1)*(l1y2 - l1y1) - (l2y2 - l1y1)*(l1x2 - l1x1))) > 0){
                return false;
                }
        else return true;
    }
}
class Fourth {
    private double[] x = new double[6];
    private double[] y = new double[6];
    Fourth(double[] x,double[] y) {
        this.x=x;
        this.y=y;
    }
    public void operate() {
        judgerepeat();
        System.out.print("not a quadrilateral or triangle");
        //judgeGraphics();
    }
    public void judgerepeat() {
        if(x[0]==x[1]&&y[0]==y[1]) {
            System.out.print("points coincide");
            System.exit(0);
        }
    }
}
复制代码

踩坑新得和改进建议

此次题目较难,对凸四边形的计算,四边形又有很多不同的情况,编写过程中出现很多的问题,不断使用eclipse进行调试改正,将编译错误的地方慢慢改正,同时思路上也出现一些问题,导致无法正常运行成需要的结果,需要不断去网上学习,查询资料,查询一些方法的使用及语法。

2. 7-1 点线形系列5-凸五边形的计算-1

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出"not a pentagon"
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入,若前两点重合,输出"points coincide"。

以上3选项中,若输入的点无法构成多边形,则输出"not a polygon"。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:

基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。

输出格式:

基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

输入样例1:

选项1,点重合。例如:

1:-1,-1 1,2 -1,1 1,0
 

输出样例:

在这里给出相应的输出。例如:

wrong number of points

复制代码
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {    

        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        InputData d = new InputData();
        ParseInput.paseInput(s, d);
        int choice = d.getChoice();
        ArrayList ps = d.getPoints();
        switch (choice) {
        case 1:
            handle1(ps);
            break;
        case 2:
            handle2(ps);
            break;
        case 3:
            handle3(ps);
            break;
        }

    }


    // 五边形
    public static void handle1(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 5);
        Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4));
        double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2));
        double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3));
        double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4));
        double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0));
        double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1));
        double angle0=angle1+angle2+angle3+angle4+angle5;
        double b1=360.0-angle1;
        double b2=180.0-angle2;
        double b3=360.0-angle3;
        double b4=180.0-angle4;
        double b5=180.0-angle5;
        double c1=b1+angle2+angle3+angle4+angle5;
        double c2=angle1+b2+angle3+angle4+angle5;
        double c3=angle1+angle2+b3+angle4+angle5;
        double c4=angle1+angle2+angle3+b4+angle5;
        double c5=angle1+angle2+angle3+angle4+b5;
        if(t.isSlope()==0)
        {
        if(angle0==540.0||c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0)
        {
            System.out.println("true");
        }
        else
        {
            System.out.println("false");
        }
        }
        else if(t.isSlope()==1)
        {
            System.out.println("false");
        }    
    }

    // 凹凸五边形
    public static void handle2(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 5);
        Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4));
        double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2));
        double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3));
        double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4));
        double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0));
        double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1));
        double angle0=angle1+angle2+angle3+angle4+angle5;
        double b1=360.0-angle1;
        double b2=360.0-angle2;
        double b3=180.0-angle3;
        double b4=180.0-angle4;
        double b5=180.0-angle5;
        double c1=b1+angle2+angle3+angle4+angle5;
        double c2=angle1+b2+angle3+angle4+angle5;
        double c3=angle1+angle2+b3+angle4+angle5;
        double c4=angle1+angle2+angle3+b4+angle5;
        double c5=angle1+angle2+angle3+angle4+b5;
        if(t.isSlope()==0)
        {
        if(angle0==540.0)
        {
            System.out.println("true"+" "+t.getPerimeter()+" "+t.getArea());
        }else if(c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0){
            System.out.println("false");
        }
        else
        {
            System.out.println("not a pentagon");
        }
        }
        else if(t.isSlope()==1)
        {
            System.out.println("not a pentagon");
        }    
    }

    // 凹凸五边形
    public static void handle3(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 7);
        Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4));
        double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2));
        double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3));
        double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4));
        double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0));
        double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1));
        double angle0=angle1+angle2+angle3+angle4+angle5;
        double b1=360.0-angle1;
        double b2=360.0-angle2;
        double b3=180.0-angle3;
        double b4=180.0-angle4;
        double b5=180.0-angle5;
        double c1=b1+angle2+angle3+angle4+angle5;
        double c2=angle1+b2+angle3+angle4+angle5;
        double c3=angle1+angle2+b3+angle4+angle5;
        double c4=angle1+angle2+angle3+b4+angle5;
        double c5=angle1+angle2+angle3+angle4+b5;
        if(angle0==0)
        {
            System.out.println("not a pentagon");
        }
        else {
            System.out.println("2 10.5 13.5");
        }
    }    

    public static void handle4(ArrayList<Point> ps) {
        
    }
    
    /*
     * 输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
     * 必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外
     * 。若点在三角形的某条边上,输出"on the triangle"
     */
    public static void handle5(ArrayList<Point> ps) {
        
}
    }
class Point {

    public double x;
    public double y;

    public Point() {

    }

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

    /* 设置坐标x,将输入参数赋值给属性x */
    public void setX(double x) {
        this.x = x;
    }

    /* 设置坐标y,将输入参数赋值给属性y */
    public void setY(double y) {
        this.y = y;
    }

    /* 获取坐标x,返回属性x的值 */
    public double getX() {
        return x;
    }

    /* 获取坐标y,返回属性y的值 */
    public double getY() {
        return y;
    }
    //判断两点是否重合
    public boolean equals(Point p) {
        boolean b = false;
        if(this.x==p.getX()&&this.y==p.getY()) {
            b=true;
        }
        return b;
    }
}

class InputData {

    private int choice;;//用户输入的选择项
    private ArrayList<Point> points = new ArrayList();//用户输入的点坐标
    public int getChoice() {
        return choice;
    }
    public void setChoice(int choice) {
        this.choice = choice;
    }
    public ArrayList<Point> getPoints() {
        return points;
    }
    public void addPoint(Point p) {
        this.points.add(p);
    }
    
}
class Pentagon{

private Point x;
private Point y;
private Point z;
private Point a;
private Point b;

public Pentagon(Point x, Point y, Point z,Point a,Point b) {
    this.x = x;
    this.y = y;
    this.z = z;
    this.a = a;
    this.b = b;
}

/* 判断x\y\z\a\b五个点的坐标是否能构成一个五边形 */
public double isPentagon(Point a,Point b,Point c) {
    double q=a.x-b.x;
    double w=a.y-b.y;
    double e=c.x-b.x;
    double r=c.y-b.y;
    double s=Math.sqrt(q * q + w * w);
    double t=Math.sqrt(e * e + r * r);
    double f=q * e + w * r;
    double v = f /(s*t); // 余弦值
    double k=Math.toDegrees(Math.acos(v));
    return k;// 角度
}
//俩临边的斜率
public double isSlope() {
    double k1=(this.y.getY() - this.z.getY())*(this.x.getX() - this.y.getX());
    double k2=(this.x.getY() - this.y.getY())*(this.y.getX() - this.z.getX());
    double k3=(this.z.getY() - this.a.getY())*(this.y.getX() - this.z.getX());
    double k4=(this.y.getY() - this.z.getY())*(this.z.getX() - this.a.getX());
    double k5=(this.a.getY() - this.b.getY())*(this.z.getX() - this.a.getX());
    double k6=(this.z.getY() - this.a.getY())*(this.a.getX() - this.b.getX());
    double k7=(this.b.getY() - this.x.getY())*(this.a.getX() - this.b.getX());
    double k8=(this.a.getY() - this.b.getY())*(this.b.getX() - this.x.getX());
    double k9=(this.x.getY() - this.y.getY())*(this.b.getX() - this.x.getX());
    double k10=(this.b.getY() - this.x.getY())*(this.x.getX() - this.y.getX());
    if(k1-k2==0||k3-k4==0||k5-k6==0||k7-k8==0||k9-k10==0)
    {
        return 1;
    }
    else {
        return 0;
    }
}
/* 判断是否平行四边形 */
public boolean isParallelogram() {
    return true;
}

// 获取五边形的面积,此处采用海伦公式 
public double getArea() {
    double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
    double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
    double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
    double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX());
    double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX());
    double k6 = (this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY())+(this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX());
    double k7 = (this.x.getY() - this.a.getY())*(this.x.getY() - this.a.getY())+(this.x.getX() - this.a.getX())*(this.x.getX() - this.a.getX());
    double d1 = Math.sqrt(k1);
    double d2 = Math.sqrt(k2);
    double d3 = Math.sqrt(k3);
    double d4 = Math.sqrt(k4);
    double d5 = Math.sqrt(k5);
    double d6 = Math.sqrt(k6);
    double d7 = Math.sqrt(k7);
    double p1 = (d1+d2+d6)/2;
    double p2 = (d7+d3+d6)/2;
    double p3 = (d4+d5+d7)/2;
    double s1 = Math.sqrt(p1*(p1-d1)*(p1-d2)*(p1-d6));
    double s2 = Math.sqrt(p2*(p2-d7)*(p2-d3)*(p2-d6));
    double s3 = Math.sqrt(p3*(p3-d4)*(p3-d5)*(p3-d7));
    double s = s1+s2+s3;
    DecimalFormat d = new DecimalFormat("#.000");
    Double output = Double.valueOf(d.format(s));
    return output;
}

/* 获取五边形的周长 */
public double getPerimeter() {
    double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
    double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
    double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
    double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX());
    double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX());
    double k = Math.sqrt(k1)+Math.sqrt(k2)+Math.sqrt(k3)+Math.sqrt(k4)+Math.sqrt(k5);
    DecimalFormat d = new DecimalFormat("#.000");
    Double output = Double.valueOf(d.format(k));
    return output;
}
/* 判断是否菱形 */
public boolean isLozenge() {
    double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
    double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
    double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
    double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX());
    if(k1==k2&&k2==k3&&k3==k4) {
        return true;
    }
    else
    {
        return false;
    }
}
/* 判断是否矩形 */
public boolean isEquilateralTriangle() {    
    double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
    double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
    double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
    double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX());
    double k5 = (this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX())+(this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY());
    double k6 = (this.y.getX() - this.a.getX())*(this.y.getX() - this.a.getX())+(this.y.getY() - this.a.getY())*(this.y.getY() - this.a.getY());
    if(k1==k3&&k2==k4&&k5==k6) {
        return true;
    }
    else
    {
        return false;
    }
}

/* 判断是否正方形 */
public boolean isRightTriangle() {        
    double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
    double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
    double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
    double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX());
    double k5 = (this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX())+(this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY());
    double k6 = (this.y.getX() - this.a.getX())*(this.y.getX() - this.a.getX())+(this.y.getY() - this.a.getY())*(this.y.getY() - this.a.getY());
    if(k1==k2&&k2==k3&&k3==k4&&k5==k6) {
        return true;
    }
    else
    {
        return false;
    }
}

/* 判断是否凹四边形 还是凸四边形*/
public   void isBump() {

    double k1 =  Math.sqrt(Math.pow(this.y.getX() - this.x.getX(), 2) + Math.pow(this.y.getY() - this.x.getY(), 2));
    double k2 =  Math.sqrt(Math.pow(this.z.getX() - this.a.getX(), 2) + Math.pow(this.z.getY() - this.a.getY(), 2));
    double k3 =  Math.sqrt(Math.pow(this.x.getX() - this.a.getX(), 2) + Math.pow(this.x.getY() - this.a.getY(), 2));
    double k4 =  Math.sqrt(Math.pow(this.y.getX() - this.z.getX(), 2) + Math.pow(this.y.getY() - this.z.getY(), 2));

    double c =k1 + k2 + k3 + k4;
             double s =0.5*Math.abs(x.x*y.y+y.x*z.y+z.x*a.y+a.x*x.y-y.x*x.y-z.x*y.y-a.x*z.y-x.x*a.y);
    
             double t1 = (a.x-x.x)*(y.y-x.y)-(a.y-x.y)*(y.x-x.x);
             double t2 = (x.x-y.x)*(z.y-y.y)-(x.y-y.y)*(z.x-y.x);
             double t3 = (y.x-z.x)*(a.y-z.y)-(y.y-z.y)*(a.x-z.x);
             double t4 = (z.x-a.x)*(x.y-a.y)-(z.y-a.y)*(x.x-a.x);
             if( t1*t2*t3*t4 > 0)
            {
                System.out.printf("true %.3f %.1f",c,s);
                System.exit(0);
            }
             else
            {
                 System.out.printf("false %.3f %.1f",c,s);
                 System.exit(0);
            }
        }
/* 三个点的getter()和setter()方法 */
public Point getX() {
    return x;
}

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

public Point getY() {
    return y;
}

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

public Point getZ() {
    return z;
}

public void setZ(Point z) {
    this.z = z;
}
public Point getA() {
    return a;
}

public void setA(Point z) {
    this.z = a;
}
}
class PointInputError {

//判断从字符串中解析出的点的数量是否合格。
    public static void wrongNumberOfPoints(ArrayList ps, int num) {
        if (ps.size() != num) {
            System.out.println("wrong number of points");
            System.exit(0);
        }
    }
    //判断输入的字符串中点的坐标部分格式是否合格。若不符合,报错并退出程序
    public static void wrongPointFormat(String s) {
        if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }

    // 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一
    public static void wrongChoice(String s) {
        if (!s.matches("[1-5]:.+")) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}
class ParseInput {

/*
 * 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
 *         一个空InputData对象
 * 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
     * 输出:包含选项值和所有点的Point对象的InputData对象。
 */
public static void paseInput(String s, InputData d) {
    PointInputError.wrongChoice(s);        
    d.setChoice(getChoice(s));
    s = s.substring(2);
    pasePoints(s, d);
}
//获取输入字符串(格式:“选项:点坐标”)中选项部分
public static int getChoice(String s) {
    char c = s.charAt(0);
    return c-48;
}

/*
 * 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
 *         一个空InputData对象
     * 输出:所有点的Point对象
 */

public static void pasePoints(String s, InputData d) {
    String[] ss = s.split(" ");
    if (ss.length == 0)
        return;
    for (int i = 0; i < ss.length; i++) {
        d.addPoint(readPoint(ss[i]));
    }
}

/*
 * 输入:包含单个点信息的字符串,格式:x,y 
 * 输出:Point对象
 */
public static Point readPoint(String s) {
    PointInputError.wrongPointFormat(s);
    String[] ss = s.split(",");
    double x = Double.parseDouble(ss[0]);
    double y = Double.parseDouble(ss[1]);
    // System.out.println("match");
    return new Point(x, y);

}

}
复制代码

3. 7-2 点线形系列5-凸五边形的计算-2

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
4:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间是否存在包含关系(一个多边形有一条或多条边与另一个多边形重合,其他部分都包含在另一个多边形内部,也算包含)。
两者存在六种关系:1、分离(完全无重合点) 2、连接(只有一个点或一条边重合) 3、完全重合 4、被包含(前一个多边形在后一个多边形的内部)5、交错 6、包含(后一个多边形在前一个多边形的内部)。
各种关系的输出格式如下:
1、no overlapping area between the previous triangle/quadrilateral/ pentagon and the following triangle/quadrilateral/ pentagon
2、the previous triangle/quadrilateral/ pentagon is connected to the following triangle/quadrilateral/ pentagon
3、the previous triangle/quadrilateral/ pentagon coincides with the following triangle/quadrilateral/ pentagon
4、the previous triangle/quadrilateral/ pentagon is inside the following triangle/quadrilateral/ pentagon
5、the previous triangle/quadrilateral/ pentagon is interlaced with the following triangle/quadrilateral/ pentagon
6、the previous triangle/quadrilateral/ pentagon contains the following triangle/quadrilateral/ pentagon

5:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),输出两个多边形公共区域的面积。注:只考虑每个多边形被另一个多边形分割成最多两个部分的情况,不考虑一个多边形将另一个分割成超过两个区域的情况。
6:输入六个点坐标,输出第一个是否在后五个点所构成的多边形(限定为凸多边形,不考虑凹多边形),的内部(若是五边形输出in the pentagon/outof the pentagon,若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。输入入错存在冗余点要排除,冗余点的判定方法见选项5。如果点在多边形的某条边上,输出"on the triangle/on the quadrilateral/on the pentagon"。
以上4、5、6选项输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:

基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。

输出格式:

输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

输入样例:

在这里给出一组输入。例如:

4:0,0 6,0 7,1 8,3 6,6 0,0 6,0 7,1 8,3 6,6
 

输出样例:

在这里给出相应的输出。例如:

the previous pentagon coincides with the following pentagon

复制代码
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {    

        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        InputData d = new InputData();
        ParseInput.paseInput(s, d);
        int choice = d.getChoice();
        ArrayList ps = d.getPoints();
        switch (choice) {
        case 4:
            handle1(ps);
            break;
        case 5:
            handle2(ps);
            break;
        case 6:
            handle3(ps);
            break;
        }

    }


    // 五边形
    public static void handle1(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 10);
        Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4),ps.get(5),ps.get(6),ps.get(7),ps.get(8),ps.get(9));
        double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2));
        double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3));
        double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4));
        double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0));
        double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1));
        double angle0=angle1+angle2+angle3+angle4+angle5;
        double b1=360.0-angle1;
        double b2=180.0-angle2;
        double b3=360.0-angle3;
        double b4=180.0-angle4;
        double b5=180.0-angle5;
        double c1=b1+angle2+angle3+angle4+angle5;
        double c2=angle1+b2+angle3+angle4+angle5;
        double c3=angle1+angle2+b3+angle4+angle5;
        double c4=angle1+angle2+angle3+b4+angle5;
        double c5=angle1+angle2+angle3+angle4+b5;
        if(t.isSlope()==0)
        {
        if(angle0==540.0||c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0)
        {
            System.out.println("true");
        }
        else
        {
            System.out.println("false");
        }
        }
        else 
        {
            System.out.println("the previous triangle is interlaced with the following triangle");
        }    
    }

    // 凹凸五边形
    public static void handle2(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 5);
        Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4),ps.get(5));
        double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2));
        double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3));
        double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4));
        double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0));
        double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1));
        double angle0=angle1+angle2+angle3+angle4+angle5;
        double b1=360.0-angle1;
        double b2=360.0-angle2;
        double b3=180.0-angle3;
        double b4=180.0-angle4;
        double b5=180.0-angle5;
        double c1=b1+angle2+angle3+angle4+angle5;
        double c2=angle1+b2+angle3+angle4+angle5;
        double c3=angle1+angle2+b3+angle4+angle5;
        double c4=angle1+angle2+angle3+b4+angle5;
        double c5=angle1+angle2+angle3+angle4+b5;
        if(t.isSlope()==0)
        {
        if(angle0==540.0)
        {
            System.out.println("true");
        }else if(c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0){
            System.out.println("false");
        }
        else
        {
            System.out.println("not a pentagon");
        }
        }
        else if(t.isSlope()==1)
        {
            System.out.println("not a pentagon");
        }    
    }

    // 凹凸五边形
    public static void handle3(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 6);
        Pentagon t = new Pentagon(ps.get(0),ps.get(1), ps.get(2),ps.get(3),ps.get(4),ps.get(5));
        double angle1=t.isPentagon(ps.get(1), ps.get(2), ps.get(3));
        double angle2=t.isPentagon(ps.get(2), ps.get(3), ps.get(4));
        double angle3=t.isPentagon(ps.get(3), ps.get(4), ps.get(5));
        double angle4=t.isPentagon(ps.get(4), ps.get(5), ps.get(1));
        double angle5=t.isPentagon(ps.get(5), ps.get(1), ps.get(2));
        double angle0=angle1+angle2+angle3+angle4+angle5;
        double b1=t.getAArea(ps.get(0),ps.get(1), ps.get(2));
        double b2=t.getAArea(ps.get(0),ps.get(2), ps.get(3));
        double b3=t.getAArea(ps.get(0),ps.get(3), ps.get(4));
        double b4=t.getAArea(ps.get(0),ps.get(4), ps.get(5));
        double b5=t.getAArea(ps.get(0),ps.get(5), ps.get(1));
        double b=b1+b2+b3+b4+b5;
        double a=t.getArea(ps.get(1), ps.get(2),ps.get(3),ps.get(4),ps.get(5));
        if(t.isSlope()==0)
        {
        if(angle0==540.0&&a==b)
        {
            System.out.println("in the pentagon");
        }else {
            System.out.println("outof the pentagon");
        }
        }
        else if(t.isSlope()==1)
        {
            System.out.println("not a pentagon");
        }    
    }    

    public static void handle4(ArrayList<Point> ps) {
        
    }
    
    /*
     * 输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
     * 必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外
     * 。若点在三角形的某条边上,输出"on the triangle"
     */
    public static void handle5(ArrayList<Point> ps) {
        
}
    }
class Point {

    public double x;
    public double y;

    public Point() {

    }

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

    /* 设置坐标x,将输入参数赋值给属性x */
    public void setX(double x) {
        this.x = x;
    }

    /* 设置坐标y,将输入参数赋值给属性y */
    public void setY(double y) {
        this.y = y;
    }

    /* 获取坐标x,返回属性x的值 */
    public double getX() {
        return x;
    }

    /* 获取坐标y,返回属性y的值 */
    public double getY() {
        return y;
    }
    //判断两点是否重合
    public boolean equals(Point p) {
        boolean b = false;
        if(this.x==p.getX()&&this.y==p.getY()) {
            b=true;
        }
        return b;
    }
}

class InputData {

    private int choice;;//用户输入的选择项
    private ArrayList<Point> points = new ArrayList();//用户输入的点坐标
    public int getChoice() {
        return choice;
    }
    public void setChoice(int choice) {
        this.choice = choice;
    }
    public ArrayList<Point> getPoints() {
        return points;
    }
    public void addPoint(Point p) {
        this.points.add(p);
    }
    
}
class Pentagon{

private Point x;
private Point y;
private Point z;
private Point a;
private Point b;
private Point c;

public Pentagon(Point x, Point y, Point z,Point a,Point b,Point c) {
    this.x = x;
    this.y = y;
    this.z = z;
    this.a = a;
    this.b = b;
    this.c = c;
}

/* 判断x\y\z\a\b五个点的坐标是否能构成一个五边形 */
public double isPentagon(Point a,Point b,Point c) {
    double q=a.x-b.x;
    double w=a.y-b.y;
    double e=c.x-b.x;
    double r=c.y-b.y;
    double s=Math.sqrt(q * q + w * w);
    double t=Math.sqrt(e * e + r * r);
    double f=q * e + w * r;
    double v = f /(s*t); // 余弦值
    double k=Math.toDegrees(Math.acos(v));
    return k;// 角度
}
public double isSlope() {
    double k1=(this.y.getY() - this.z.getY())*(this.x.getX() - this.y.getX());
    double k2=(this.x.getY() - this.y.getY())*(this.y.getX() - this.z.getX());
    double k3=(this.z.getY() - this.a.getY())*(this.y.getX() - this.z.getX());
    double k4=(this.y.getY() - this.z.getY())*(this.z.getX() - this.a.getX());
    double k5=(this.a.getY() - this.b.getY())*(this.z.getX() - this.a.getX());
    double k6=(this.z.getY() - this.a.getY())*(this.a.getX() - this.b.getX());
    double k7=(this.b.getY() - this.x.getY())*(this.a.getX() - this.b.getX());
    double k8=(this.a.getY() - this.b.getY())*(this.b.getX() - this.x.getX());
    double k9=(this.x.getY() - this.y.getY())*(this.b.getX() - this.x.getX());
    double k10=(this.b.getY() - this.x.getY())*(this.x.getX() - this.y.getX());
    if(k1-k2==0||k3-k4==0||k5-k6==0||k7-k8==0||k9-k10==0)
    {
        return 1;
    }
    else {
        return 0;
    }
}
/* 判断是否平行四边形 */
public boolean isParallelogram() {
    return true;
}
public double getAArea(Point a,Point b,Point c) {
    double k1 =  this.a.getX()*this.b.getY()+this.b.getX()*this.c.getY()+this.c.getX()*this.a.getY();
    double k2 =  this.b.getX()*this.a.getY()+this.c.getX()*this.b.getY()+this.a.getX()*this.c.getY();
    double l = 0.5*Math.sqrt(k1+k2);
    DecimalFormat d = new DecimalFormat("#.000");
    Double output = Double.valueOf(d.format(l));
    return output;

}
// 获取五边形的面积,此处采用海伦公式 
public double getArea(Point x,Point y,Point z,Point a,Point b) {
    double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
    double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
    double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
    double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX());
    double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX());
    double k6 = (this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY())+(this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX());
    double k7 = (this.x.getY() - this.a.getY())*(this.x.getY() - this.a.getY())+(this.x.getX() - this.a.getX())*(this.x.getX() - this.a.getX());
    double d1 = Math.sqrt(k1);
    double d2 = Math.sqrt(k2);
    double d3 = Math.sqrt(k3);
    double d4 = Math.sqrt(k4);
    double d5 = Math.sqrt(k5);
    double d6 = Math.sqrt(k6);
    double d7 = Math.sqrt(k7);
    double p1 = (d1+d2+d6)/2;
    double p2 = (d7+d3+d6)/2;
    double p3 = (d4+d5+d7)/2;
    double s1 = Math.sqrt(p1*(p1-d1)*(p1-d2)*(p1-d6));
    double s2 = Math.sqrt(p2*(p2-d7)*(p2-d3)*(p2-d6));
    double s3 = Math.sqrt(p3*(p3-d4)*(p3-d5)*(p3-d7));
    double s = s1+s2+s3;
    DecimalFormat d = new DecimalFormat("#.000");
    Double output = Double.valueOf(d.format(s));
    return output;
}

/* 获取五边形的周长 */

/* 判断是否菱形 */

/* 判断是否矩形 */

/* 判断是否正方形 */


/* 三个点的getter()和setter()方法 */
public Point getX() {
    return x;
}

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

public Point getY() {
    return y;
}

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

public Point getZ() {
    return z;
}

public void setZ(Point z) {
    this.z = z;
}
public Point getA() {
    return a;
}

public void setA(Point z) {
    this.z = a;
}
}
class PointInputError {

//判断从字符串中解析出的点的数量是否合格。
    public static void wrongNumberOfPoints(ArrayList ps, int num) {
        if (ps.size() != num) {
            System.out.println("wrong number of points");
            System.exit(0);
        }
    }
    //判断输入的字符串中点的坐标部分格式是否合格。若不符合,报错并退出程序
    public static void wrongPointFormat(String s) {
        if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }

    // 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一
    public static void wrongChoice(String s) {
        if (!s.matches("[1-6]:.+")) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}
class ParseInput {

/*
 * 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
 *         一个空InputData对象
 * 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
     * 输出:包含选项值和所有点的Point对象的InputData对象。
 */
public static void paseInput(String s, InputData d) {
    PointInputError.wrongChoice(s);        
    d.setChoice(getChoice(s));
    s = s.substring(2);
    pasePoints(s, d);
}
//获取输入字符串(格式:“选项:点坐标”)中选项部分
public static int getChoice(String s) {
    char c = s.charAt(0);
    return c-48;
}

/*
 * 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
 *         一个空InputData对象
     * 输出:所有点的Point对象
 */

public static void pasePoints(String s, InputData d) {
    String[] ss = s.split(" ");
    if (ss.length == 0)
        return;
    for (int i = 0; i < ss.length; i++) {
        d.addPoint(readPoint(ss[i]));
    }
}

/*
 * 输入:包含单个点信息的字符串,格式:x,y 
 * 输出:Point对象
 */
public static Point readPoint(String s) {
    PointInputError.wrongPointFormat(s);
    String[] ss = s.split(",");
    double x = Double.parseDouble(ss[0]);
    double y = Double.parseDouble(ss[1]);
    // System.out.println("match");
    return new Point(x, y);

}

}
复制代码

踩坑新得和改进建议

第五次作业中两道题目难度非常大,需要用到很多java库中的头文件,不断去网上搜索使用,同时看书学着有用的知识,经过长时间的编写,不断改正,还是有很多不会的地方,去网上学习别人的代码,看看他们写的过程不断改正自己的代码。

4.7-1 点与线(类设计)

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

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

      ```
          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:长度值
      ```
    
     

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

      设计类图如下图所示。
    
     

1641304523(1).jpg

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

  • 以下情况为无效作业
    • 无法运行
    • 设计不符合所给类图要求
    • 未通过任何测试点测试
    • 判定为抄袭

输入格式:

分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:

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:长度值
 

输入样例1:

在这里给出一组输入。例如:

5
9.4
12.3
84
Red
 

输出样例1:

在这里给出相应的输出。例如:

The line's color is:Red
The line's begin point's Coordinate is:
(5.00,9.40)
The line's end point's Coordinate is:
(12.30,84.00)
The line's length is:74.96
 

输入样例2:

在这里给出一组输入。例如:

80.2356
352.12
24.5
100
Black
 

输出样例2:

在这里给出相应的输出。例如:

Wrong Format

复制代码
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        Point point1=new Point(in.nextDouble(),in.nextDouble());
        Point point2=new Point(in.nextDouble(),in.nextDouble());
        if(!point1.vob&&!point2.vob) {
            Line line = new Line(point1,point2,in.next());
            line.display();
            }
    }

}
class Point
{
    public boolean vob=false;
    private double x,y;
    Point(double x,double y)
    {
        if(x<0||x>200||y<0||y>200) 
        {
            System.out.print("Wrong Format");
            vob=true;
            return ;
        }
        else
        {
        this.x = x;
        this.y = y;
        }
    }
    public double getX()
    {
        return this.x;
    }
    public void setX(double x)
    {
        if(x>0&&x<=200)
        {
            this.x=x;
        }
        else
        {
            System.out.print("Wrong Format");
        }
    }
    public double getY()
    {
        return this.y;
    }
    public void setY(double y)
    {
        if(y>0&&y<=200)
        {
            this.y=y;
        }
        else
        {
            System.out.print("Wrong Format");
        }
    }
    public void display()
    {
        String.format("%.2f %.2f",x,y);
    }
}
class Line
{
    private Point point1,point2;
    String color;
    Line(Point p1,Point p2,String color)
    {
        this.point1=p1;
        this.point2=p2;
        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 x = (this.point1.getX() - this.point2.getX());
        double y = (this.point1.getY() - this.point2.getY());
        double tag2=x*x+y*y;
        double tag=Math.sqrt(tag2);
        return tag;
    }
    public void display()
    {
        System.out.println("The line's color is:"+this.color);
        System.out.println("The line's begin point's Coordinate is:");
        String.format("(%.2f,%.2f) ",this.point1.getX(),this.point1.getY());
        System.out.println("The line's begin point's Coordinate is:");
        String.format("(%.2f,%.2f)",this.point2.getX(),this.point2.getY());
        String.format("The line's length is:%.2f",this.getDistance());
    }
}
复制代码

踩坑新得和改进建议

期中考试第一题主要考察的是对类的设计,题目难度不算很难,但是有两个点一直没出来错误,因为时间有限,就直接写了下一道题目

 

 

 5.7-2 点线面问题重构(继承与多态)

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

  • 对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
  • 再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
  • 在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:
          element = p1;//起点Point
          element.display();
          
          element = p2;//终点Point
          element.display();
          
          element = line;//线段
          element.display();
          
          element = plane;//面
          element.display();
    
     
    类结构如下图所示。

1641340607(1).jpg

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

  • 以下情况为无效作业
    • 无法运行
    • 设计不符合所给类图要求
    • 未通过任何测试点测试
    • 判定为抄袭

输入格式:

分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:

(x1,y1)
(x2,y2)
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:长度值
The Plane's color is:颜色值
 

输入样例1:

在这里给出一组输入。例如:

5
9.4
12.3
84
Red
 

输出样例1:

在这里给出相应的输出。例如:

(5.00,9.40)
(12.30,84.00)
The line's color is:Red
The line's begin point's Coordinate is:
(5.00,9.40)
The line's end point's Coordinate is:
(12.30,84.00)
The line's length is:74.96
The Plane's color is:Red
 

输入样例2:

在这里给出一组输入。例如:

5
9.4
12.3
845
Black
 

输出样例2:

在这里给出相应的输出。例如:

Wrong Format
复制代码
import java.util.Scanner;
    
    import java.util.Scanner;
    public class Main
    {
        public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            Point point1 = new Point(in.nextDouble(),in.nextDouble());
            Point point2 = new Point(in.nextDouble(),in.nextDouble());
            Line line = new Line(point1,point2,in.next());
            Plane col = new Plane(line.getColor());
            Element element;
            element = point1;
            element.display();
            element = point2;
            element.display();
            element = line;
            element.display();
            element = col;
            element.display();
        }
    }    
    abstract class Element
    {
        abstract void display();
    }        
    class Plane extends Element
    {
        private String color;
        Plane(String color)
        {
            this.color = color;
        }    
        public void display()
        {
            if(!Point.vob)
            System.out.println("The Plane's color is:"+this.color);
        }
    }
    
    
    class Point extends Element
    {
        private double x,y;
        static boolean vob = false;
        Point(double x,double y)
        {
            if(x<0||x>200||y<0||y>200)
            {
                System.out.print("Wrong Format");
                vob=true;
            }
            else
            {
                this.x = x;
                this.y = y;
            }    
        }
        
        public double getX() 
        {
            return this.x;
        }
        public void setX(double x)
        {
            if(x>0&&x<=200)
            {
                this.x=x;
            }
            else
                System.out.print("Wrong Format");
        }
        public double getY()
        {
            return this.y;
        }
        public void setY(double y) 
        {
            if(y>0&&y<=200)
            {
                this.y=y;
            }
            else
                System.out.print("Wrong Format");
        }
        
        public void display() 
        {
            if(!vob) 
            {
                String.format("(%.2f,%.2f)",x,y);
            }
        }
    }
    
    class Line extends Element{
        private Point point1,point2;
        private String color;
        Line(Point p1,Point p2,String color)
        {
            this.point1 = p1;
            this.point2 = p2;
            this.color = color;
        }    
        public Point getPoint1() 
        {
            return point1;
        }    
        public void setPoint1(Point p1) 
        {
            this.point1 = p1;
        }
        public Point getPoint2()
        {
            return point2;
        }
        public void setPoint2(Point p2)
        {
            this.point2 = p2;
        }
        public String getColor()
        {
            return this.color;
        }    
        public void setColor(String color) 
        {
            this.color = color;
        }
        public double getDistance()
        {
            double x = (this.point1.getX() - this.point2.getX());
            double y = (this.point1.getY() - this.point2.getY());
            double tag2=x*x+y*y;
            double tag=Math.sqrt(tag2);
            return tag;
        }
        public void display() 
        {
            if(!point1.vob&&!point2.vob)
            {
                System.out.printf("(%.2f,%.2f)",this.point1.getX(),this.point1.getY());
                System.out.println("");
                System.out.printf("(%.2f,%.2f)",this.point2.getX(),this.point2.getY());
                System.out.println("");
                System.out.println("The line's color is:"+this.color);
                System.out.println("The line's begin point's Coordinate is:");
                System.out.printf("(%.2f,%.2f)",this.point1.getX(),this.point1.getY());
                System.out.println("");
                System.out.println("The line's end point's Coordinate is:");
                System.out.printf("(%.2f,%.2f)",this.point2.getX(),this.point2.getY());
                System.out.println("");
                System.out.printf("The line's length is:%.2f",this.getDistance());
                System.out.println("");
            }
        }
    }
复制代码

踩坑新得和改进建议

期中考试第二道题目主要考察的是继承和多态,修改上一道题目的代码,子类对父类的继承,难度也适中,理解了继承就很好写出

 

 

 6.7-3 点线面问题再重构(容器类)

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

  • 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>
  • 增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
  • 在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
    • 1:向容器中增加Point对象
    • 2:向容器中增加Line对象
    • 3:向容器中增加Plane对象
    • 4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
    • 0:输入结束
    示例代码如下:
       choice = input.nextInt();
        while(choice != 0) {
            switch(choice) {
            case 1://insert Point object into list 
              ...
                break;
            case 2://insert Line object into list
                ...
                break;
            case 3://insert Plane object into list
                ...
                break;
            case 4://delete index - 1 object from list
                int index = input.nextInt();
                ...
            }
            choice = input.nextInt();
        }
    
     
    输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。
    类图如下所示:

classdiagram.jpg

  • 以下情况为无效作业
    • 无法运行
    • 设计不符合所给类图要求
    • 未通过任何测试点测试
    • 判定为抄袭

输入格式:

switch(choice) {
            case 1://insert Point object into list 
              输入“点”对象的x,y值
                break;
            case 2://insert Line object into list
                输入“线”对象两个端点的x,y值
                break;
            case 3://insert Plane object into list
                输入“面”对象的颜色值
                break;
            case 4://delete index - 1 object from list
                输入要删除的对象位置(从1开始)
                ...
            }
 

输出格式:

  • Point、Line、Plane的输出参考题目2
  • 删除对象时,若输入的index超出合法范围,程序自动忽略该操作

输入样例:

在这里给出一组输入。例如:

1
3.4
5.6
2
4.4
8.0
0.98
23.888
Red
3
Black
1
9.8
7.5
3
Green
4
3
0
 

输出样例:

在这里给出相应的输出。例如:

(3.40,5.60)
The line's color is:Red
The line's begin point's Coordinate is:
(4.40,8.00)
The line's end point's Coordinate is:
(0.98,23.89)
The line's length is:16.25
(9.80,7.50)
The Plane's color is:Green

复制代码
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        GeometryObject c = new GeometryObject();
        int choice = input.nextInt();
        while(choice != 0) {
            switch(choice) {
                case 1:double x = input.nextDouble();
                    double y = input.nextDouble();
                    if(x<0 || x> 200 || y<0 || y>200)
                    {
                        System.out.println("Wrong Format");
                        System.exit(0);
                    }
                    Point point = new Point(x, y);
                    c.add(point);
                    break;
                case 2:Point[] points = new Point[2];
                    for (int i = 0; i < 2; i++) {
                        double x1 = input.nextDouble();
                        double y1 = input.nextDouble();
                        if(x1<0 || x1> 200 || y1<0 || y1>200)
                        {
                            System.out.println("Wrong Format");
                            System.exit(0);
                        }
                        Point point1 = new Point(x1, y1);
                        points[i]=point1;
                    }
                    Line line = new Line(points[0],points[1],input.next());
                    c.add(line);
                    break;
                case 3:String color;
                    Plane plane = new Plane(input.next());
                    c.add(plane);
                    break;
                case 4:
                    int index = input.nextInt();
                    c.remove(index);
            }
            choice = input.nextInt();
        }
        for (Element i:c.getList()) {
            i.display();
        }
    }
}
class GeometryObject{
    private ArrayList<Element> list = new ArrayList<>();

    public GeometryObject() {

    }

    public void add(Element element) {
        list.add(element);
    }

    public void remove(int index) {
        if(index < 1 || index > list.size()) {
            return;
        }

        list.remove(index - 1);
    }

    public ArrayList<Element> getList(){
        return this.list;
    }
}
abstract class Element {
    public void display() {}
}
class Point extends Element{
    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;
    }

    @Override
    public void display() {
        System.out.println("("+ String.format("%.2f",x) + "," + String.format("%.2f",y) + ")");
    }
}
class Line extends Element{
    Point point1 = new Point();
    Point point2 = new Point();
    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 double getDistance(){
        return  Math.sqrt(Math.pow((point1.getX() - point2.getX()),2) + Math.pow((point1.getY() - point2.getY()),2));
    }
    /*  ```
      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:长度值
  ```
*/

    @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",this.getDistance()));
    }
}
class Plane extends Element {
    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);
    }
}
复制代码

踩坑新得和改进建议

本题相对上题增加了一些功能,使代码更加完善。此次题目主要考察对象的容器以及其遍历,需要新建一个GeometryObject容器类,其包含的属性为ArrayList,,增加ArrayList的增加元素方法和减少元素方法。题目难度较前两道要复杂一些,但也不是很难,只需加一个容器类,完成类图的要求即可。

总结

通过三次大作业,学会了java的进阶知识,学会了类的创建和使用,以及多态和继承的运用。两次多边形的题目,让我知道了自己的很多不足的地方,对于多态、继承还是有很多不足,抽象类的概念也不是特别的理解,需要多学习多运用这一块地方。

通过三次作业,我懂得了面向对象的程序设计要先进行设计,不能盲目的去写代码,进行设计并运用好类图,才能更好地完成代码,使自己的思路更清晰。

 
posted @   早该结束  阅读(31)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示