题目集4~6的总结性Blog

前言:

1.知识点:

  • java中正则表达式的运用;
  • 类的构造方法、方法的调用、参数传递、对象的构造与使用;

  • Java中的控制流程(循环结构、控制结构等);
  • Java中的继承;
  • Java中的面向对象设计;
  • java中的排序算法;
  • java中的类方法的编写以及灵活运用;
  • 实现接口及多态性 等;

2.题量及难度:

  • 第四次题集题目数量只有三个但是前两题题目量都较多第一题题目难度也较高综合性很高还有就是需要用到较为复杂的正则表达式;
  • 第五次题集题目数量适中前三题题目难度较为简单代码长度也不长后两题题目难度较高代码较为复杂;
  • 第六题题集题目数量是最多的但是题目难度是循序渐近的前面几题主要考察了正则表达式的运用后两题主要考察了多态与继承等等难度中等;

设计与分析:

1.题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较

 

 

 

从类图分析就看的出来题集04-7-2的日期问题面向对象设计(聚合一)在聚合上采用的层层嵌套的模式Year类与Month类聚合;Month类与Day类聚合;Day类与DateUtil类聚合 Main类再依赖于DateUtil类这种方式设计会让类与类之间的关系更加紧密而且封装性也更强很多方法都在Main没有展示出来在其余的内部类自己解决了但是调用起来也会再某些方面更加的繁杂以及对于参数的传递也会更加的复杂做很多没有必要的重复传参而且在类图上可以看到基本上就每一个类都有get set 无形增加了复杂度

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    int d;
    int m;
    int y;
    int x=input.nextInt();//输入操作
    if(x>0&&x<4) {
        switch(x) {
        case(1):{
             y=input.nextInt();
             m=input.nextInt();
             d=input.nextInt();
             int n=input.nextInt();
             DateUtil date=new DateUtil();
             Year year =new Year();
             Month month =new Month();
             Day day=new Day();
             if(date.check(d, m, y)) {
                year.setValue(y);
                month.setValue(m);
                month.setYear(year);
                day.setValue(d);
                day.setMonth(month);
                date.setDay(day);
                date.getNextNDays(n);
                date.showDate();
             }
             else
                 System.out.println("Wrong Format");
        }
        case(2):{
             y=input.nextInt();
             m=input.nextInt();
             d=input.nextInt();
             int n=input.nextInt();
             DateUtil date=new DateUtil();
             Year year =new Year();
             Month month =new Month();
             Day day=new Day();
             if(date.check(d, m, y)) {
                     year.setValue(y);
                    month.setValue(m);
                    month.setYear(year);
                    day.setValue(d);
                    day.setMonth(month);
                    date.setDay(day);
                    date.getPreviousNDays(n);
                    date.showDate();
             }
             else
                 System.out.println("Wrong Format");
             
        }
        case(3):{
             y=input.nextInt();
             m=input.nextInt();
             d=input.nextInt();
             int y2=input.nextInt();
             int m2=input.nextInt();
             int d2=input.nextInt();
             DateUtil date=new DateUtil();
             DateUtil date2=new DateUtil();
             Year year =new Year();
             Month month =new Month();
             Day day=new Day();
             Year year2 =new Year();
             Month month2 =new Month();
             Day day2=new Day();
             if(date.check(d, m, y)&&date2.check(d2, m2, y2)) {
                     year.setValue(y);
                    month.setValue(m);
                    month.setYear(year);
                    day.setValue(d);
                    day.setMonth(month);
                    date.setDay(day);
                    year2.setValue(y2);
                    month2.setValue(m2);
                    month2.setYear(year2);
                    day2.setValue(d2);
                    day2.setMonth(month2);
                    date2.setDay(day2);
                    System.out.println(Math.abs(date.getDaysofDates(date)-date2.getDaysofDates(date2)));
             }
             else
                 System.out.println("Wrong Format");
            
        }
        }
    }
    else
        System.out.println("Wrong Format");
}
}
class DateUtil{
    private Day Day;
    
