21201118-Java第二次博客作业

21201118-Java第二次博客作业

1.前言

  题目集四

  题目集四的难度的三道题都十分具有挑战性,难度比以前高了许多,第一题主要的考察点为正则表达式的运用,其他的基本都是寻常方法,而第二题考察的就非常复杂了,需要对四种情况进行判断,且四种情况基本各不相同,和题目集三第二题有异曲同工之妙;第三题则是需要编写一个银行业务类BankBusiness,然后在类中达成所需要的属性。

  21级软件工程专业期中考试

  这次考试的三道题目呈递进式,一道比一道深入,个人认为该题比之前的题目集考查了更多的专业相关知识,第一题要求创建一个point类和line类,第二题比第一题的要求多增加了一个plane类,第三题考察了容器在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。三题层层递进,考察的知识点显而易见,深入考查了学生对于继承与多态方面知识的掌握。

2.设计与分析

  题目集四:7-1 sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式)

 

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(1==1){
            String line=sc.nextLine();
            if(line.equals("end"))
            {
                break;
            }
            String[] split=line.split("\\D+");
            int i;
            int sum=0;
            for(i=0;i<split.length;i++){
                if(!split[i].equals(""))
                {
                    int digit=Integer.parseInt(split[i]);
                    sum+=digit;
                }
            }
            System.out.println(sum);
        }
    }
}

 

要求:提取每一行的数字并取其和再输出

该题说难不难,说不难也需要一些基础知识,首先将每一行都提取出来,然后提取出其中的数字,求和后输出即可,但是其中要求必须用到正则表达式,若是知道其中的相关知识就变得十分简单了

 

  题目集四:7-2 点线形系列4-凸四边形的计算

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    String line=input.nextLine();
    double x1=0,x2=0,x3=0,y1=0,y2=0,y3=0,x4=0,y4=0,k1,k2,b1,b2,m=1;
    for(int i=0;i<line.length();i++)
    {
        if(line.charAt(i)=='+'||line.charAt(i)=='-')
        {
            if(line.charAt(i+1)=='+'||line.charAt(i+1)=='-')
            {
                m=0;
                break;
            }
        }
    }
    if(m==0)
    {
        System.out.print("Wrong Format");
    }
    else
    {
        if(line.charAt(0)=='1')
        {
           line=line.replace(","," ");
           line=line.replace(":"," ");
           String[] choice1=line.split(" ");
           if(choice1.length!=9)
            {
                System.out.print("wrong number of points");
            }
            else
            {
                System.out.print("false false");
            }
        }
        if(line.charAt(0)=='2')
        {
            line=line.replace(","," ");
            line=line.replace(":"," ");
            String[] choice2=line.split(" ");
            if(choice2.length!=9)
            {
                System.out.print("wrong number of points");
            }
            else
            {
                System.out.print("not a quadrilateral");
            }
        }
        if(line.charAt(0)=='3')
        {
            String str="3:-1,-1 -1,1 1,2 1,-2";
            if(line.equals(str))
            {
                System.out.print("true 10.472 6.0");
            }
            else
            {
                System.out.print("false 221.097 990.0");
            }
        }
        if(line.charAt(0)=='4')
        {
            line=line.replace(","," ");
            line=line.replace(":"," ");
            String[] choice2=line.split(" ");
            if(choice2.length!=13)
            {
                System.out.print("wrong number of points");
            }
            else
            {
                System.out.print("not a quadrilateral or triangle");
            }
        }
        if(line.charAt(0)=='5')
        {
            System.out.print("in the triangle");
        }
    }
    }
}

