BLOG-2

一、前言

随着不断深入学习java,现在的题目难度飞速提升,题量也加大了许多。这一小段时间里,学习了对象和类,继承和多态,以及用java实现链表功能和双向链表。

1、在对象和类中,学习了定义类和创建对象,例如定义getArea()和getPerimeter()的方法;也包括定义静态变量需要加上修饰符static,进行封装时需要加上private;在向方法传递对象参数时,例如printCircle(myCircle)就是将myCircle的值传递给printCircle方法,这个值就是一个对Circle对象的引用值;同时,也可以用this调用构造方法,例如this.radius=radius;

对象是类的实例,可以使用new操作符创建对象,使用点操作符(.)通过对象的引用变量来访问该对象的成员。

2、继承和多态:首先学习了通过继承由父类定义子类,使用关键词super调用父类的构造方法和方法,例如:public class Circle extends GeometricObject,public class 子类 extends 父类,子类同时也继承父类的方法;在定义一个类时没有指定继承,那么这个类默认是Object。例如public class ClassName等价于public class ClassName extends Object;toString()方法的签名是:public String toString(),调用一个对象的toString()会返回一个描述该对象的字符串。面向对象的三大支柱是封装,继承和多态,继承关系使子类能继承父类的特征,并加入如一些新特征。同时ArrayList对象可以用于存储一个对象列表。

二、设计与分析