    void DateUtil() {}
void DateUtil(int d,int m,int y) {
        
    }
    void setDay(Day d) {
        this.Day=d;
    }
    Day getDay() {
        return Day;
    }
    boolean check(int d,int m,int y) {
        int[] mon2={0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(y%4==0&&y%100!=0||y%400==0) {
            mon2[2]=29;
        }
        if(d>=1&&d<=mon2[m]&&m>=1&&m<=12&&y>=1900&&y<=2050)
            return true;
        else 
            return false;
    }
    int[] mon={0,31,28,31,30,31,30,31,31,30,31,30,31};
    private int getDayOfMonth(Year year, Month month) {
         int day = mon[month.value];
        if (month.value == 2 && year.isLeapYear()) {
            day = 29;
        }
        return day;
    }
    /*boolean equal(DateUtil date) {
        
    }
    boolean compare(DateUtil date) {
        
    }*/
    void showDate() {
        System.out.println(Day.month.year.value + "-" + Day.month.value + "-" + Day.value);
    }
    DateUtil getNextNDays(int n) {
        for (int i=0;i<n;i++) {
            Day.value++;
            if(Day.value>getDayOfMonth(Day.month.year,Day.month)) {
                Day.value=1;
                Day.month.value++;
                if(Day.month.value>12) {
                    Day.month.value=1;
                    Day.month.year.value++;
                }
            }
        }
        return this;
    }
    DateUtil getPreviousNDays(int n) {
        for (int i=0;i<n;i++) {
            Day.value--;
            while(Day.value<1) {
                Day.month.value--;
                if(Day.month.value<1) {
                    Day.month.value=12;
                    Day.month.year.value--;
                }
                Day.value+=getDayOfMonth(Day.month.year,Day.month);
            }
        }
        return this;
    }
    long getDaysofDates(DateUtil date) {
        long flag;
        int a=(int)(date.Day.month.year.value-1)/4;
        int b=(int)(date.Day.month.year.value-1)/100;
        int c=(int)(date.Day.month.year.value-1)/400;
        flag=366*(a-b+c)+365*(date.Day.month.year.value-(a-b+c));
        if(date.Day.month.year.isLeapYear()) {
            mon[2]=29;
        }
        for(int i=1;i<date.Day.month.value;i++) {
            flag+=mon[i];
        }
        flag+=date.Day.value;
    return flag;
}
}
class Year{
    int value;
    Year(){}
    int getValue() {
        return value;
    }
    void setValue(int value) {
        this.value=value;
    }
    public boolean isLeapYear() {
        if(value%4==0&&value%100!=0||value%400==0)
            return true;
        else
            return false;
    }
    void yearIncrement() {
        value++;
    }
    void yearReduction() {
        value--;
    }
}
class Month{
    int value;
    Year year;
    Year getYear() {
        return year;
    }
    void setYear(Year year) {
        this.year=year;
    }
    int getValue() {
        return value;
    }
    void setValue(int value) {
        this.value=value;
    }
    void resetMin() {
        value=1;
    }
    void resetMax() {
        value=12;
    }
    void monthIncrement() {
        value++;
    }
    void monthReduction() {
        value--;
    }
}
class Day{
    int value;
    Month month;
    public int[] mon={31,28,31,30,31,30,31,31,30,31,30,31};
    Day(){}
    int getValue() {
        return value;
    }
    void setValue(int value) {
        this.value=value;
    }
    Month getMonth() {
        return month;
    }
    void setMonth(Month month) {
        this.month=month;
    }
    void resetMin() {
        value=1;
    }
    void resetMax(int n) {
        value=mon[n];
    }
    void dayIncrement() {
        value++;
    }
    void dayReduction() {
        value--;
    }
}

 

 

 

 平均复杂度在2左右comments工具类基本无使用

在题集五的7-5日期问题面向对象设计(聚合二)就没有采取上一次的层层聚合状态日期类的都是对DateUtil进行了聚合层次变得简单清晰了就传参这一块就不用那么麻烦了还有也更加清晰了代码的简洁

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year = 0;
        int month = 0;
        int day = 0;

        int choice = input.nextInt();

