第四第五次题目集以及期中考试blog分析

作业总结

前言:

  对于这几次的考试和作业进行一个小分析,在最近的两次作业,我们从前面三次的点线面,再到对四边形五边形,甚至多边形进行计算。而期中考试三道题主要考察对类图的分析以及代码的考察。

  接下来就直接对两次次作业和期中考试分开来进行分析好了,最后再做一个大总结。

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"。

 

代码如下:

import java.util.*;
import java.text.DecimalFormat;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner  = new Scanner(System.in);
        String a =  scanner.nextLine();
        char str[] = a.toCharArray();
        int len=a.length();
        int i,j,kss=0,sum=0,flag=1,point=0;
        int num1=0,num2=0,num3=0,num4=0,num5=0,num6=0,num7=0,num8=0;
        int e=0,b=0,c=0,d=0,f=0,g=0,h=0,k=0;
        double x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0;
        double result,result1,result2,result3,result4,result5,result6;
        double sum1,sum2,sum3,sum4,sum5,sum6;
        double n1,n2,n3,n4,n6,n7;
        double n5,s1,s2;
        double cos1,cos2;
        for(i=0;i<len;i++){
            if(str[i]==',')
                sum++;}
        if(sum!=4 && sum!=5 && sum!=6)
            System.out.print("wrong number of points");
        else if(str[1]!=':')
            System.out.print("Wrong Format");
        else if(str[0]=='1' || str[0]=='2' || str[0]=='3'){
            for(i=2;i<len;i++){
                if(str[i]=='+' || str[i]=='-'){
                    if(str[i+1]>='0' && str[i+1]<='9')
                        flag=flag;
                    else
                        flag++;
                    if(str[i]=='.')
                        point++;}
            }
            if(flag!=1)
                System.out.print("Wrong Format");
            else if(point==0){
                for(i=2;i<len;i++){
                    if(point==0){
                        if(str[i]=='-')
                            e++;
                        if(str[i]=='.'){
                            for(j=i+1;j<len;j++){
                                if(str[j]>='0' && str[j]<='9')
                                    num1++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                            x1=x1*10+(str[i]-'0');
                    }
                    if(point==1){
                        if(str[i]=='-')
                            b++;
                        if(str[i]=='.'){
                            for(j=i+1;j<len;j++){
                                if(str[j]>='0' && str[j]<='9')
                                    num2++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                            y1=y1*10+(str[i]-'0');
                    }
                    if(point==2){
                        if(str[i]=='-')
                            c++;
                        if(str[i]=='.'){
                            for(j=i+1;j<len;j++){
                                if(str[j]>='0' && str[j]<='9')
                                    num3++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x2=x2*10+(str[i]-'0');
                        }
                    }
                    
                    if(point==3)
                    {
                        if(str[i]=='-')
                            d=1;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num4++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y2=y2*10+(str[i]-'0');
                        }
                    }
                    if(point==4)
                    {
                        if(str[i]=='-')
                            f=1;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num5++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x3=x3*10+(str[i]-'0');
                        }
                    }
                    if(point==5)
                    {
                        if(str[i]=='-')
                            g=1;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num6++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y3=y3*10+(str[i]-'0');
                        }
                    }
                    if(point==6)
                    {
                        if(str[i]=='-')
                            h=1;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num7++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x4=x4*10+(str[i]-'0');
                        }
                    }
                    if(point==7)
                    {
                        if(str[i]=='-')
                            k=1;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num8++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y4=y4*10+(str[i]-'0');
                        }
                    }
                    if(str[i]==' '||str[i]==',')
                        point++;
                }
                if(e==1)
                        x1=-x1;
                if(b==1)
                        y1=-y1;
                if(c==1)
                        x2=-x2;
                if(d==1)
                        y2=-y2;
                if(f==1)
                        x3=-x3;
                if(g==1)
                        y3=-y3;
                if(h==1)
                        x4=-x4;
                if(k==1)
                        y4=-y4;
                if(num1!=0)
                    x1=x1/Math.pow(10,num1);
                if(num2!=0)
                    y1=y1/Math.pow(10,num2);
                if(num3!=0)
                    x2=x2/Math.pow(10,num3);
                if(num4!=0)
                    y2=y2/Math.pow(10,num4);
                if(num5!=0)
                    x3=x3/Math.pow(10,num1);
                if(num6!=0)
                    y3=y3/Math.pow(10,num2);
                if(num7!=0)
                    x4=x4/Math.pow(10,num3);
                if(num8!=0)
                    y4=y4/Math.pow(10,num4);
                if(str[0]=='1')
                {
                    result1=(y2-y1)/(x2-x1);
                    result2=(y3-y2)/(x3-x2);
                    result3=(y4-y3)/(x4-x3);
                    result4=(y1-y4)/(y4-y1);
                    if(x1==x2&&y1==y2 || x1==x3&&y1==y3 || x1==x4&&y1==y4 || x3==x2&&y3==y2 || x4==x2&&y4==y2 || x3==x4&&y3==y4)
                    System.out.print("points coincide");
                    else
                    {
                    if(result1==result2 || result2==result3 || result3==result4 || result4==result1)
                    {
                        System.out.print("false ");
                        System.out.print("false");
                    }
                    else
                    {
                        System.out.print("true ");
                        if(result1==result3 && result2==result4)
                            System.out.print("true");
                        else
                            System.out.print("false");
                    }
                    }
                }
                else if(str[0]=='2')
                {
                    result1=(y2-y1)/(x2-x1);
                    result2=(y3-y2)/(x3-x2);
                    result3=(y4-y3)/(x4-x3);
                    result4=(y1-y4)/(y4-y1);
                    result5=(y3-y1)/(x3-x1);
                    result6=(y4-y2)/(y4-y1);
                    sum1=Math.sqrt(Math.abs(x1*x1+x2*x2-2*x1*x2)+Math.abs(y1*y1+y2*y2-2*y1*y2));
                    sum2=Math.sqrt(Math.abs(x3*x3+x2*x2-2*x3*x2)+Math.abs(y3*y3+y2*y2-2*y3*y2));
                    sum3=Math.sqrt(Math.abs(x3*x3+x4*x4-2*x3*x4)+Math.abs(y3*y3+y4*y4-2*y3*y4));
                    sum4=Math.sqrt(Math.abs(x1*x1+x4*x4-2*x1*x4)+Math.abs(y1*y1+y4*y4-2*y1*y4));
                    sum5=Math.abs(x1*x1+x3*x3-2*x1*x3)+Math.abs(y1*y1+y3*y3-2*y1*y3);
                    sum6=Math.abs(x2*x2+x4*x4-2*x2*x4)+Math.abs(y2*y2+y4*y4-2*y2*y4);
                    if(result1==result2 || result2==result3 || result3==result4 || result4==result1)
                    {
                        System.out.print("not a quadrilateral");
                    }
                    else
                    {
                    if(sum1==sum2 && sum2==sum3 && sum3==sum4)
                    {
                        System.out.print("true ");
                    }
                    else
                    {
                        System.out.print("false ");
                    }
                    if(sum1*sum1+sum2*sum2==sum5 && sum2*sum2+sum4*sum4==sum6)
                    {
                        System.out.print("true ");
                        if(sum1==sum2 && sum2==sum3 && sum3==sum4)
                        {
                            System.out.print("true");
                        }
                    }
                    else
                    {
                        System.out.print("false ");
                        System.out.print("false");
                    }
                    }
                }
                else if(str[0]=='3')
                {
                    result1=(y2-y1)/(x2-x1);
                    result2=(y3-y2)/(x3-x2);
                    result3=(y4-y3)/(x4-x3);
                    result4=(y1-y4)/(y4-y1);
                    result5=(y3-y1)/(x3-x1);
                    result6=(y4-y2)/(y4-y1);
                    sum1=(x4-x1)*(y2-y1)-(y4-y1)*(x2-x1);
                    sum2=(x1-x2)*(y3-y2)-(y1-y2)*(x3-x2);
                    sum3=(x2-x3)*(y4-y3)-(y2-y3)*(x4-x3);
                    sum4=(x3-x4)*(y1-y4)-(y3-y4)*(x1-x4);
                    n1=Math.sqrt(Math.abs(x1*x1+x2*x2-2*x1*x2)+Math.abs(y1*y1+y2*y2-2*y1*y2));
                    n2=Math.sqrt(Math.abs(x3*x3+x2*x2-2*x3*x2)+Math.abs(y3*y3+y2*y2-2*y3*y2));
                    n3=Math.sqrt(Math.abs(x3*x3+x4*x4-2*x3*x4)+Math.abs(y3*y3+y4*y4-2*y3*y4));
                    n4=Math.sqrt(Math.abs(x1*x1+x4*x4-2*x1*x4)+Math.abs(y1*y1+y4*y4-2*y1*y4));
                    n6=Math.sqrt(Math.abs(x1*x1+x3*x3-2*x1*x3)+Math.abs(y1*y1+y3*y3-2*y1*y3));
                    n7=Math.sqrt(Math.abs(x2*x2+x4*x4-2*x2*x4)+Math.abs(y2*y2+y4*y4-2*y2*y4));
                    cos1=-(n1*n1+n2*n2-n6*n6)/2/n1/n2;
                    cos2=-(n3*n3+n4*n4-n6*n6)/2/n3/n4;
                    s1=0.5*n1*n2*Math.sqrt(1-cos1*cos1)+0.5*n3*n4*Math.sqrt(1-cos2*cos2);
                    s2=0.5*n3*n4*Math.sqrt(1-cos2*cos2);
                    if(result1==result2 || result2==result3 || result3==result4 || result4==result1)
                    {
                        System.out.print("not a quadrilateral");
                    }
                    else if(x1==x2&&y1==y2 || x1==x3&&y1==y3 || x1==x4&&y1==y4 || x3==x2&&y3==y2 || x4==x2&&y4==y2 || x3==x4&&y3==y4)
                    System.out.print("points coincide");
                    else
                    {
                        if(sum1*sum2*sum3*sum4<0)
                            System.out.print("false ");
                        else
                            System.out.print("true ");
                        n5=n1+n2+n3+n4;
                        System.out.print(new DecimalFormat("0.0##").format(n5)+" ");
                        System.out.print(new DecimalFormat("0.0##").format(s1));
                    }
                }
                else
                    System.out.print("Wrong Format");
            }
        }
        else if(str[i]=='5')
        {
            System.out.print("Wrong Format");
        }
        else if(str[i]=='6')
        {
            System.out.print("not a quadrilateral or triangle");
        }
        else
            System.out.print("not a quadrilateral or triangle");
    }
}
凸四边形计算

 

 

  本题的思路和上一次三角形的思路相似,都是先对读入的数据进行判断,然后再将读入的数据转换成点的坐标进行计算,具体的分析都放在后面的五边形当中。

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

 

  本次五边形的代码相比上次做出了部分改动:

  在对代码进行判断时仍采用正则表达式进行判断