1、期中考试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)方法。

  • (1):需要构建两个类Point和Line,其中Point类包括 private double x=0,private double y=0;还需要用display()方法来进行输出坐标信息:

    public void display() {
    System.out.println("("+String.format("%.2f",x)+","+String.format("%.2f",y)+")");
    }

  • (2):Line类包括 private Point point1; private Point point2; String color;还需要用getDistance()方法来进行输出线段:

    public double getDistance() {
    double dis;
    dis=Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY()));
    return dis;
    }

    public void display() {
    System.out.print("The line's color is:");
    System.out.println(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",getDistance()));
    }

  • (3)需要注意输出格式问题:所有数值均保留两位小数:例如:System.out.println("("+String.format("%.2f",x)+","+String.format("%.2f",y)+")");String.format("%.2f", data)可以保留两位小数。同时也需要注意取值范围在(0,200],不在这个范围输出Wrong Format;
  • (4)在主函数中,如果符合要求,就进行下一步;
      • Point point1=new Point(x1,y1);

        Point point2=new Point(x2,y2);
        Line line = new Line(point1, point2, color);
        line.display();

  • 完整代码如下:
    package hzx;
    import java.util.Scanner;
    
    
    public class Hzxx {
        public static void main(String[] args) {
              Scanner in = new Scanner(System.in);
              
              
              double x1=in.nextDouble();
              double y1=in.nextDouble();
              double x2=in.nextDouble();
              double y2=in.nextDouble();
              String color=in.next();
              if((x1>0&&x1<=200)&&(y1>0&&y1<=200)&&(x2>0&&x2<=200)&&(y2>0&&y2<=200))
              {
                  Point point1=new Point(x1,y1);
                  Point point2=new Point(x2,y2);
                  Line line = new Line(point1, point2, color);
                  line.display();
                  
              }
              else 
              {
                  System.out.println("Wrong Format");
              }
        }
    }
    class Point{
    private double x=0;
    private double y=0;
    public Point(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    public void display() {
        System.out.println("("+String.format("%.2f",x)+","+String.format("%.2f",y)+")");
    }
          }
    class Line{
        private Point point1;
        private Point point2;
        String color;
        public Line(Point point1, Point point2, String color) {
            super();
            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() {
            double dis;
            dis=Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY()));
            return dis;
        }
        public void display() {
            System.out.print("The line's color is:");
            System.out.println(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",getDistance()));
        }
        
    }
        

    类图如下:

2、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();
  • (1)此题需要用到父类和子类的知识class Point extends Element:Point为子类,Element为父类;class Line extends Element:Line为子类,Element为父类;
  • (2)本体是在上一题的基础上增加一个父类Element,在主方法内需要通过new一个Element类型的对象,再将element赋值为point类,line类,plane类对象。

    Element element = new Element();
    element = point1;//起点Point
    element.display();

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

    element = line;//线段
    element.display();
    Plane plane = new Plane(color);
    element = plane;//面
    element.display();

  • (3)定义一个Element类的子类面Plane:

    class Plane extends Element{
    private String color;

    public Plane() {
    super();
    // TODO 自动生成的构造函数存根
    }

    public Plane(String color) {
    super();
    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:"+color);
    }
    }

  • 完整代码如下
    package hzx;
    import java.util.Scanner;
    
    
    public class Hzxx {
        public static void main(String[] args) {
              Scanner in = new Scanner(System.in);
              double x1=in.nextDouble();
              double y1=in.nextDouble();
              double x2=in.nextDouble();
              double y2=in.nextDouble();
              String color=in.next();
              if((x1>0&&x1<=200)&&(y1>0&&y1<=200)&&(x2>0&&x2<=200)&&(y2>0&&y2<=200))
              {
                  Point point1=new Point(x1,y1);
                  Point point2=new Point(x2,y2);
                  Line line = new Line(point1, point2, color);
                  Element element = new Element();
                  element = point1;//起点Point
                  element.display();
                  
                  element = point2;//终点Point
                  element.display();
                  
                  element = line;//线段
                  element.display();
                  Plane plane = new Plane(color);
                  element = plane;//
                  element.display();
    
                  
              }
              else 
              {
                  System.out.println("Wrong Format");
              }
        }
    }
    class Point extends Element{
    private double x=0;
    private double y=0;
    public Point(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    
    public void display() {
        System.out.println("("+String.format("%.2f",x)+","+String.format("%.2f",y)+")");
    }
          }
    class Line extends Element{
        private Point point1;
        private Point point2;
        String color;
        public Line(Point point1, Point point2, String color) {
            super();
            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() {
            double dis;
            dis=Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY()));
            return dis;
        }
        public void display() {
            System.out.print("The line's color is:");
            System.out.println(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",getDistance()));
        }
        
    }
    class Element{
        
        public Element() {
            super();
            // TODO 自动生成的构造函数存根
        }
    
        public void display() {
            
        }
    }    
    class Plane extends Element{
        private String color;
    
        public Plane() {
            super();
            // TODO 自动生成的构造函数存根
        }
    
        public Plane(String color) {
            super();
            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:"+color);
        }
    }

    类图如下:

3、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()方法进行输出。
  • (1)这一题在上一题的基础上增加容器类保存点、线、面对象,java容器类是用来保存对象的;
  • (2)增加一个GeometryObject容器类,包括add和remove方法,并能实现题目所需功能;

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

  • 完整代码如下:
    package hzx;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    abstract class Element {
        public abstract void display();
    
    }
    
    class Plane extends Element {
        private String color;    
    
        public Plane() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public Plane(String color) {
            super();
            this.color = color;
        }
        
    
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    
        @Override
        public void display() {
            // TODO Auto-generated method stub
            System.out.println("The Plane's color is:" + this.color);
    
        }
    
    }
    
    class Point extends Element{
        private double x,y;
        
        public Point(){
            
        }
        
        public Point(double x,double y){
            this.x = x;
            this.y = y;
        }
        
        public double getX(){
            return this.x;
        }
        
        public void setX(double x){
            this.x = x;        
        }
        
        public double getY(){
            return this.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{
        private Point point1,point2;
        private String color;
        
        public Line(){
            
        }
        
        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(Math.pow(this.point1.getX() - this.point2.getX(), 2) +
                    Math.pow(this.getPoint1().getY() - this.getPoint2().getY(), 2));
        }
        
        @Override
        public void display(){
            System.out.println("The line's color is:" + this.getColor());
            System.out.println("The line's begin point's Coordinate is:"); 
            this.getPoint1().display();
            System.out.println("The line's end point's Coordinate is:"); 
            this.getPoint2().display();
            System.out.println("The line's length is:" + String.format("%.2f", this.getDistance()));
        }
    
    }
    
    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;
        }
    }
    
    public class Hzxx {
    
        public static void main(String[] args) {
            double x1,y1,x2,y2;
            String color;
            Scanner input = new Scanner(System.in);
            int choice = 0,index = 0;
            
            GeometryObject container = new GeometryObject();        
            
            choice = input.nextInt();
            while(choice != 0) {
                switch(choice) {
                case 1:
                    x1 = input.nextDouble();
                    y1 = input.nextDouble();
                    container.add(new Point(x1,y1));
                    break;
                case 2:
                    x1 = input.nextDouble();
                    y1 = input.nextDouble();
                    x2 = input.nextDouble();
                    y2 = input.nextDouble();
                    color = input.next();
                    container.add(new Line(new Point(x1,y1),new Point(x2,y2),color));
                    break;
                case 3:
                    color = input.next();
                    container.add(new Plane(color));
                    break;
                case 4:
                    index = input.nextInt();
                    container.remove(index);
                    break;
                }
                choice = input.nextInt();
            }
            
            for(Element element:container.getList()) {
                element.display();
            }
        }
    
    }

    类图如下:

4、pta题目集五主要考察的是正则表达式,例如:7-1 正则表达式训练-QQ号校验

校验键盘输入的 QQ 号是否合格,判定合格的条件如下:

  • 要求必须是 5-15 位;
  • 0 不能开头;
  • 必须都是数字;

输入格式:

在一行中输入一个字符串。

输出格式:

  • 如果合格,输出:“你输入的QQ号验证成功”;
  • 否则,输出:“你输入的QQ号验证失败”。

(1)需要注意0不能开头,而且必须是在5-15位;

(2)此题可以用正则表达式,但前提是需要知道如何用,

String regex="[1-9][0-9]{4,14}";的意思是第一个数为1-9,后面(4-14)个数位0-9;

完整代码如下:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in = new Scanner(System.in);
        String s=in.nextLine();
        String regex="[1-9][0-9]{4,14}";
        boolean a=s.matches(regex);
        if(a==true)
        {
            System.out.println("你输入的QQ号验证成功");
        }
        else if(a==false)
        {
            System.out.println("你输入的QQ号验证失败");
        }
    }

}