        if (choice == 1) { // test getNextNDays method
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            m = input.nextInt();

            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
            System.out.println(date.getNextNDays(m).showDate());
        } else if (choice == 2) { // test getPreviousNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            n = input.nextInt();

            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(
                    date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            int anotherYear = Integer.parseInt(input.next());
            int anotherMonth = Integer.parseInt(input.next());
            int anotherDay = Integer.parseInt(input.next());

            DateUtil fromDate = new DateUtil(year, month, day);
            DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

            if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
                System.out.println("The days between " + fromDate.showDate() +
                        " and " + toDate.showDate() + " are:"
                        + fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else{
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}

class DateUtil {
    private int year;
    private int month;
    private int day;

    public DateUtil(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public DateUtil(){}

    public void setYear(int year) {
        this.year = year;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }

    private final int[] DAY_OF_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    private int getDayOfMonth(int year, int month) {
        int days = DAY_OF_MONTH[month - 1];
        if (month == 2 && isLeapYear(year)) {
            days = 29;
        }
        return days;
    }

    public boolean checkInputValidity()//检测输入的年、月、日是否合法
    {
        if (year < 1820 || year > 2020) return false;
        if (month < 1 || month > 12) return false;
//        int _day = this.getDayOfMonth(year, month);
//        return day <= _day;
        return day >= 1 && day <= 31;
    }

    public boolean isLeapYear(int year)//判断year是否为闰年
    {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }


    public DateUtil getNextNDays(int n)//取得year-month-day的下n天日期
    {
        int year = this.year;
        int month = this.month;
        int day = this.day;
//        day = Math.min(day, this.getDayOfMonth(year, month));
        for (int i = 0; i < n; i++) {
            day++;
            if (day > getDayOfMonth(year, month)) {
                day = 1;
                month++;
                if (month > 12) {
                    month = 1;
                    year++;
                }
            }
        }
        return new DateUtil(year, month, day);
    }

    public DateUtil getPreviousNDays(int n)//取得year-month-day的前n天日期
    {
        int year = this.year;
        int month = this.month;
        int day = this.day;
        for (int i = 0; i < n; i++) {
            day--;
            while (day < 1) {
                month--;
                if (month < 1) {
                    month = 12;
                    year--;
                }
                day += getDayOfMonth(year, month);
            }
        }
        return new DateUtil(year, month, day);
    }

    public boolean compareDates(DateUtil date)//比较当前日期与date的大小(先后)
    {
        if (this.year > date.year) return true;
        if (this.year == date.year) {
            if (this.month > date.month) return true;
            if (this.month == date.month) {
                if (this.day >= date.day) return true;
            }
        }
        return false;
    }

    public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等
    {
        if (date != null) {
            if (year == date.year && month == date.month && day == date.day) {
                return true;
            }
        }
        return false;
    }
    
    private static final int[] mon = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
    public int getDaysofDates(DateUtil date)//求当前日期与date之间相差的天数
    {
        DateUtil dateUtil1 = this; //
        DateUtil dateUtil2 = date; //
        if (this.compareDates(date)) {
            dateUtil1 = date;
            dateUtil2 = this;
        }

        int days;
        int leapYearNum = 0;
        for (int i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) {
            if (isLeapYear(i)) {
                leapYearNum++;
            }
        }

        days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum;

        int d1 = mon[dateUtil1.getMonth() - 1] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0);
        int d2 = mon[dateUtil2.getMonth() - 1] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0);
        return days - d1 + d2;
    }
    
    public String showDate()//以“year-month-day”格式返回日期值
    {
        return year + "-" + month + "-" + day;
    }
}

 

 

 从组织图中红色线表示当前的情况,在绿色范围则表示俩良好

 

 

2.题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)

题集4(7-3)

import java.util.*;
class Shape{
    public Shape(){
        System.out.println("Constructing Shape");
    }
    public double getArea(){
        return 0.0;
    }
}

class Circle extends Shape{
    private double radius;
    public Circle(){
        System.out.println("Constructing Circle");
    }
    public void setRadius(double radius){
        this.radius = radius;
}
    public double getRadius(){
        return radius;
    }
    
    @Override
    public double getArea(){
        return radius * radius * Math.PI;
    }
}

class Rectangle extends Shape{
    private double width;
    private double length;
    public Rectangle(){
        System.out.println("Constructing Rectangle");
    }
    public double getWidth(){
        return this.width;
    }
    public void setWidth(double width){
            this.width = width;
    }
    public double getLength(){
        return this.length;
    }
    public void setLength(double length){
            this.length = length;
    }
    @Override
    public double getArea(){
        return width * length;
    }
}

class Ball extends Circle{
    public Ball(){
        System.out.println("Constructing Ball");
    }
    @Override
    public double getArea(){
        return 4 * super.getRadius() * super.getRadius() * Math.PI;
    }
    public double getVolume(){
        return 4.0 / 3.0 * Math.PI * Math.pow(super.getRadius(), 3);
    }
}

class Box extends Rectangle{
    private double height;
    public Box(){
        System.out.println("Constructing Box");
    }
    public void setHeight(double height){
            this.height = height;
    }
    public double getHeight(){
        return height;
    }
    @Override
    public double getArea(){
        return (super.getArea() + height * super.getWidth() + height * super.getLength()) * 2;
    }
    public double getVolume(){
        return height * super.getArea();
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int flag = input.nextInt();
        switch (flag){
            case 1 :{
                double r = input.nextDouble();
                if (r <= 0){
                    System.out.println("Wrong Format");
                 break;
                }
                else {
                Circle circle = new Circle();
                circle.setRadius(r);
                System.out.printf("Circle's area:%.2f\n", circle.getArea());
                break;}
            }
            case 2 :{
                double length = input.nextDouble();
                double width = input.nextDouble();
                if (length <= 0 || width <= 0){
                    System.out.println("Wrong Format");
                    break;
                }
                else {
                Rectangle rectangle = new Rectangle();
                rectangle.setLength(length);
                rectangle.setWidth(width);
                System.out.printf("Rectangle's area:%.2f\n", rectangle.getArea());
                break;}
            }
            case 3 :{
                double radius = input.nextDouble();
                if (radius <= 0){
                    System.out.println("Wrong Format");
                   break;
                }
                else {
                Ball ball = new Ball();
                ball.setRadius(radius);
                System.out.printf("Ball's surface area:%.2f\n", ball.getArea());
                System.out.printf("Ball's volume:%.2f\n", ball.getVolume());
                break;}
            }
            case 4 :{
                double length = input.nextDouble();
                double width = input.nextDouble();
                double height = input.nextDouble();
                if (length <= 0 || width <= 0 || height <= 0){
                    System.out.println("Wrong Format");
                    break;
                }
                else {
                Box box = new Box();
                box.setLength(length);
                box.setWidth(width);
                box.setHeight(height);
                System.out.printf("Box's surface area:%.2f\n", box.getArea());
                System.out.printf("Box's volume:%.2f\n", box.getVolume());
                break;}
            }
            default :{
                System.out.println("Wrong Format");
            }
        }
    }
}

此题主要运用的是对继承的运用理解父类 类Shape被类Circle 类Rectangle 承类      父类类Circle 被类Ball继承 父类 类Rectangle被类Box继承同时在子类上都运用了方法重写(@Override)也同时进行了封装。

题集题目集6(7-5)

import java.util.*;
class Shape{
    public Shape(){
        
    }
    public double getArea(){
        return 0.0;
    }
}

class Circle extends Shape{
    private double radius;
 
    public boolean validate() {
        if(radius>0)
            return true;
        else
            return false;
    }
    public Circle(double radius){
        super();
        this.radius = radius;
}
    public double getRadius(){
        return radius;
    }
    
    @Override
    public double getArea(){
        return radius * radius * Math.PI;
    }
}

class Rectangle extends Shape{
    private double width;
    private double length;
    public boolean validate() {
        if(width>=0&&length>=0)
            return true;
        else
            return false;
    }
    public double getLength(){
        return this.length;
    }
    public Rectangle(double length,double width){
            this.length = length;
            this.width = width;
    }
    @Override
    public double getArea(){
        return width * length;
    }
}
class Triangle extends Shape{
        private double side1,side2,side3;
     public Triangle(){
           
        }
     public Triangle(double a ,double b,double c) {
         side1=a;
         side2=b;
         side3=c;
     }
     @Override
        public double getArea(){
         double p=(double)(side1+side2+side3)/2;
         double s=Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
         return s;
        }
     public boolean validate() {
         if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1)
             return true;
         else
             return false;
        }
}
class Ball extends Circle{
 