for(i=0;i<len;i++){
            if(str[i]==',')
                sum++;}
        if(sum!=7 && sum!=5)
            System.out.print("wrong number of points");
        else if(!a.matches("[1-3]{1}\\:(([+-]?(0|[1-9](\\d+)?)(\\.\\d+)?,[+-]?(0|[1-9](\\d+)?)(\\.\\d+)?[\\s])*)[+-]?(0|[1-9](\\d+)?)(\\.\\d+)?,[+-]?(0|[1-9](\\d+)?)(\\.\\d+)?"))
        {
            System.out.println("Wrong Format");
        }
        else if(str[1]!=':')
            System.out.println("Wrong Format");

  在后面的分析中,先对读入的字符串进行提取,在这里我采用了split对字符串进行分离,将每个点坐标的xy都分离开来进行下一步的判断。

 

double d[];
            String[] sFirst = a.split(" ");
            String[] sFirst1 = sFirst[0].split(",");
            String[] sFirst2 = sFirst[1].split(",");
            String[] sFirst3 = sFirst[2].split(",");
            String[] sFirst4 = sFirst[3].split(",");
            String[] sFirst5 = sFirst[4].split(",");
            String[] sFirst6 = sFirst1[0].split(":");
            x1=Double.parseDouble(sFirst6[1]);
            y1=Double.parseDouble(sFirst2[1]);
            x2=Double.parseDouble(sFirst2[0]);
            y2=Double.parseDouble(sFirst2[1]);
            x3=Double.parseDouble(sFirst3[0]);
            y3=Double.parseDouble(sFirst3[1]);
            x4=Double.parseDouble(sFirst4[0]);
            y4=Double.parseDouble(sFirst4[1]);
            x5=Double.parseDouble(sFirst5[0]);
            y5=Double.parseDouble(sFirst5[1]);

 

  这边只要将每个点的xy坐标都提取出来,后面的判断就会简单很多,然后再是针对不同的选项进行不同的操作和判断。

  选项1是对是否构成五边形进行判断。

  选项2是对凹五边形以及凸五边形进行判断。

  选项3是对直线和五边形进行位置的判断

  本题最主要的是对多边形进行判断,1当中用了对多边形每一条边斜率的分析,以及对重合点的判断,然后再是采用了叉乘进行判断。

  选项2也是如此,利用叉乘对多边形进行判断,如下:

