ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PTA题目集/期中考试总结性Blog

2022-05-14 19:34:36  阅读:158  来源: 互联网

标签:getX 总结性 double PTA Blog x2 x3 x1 Math


一、前言

    首先对这几次题目的知识点、题量、难度进行总结。PTA第四次作业,7-1考察的是对于正则表达式的使用,题目没有很难,只需要了解一下正则表达式的使用就行了。7-3考察的是编写一个银行业务类BankBusiness,然后实现一些简单的功能。7-2是这次作业中难的题目,是点线形系列4-凸四边形的计算,考察的是我们对于类的更加深入的使用。PTA第五次作业,在三角形,四边形之后的五边形(点线形系列5-凸五边形的计算)。这道题目相较于三角形和四边形又更难了,不仅仅考察的是我们对编程语言的使用,还有对于数学知识的了解。期中考试,7-1 点与线7-2 点线面问题重构7-3 点线面问题再重构。分别考察了类的设计,继承与多态,容器的知识点,难度是层层递进的,对我来说也是有点难度的。

二、设计分析

    这里就对于第四次作业的7-2、第五次作业以及期中考试的题目进行分析。这几道题目对于我来说都是挺有难度的,还有部分题目还不会写。

1)、第四次作业的7-2

 

这道题是关于四边形的计算,其中要求如下:

输入格式输出格式如下

 

还是和之前的三角形的差不多。

首先运用正则表达式的方法对输入的内容进行匹配,将里面存在的数字通过数组保存下来

Scanner input = new Scanner(System.in);
String arr = input.nextLine();
String[] str1Array = arr.split("\\:|\\,|\\ ");
double[] num = new double[str1Array.length];

然后通过一个函数来进行选择

public static void panduan(double num[])
    {
        double x1=num[1];
        double y1=num[2];
        double x2=num[3];
        double y2=num[4];
        double x3=num[5];
        double y3=num[6];
        double x4=num[7];
        double y4=num[8];


        if(num[0]==1)
        {
            op1(x1,y1,x2,y2,x3,y3,x4,y4);
        }
        else if(num[0]==2)
        {
            op2(x1,y1,x2,y2,x3,y3,x4,y4);
        }
        else if(num[0]==3) 
        {
            op3(x1,y1,x2,y2,x3,y3,x4,y4);
        }
}

 

首先是选项一:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。

四边形的话,只需要四个点不重合其中三个点不共线就能构成四边形,再通过数学知识中的两对边平行构成平行四边形来判断是否是平行四边形。

 

public static void op1(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) 
    {
         double kab= (y1-y2)/(x1-x2);
         double kdc= (y4-y3)/(x4-x3);
         double kad= (y1-y4)/(x1-x4);
         double kbc= (y2-y3)/(x2-x3);
        if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x2==x3&&y2==y3)||(x3==x4&&y3==y4)||(x2==x4&&y2==y4))
        {
            System.out.println("points coincide");
        }
        else 
        {
             double k1 = (y1-y2)/(x1-x2);
             double k2 = (y2-y3)/(x2-x3);
             double p = 0;
             double m = 0;
             p = jiaodian1(x1,y1,x2,y2,x3,y3,x4,y4);
             m = jiaodian2(x1,y1,x2,y2,x3,y3,x4,y4);
             if((k1 == k2)||(x1==x2&&x2==x3)||(x1==x2&&x2==x4)||(x2==x3&&x3==x4)||p == 1)
             {
                 System.out.println("false false");
             }
             else 
             {
                 if(x1==x4&&x2==x3&&kbc==kad)
                 {
                     System.out.println("true true");
                 }
                 else {
                 if((kab == kdc)&&(kad == kbc))
                 {
                     System.out.println("true true");
                 }
                 else 
                 {
                     System.out.println("true false");
                 }
                 } 
             }
            
        }
    }

 

选项二:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral",这里呢通过数学知识,菱形--四条边全部相等,矩形--邻边互相垂直,正方形--同时满足菱形和矩形的条件。

首先是菱形的(这里直接用boolean会好一点,一开始没想到)