    public Ball(double radius) {
        super(radius);
        // TODO Auto-generated constructor stub
    }
    @Override
    public double getArea(){
        return 4 * super.getRadius() * super.getRadius() * Math.PI;
    }
    public double getVolume(){
        return 4.0 / 3.0 * Math.PI * Math.pow(super.getRadius(), 3);
    }
}

class Box extends Rectangle{
    public Box(double length, double width) {
        super(length, width);
        // TODO Auto-generated constructor stub
    }
    private double height;
    
    
    public void setHeight(double height){
            this.height = height;
    }
    public double getHeight(){
        return height;
    }


}
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x=input.nextInt();
        int y=input.nextInt();
        int z=input.nextInt();//三角形个数
        if(x>=0&&y>=0&&z>=0) {
            Circle c[]=new Circle[x];
            Rectangle r[] = new Rectangle[y];
             Triangle t[] = new Triangle[z];
            ArrayList <Double> area =new ArrayList<Double>();
            for(int i=0;i<x;i++) {
                c[i]=new  Circle(input.nextDouble());
                if(c[i].validate())
                    area.add(c[i].getArea());
                else {
                   System.out.println("Wrong Format");
                    System.exit(0);
                }
            }
           for(int i=0;i<y;i++) {
               r[i]=new Rectangle(input.nextDouble(),input.nextDouble());
               if(r[i].validate())
                   area.add(r[i].getArea());
               else {
                  System.out.println("Wrong Format");
                    System.exit(0);
               }
           }
           for(int i=0;i<z;i++) {
               t[i]=new Triangle(input.nextDouble(),input.nextDouble(),input.nextDouble());
               if(t[i].validate())
                   area.add(t[i].getArea());
               else {
                  System.out.println("Wrong Format");
                    System.exit(0);
               }
           }
          System.out.println("Original area:");
         double sum = 0;//面积总和
         for(int i = 0 ; i<area.size();i++){
            System.out.printf("%.2f ",area.get(i));
            sum+=area.get(i);
        }
         System.out.println();
         System.out.printf("Sum of area:%.2f\n",sum);
         System.out.println("Sorted area:");
         Collections.sort(area);
         for(int i = 0 ; i<area.size();i++){
            System.out.printf("%.2f ",area.get(i));
            
        }
         System.out.println();
         System.out.printf("Sum of area:%.2f\n",sum);
        }
        else
            System.out.println("Wrong Format");
    }             
}
import java.util.*;
class Shape{
    public Shape(){
        
    }
    public double getArea(){
        return 0.0;
    }
}