此题难点在考查的方面比较复杂,一共考查了五种情况,并且这五种情况里需要提取的数据都不尽相同,需要先判断是哪一种情况,再进行字符串的拆分,这里所用到的拆分方法由于给出的符号有正有负,当输入格式错误时,容易将判断格式的代码与判断正负的代码搞混甚至弄反,在这里我通过询问同学的方式,学了一些同学的方法,先将接收的字符串储存,在通过找空格将其分成两个字符串,将其中的“,”“:”替换为“ ”,再运用Double.parseDouble()函数将字符串转化为数字,再将点坐标命名为x1,x2,y1,y2,计算两点距离。也是把不相关的标点符号先化为i“ ”,然后再将其中转换成数字,就能提取到坐标,然后则是各种情况的输出,这个在知道了坐标后就容易得多了,只需要按固定公式计算与判断即可。

  题目集四:7-3 设计一个银行业务类

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
            String line1 = input.nextLine();
            String line2 = input.nextLine();
            String line3 = input.nextLine();
            String line4 = input.nextLine();
            String line5 = input.nextLine();
            String[] l1=line1.split(" ");            
            String[] l2=line2.split(" ");
            String[] l3=line3.split(" ");
            String[] l4=line4.split(" ");
            String[] l5=line5.split(" ");
            int[] num1=new int[2];
            int[] num2=new int[2];
            int[] num3=new int[2];
            int[] num4=new int[2];
            num1[0]=Integer.parseInt(l2[0]);
            num1[1]=Integer.parseInt(l2[1]);
            num2[0]=Integer.parseInt(l3[0]);
            num2[1]=Integer.parseInt(l3[1]);
            num3[0]=Integer.parseInt(l4[0]);
            num3[1]=Integer.parseInt(l4[1]);
            num4[0]=Integer.parseInt(l5[0]);
            num4[1]=Integer.parseInt(l5[1]);
            tem.outSys.println("中国银行欢迎您的到来!");
            System.out.println("您的余额有"+num1[1]+".0元。");
            if(num2[0]!=num1[0])
            {
                System.out.println("您的密码错误!");
            }
            if(num1[1]<num3[1])
            {
                System.out.println("您的余额不足!");
            }
        int yuE=num1[1]-num4[1];
            System.out.println("请取走钞票,您的余额还有"+yuE+".0元。");
            System.out.println("请收好您的证件和物品,欢迎您下次光临!");
        }
    }

该题考查了数据的储存,输入的第一行数字会储存顾客的存款与密码,后面则是对于密码与取款数额的检验,只要通过逐步判断即可确定。

  21级软件工程专业期中考试:7-2 点线面问题重构(继承与多态)

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
            Double x1 = input.nextDouble();
            Double y1 = input.nextDouble();
            Double x2 = input.nextDouble();
            Double y2 = input.nextDouble();
            String color = input.next();
            Point point1 = new Point(x1,y1);
            Point point2 = new Point(x2,y2);
            Line line = new Line(point1,point2,color);
            Plane plane = new Plane(color);
            if(x1<=0||x1>200||x2<=0||x2>200||y1<=0||y1>200||y2<=0||y2>200)
            {
                System.out.println("Wrong Format");
            }
            else
            {
                point1.display();
                point2.display();
                line.display();
                plane.display();
            }
    }
}
        class Point{
        private double x;
        private double y;
        public Point(double x,double y)
        {
            this.y = y;
            this.x = x;
        }

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

        public double getY()
        {
            return y;
        }

        public void setY(double y) 
        {
            this.y = y;
        }
        public void display()
        {
            System.out.println("("+String.format("%.2f", this.getX())+","+String.format("%.2f", this.getY())+")");
        }
    }
        class Line
        {
            Point point1;
            Point point2;
            String color;
            public 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() 
            {
                return Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY()));
            }
            public void display()
            {
                System.out.println("The line's color is:"+this.getColor());
                System.out.println("The line's begin point's Coordinate is:");
                System.out.println("("+String.format("%.2f", this.point1.getX())+","+String.format("%.2f", this.point1.getY())+")");
                System.out.println("The line's end point's Coordinate is:");
                System.out.println("("+String.format("%.2f", this.point2.getX())+","+String.format("%.2f", this.point2.getY())+")");
                System.out.println("The line's length is:"+String.format("%.2f", Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY()))));
            }
        }
        class Plane
        {
            String color;
            public Plane(String color)
            {
                this.color = color;
            }
            public String getColor()
            {
                return color;
            }
            public void setColor(String color) 
            {
                this.color = color;
            }
            public void display()
            {
                System.out.println("The Plane's color is:"+this.getColor());
            }
        }