public static int linxing (double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
    {
        int sq = 0 ;
        double lab = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
        double lbc = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2)); 
        double lcd = Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2)); 
        double lda = Math.sqrt(Math.pow(x1-x4,2)+Math.pow(y1-y4,2)); 
        if(lab==lbc&&lab==lcd&&lcd==lda)
        {
            sq = 1;
        }
        return sq ;
    }

 

 

然后是矩形(这里直接用boolean会好一点,一开始没想到)

 

public static int juxing (double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
    {
        int sq = 0;
        double kab= (y2-y1)/(x2-x1);
        double kbc= (y3-y2)/(x3-x2);
        double kcd= (y4-y3)/(x4-x3);
        double kda= (y1-y4)/(y1-x4);
        if(((x1==x4 && x2==x3 )&&(y1==y2 && y3==y4))||((x1==x2 && x4==x3 )&&(y1==y4 && y3==y2)))
        {
            sq = 1;
        }
        else 
        {
              if(kab*kbc==-1&&kcd*kda==-1)
              {
                  sq = 1;
              }
        }
        return sq ;
    }

 

最后是正方形:

public static int fangxing(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
    {
        int m1 = linxing(x1,y1,x2,y2,x3,y3,x4,y4);
        int m2 = juxing(x1,y1,x2,y2,x3,y3,x4,y4);
        int sq = 0;
        if(m1 == 1 && m2 == 1)
        {
            sq = 1;
        }
        return sq ;
    }

这样第二个选项就基本满足了,但是最后还是没有通过所有的测试点,可能是判断的方式不够严谨

 

 选项三:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"

 

 

public static void op3(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) 
    {
        int p = jiaodian1(x1,y1,x2,y2,x3,y3,x4,y4);
        double k1 = (y1-y2)/(x1-x2);
        double k2 = (y2-y3)/(x2-x3);
        double lab = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
        double lbc = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2)); 
        double lcd = Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2)); 
        double lda = Math.sqrt(Math.pow(x1-x4,2)+Math.pow(y1-y4,2)); 
        double lac = Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2)); 
        double t1 = (x4-x1)*(y2-y1)-(y4-y1)*(x2-x1);
        double t2 = (x1-x2)*(y3-y2)-(y1-y2)*(x3-x2);
        double t3 = (x2-x3)*(y4-y3)-(y2-y3)*(x4-x3);
        double t4 = (x3-x4)*(y1-y4)-(y3-y4)*(x1-x4);
        if((k1 == k2)||(x1==x2&&x2==x3)||(x1==x2&&x2==x4)||(x2==x3&&x3==x4)||p == 1)
        {
                System.out.println("not a quadrilateral");
        }
        else 
        {
            
            
            if(t1*t2*t3*t4 < 0)
            {
                System.out.print("false ");
            }
            else 
            {
                System.out.print("true ");
            }
            double C = lab + lbc +lcd + lda ;
            double c = C*1000 % 10;
            //System.out.println(C);
            //System.out.println(c);
            if(c==0)
            {
                System.out.print(C+" ");
            }
            else 
            {
                System.out.print(String.format("%.3f ", C));
            }
        }
        //S=%√[p(p-a)(p-b)(p-c)] 
        double S = 0 ;
        double s1,s2;
        double p1 = (lab+lbc+lac)/2;
        double p2 = (lcd+lda+lac)/2;
        s1 = Math.sqrt(p1*(p1-lab)*(p1-lbc)*(p1-lac));
        s2 = Math.sqrt(p2*(p2-lcd)*(p2-lda)*(p2-lac));
        S = s1 + s2; 
       // double s = S*1000 % 10;
        //if(s==0)
        //{
            //System.out.print(S);
        //}
        //else 
        //{
            System.out.print(String.format("%.1f", S));
        //}
        
    }