class Circle extends Shape{
    private double radius;
    public Circle(){
       
    }
    public void setRadius(double radius){
        this.radius = radius;
}
    public double getRadius(){
        return radius;
    }
    
    @Override
    public double getArea(){
        return radius * radius * Math.PI;
    }
}

class Rectangle extends Shape{
    private double width;
    private double length;
    public Rectangle(){
       
    }
    public double getWidth(){
        return this.width;
    }
    public void setWidth(double width){
            this.width = width;
    }
    public double getLength(){
        return this.length;
    }
    public void setLength(double length){
            this.length = length;
    }
    @Override
    public double getArea(){
        return width * length;
    }
}

class Ball extends Circle{
    public Ball(){
        System.out.println("Constructing Ball");
    }
    @Override
    public double getArea(){
        return 4 * super.getRadius() * super.getRadius() * Math.PI;
    }
    public double getVolume(){
        return 4.0 / 3.0 * Math.PI * Math.pow(super.getRadius(), 3);
    }
}

class Box extends Rectangle{
    private double height;
    public Box(){
        System.out.println("Constructing Box");
    }
    public void setHeight(double height){
            this.height = height;
    }
    public double getHeight(){
        return height;
    }
    @Override
    public double getArea(){
        return (super.getArea() + height * super.getWidth() + height * super.getLength()) * 2;
    }
    public double getVolume(){
        return height * super.getArea();
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
       
       
                double r = input.nextDouble();
                double length = input.nextDouble();
                double width = input.nextDouble();
                if (r <= 0||length <= 0 || width <= 0){
                    System.out.println("Wrong Format");
                 
                }
                else {
                Circle circle = new Circle();
                circle.setRadius(r);
                System.out.printf("%.2f\n", circle.getArea());            
                Rectangle rectangle = new Rectangle();
                rectangle.setLength(length);
                rectangle.setWidth(width);
                System.out.printf("%.2f\n", rectangle.getArea());
              
                }             
    }
    }

 

题目集6(7-5、7-6)

 7-5的题目运用的是图形的继承以及多态其余三个图形继承Shape这个父类并对其中的父类方法进行方法重写方法重载等

7-6的题目主要用的不同点是接口(interface)一个GetArea的接口ain类的主方法中分别定义一个圆类对象及矩形类对象

