夜醉云

Java第二次Blog
  •  前言

         1. 习题集四前言

         本次题集共有三题, 第一题是对正则表达式的考查,要求用正则表达式来完成题目,输入一段话,将每一行中的数字提取出来并相加,最后输出每一行数字的总和;第二题仍是对点线坐标的考查,这次主要考的是凸四边形的相关计算,题目较难,对数学知识有一定的考量,同时也是对情况多样性的一种考查,完成情况较差,只拿了30分(满分是70分);第三题则是要求写一个银行业务类,实现设置密码,存款,取款等一系列操作,同时输出对应的金额,这道题相对来说是简单的,得到了满分。

         2. 期中考试题集前言

         期中考试题集是对点与线(类设计)的一种不断深入考查,给出相应的类图,要求写出对应的代码,第一题是简单类的考查,涉及到私有属性,方法调用和使用的考查;第二题则是加入了抽象类的考查,还有父类子类的关系、抽象方法的考查等;第三题是在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象,主要考查多态等相关问题。三次题目不是很难,但是未在指定时间内完成,技术上仍有很大的不足,知识点运用不熟悉,前两题耗费较多的时间。

         3.农夫过河题集前言

        本次题集是对给定代码的一个改写,第一次是将程序代码补充完整,实现农夫过河游戏功能:由用户选择角色过河,系统自动判断游戏的胜负:当出现有生物被吃掉的时候,游戏失败,所有角色都到了河的另一边,游戏成功,为了确保结果的正确性,应该进行多次测试;第二次则是增加Boat(船)类,设计船类的相应方法,并修改main方法中相关的代码,完善农夫带东西过河的功能,同时将数据进行封装,涉及到属性和方法的使用和调用;第三次是继承和多态的考查,写入一个父类,通过继承的方式让每个子类都可以调用父类的方法,也应该进行多次测试,不仅仅是正确的过河方式,还有生物在另外一岸,农夫无法带的问题,以及生物被吃游戏结束等问题。

  • 习题集的得分情况:

     习题集四:60分(满分100分)

  期中考试题集:72分(满分100分)

  农夫过河问题:86分(满分100分)

  •   设计与分析

       (1)银行业务类类图:

                 

import java.util.Scanner;
import java.util.regex.*;
import java.util.Arrays;
public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        do{
            int sum=0;
            String s=in.nextLine();
            if(s.equals("end")){
                break;
            }
            Pattern a=Pattern.compile("\\d+");
            Matcher b=a.matcher(s);
            while(b.find()) {
                sum+=Integer.parseInt(b.group());
            }
            System.out.println(sum);
        }while(true);
    }
}

①设计思路:输入一行数据时,用空格来间隔,当检测到空格时转为下一个数据,将一行输入的数值存到不同的数据中;

     代码分析:第一个数据用input.next()输入,第二个则采用imput.nextInt();

        String username;
        int key;
        double balance = 0.0;
        username = input1.next();
        key = input1.nextInt();

    第一题的简单类图:

    

import java.util.Scanner;
import java.util.regex.*;
import java.util.Arrays;
public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        do{
            int sum=0;
            String s=in.nextLine();
            if(s.equals("end")){
                break;
            }
            Pattern a=Pattern.compile("\\d+");
            Matcher b=a.matcher(s);
            while(b.find()) {
                sum+=Integer.parseInt(b.group());
            }
            System.out.println(sum);
        }while(true);
    }
}

①设计思路:用正则表达式来判断一行中的数字,提取出所有的数字;

     代码分析:运用Pattern和Matcher函数将数字分割并从字符串中提取,再利用find()函数找到每个数字,强制类型转化后得到数字;

       Pattern a=Pattern.compile("\\d+");
            Matcher b=a.matcher(s);
            while(b.find()) {
                sum+=Integer.parseInt(b.group());
            }