面积是通过将四边形拆分成两个三角形,然后再运用海伦公式(//S=%√[p(p-a)(p-b)(p-c)] )进行面积的计算,最后再相加即可。

 

 最后还是有一个测试点没有通过,不知道问题出现在哪里了。然后剩下的45两个选项没有想出怎么写,暂时还不会写。

全部代码:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        String arr = input.nextLine();
        String[] str1Array = arr.split("\\:|\\,|\\ ");
        double[] num = new double[str1Array.length];
        int m = 0;
        for (int i = 0; i < str1Array.length; i++) 
        {
            num[i] = Double.parseDouble(str1Array[i]);
            //System.out.print(num[i]+" ");
        }
        for(int k = 0; k < arr.length();k++)
        {
            if(arr.charAt(k) == ',')
            m++;
        }
        double j = num[0];
        int n = 0;
        //System.out.println(m);
        int u =0;
        if(arr.charAt(1) != ':')
        {
            System.out.print("Wrong Format");
        }
        
        else if(arr.charAt(u) == '+'&&arr.charAt(u+1) == '+')
        {
            System.out.print("Wrong Format");
            u++;
        }
        else
        {
        if(douhao(j,m) == 0)
        {
            panduan(num);
        }
        }
    }
    public static int douhao(double j,int m)
    {
        int n = 0;
        if(j == 1 && m != 4)
        {
            System.out.println("wrong number of points");
            n = 1;
        }
        else if(j == 2 && m != 4 )
        {
            System.out.println("wrong number of points");
             n = 1;
             
        }
        else if(j == 3 && m != 4 )
        {
            System.out.println("wrong number of points");
             n = 1;
             
        }
        else if(j == 4 && m != 6 )
        {
            System.out.println("wrong number of points");
             n = 1;
        
        }
        else if(j == 5 && m != 5 )
        {
            System.out.println("wrong number of points");
             n = 1;
        
        }
        return n;
    }
    public static void panduan(double num[])
    {
        double x1=num[1];
        double y1=num[2];
        double x2=num[3];
        double y2=num[4];
        double x3=num[5];
        double y3=num[6];
        double x4=num[7];
        double y4=num[8];
        //double x5=num[9];
        //double y5=num[10];
        //double x6=num[11];
        //double y6=num[12];

        if(num[0]==1)
        {
            op1(x1,y1,x2,y2,x3,y3,x4,y4);
        }
        else if(num[0]==2)
        {
            op2(x1,y1,x2,y2,x3,y3,x4,y4);
        }
        else if(num[0]==3) 
        {
            op3(x1,y1,x2,y2,x3,y3,x4,y4);
        }
        else if(num[0]==4)
        {
            op4();
        }
        else if (num[0]==5)
        {
            op5();
        }
    }
    public static void op1(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) 
    {
         double kab= (y1-y2)/(x1-x2);
         double kdc= (y4-y3)/(x4-x3);
         double kad= (y1-y4)/(x1-x4);
         double kbc= (y2-y3)/(x2-x3);
        if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x2==x3&&y2==y3)||(x3==x4&&y3==y4)||(x2==x4&&y2==y4))
        {
            System.out.println("points coincide");
        }
        else 
        {
             double k1 = (y1-y2)/(x1-x2);
             double k2 = (y2-y3)/(x2-x3);
             double p = 0;
             double m = 0;
             p = jiaodian1(x1,y1,x2,y2,x3,y3,x4,y4);
             m = jiaodian2(x1,y1,x2,y2,x3,y3,x4,y4);
             if((k1 == k2)||(x1==x2&&x2==x3)||(x1==x2&&x2==x4)||(x2==x3&&x3==x4)||p == 1)
             {
                 System.out.println("false false");
             }
             else 
             {
                 if(x1==x4&&x2==x3&&kbc==kad)
                 {
                     System.out.println("true true");
                 }
                 else {
                 if((kab == kdc)&&(kad == kbc))
                 {
                     System.out.println("true true");
                 }
                 else 
                 {
                     System.out.println("true false");
                 }
                 } 
             }
            
        }
    }
    public  static int jiaodian1(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
    {
       int p = 0;
       double a1=y2-y1;
         double b1=(x2-x1);
         double c1=x1*y2-x2*y1;
         double a2=y4-y3;
         double b2=(x4-x3);
         double c2=x3*y4-x4*y3;
         double d=a1*b2-a2*b1;
         //System.out.print((b2*c1-b1*c2)/d + "," + (a2*c1-a1*c2)/d);   
         if(((b2*c1-b1*c2)/d) > x1 && ((b2*c1-b1*c2)/d) < x2 && ((a2*c1-a1*c2)/d) < y3 && ((a2*c1-a1*c2)/d) > y4) 
         {
             p = 1;   
         }
       //System.out.println(p);
        return p;
    }
    public  static int jiaodian2(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
    {
        int p = 0;
           double a1=y4-y1;
             double b1=(x4-x1);
             double c1=x1*y4-x4*y1;
             double a2=y2-y3;
             double b2=(x2-x3);
             double c2=x3*y2-x2*y3;
             double d=a1*b2-a2*b1;
             //System.out.print((b2*c1-b1*c2)/d + "," + (a2*c1-a1*c2)/d);   
             if(((b2*c1-b1*c2)/d) > x1 && ((b2*c1-b1*c2)/d) < x4 && ((a2*c1-a1*c2)/d) < y3 && ((a2*c1-a1*c2)/d) > y2) 
             {
                 p = 1;   
             }
        return p;
    }
    public static void op2(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) 
    {
         int p = jiaodian1(x1,y1,x2,y2,x3,y3,x4,y4);
         double k1 = (y1-y2)/(x1-x2);
         double k2 = (y2-y3)/(x2-x3);
         if((k1 == k2)||(x1==x2&&x2==x3)||(x1==x2&&x2==x4)||(x2==x3&&x3==x4)||p == 1)
         {
                 System.out.println("not a quadrilateral");
         }
        else 
        {
            int sq1 = linxing(x1,y1,x2,y2,x3,y3,x4,y4);
            if(sq1 == 1)
            {
                System.out.print("true ");
            }
            else 
            {
                System.out.print("false ");
            }
            int sq2 = juxing(x1,y1,x2,y2,x3,y3,x4,y4);
            if(sq2 == 1)
            {
                System.out.print("true ");
            }
            else 
            {
                System.out.print("fasle ");
            }
            int sq3 = fangxing(x1,y1,x2,y2,x3,y3,x4,y4);
            if(sq3 == 1)
            {
                System.out.print("true");
            }
            else 
            {
                System.out.print("false");
            }
        }
    }
    public static int fangxing(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
    {
        int m1 = linxing(x1,y1,x2,y2,x3,y3,x4,y4);
        int m2 = juxing(x1,y1,x2,y2,x3,y3,x4,y4);
        int sq = 0;
        if(m1 == 1 && m2 == 1)
        {
            sq = 1;
        }
        return sq ;
    }
    public static int linxing (double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
    {
        int sq = 0 ;
        double lab = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
        double lbc = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2)); 
        double lcd = Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2)); 
        double lda = Math.sqrt(Math.pow(x1-x4,2)+Math.pow(y1-y4,2)); 
        if(lab==lbc&&lab==lcd&&lcd==lda)
        {
            sq = 1;
        }
        return sq ;
    }
    public static int juxing (double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
    {
        int sq = 0;
        double kab= (y2-y1)/(x2-x1);
        double kbc= (y3-y2)/(x3-x2);
        double kcd= (y4-y3)/(x4-x3);
        double kda= (y1-y4)/(y1-x4);
        if(((x1==x4 && x2==x3 )&&(y1==y2 && y3==y4))||((x1==x2 && x4==x3 )&&(y1==y4 && y3==y2)))
        {
            sq = 1;
        }
        else 
        {
              if(kab*kbc==-1&&kcd*kda==-1)
              {
                  sq = 1;
              }
        }
        return sq ;
    }
    public static void op3(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) 
    {
        int p = jiaodian1(x1,y1,x2,y2,x3,y3,x4,y4);
        double k1 = (y1-y2)/(x1-x2);
        double k2 = (y2-y3)/(x2-x3);
        double lab = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
        double lbc = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2)); 
        double lcd = Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2)); 
        double lda = Math.sqrt(Math.pow(x1-x4,2)+Math.pow(y1-y4,2)); 
        double lac = Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2)); 
        double t1 = (x4-x1)*(y2-y1)-(y4-y1)*(x2-x1);
        double t2 = (x1-x2)*(y3-y2)-(y1-y2)*(x3-x2);
        double t3 = (x2-x3)*(y4-y3)-(y2-y3)*(x4-x3);
        double t4 = (x3-x4)*(y1-y4)-(y3-y4)*(x1-x4);
        if((k1 == k2)||(x1==x2&&x2==x3)||(x1==x2&&x2==x4)||(x2==x3&&x3==x4)||p == 1)
        {
                System.out.println("not a quadrilateral");
        }
        else 
        {
            
            
            if(t1*t2*t3*t4 < 0)
            {
                System.out.print("false ");
            }
            else 
            {
                System.out.print("true ");
            }
            double C = lab + lbc +lcd + lda ;
            double c = C*1000 % 10;
            //System.out.println(C);
            //System.out.println(c);
            if(c==0)
            {
                System.out.print(C+" ");
            }
            else 
            {
                System.out.print(String.format("%.3f ", C));
            }
        }
        //S=%√[p(p-a)(p-b)(p-c)] 
        double S = 0 ;
        double s1,s2;
        double p1 = (lab+lbc+lac)/2;
        double p2 = (lcd+lda+lac)/2;
        s1 = Math.sqrt(p1*(p1-lab)*(p1-lbc)*(p1-lac));
        s2 = Math.sqrt(p2*(p2-lcd)*(p2-lda)*(p2-lac));
        S = s1 + s2; 
       // double s = S*1000 % 10;
        //if(s==0)
        //{
            //System.out.print(S);
        //}
        //else 
        //{
            System.out.print(String.format("%.1f", S));
        //}
        
    }
    public static void op4() 
    {
        System.out.println("not a quadrilateral or triangle");
    }
    public static void op5() 
    {
        System.out.println("in the triangle");
    }
}


 

 
2)第五次作业点线形系列5-凸五边形的计算

 


 

 

 输入输出要求:

 

 