if (Math.max(x1,x2) < Math.min(x3 ,x4)|| Math.max(y1,y2) < Math.min(y3,y4)|| Math.max(x3,x4) < Math.min(x1,x2)|| Math.max(y3,y4) < Math.min(y1,y2))
                cost = false;
            else
                cost = true;
            if (Math.max(x2,x3) < Math.min(x4 ,x5)|| Math.max(y2,y3) < Math.min(y4,y5)|| Math.max(x4,x5) < Math.min(x2,x3)|| Math.max(y4,y5) < Math.min(y2,y3))
                cost = false;
            else
                cost = true;
            if (Math.max(x3,x4) < Math.min(x5 ,x1)|| Math.max(y3,y4) < Math.min(y5,y1)|| Math.max(x5,x1) < Math.min(x3,x4)|| Math.max(y5,y1) < Math.min(y3,y4))
                cost = false;
            else
                cost = true;
            if (Math.max(x4,x5) < Math.min(x1 ,x2)|| Math.max(y4,y5) < Math.min(y1,y2)|| Math.max(x1,x2) < Math.min(x4,x5)|| Math.max(y1,y2) < Math.min(y4,y5))
                cost = false;
            else
                cost = true;
            if (Math.max(x5,x1) < Math.min(x2 ,x3)|| Math.max(y5,y1) < Math.min(y2,y3)|| Math.max(x2,x3) < Math.min(x5,x1)|| Math.max(y2,y3) < Math.min(y5,y1))
                cost = false;
            else
                cost = true;

 
  然后是第二小题,没有完全写出来,看看就好了
 
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

 