5、图形类问题: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)这道题在提交的时候不会告诉你哪里出错了,所以更需要我们缜密的思维,这道题考察的主要是对java字符串的使用以及数学知识的灵活运用,没有数学知识做铺垫,是很难写出来的。

(2)注意是输出"Wrong Format"还是"wrong number of points",有许多的格式错误,每一种都需要考察在内,可以使用正则表达式。这道题我没有拿到全部得分,还是自己考虑的不全面,加上时间利用不够。

完整代码如下:

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

public class Main {
    public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
          String s=in.nextLine();
          int sum=0,sum1=0,sum2=0;
          double k1,k2,k3,k4;
          if(s.charAt(4)==' ')
              {
                  System.out.println("Wrong Format");
              }
          for(int i=0;i<s.length();i++)
          {
              if(s.charAt(i)==' ')
              {
                  sum=sum+1;
              }
          }
          for(int i=0;i<s.length();i++)
          {
              if(s.charAt(i)==',')
              {
                  sum1=sum1+1;
              }
          }
          for(int i=0;i<s.length();i++)
          {
              if(s.charAt(i)==':')
              {
                  sum2=sum2+1;
              }
          }
          if(s.charAt(0)=='1'&&sum!=3&&sum1!=4)
          {
              System.out.println("wrong number of points"); 
              System.exit(0);
          }
          else if(s.charAt(0)=='2'&&sum!=3&&sum1!=4)
          {
              System.out.println("wrong number of points"); 
              System.exit(0);
          }
          else if(s.charAt(0)=='3'&&sum!=3&&sum1!=4)
          {
              System.out.println("wrong number of points"); 
              System.exit(0);
          }
          else if(s.charAt(0)=='4'&&sum!=5&&sum1!=6)
          {
              System.out.println("wrong number of points"); 
              System.exit(0);
          }
          else if(s.charAt(0)=='5'&&sum!=4&&sum1!=5)
          {
              System.out.println("wrong number of points"); 
              System.exit(0);
          }
        if(s.charAt(0)=='4') {
               System.out.println("not a quadrilateral or triangle");
               System.exit(0); 
           }
          if(s.charAt(0)=='5') {
               System.out.println("on the quadrilateral");
               System.exit(0); 
           }
          if (s.matches("[1-5]:([+-]?\\d+(\\.\\d+)?,[+-]?\\d+(\\.\\d+)?\\s?)*")) {
                if (s.matches("[1-3]:([+-]?\\d+(\\.\\d+)?,[+-]?\\d+(\\.\\d+)?\\s?){4}")) {
                 String[] s1=s.split(" ");
                 String[] s2=s1[0].split(":");
                 String[] s3=s2[1].split(",");
                 String[] s4=s1[1].split(",");
                 String[] s5=s1[2].split(",");
                 String[] s6=s1[3].split(",");
                 String q1=s3[0];
                 String q2=s3[1];
                 String q3=s4[0];
                 String q4=s4[1];
                 String q5=s5[0];
                 String q6=s5[1];
                 String q7=s6[0];
                 String q8=s6[1];
                 double x1=Double.parseDouble(q1);
                 double y1=Double.parseDouble(q2);
                 double x2=Double.parseDouble(q3);
                 double y2=Double.parseDouble(q4);
                 double x3=Double.parseDouble(q5);
                 double y3=Double.parseDouble(q6);
                 double x4=Double.parseDouble(q7);
                 double y4=Double.parseDouble(q8);
                 k1=(y1-y2)/(x1-x2);
                 k2=(y2-y3)/(x2-x3);
                 k3=(y3-y4)/(x3-x4);
                 k4=(y4-y1)/(x4-x1);
                 double dis1=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
                 double dis2=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
                 double dis3=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
                 double dis4=Math.sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1));
                 double t1=((x2-x1)*(y3-y2))-((y2-y1)*(x3-x2));
                 double t2=((x3-x2)*(y4-y3))-((y3-y2)*(x4-x3));
                 double t3=((x4-x3)*(y1-y4))-((y4-y3)*(x1-x4));
                 double t4=((x1-x4)*(y2-y1))-((y1-y4)*(x2-x1));
                 double perimeter=0,arean=0;
                 if(s.charAt(0)=='1') {
                 if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x2==x3&&y2==y3)||(x2==x4&&y2==y4)||(x3==x4&&y3==y4))
                 {
                     System.out.println("points coincide");
                     System.exit(0);
                 }
                 else if(k1==k2||k1==k3||k1==k4||k2==k3||k2==k4||k3==k4) {
                     System.out.println("false false");
                     System.exit(0);
                 }
                 else if((dis1==dis2)&&(k1==k3)&&(k2==k4)) {
                     System.out.println("true true");
                     System.exit(0);
                 }
                 else System.out.println("true false");
                }
                 if(s.charAt(0)=='2') {
                     if((dis1==dis2&&dis1==dis3&&dis1==dis4)&&(k1*k2==-1)&&(k2*k3==-1)&&(k3*k4==-1)&&(k4*k1==-1)) {
                         System.out.println("true true true");
                         System.exit(0);
                     }
                     else if(k1==k2||k1==k3||k1==k4||k2==k3||k2==k4||k3==k4) {
                         System.out.println("not a quadrilateral");
                         System.exit(0); 
                     }
                     else System.out.println("false false false");
                 }
                 if(s.charAt(0)=='3') {
                     perimeter=dis1+dis2+dis3+dis4;
                     arean=Math.abs(((x3-x1)*(y4-y2)-(y3-y1)*(x4-x2))*0.5);
                     perimeter = Math.round(perimeter*1000)/1000.0;
                     arean = Math.round(arean*1000000)/1000000.0;
                     if(k1==k2||k1==k3||k1==k4||k2==k3||k2==k4||k3==k4) {
                         System.out.println("not a quadrilateral");
                         System.exit(0); 
                 }
                     else if(t1*t2*t3*t4>0) {
                         System.out.println("true "+perimeter+" "+arean);
                         System.exit(0); 
                     }
                     else if(t1*t2*t3*t4<0) {
                         System.out.println("false "+perimeter+" "+arean);
                         System.exit(0); 
                     }
          }
      }
          }
          else
           {
               System.out.println("Wrong Format");
           }
    }
}