(2

 

import java.util.Scanner;
public class Main{
    public static void main(String[] agrs){
        Scanner input = new Scanner(System.in);
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2= input.nextDouble();
        double y2 = input.nextDouble();
        String s = input.next();
        
        Point point1 = new Point(x1,y1);
        Point point2 = new Point(x2,y2);
            
        Line b = new Line(point1,point2,s);
        
        if(x1>0 && x1<=200 && x2>0 && x2<=200 && y1>0 && y1<=200 && y2>0 && y2<=200) {
            System.out.println("The line's color is:" + b.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:");
            b.display();
        }
        else {
            System.out.println("Wrong Format");
        }
        
    }
}
    
class Point{
    private double x;
    private double y;
    public Point(){
        
    }
    public Point(double x,double y){
        this.x = x;
        this.y = y;
    }
    public double getX(){
        return x;
    }
    public void setX(double x){
        this.x = x;
    }
    public double getY(){
        return y;
    }
    public void setY(double y){
        this.y = y;
    }
    public void display(){
        System.out.printf("(%.2f,%.2f)\n",this.x,this.y);
    }
}
class Line{
    private Point point1;
    private Point point2;
    private String color;
    public Line(){
       this.point1 = point1;
       this.point2 = point2;
       this.color = color;
    }
    public Line(Point p1,Point p2,String s){
        this.point1 = p1;
        this.point2 = p2;
        this.color = s;
    }
    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 getDistance();
    }
    public void display(){
        double distance = Math.sqrt(Math.pow((point1.getX()-point2.getX()),2)+Math.pow((point1.getY()-point2.getY()),2));
        System.out.printf("%.2f",distance);
    }
}

import java.util.Scanner;
public class Main{
    public static void main(String[] agrs){
        Scanner input = new Scanner(System.in);
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2= input.nextDouble();
        double y2 = input.nextDouble();
        String s = input.next();
        
        Point point1 = new Point(x1,y1);
        Point point2 = new Point(x2,y2);
            
        Line b = new Line(point1,point2,s);
        
        Plane colour = new Plane(s);
        
        Element element = new Element(){
            public void display()
        };
        if(x1>0 && x1<=200 && x2>0 && x2<=200 && y1>0 && y1<=200 && y2>0 && y2<=200) {
            element = point1;
            element.display();
            element = point2;
            element.display();
            System.out.println("The line's color is:" + b.getColor());
            System.out.println("The line's begin point's Coordinate is:");
            element = point1;
            element.display();
            System.out.println("The line's end point's Coordinate is:");
            element = point2;
            element.display();
            System.out.print("The line's length is:");
            element = b;
            element.display();
            System.out.println("");
            System.out.print("The Plane's color is:");
            element = colour;
            element.display();
        }
        else {
            System.out.println("Wrong Format");
        }
        
    }
}

abstract class Element{
    public abstract void display();
}

class Point extends Element{
    private double x;
    private double y;
    public Point(){
        
    }
    public Point(double x,double y){
        this.x = x;
        this.y = y;
    }
    public double getX(){
        return x;
    }
    public void setX(double x){
        this.x = x;
    }
    public double getY(){
        return y;
    }
    public void setY(double y){
        this.y = y;
    }
    public void display(){
        
        System.out.printf("(%.2f,%.2f)\n",this.x,this.y);
    }
}

class Line extends Element{
    private Point point1;
    private Point point2;
    private String color;
    public Line(){
       this.point1 = point1;
       this.point2 = point2;
       this.color = color;
    }
    public Line(Point p1,Point p2,String s){
        this.point1 = p1;
        this.point2 = p2;
        this.color = s;
    }
    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 getDistance();
    }
    public void display(){
        double distance = Math.sqrt(Math.pow((point1.getX()-point2.getX()),2)+Math.pow((point1.getY()-point2.getY()),2));
        System.out.printf("%.2f",distance);
    }
}

class Plane extends Element{
    private String color;
    Plane(){
        
    }
    Plane(String color){
        this.color = color; 
    }
    public String getColor(){
        return color;
    }
    public void setColor(String color){
        this.color = color;
    }
    public void display(){
        System.out.print(this.color);
    }
}

(3)农夫过河问题中的部分代码分析和思路

①设计思路:当使用private时,需要用get()来进行对数据的访问,用set()来进行数据的使用;

     代码分析:在get()函数中return需要访问的值,set()对访问的值进行修改;