选项1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。

判断五边形就只需要判断五个点不共线同时不重合就行了,和四边形的差不多。

 

// 输入五个点坐标,判断是否是五边形,判断结果为true/false。
    public static void handle1(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 5);
        Triangle t = new Triangle(ps.get(0), ps.get(1), ps.get(2),ps.get(3), ps.get(4));
        System.out.println(t.ispentagon());
        System.exit(0);
    }

 

然后通过调用函数实现判断的要求

/* 判断x\y\z\m\n五个点的坐标是否能构成一个五角形 */
    public boolean ispentagon() {
        double k1,k2,k3,k4,k5;
        if(this.x.getX()==this.y.getX()&&this.y.getX()==this.z.getX()||//ABC
           this.x.getX()==this.y.getX()&&this.y.getX()==this.m.getX()||//ABD
           this.x.getX()==this.y.getX()&&this.y.getX()==this.n.getX()||//ABE
           this.x.getX()==this.z.getX()&&this.z.getX()==this.m.getX()||//ACD
           this.x.getX()==this.z.getX()&&this.z.getX()==this.n.getX()||//ACE
           this.x.getX()==this.m.getX()&&this.m.getX()==this.n.getX()||//ADE
           this.y.getX()==this.z.getX()&&this.z.getX()==this.m.getX()||//BCD
           this.y.getX()==this.z.getX()&&this.z.getX()==this.n.getX()||//BCE
           this.y.getX()==this.m.getX()&&this.m.getX()==this.n.getX()||//BDE
           this.z.getX()==this.m.getX()&&this.m.getX()==this.n.getX()||//CDE
           x.equals(y)||x.equals(z)||x.equals(m)||x.equals(n)||y.equals(z)||y.equals(m)||y.equals(n)||z.equals(m)||z.equals(n)||m.equals(n)
                )           
           
        {
            return false;
        }
        else
        {
            k1 = (this.x.getY()-this.y.getY())/(this.x.getX()-this.y.getX());
            k2 = (this.y.getY()-this.z.getY())/(this.y.getX()-this.z.getX());
            k3 = (this.z.getY()-this.m.getY())/(this.z.getX()-this.m.getX());
            k4 = (this.m.getY()-this.n.getY())/(this.m.getX()-this.n.getX());
            k5 = (this.n.getY()-this.x.getY())/(this.n.getX()-this.x.getX());
            if(k1 == k2 || k2 == k3 || k3 == k4 || k4 == k5 || k5 == k1 )
                return false;
            else
                return true;
        }
    }