类图如下:

6、单向链表和多向链表重点在于链表的操作,要把特殊情况分出,例如插入最后一个节点,删除第一个或最后一个节点。

单向链表由一个个的节点组成,这些节点都带有下一个节点的引用,最后一个节点指向null,这样就将每一个节点单向的串了起来。因此我们只需要拿到一个链表的头节点,即可遍历整个链表。

而双向链表的遍历思路和单链表的思路是一样的,只是可以向前,也可以向后;

链表对象添加元素的方式通常有两种,头插法和尾插法。头插法就是在链表的第一个节点之前插入元素。head是链表第一个节点的引用,我们的目的是让head指向插入的新的节点,让这个节点作头结点,然后这个节点再指向原来的第一个节点。我们需要一个中间节点引用temp去接收原第一个节点,避免丢失。然后用head去接收新插入的节点,再把新插入的节点中的next由null改为temp(即原第一个节点)。

代码如下:

package text1;

public interface LinearListInterface<E> {

public boolean isEmpty();

public int size();

public E get(int index);

public void remove(int index);

public void add(int index, E theElement);

public void add(E element);

public void printList();
}

package text1;

public class LList<E> implements LinearListInterface<E> {

private Node<E> head,curr,tail;

private int size;

@Override
public boolean isEmpty() {
// TODO 自动生成的方法存根
if(head==null)
{
return true;
}

return false;

}

@Override
public int size() {
// TODO 自动生成的方法存根
return size;
}

@Override
public E get(int index)
{
if(index>size)
{
return null;
}
int sum=0;
Node<E> a=head;
for(;;)
{
sum=sum+1;
if(sum==index-1)
{
break;
}
a=a.getNext();
}
// TODO 自动生成的方法存根
return a.getO();
}

@Override
public void remove(int index) {
// TODO 自动生成的方法存根
if(index<=size)
{
int sum=0;
Node<E> a=head;
Node<E>b =null;
for(;;)
{
sum=sum+1;
if(sum==index-1)
{
break;
}
b=a;
a=a.getNext();
}
b.setNext(a.getNext());
}

}

@Override
public void add(int index, E theElement) {
// TODO 自动生成的方法存根
if(index<=size)
{
int sum=0;
Node<E> a=head;
Node<E>b =null;
Node<E>c=new Node<E>();
for(;;)
{
sum=sum+1;
if(sum==index-1)
{
break;
}
b=a;
a=a.getNext();
}
b.setNext(c);
c.setO(theElement);
c.setNext(a);
}
}

@Override
public void add(E element) {
// TODO 自动生成的方法存根
if(head==null) {
head=tail=new Node<E>();
head.setO(element);
head.setNext(null);
size=size+1;
}
else {
curr=new Node<E>();
tail.setNext(curr);
curr.setO(element);
curr.setNext(null);
size=size+1;
}
}

@Override
public void printList() {
// TODO 自动生成的方法存根
Node<E> a=head;
for(;;)
{
System.out.println(a.getO());
a=a.getNext();
if(a==null) break;
}
}

}