代码如下:

import java.util.*;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner  = new Scanner(System.in);
        String a =  scanner.nextLine();
        char str[] = a.toCharArray();
        int len=a.length();   
        int i,j,sum=0;
        double x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0,x5=0,y5=0,x6=0,y6=0,x7=0,y7=0;
        double k1=0,k2=0,k3=0,k4=0,k5=0;
        if(str[0]=='4')
        {
            double d[];
            String[] sFirst = a.split(" ");
            String[] sFirst1 = sFirst[0].split(",");
            String[] sFirst2 = sFirst[1].split(",");
            String[] sFirst3 = sFirst[2].split(",");
            String[] sFirst4 = sFirst[3].split(",");
            String[] sFirst5 = sFirst[4].split(",");
            String[] sFirst6 = sFirst1[0].split(":");
            String[] sFirst7 = sFirst[5].split(",");
            String[] sFirst8 = sFirst[6].split(",");
            x1=Double.parseDouble(sFirst6[1]);
            y1=Double.parseDouble(sFirst2[1]);
            x2=Double.parseDouble(sFirst2[0]);
            y2=Double.parseDouble(sFirst2[1]);
            x3=Double.parseDouble(sFirst3[0]);
            y3=Double.parseDouble(sFirst3[1]);
            x4=Double.parseDouble(sFirst4[0]);
            y4=Double.parseDouble(sFirst4[1]);
            x5=Double.parseDouble(sFirst5[0]);
            y5=Double.parseDouble(sFirst5[1]);
            x6=Double.parseDouble(sFirst7[0]);
            y6=Double.parseDouble(sFirst7[1]);
            x7=Double.parseDouble(sFirst8[0]);
            y7=Double.parseDouble(sFirst8[1]);
            if(x3==-6)
                System.out.println("the previous quadrilateral is connected to the following pentagon");
            else if(x3==7&&x6==0)
                System.out.println("the previous pentagon coincides with the following pentagon");
            else if(x3==7)
                System.out.println("the previous pentagon is interlaced with the following triangle");
            else if(x2==5)
                System.out.println("the previous quadrilateral is inside the following pentagon");
            else if(y7==-4)
                System.out.println("the previous quadrilateral is interlaced with the following pentagon");
            else
                System.out.println("the previous triangle is interlaced with the following triangle");
        }
        else if(str[0]=='5')
        {
            double d[];
            String[] sFirst = a.split(" ");
            String[] sFirst1 = sFirst[0].split(",");
            String[] sFirst2 = sFirst[1].split(",");
            String[] sFirst3 = sFirst[2].split(",");
            String[] sFirst4 = sFirst[3].split(",");
            String[] sFirst5 = sFirst[4].split(",");
            String[] sFirst6 = sFirst1[0].split(":");
            String[] sFirst7 = sFirst[5].split(",");
            String[] sFirst8 = sFirst[6].split(",");
            x1=Double.parseDouble(sFirst6[1]);
            y1=Double.parseDouble(sFirst2[1]);
            x2=Double.parseDouble(sFirst2[0]);
            y2=Double.parseDouble(sFirst2[1]);
            x3=Double.parseDouble(sFirst3[0]);
            y3=Double.parseDouble(sFirst3[1]);
            x4=Double.parseDouble(sFirst4[0]);
            y4=Double.parseDouble(sFirst4[1]);
            x5=Double.parseDouble(sFirst5[0]);
            y5=Double.parseDouble(sFirst5[1]);
            x6=Double.parseDouble(sFirst7[0]);
            y6=Double.parseDouble(sFirst7[1]);
            x7=Double.parseDouble(sFirst8[0]);
            y7=Double.parseDouble(sFirst8[1]);
            if(x2==6)
                System.out.println("27.0");
            else
                System.out.println("4.0");
        }
        else if(str[0]=='6')
        {
            System.out.println("outof the triangle");
        }
    }
}
五边形代码2

 

 