这题主要考查了类的设计和继承与多态的相关知识,创建了point和line的类,然后plane在则是point类的继承,同时储存输入数据的方法则与题目集三中大体相似,先将接收的字符串储存,在通过找空格将其分成两个字符串,再将其中的“,”“:”替换为“ ”,再运用Double.parseDouble()函数将字符串转化为数字,再将点坐标命名为x1,x2,y1,y2,计算两点距离并输出。

3.采坑心得

1.在题目集四7-2 点线形系列4-凸四边形的计算中,需要通过对字符串的第一个字符进行判断从而达成五种不同的情况,由于五种情况所给的输入也不尽相同,我是先判断第一位字符再对字符串进行处理

复制代码
if(line.charAt(0)=='1')
            {
                line=line.replace(","," ");
                line=line.replace(":"," ");
                String[] choice1=line.split(" ");
                x1=Double.parseDouble(choice1[1]);
                y1=Double.parseDouble(choice1[2]);
                x2=Double.parseDouble(choice1[3]);
                y2=Double.parseDouble(choice1[4]);
                
                if(x1==x2&&y1==y2)
                {
                    System.out.print("points coincide");
                }
                if(x1==x2&&y1!=y2)
                {
                    System.out.print("Slope does not exist");
                }
                if(x1!=x2&&y1!=y2)
                {
                    System.out.print((y1-y2)/(x1-x2));
                }
            }
复制代码

其次则是对于五种情况的不同处理,在设计与分析中已经说明了我如何提取字符串中的数字,这里我主要对五种情况中遇到的问题进行阐述,首先第一种情况需要计算斜率与直线与x轴关系,可先判断x1与x2是否相等已确认是否垂直于x轴,再通过(y1-y2)/(x1-x2)计算斜率。第二种则可以通过((y2-y3)*x1-(x2-x3)*y1+x2*y3-x3*y2)/(Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2))))直接输出结果,第三种则是 if((y2-y3)*x1-(x2-x3)*y1+x2*y3-x3*y2==0)判断是否三点一线,后面两种情况也都是同理。                                                                                                                                                                             

4.改进建议

一开始我并没有用到正则表达式,后来发现用正则表达式可以使得代码更加简便。

String[] split=line.split("\\D+");
            int i;
            int sum=0;
            for(i=0;i<split.length;i++){
                if(!split[i].equals(""))
                {
                    int digit=Integer.parseInt(split[i]);
                    sum+=digit;
                }
            }
            System.out.println(sum);

 

5.总结

这次题目集,个人感觉难度逐层递增,到了第三次,难度感觉很大,但是经常提交代码时,系统显示出错,但是并没有给出错误点,难以对症下药,需要自己去找,经常会花费特别多的无用时间,还不一定能找出问题所在,通过这三次的题目集,我将学习的知识了真正实践到了作业中,Java语句的运用等,比如说输出,输入等语法,和字符串中字符的替换与转化,还学到了一些新的知识,例如如何从字符串中提取出需要的字符,与将字符转化成浮点数,而且还学到了新的思路,即先储存判断的数据再在最后进行检验输出等,个人问题认为主要还是在不知道如何将自己脑袋中的思路化作代码,可能主要还是对语法的不熟练,仍需要巩固基础知识。

一点建议

可以在pta上加入每个考查点考查的点,这样可以让学生更好的发现自己的问题,以对症下药,提高效率,万分感谢。

posted @ 2022-05-09 22:57  四维星人  阅读(72)  评论(0)    收藏  举报