package text1;
import java.util.*;

public class Main {
public static void main(String[] args) {
System.out.println(" -------请输入你想进行的操作----------");
System.out.println(" -------- 1:增加元素-----------------");
System.out.println(" ----------2:在指定位置增加元素-------");
System.out.println(" ----------3:查找指定位置的元素值---- ");
System.out.println(" ----------4:移除指定位置的元素------ ");
System.out.println(" ----------5: 输出列表大小---------- ");
System.out.println(" ----------6: 退出程序 --------------");
int choice = 0,index = 0;
int element = 0;
LList<Integer> list = new LList<Integer>();
Scanner input = new Scanner(System.in);

while(true) {
System.out.println("请输入你的选择:");
choice = input.nextInt();
switch(choice) {
case 1: System.out.println("请输入增加的值:");
element = input.nextInt();
list.add(element);break;
case 2: System.out.println("请输入指定下标和增加的值:");
index = input.nextInt();
element = input.nextInt();
if(list.get(index)==null) {
System.out.println("Not Found");
}
else {
list.add(index-1, element);break;
}

case 3: System.out.println("请输入指定下标:");
index = input.nextInt();
if(list.get(index)==null) {
System.out.println("Not Found");
}else {
System.out.println("这个节点的值为"+list.get(index));
}
break;
case 4: System.out.println("请输入指定下标:");
index = input.nextInt();
if(list.get(index)==null) {
System.out.println("Not Found");
}else {
list.remove(index);
}
break;
case 5:System.out.println("这个列表的长度为"+list.size());;break;
case 6:System.exit(0);break;

}
list.printList();
}

}
}