选项二:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出"not a pentagon",凹凸五边形通过判断任意两条邻边的叉乘是否大于0,如果全部大于零那么就是凸五边形。面积通过将五边形切割成三个三角形,再运用海伦公式来计算面积求和。

//输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),
    //如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 
    //若五个点坐标无法构成五边形,输出"not a pentagon"
    public static void handle2(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 5);
        Triangle t = new Triangle(ps.get(0), ps.get(1), ps.get(2),ps.get(3), ps.get(4));
        boolean flag = true;
        if(t.ispentagon() == false)
        {
            System.out.println("not a pentagon");
        }
        else 
        {
            
            boolean flag2 = t.isconcavePentagon();
            if(flag2 == true) {
                System.out.print("true ");
                System.out.print(OutFormat.doubleFormat(t.perimeter())+" ");
                System.out.print(OutFormat.doubleFormat(t.area()));
                       
                }
            else 
                System.out.println("false");
        }
    }

判断凹凸性:

//判断凹凸性
    public boolean isconcavePentagon() {
        Line cross = new Line();
        double C1 = Line.cross(this.x.getX(),this.x.getY(),this.y.getX(),this.y.getY(),this.z.getX(),this.z.getY());
        double C2 = Line.cross(this.y.getX(),this.y.getY(),this.z.getX(),this.z.getY(),this.m.getX(),this.m.getY()); 
        double C3 = Line.cross(this.z.getX(),this.z.getY(),this.m.getX(),this.m.getY(),this.n.getX(),this.n.getY()); 
        double C4 = Line.cross(this.m.getX(),this.m.getY(),this.n.getX(),this.n.getY(),this.x.getX(),this.x.getY());  
        double C5 = Line.cross(this.n.getX(),this.n.getY(),this.x.getX(),this.x.getY(),this.y.getX(),this.y.getY()); 
        if(C1 > 0&& C2 > 0&& C3 > 0&& C4 > 0&& C5 > 0)
        {
            return true;
        }
        else
            return false;
    }