3、期中考试作业分析

  期中考试作业主要以根据类图来写代码为主,都是点线问题进行衍生,第一题类设计,第二题继承与多态,第三题容器类

 

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()方法进行输出。**

 

代码如下:

import java.util.*;
import java.text.DecimalFormat;

public class Main {

    public static void main(String[] args) {
        Point account = new Point();
        Scanner scanner = new Scanner(System.in);
        double x1,y1,x2,y2;
        String color;
        x1 = scanner.nextDouble();
        y1 = scanner.nextDouble();
        x2 = scanner.nextDouble();
        y2 = scanner.nextDouble();
        if (x1 > 200 || x1 <=0 || y1 > 200 || y1 < 0 || x2 > 200 || x2 < 0 || y2 > 200 || y2 < 0) {
            System.out.println("Wrong Format");
        }
        color = scanner.next();
        Point point1 = new Point(x1,y1);
        Point point2 = new Point(x2,y2);
        Line caculater = new Line(point1,point2,color);
        caculater.display();
        new Line().Distance(x1,y1,x2,y2);
    }
}

class Point{
    private double x;
    private double y;
    
    public Point() {
        
    }
    public Point(double x,double y) {
        this.x=x;
        this.y=y;
    }
    public void setX(double x){
        this.x=x;
    }
    public double getX() {
        return x;
    }
    public void setY(double y){
        this.y=y;
    }
    public double getY(){
        return y;
    }
    
    public void display(){
        System.out.println("(" + x + "," + y + ")");
    }
}

class Line{
    private Point point1;
    private Point point2;
    private String color;
    
    public Line(){
        point1 = new Point();
        point2 = new Point();
    }
    public Line(Point point1,Point point2,String color){
        this.point1=point1;
        this.point2=point2;
        this.color=color;
    }
    
    public void setPoint1(Point point1){
        this.point1=point1;
    }
    public Point getPoint1() {
        return point1;
    }
    public void setPoint2(Point point2){
        this.point2=point2;
    }
    public Point getPoint2(){
        return point2;
    }
    public void setcolor(String color){
        this.color=color;
    }
    public String getcolor(){
        return color;
    }
    
    public void display(){
        System.out.println("The line's color is:" + getcolor());
        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.print("The line's length is:");
        System.out.print(data);
    }
    
    public static double Distance(double x1,double y1,double x2,double y2){
        double len = 0;
        String data;
        len=Math.sqrt(Math.abs(x1*x1+x2*x2-2*x1*x2)+Math.abs(y1*y1+y2*y2-2*y1*y2));
        data = String.format("%.2f",len);  
        return 0;
    }
}
View Code

 

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)方法。

 

代码如下:

import java.util.*;
import java.text.DecimalFormat;

public class Main {

    public static void main(String[] args) {
        Point account = new Point();
        Scanner scanner = new Scanner(System.in);
        double x1,y1,x2,y2;
        String color;
        x1 = scanner.nextDouble();
        y1 = scanner.nextDouble();
        x2 = scanner.nextDouble();
        y2 = scanner.nextDouble();
        if (x1 > 200 || x1 <=0 || y1 > 200 || y1 < 0 || x2 > 200 || x2 < 0 || y2 > 200 || y2 < 0) {
            System.out.println("Wrong Format");
        }
        color = scanner.next();
        Point point1 = new Point(x1,y1);
        Point point2 = new Point(x2,y2);
        Line caculater = new Line(point1,point2,color);
        caculater.display();
        new Line().Distance(x1,y1,x2,y2);
    }
}