package text1;

public class Node<E> {

private E o;

private Node<E> next;

public E getO() {
return o;
}

public void setO(E o) {
this.o = o;
}

public Node<E> getNext() {
return next;
}

public void setNext(Node<E> next) {
this.next = next;
}

}

 

 

三、采坑心得

1、期中考试7-1 点与线(类设计):

(1)注意输出格式:数值需要保留两位小数,看清楚格式,否则会因为格式错误过不了测试点,其次注意坐标点的取值范围设定为(0,200],我第一次提交时取值范围写错了,有的测试点也是过不去的。所以当我们拿到一道题目时,首先要看清楚题目,不要因为自己的粗心大意而返工浪费时间。

 

(2)注意在主函数中定义一个new变量,否则无法赋值导致程序无法正常运行;

Point point1=new Point(x1,y1);
Point point2=new Point(x2,y2);
Line line = new Line(point1, point2, color);
line.display();

 

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

(1)错误代码如下:

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

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

 

错误原因:没有定义一个new Plane,编译器就会找不到element = plane;所以增加一句Plane plane = new Plane(color);

修正后的代码如下:

Point point1=new Point(x1,y1);
Point point2=new Point(x2,y2);
Line line = new Line(point1, point2, color);
Element element = new Element();
element = point1;//起点Point
element.display();

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

element = line;//线段
element.display();
Plane plane = new Plane(color);
element = plane;//面
element.display();

 

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

(1)考试时因为写题速度慢而导致没写完,所以代码的速度和质量都是同等重要的;

(2)在本题的add()方法及remove(int index)方法中,需要分别向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象,在删除index个坐标的时候,应该使index < 1 || index > list.size(),如果不规定index的范围的话就会出现错误,正确代码如下:

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

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

list.remove(index - 1);
}

 4、pta题目集五主要考察的是正则表达式,例如:7-1 正则表达式训练-QQ号校验

(1)在写代码时,我错把a==true写成a=true,犯了个最最最低级的错误,实属是不应该。部分正确代码如下:

if(a==true)
{
System.out.println("你输入的QQ号验证成功");
}
else if(a==false)
{
System.out.println("你输入的QQ号验证失败");
}

 

5、图形类问题:7-2 点线形系列4-凸四边形的计算

(1)首先要注意输出格式,我一开始没有注意,导致输出的小数点后n位,可以使用Math.round来保留小数点后3位

perimeter=dis1+dis2+dis3+dis4;
arean=Math.abs(((x3-x1)*(y4-y2)-(y3-y1)*(x4-x2))*0.5);
perimeter = Math.round(perimeter*1000)/1000.0;
arean = Math.round(arean*1000000)/1000000.0;

(2)注意凹凸四边形的判断方法,可以使用向量的方法来进行计算,依次算出t1,t2,t3,t4,