计算面积:

//计算面积
    public double  area() {
        
        double AB = Math.sqrt(Math.pow(this.x.getX()-this.y.getX(),2)+Math.pow(this.x.getY()-this.y.getY(),2));
        double BC = Math.sqrt(Math.pow(this.y.getX()-this.z.getX(),2)+Math.pow(this.y.getY()-this.z.getY(),2));
        double CD = Math.sqrt(Math.pow(this.z.getX()-this.m.getX(),2)+Math.pow(this.z.getY()-this.m.getY(),2));
        double DE = Math.sqrt(Math.pow(this.m.getX()-this.n.getX(),2)+Math.pow(this.m.getY()-this.n.getY(),2));
        double EA = Math.sqrt(Math.pow(this.n.getX()-this.x.getX(),2)+Math.pow(this.n.getY()-this.x.getY(),2));
        double AC = Math.sqrt(Math.pow(this.x.getX()-this.z.getX(),2)+Math.pow(this.x.getY()-this.z.getY(),2));
        double AD = Math.sqrt(Math.pow(this.x.getX()-this.m.getX(),2)+Math.pow(this.x.getY()-this.m.getY(),2));
        
        double p1 = (AB + BC + AC)/2;
        double p2 = (AC + CD + AD)/2;
        double p3 = (AD + EA + DE)/2;
        
        double S1 = Math.sqrt(p1*(p1-AB)*(p1-BC)*(p1-AC));//ABC
        double S2 = Math.sqrt(p2*(p2-AC)*(p2-CD)*(p2-AD));//ACD
        double S3 = Math.sqrt(p3*(p3-AD)*(p3-EA)*(p3-DE));//ADE
        
        double area = S1 + S2 + S3;
        
        return area;
    }

计算周长:

//计算周长
    public double perimeter()
    {
        double AB = Math.sqrt((this.x.getX()-this.y.getX())*(this.x.getX()-this.y.getX())+(this.x.getY()-this.y.getY())*(this.x.getY()-this.y.getY()));
        double BC = Math.sqrt((this.y.getX()-this.z.getX())*(this.y.getX()-this.z.getX())+(this.y.getY()-this.z.getY())*(this.y.getY()-this.z.getY()));
        double CD = Math.sqrt((this.z.getX()-this.m.getX())*(this.z.getX()-this.m.getX())+(this.z.getY()-this.m.getY())*(this.z.getY()-this.m.getY()));
        double DE = Math.sqrt((this.m.getX()-this.n.getX())*(this.m.getX()-this.n.getX())+(this.m.getY()-this.n.getY())*(this.m.getY()-this.n.getY()));
        double EA = Math.sqrt((this.n.getX()-this.x.getX())*(this.n.getX()-this.x.getX())+(this.n.getY()-this.x.getY())*(this.n.getY()-this.x.getY()));
    
       double perimeter = AB + BC + CD + DE + EA;  
       return perimeter;
    }