3.对三次题目集中用到的正则表达式技术的分析总结

在正则表达式的调用经常会输出空 (原因是“[""]"以及”(“”)“的运用会出问题就是[ ]中的正则表达式只能判断一位的的能力如果在其中判断多个采用|(或符号)的时候就会输出空同时基本也不会报错能运行

正则表达式的基本语法灵活调用还是需要时间去掌握的  部分(.  . 和 \   . 在正则表达式中表示任意一个字符           \ 在正则表达式当中表示转义字符,比如.在正则表达式当中表示任意一个字符,要是单纯的想要表达一个.的时候,就需           要用到\转义字符来表示, \. 在java程序当中\也是被认为特殊字符,也需要进行转义,应该用\\.

        字符集合[]
          [] 用来描述一个单一的字符,方括号内部可以定义这个字符的内容,也可以描述一个范围
          [abc] abc中的任意一个字符
          [a-z] 小写字母当中a-z的任意一个单词
          [A-Z] 大写字母当中A-Z中的任意一个单词
          [0-9] 数字0-9中的任意一个数字
          [a-zA-Z0-9] a-zA-z0-9中的任意一个字符
          [a-z&&[^bc]] a-z中除了bc以外的任意一个字母

4.题目集5(7-4)中Java集合框架应用的分析总结

在题集5中的7-4中Java集合框架应用的分析

  • Collection:普通集合的最基本接口 不唯一 无序;
  • List:Collection的子接口 不唯一 有序;
  • Set:Collection的子接口 唯一 无序;
  • Map:键值对集合 key–》value(key:不允许重复 value:允许重复);
  • Array可以容纳基本类型和对象,而ArrayList只能容纳对象;

  • Array是指定大小的,而ArrayList大小是动态变化的;

  • Array没有提供ArrayList那么多功能,比如addAll、removeAll和iterator等。尽管ArrayList明显是更好的选择,但也有些时候Array比较好用;

踩坑心得:

在7-4 正则表达式训练-学号校验 (7 分)中开始没注意中括号的具体隐藏他只能判断一个字符的能力

 

import java.util.Scanner;
public class Main{
public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String pattern ="2020(1[1-7]|61|7[1-3]|8[1-2])(0[1-9]|[1-3][0-9]|40)";
        boolean flag=str.matches(pattern);
        if(flag)
            System.out.println("正确");
        else
            System.out.println("错误");
        }
}

 

还有在7-5 图形继承与多态 (50 分)这题中在Main函数的编写中其中再不断由控制器的输入时对于参数的传递出现了问题在传参的时候直接用input.nextDouble但是在以类同名的构造方法中要用new声明一下

不然参数传不进去

c[i]=new  Circle(input.nextDouble());

 r[i]=new Rectangle(input.nextDouble(),input.nextDouble());

以及对于后续的换行格式化输出等等

System.out.println("Original area:");
double sum = 0;//面积总和
for(int i = 0 ; i<area.size();i++){
System.out.printf("%.2f ",area.get(i));
sum+=area.get(i);
}
System.out.println();
System.out.printf("Sum of area:%.2f\n",sum);
System.out.println("Sorted area:");
Collections.sort(area);
for(int i = 0 ; i<area.size();i++){
System.out.printf("%.2f ",area.get(i));

}
System.out.println();
System.out.printf("Sum of area:%.2f\n",sum);
}
else
System.out.println("Wrong Format");

 

改进建议:

  • 多灵活运用正则表达式熟悉正则表达式中相关联的函数用法等
  • 多查阅一些资料不要卡在一个地方就一直纠结
  • 多使用Java中的思想封装啊多态啊接口啊理解其中的优缺点以及适用性
  • 对于一些难度较高的问题要勇于尝试以及需要多花时间再积累好自己的经验

总结:

  • 多提高自己的编程能力;
  • 多总结正则表达式的编程经验以及对于语法的简化;
  • 学习到了很多新的编程知识以及对于正则表达式能力的运用封装的熟练运用以及对于接口啊方法种在以及重写的区分等的初步了解以及运用;
  • 希望老师能题集结束公布部分优秀代码;
  • 自己在正则表达式以及对于很多Java中的思想还需要大量学习还需要掌握其中的抽象多态等的灵活运用;
posted @ 2021-04-29 12:32  AverageJoe24  阅读(71)  评论(0)    收藏  举报