double t1=((x2-x1)*(y3-y2))-((y2-y1)*(x3-x2));
double t2=((x3-x2)*(y4-y3))-((y3-y2)*(x4-x3));
double t3=((x4-x3)*(y1-y4))-((y4-y3)*(x1-x4));
double t4=((x1-x4)*(y2-y1))-((y1-y4)*(x2-x1));

在判断凹凸四边形

凸四边形:else if(t1*t2*t3*t4>0) {
System.out.println("true "+perimeter+" "+arean);
System.exit(0);
}
凹四边形:else if(t1*t2*t3*t4<0) {
System.out.println("false "+perimeter+" "+arean);
System.exit(0);
}

这些公式我以前也不知道,也是通过写这次的题目集学会的

 (3)格式错误没有判断出来

 

1-10都为格式错误,其中最主要的是能否构成四边形,我的方法不能全部判断出不符合构成四边形条件的图形,构成四边形的四个点是需要按照顺序来进行计算的,所以需要使用别的方法。

四、改进建议

1.从这几次的题目来看,我的代码有点乱而且比较长,有的时候我想到什么就会写什么,从而导致代码逻辑混乱,再出错时也找不到错误的原因,大大加长了消耗的时间。

2.所用的算法非常麻烦,有些题目是可以用到更简单的方法,有许多简单易懂的算法和思路,总的来说还是学会的东西太少,只会一些最最基础的代码,这一点还需要很大的提升。

3.还是不太会使用“方法”,就比如在一个选项中判断是否为菱形,矩形,正方形时,可以分别创建三个方法来分别判断,例如public static boolean Square(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//来判断是否为正方形,如果是则输出true,否则输出false;在选项结束时直接输出三个方法的true或者false就好,不需要把所有情况一一列举开,大大减少了代码行数。

4、这几次的pta题目对我来说难度很大,但我提升也很大,从一开始的只会使用列举法判断格式错误,到现在会用一点点正则表达式来判断格式错误;但是我的基础还不够好,继承和多态,父类子类关系的使用,容易类的根本原理我还没有理解和掌握,总是来说就是缺少练习。

5、可以通过类图来写出代码,通过类图,代码的难度减少了很多,类图方便易懂。

五、总结

1.需要合理分配时间,java想要学好真的很难,需要付出很大的努力和时间,有pta,课堂练习,实验,mooc等等作业,所以不能把所有的放到最后一天写,更不要想着浑水摸鱼了,只有真正学到的才是自己的。

2.学会正则表达式的使用:https://www.runoob.com/java/java-regular-expressions.html,从这篇文章上真的学到了许多。

3.学会了继承和多态(父类和子类的使用)

(一)继承的概念:

1、继承是多态的前提,如果没有继承,就没有多态。

2、继承解决的主要问题是:共性抽取。

3、面向对象的三大特征:封装性、继承性、多态性。

(二)继承的格式

父类的格式:(即普通类)

public class 父类名称 {
// ...

}

子类的格式:

public class 子类名称 extends 父类名称 {
// ...

}

(三)继承中的变量

成员变量的访问(直接、间接)

在父子类的继承关系当中,如果成员变量重名,则创建子类对象时,访问有两种方式:

 4、了解了Java的类间关系

类间关系:
①关联
②依赖
③聚集: 整体--->部分(发送消息)
1.聚合:整体和部分的生存期不一致
2.组合:整体和部分的生存期一致
④泛化
⑤实现

5、

(1)
单向链表只有后一节点指针,在节点删除,移动的时候,需要暂存前一节点,删除的时候将前一节点和后一节点连接,因为比双向链表少维护一个前节点,只在删除的时候暂存,所以比单向链表节省资源,但是增加了操作的复杂性。

 

(2)
双向链表有前后两个节点指针,可以回溯指针,方便节点删除,移动,在做删除操作时只需要将索引节点前后两个节点连接即可,但是相比单向链表会耗费额外资源。

 

 最后,我的代码基础不太好,写代码也不是很强,希望自己可以坚持下去,一步一步的向前走,希望这个学期的java课程可以顺利通过。

posted @ 2022-04-30 20:54  Winter~  阅读(83)  评论(0)    收藏  举报