这样就能够实现要求。

然后通过1、判断从字符串中解析出的点的数量是否合格。2、判断输入的字符串中点的坐标部分格式是否合格。若不符合,报错并退出程序3、输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一来找出错误的格式输入

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

}

 

剩下的选项三、四、五、六暂时没想到怎么写。

3)期中考试试题:

 


 

 

 这道题目就是一个简单的对于类的设计使用的题目。

输入输出格式:

 

 

 

这道题目设计了两个类,第一个点类
class Point
{
    private double x;
    private double y;

    public Point(double x,double y)
    {
        this.x = x;
        this.y = y;
    }
    public void setX(double x)
    {
            this.x = x;    
    }
    public void setY(double y)
    {
            this.y = y;
    }
    public double getX(double x)
    {
        return x;
    }
    public double getY(double y)
    {
        return y;
    }
    public void display() 
    {
        System.out.println("The line's begin point's Coordinate is:"+"\n"+"("+String.format("%.2f", x)+","+String.format("%.2f", y)+")");
    }
}

第二个线类

class Line
{
    private Point p1;  
    private Point p2;
    private String color; 
    public  Line (Point p1,Point p2,String color)
    {
        this.p1 = p1;
        this.p2 = p2;
        this.color = color;    
    }

}

 

通过main来调用点类中的display方法来实现要求。
完整代码:
import java.util.Scanner;
class Point
{
    private double x;
    private double y;

    public Point(double x,double y)
    {
        this.x = x;
        this.y = y;
    }
    public void setX(double x)
    {
            this.x = x;    
    }
    public void setY(double y)
    {
            this.y = y;
    }
    public double getX(double x)
    {
        return x;
    }
    public double getY(double y)
    {
        return y;
    }
    public void display() 
    {
        System.out.println("The line's begin point's Coordinate is:"+"\n"+"("+String.format("%.2f", x)+","+String.format("%.2f", y)+")");
    }
}
class Line
{
    private Point p1;  
    private Point p2;
    private String color; 
    public  Line (Point p1,Point p2,String color)
    {
        this.p1 = p1;
        this.p2 = p2;
        this.color = color;    
    }

}
public class Main
{
    public static void main (String[] args)
    {
        Scanner in = new Scanner (System.in);
        double x1 = in.nextDouble(),
               y1 = in.nextDouble(),
               x2 = in.nextDouble(),
               y2 = in.nextDouble();
        Point p1 = new Point(x1,y1);
        Point p2 = new Point(x2,y2);
        Line l = new Line(p1,p2,in.next());
    if(x1<=0||y1>200||x2<=0||y2>200)
    {
        System.out.println("Wrong Format");
    }
    else 
    {
        p1.display();
        p2.display();
    }
    }
    
}

三、踩坑心得

(1)首先还是对于错误格式的判断,在四边形的判断中又碰到了这个问题,当时还是没有解决这个问题,到了五边形的时候,参考了老师发在群里的代码,解决了这个问题

 

 

 

 

 (2)在五边形的的判定的时候,发现有一个测试点一直无法通过,最后在将图画出来之后,推测是会出现两条线相交的情况,最后经过验证之后发现确实是这种情况

 

 



 

四.改进建议

 

做的题目中,首先是剩下没有完成的选项还需要去完成,然后题目五里面非五边形异常的情况还需要进行改进,没有将全部的情况充分分析完成,存在不足的地方,题目集四中,选项一和选项四还有测试点没有通过,还需要进行改进,同时应该多运用学到的类,继承,多态的知识来将代码优化的更加简洁。

 

五.总结 

 

通过这次的题目初步掌握了类的基本用法,但是对于复杂的情况出现是还是不能够清晰的分析出题目中的需求。同时对于多态和继承、容器的知识的掌握不是很清晰明白,还需要去下功夫,好好学习

标签:getX,总结性,double,PTA,Blog,x2,x3,x1,Math
来源: https://www.cnblogs.com/wanxiao-/p/16271052.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有