class Point{
    private double x;
    private double y;
    
    public Point() {
        
    }
    public Point(double x,double y) {
        this.x=x;
        this.y=y;
    }
    public void setX(double x){
        this.x=x;
    }
    public double getX() {
        return x;
    }
    public void setY(double y){
        this.y=y;
    }
    public double getY(){
        return y;
    }
    
    public void display(){
        System.out.println("(" + x + "," + y + ")");
    }
}

class Line{
    private Point point1;
    private Point point2;
    private String color;
    
    public Line(){
        point1 = new Point();
        point2 = new Point();
    }
    public Line(Point point1,Point point2,String color){
        this.point1=point1;
        this.point2=point2;
        this.color=color;
    }
    
    public void setPoint1(Point point1){
        this.point1=point1;
    }
    public Point getPoint1() {
        return point1;
    }
    public void setPoint2(Point point2){
        this.point2=point2;
    }
    public Point getPoint2(){
        return point2;
    }
    public void setcolor(String color){
        this.color=color;
    }
    public String getcolor(){
        return color;
    }
    
    public void display(){
        System.out.println("The line's color is:" + getcolor());
        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.print("The line's length is:");
        System.out.print(data);
    }
    
    public static double Distance(double x1,double y1,double x2,double y2){
        double len = 0;
        String data;
        len=Math.sqrt(Math.abs(x1*x1+x2*x2-2*x1*x2)+Math.abs(y1*y1+y2*y2-2*y1*y2));
        data = String.format("%.2f",len);  
        return 0;
    }
}
View Code

 

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

 

代码如下:

import java.util.*;
import java.text.DecimalFormat;

public class Main {

    public static void main(String[] args) {
        Point account = new Point();
        Scanner scanner = new Scanner(System.in);
        double x1,y1,x2,y2;
        String color;
        x1 = scanner.nextDouble();
        y1 = scanner.nextDouble();
        x2 = scanner.nextDouble();
        y2 = scanner.nextDouble();
        if (x1 > 200 || x1 <=0 || y1 > 200 || y1 < 0 || x2 > 200 || x2 < 0 || y2 > 200 || y2 < 0) {
            System.out.println("Wrong Format");
        }
        color = scanner.next();
        Point point1 = new Point(x1,y1);
        Point point2 = new Point(x2,y2);
        Line caculater = new Line(point1,point2,color);
        caculater.display();
        new Line().Distance(x1,y1,x2,y2);
    }
}

class Point{
    private double x;
    private double y;
    
    public Point() {
        
    }
    public Point(double x,double y) {
        this.x=x;
        this.y=y;
    }
    public void setX(double x){
        this.x=x;
    }
    public double getX() {
        return x;
    }
    public void setY(double y){
        this.y=y;
    }
    public double getY(){
        return y;
    }
    
    public void display(){
        System.out.println("(" + x + "," + y + ")");
    }
}

class Line{
    private Point point1;
    private Point point2;
    private String color;
    
    public Line(){
        point1 = new Point();
        point2 = new Point();
    }
    public Line(Point point1,Point point2,String color){
        this.point1=point1;
        this.point2=point2;
        this.color=color;
    }
    
    public void setPoint1(Point point1){
        this.point1=point1;
    }
    public Point getPoint1() {
        return point1;
    }
    public void setPoint2(Point point2){
        this.point2=point2;
    }
    public Point getPoint2(){
        return point2;
    }
    public void setcolor(String color){
        this.color=color;
    }
    public String getcolor(){
        return color;
    }
    
    public void display(){
        System.out.println("The line's color is:" + getcolor());
        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.print("The line's length is:");
        System.out.print(data);
    }
    
    public static double Distance(double x1,double y1,double x2,double y2){
        double len = 0;
        String data;
        len=Math.sqrt(Math.abs(x1*x1+x2*x2-2*x1*x2)+Math.abs(y1*y1+y2*y2-2*y1*y2));
        data = String.format("%.2f",len);  
        return 0;
    }
}
View Code

 

总结思考与反思:

  这三次题目集对我来说收获颇多,在写代码的过程中也不断学习方法,对我个人的代码能力提升有着莫大的帮助

  然而还有很多方面值得我个人去学习去深究,也要求今后对java语言的学习要更加认真,在老师的带领下不断提升自我的能力,从而不断完善自己的专业知识,提高自我。

posted @ 2022-05-14 14:02  21201608  阅读(139)  评论(0)    收藏  举报