class object{
    private boolean isAlive = true;
    private boolean crossRiver = false;
    private String name;
    public String getname() {
        return name;
    }public void setname(String name) {
        this.name = name;
    }
    public boolean getcrossRiver() {
        return crossRiver;
    }
    public void setcrossRiver() {
        crossRiver=!crossRiver;
    }
    public boolean getisAlive() {
        return isAlive;
    }
    public void setisAlive() {
        isAlive=!isAlive;
    }
}

 设计思路:当使用private时,需要用get()来进行对数据的访问,用set()来进行数据的使用;

     代码分析:在get()函数中return需要访问的值,set()对访问的值进行修改;

class Wolf extends object{

    public String getname() {
        return super.getname();
    }
    public boolean getcrossRiver() {
        return super.getcrossRiver();
    }
    public boolean getisAlive() {
        return super.getisAlive();
    }
    Wolf(String name){        
        
        super.setname(name);
        System.out.println("啊呜~~~我" + super.getname() + "狼又回来了");
    }
    public void setcrossRiver() {
        super.setcrossRiver();
    }
    public void eatSheep(object sheep,object farmer){
        if(super.getcrossRiver() == sheep.getcrossRiver() && super.getcrossRiver()!=farmer.getcrossRiver()){
            sheep.setisAlive();
        }
    }
    public void showStatus(){
        System.out.printf("Wolf " + super.getname() + " is alive  :%s  Wolf "+ super.getname() +" has Cross  :%s\n",super.getisAlive(),super.getcrossRiver());
    }
    public void setisAlive() {
        super.setisAlive();
    }
}

设计思路:父类与子类的继承关系,用extends来连接,子类中采用super关键字;

     代码分析:super表示通过当前子类的构造方法调用父类的无参数构造方法。所以必须保证父类的无参数构造方法是存在的

class Sheep extends object{

    public String getname() {
        return super.getname();
    }
    public boolean getcrossRiver() {
        return super.getcrossRiver();
    }
    public boolean getisAlive() {
        return super.getisAlive();
    }
    Sheep(String name){
        super.setname(name);
        System.out.println("咩咩,我是可爱的小羊" + super.getname());
    }
    public void eatCabbage(object cabbage,object farmer){
        if(super.getcrossRiver() == cabbage.getcrossRiver() && super.getcrossRiver() != farmer.getcrossRiver()) {
            cabbage.setisAlive();
        }
    }
    public void showStatus(){
        System.out.printf("Sheep " + super.getname() +  " is alive :%s  Sheep " + super.getname() + " has Cross :%s\n",super.getisAlive(),super.getcrossRiver());
    }
    public void setcrossRiver() {
        super.setcrossRiver();
    }
    public void setisAlive() {
        super.setisAlive();
    }
}

 

 

                      

 

 

    

 

 

  •   踩坑心得

①正则表达式默认进行贪婪匹配,可以用?来实现非贪婪匹配;

②字符串的直接比较可以采用equals连接前后两者,没必要依次比较每一个字符;

③Find函数用来对原始数据中某个字符串进行定位,以确定其位置,可以简化程序代码,使程序更加简洁明了;

④不应该空定义抽象方法,切记切记切记!!!本次期中考试题集第二题因此导致0分;

⑤Java中想要在一行输入两个数据时,可以采用input.next()方式来进行输入

⑥不断优化实现功能的代码算法,实现对代码的凝练和升华,不断熟悉代码和知识,加快做题速度。

  •  改进建议

①代码尽可能融入新的知识,一个功能一个类,避免一个类复用,导致代码的容错率降低,后期应逐步加入继承等新知识。

②顺序结构不够简洁,逻辑较为混乱,应在设计代码时提前用流程图进行规划,使程序清晰易读。

③不断考虑可能会出现的情况,不仅仅局限于测试点的通过。

④正则表达式的学习还不到位,仍然需要继续在网上查找资料来学习。

⑤在一些代码的关键节点,我应该加上一些注释,方便之后的查阅和修改。

⑥在进行继承关系描述时,需要注意格式问题,用super关键字来调用父类中的方法。

  • 总结

①深入了解了正则表达式后学会了简单运用正则表达式。

②这几周学会了类图和流程图的制作。

③学会了继承和多态等知识点,对代码之间的联系理解进一步加深。

④对知识点的运用不熟悉,导致期中考试的成绩很低,最后一道题甚至没有做完,值得反思,要在题目中不断加入新学到的知识,加快做题速度。

 

posted on 2022-05-09 21:43  夜醉云  阅读(41)  评论(0编辑  收藏  举报