题目集4-6次总结

发布时间 2023-04-27 23:59:50作者: 邓烜

(1)前言:

    第四次的题目集有七道题,它主要考察我们一些一维数组的定义、创建及使用,利用arrays.sort(数组命)的方式给数组从小到大排序,利用sqlit将一个字符串按特定的符号分割并穿入一个数组当中,初步尝试对象的封装,初步了解Integer类中的parsenInt方法(用于将一个字符类型的变量转换成整形类型的变量),Local类中的of()、isAfter()、isBefore()、until()等方法(它们分别是创建一个LocalDate类型的值,判断2个LocalDate类型的日期的先后,2个LocalDate类型日期中间的天数),ChronoUnit类中DAYS、WEEKS、MONTHS等方法(分别求2个日期的天数,周数、月数),总体上难度适中,所接触的新方法比较多。第五次的题目集一共有六道题 ,这次的题目集主要考察我们对于正则表达式的使用,以及对日期类设计的迭代。这次根据特定的有条理的类图来写程序,考察父类子类的关系及运用,难度也适中。第六次题目集只有一道题,为菜单程序设计,要求对于父类子类的运用熟练,难度较大。

(2)设计与分析:

  题目集四 7-5  

参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:


应用程序共测试三个功能:

求下n天
求前n天
求两个日期相差的天数
注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

输入格式:
有三种输入方式(以输入的第一个数字划分[1,3]):

1 year month day n //测试输入日期的下n天
2 year month day n //测试输入日期的前n天
3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
输出格式:
当输入有误时,输出格式如下:
Wrong Format
当第一个数字为1且输入均有效,输出格式如下:
year-month-day
当第一个数字为2且输入均有效,输出格式如下:
year-month-day
当第一个数字为3且输入均有效,输出格式如下:
天数值

看到题目我会先考虑main函数里面应该会有什么,之后从Year类开始,慢慢递增写Month类、Day类、DateUtil类。

Year类:

根据类图写函数从无参构造方法和有参构造方法到闰年的判断、数据是否合法、年份的增减这与之前的日期类多了年份的增减其次年月日种种类全都放在一个Date类里,不符合单一原则。

class Year{
    int value;
    //默认构造方法
    Year(){
        
    }
    //带参构造方法
    Year(int value){
        this.value = value;
    }
    //value getter
    int getValue() {
        return this.value;
    }
     //value setter
    void setValue (int value) {
        this.value = value;
    }
   //判断是否为闰年
     public boolean isLeapYear() {
            boolean a;
            if((value % 4 == 0 && value % 100 != 0) || value % 400 == 0)
                a = true;
            else a = false;
            return a;
    }
    //检验数据是否合法
    boolean validate() {
        boolean a;
        if(value >= 1900 && value <= 2050) {
            a = true;
        }
        else a = false;
        return a;
    }
    //年份加一
    void yearIncrement() {
        this.value = this.value + 1;
    }
    //年份减一
    void yearReduction() {
        this.value = this.value - 1;
    }
}

 

Month类:

这次的year类型不再是int而是Year,这样其中year可以用到Year里面的方法,会更加简便。在带参构造方法时应该用Year里面的带参构造方法去构造year。

class Month{
    int value;
    Year year;
    //无参构造方法
    Month(){
        
    }
    //带参构造方法
    Month(int yearValue , int monthValue){
        this.value = monthValue;
        this.year = new Year(yearValue);
    }
    //value getter
    int getValue() {
        return this.value;
    }
    //value setter
    void setValue (int value) {
        this.value = value;
    }
    //year getter
    Year getYear() {
        return this.year;
    }
    //year setter
    void setYear (Year year) {
    this.year = year;
    }
    //月份复位(1)
    void resetMin() {
        this.value = 1;
    }
    //月份设置为12
    void reserMax() {
        this.value = 12;
    }
    //校验数据合法性
    boolean validate() {
        boolean a;
        if(value >= 1 && value <= 12) {
            a = true;
        }
        else a = false;
        return a;
    }
    //月份加一
    void monthIncrement() {
        this.value = this.value + 1;
    }
    //月份减一
    void monthReduction() {
        this.value = this.value - 1;
    }
}

Day类:

其中在建立对象时,不在有Year类型的变量,因为在构造Month时包含了Year类型的变量,同时应该构建一个含每个月天数的数组,这样可以更加实现resetMax和validate这2个方法。

class Day{
    int value;
    Month month;
    int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
    //默认构造方法
    Day(){
        
    }
    //带参构造方法
    Day(int yearValue,int monthValue,int dayValue){
        this.value = dayValue;
        this.month = new Month(yearValue,monthValue);
    }
    //value getter
    int getValue() {
        return this.value;
    }
    //value setter
    void setValue (int value) {
        this.value = value;
    }
    //month getter
    Month getMonth() {
        return this.month;
    }
    //month setter
    void setMonth (Month month) {
    this.month = month;
    }
    //日期复位(1)
    void resetMin() {
        this.value = 1;
    }
    //日期设为该为的最大值
    void resetMax() {
        this.value = mon_maxnum[month.value-1];
    }
    //校验日期的合法性
       public boolean validate(){
           boolean a = false;
           if(month.value<=12&&month.value>=1) {
            if(this.getMonth().getYear().isLeapYear())
                mon_maxnum[1]=29;
            if(value>=1&&value<=mon_maxnum[month.getValue()-1])
                a = true;
            else
                a = false;
           }
            return a;
        }
    //日期加1
    void dayIncrement() {
        this.value = this.value + 1;
    }
    //日期减1
    void dayReduction() {
        this.value = this.value - 1;
    }
}

DateUtil类:

这个类时实现求前n天、后n天以及2个日期之间天数的类。这一次较之前的题目,在整体结构上差距不大,主要不同在变量的表达方式上。

class DateUtil{
    Day day;
    //默认构造方法
    DateUtil(){
        
    }
    //带参构造方法
    DateUtil(int d,int m,int y){
        this.day = new Day(d,m,y);
    }
    //day getter
    Day getDay() {
        return day;
    }
    //day setter
    void setDay(Day d) {
        this.day = d;
    }
    //检查数据合法性
    boolean checkInputValidity() {
        boolean a;
        if(this.getDay().validate()&&this.getDay().getMonth().validate()&&this.getDay().getMonth().getYear().validate()) {
            a=true;
        }
        else a=false;
        return a;
    }
    //比较2个日期的大小
     boolean compareDates(DateUtil date) {
         boolean a;
        if(date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue())
            a = false;
        else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue())
            a = false;
        else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue())
            a = false;
        else
            a = true;
        return a;
    }
    //判断2个日期是否相等
     boolean equalTwoDates(DateUtil date){
         boolean a;
        if(this.getDay().getValue()==date.getDay().getValue()&&this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()&& this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue())
            a = true;
        else
            a = false;
        return a;
    }
     //日期值格式化
      String showDate(){
         return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue();
     }
      //求下n天
   public DateUtil getNextNDays(int n){
       int year2 = 0,month2 = 0,day2 = 0;
       int i = 1;
       year2 = this.getDay().getMonth().getYear().getValue();
       month2 = this.getDay().getMonth().getValue();
       day2 = this.getDay().getValue();
       while(i<=n) {
           if(!new Year(year2).isLeapYear()) {
               this.getDay().mon_maxnum[1]=28;
           }
           else this.getDay().mon_maxnum[1]=29;
            if(day2+1<=this.getDay().mon_maxnum[month2-1])
                day2++;
            else if(month2+1<=12)
            {
                month2++;
                day2=1;
            }
            else 
            {
                year2++;
                month2=1;
                day2=1;
            }
            i++;
       }
      return new DateUtil(year2,month2,day2);
   }
        //求前n天
      public DateUtil getPreviousNDays(int n){
       int year2 = 0,month2 = 0,day2 = 0;
       int i = 1;
       year2 = this.getDay().getMonth().getYear().getValue();
       month2 = this.getDay().getMonth().getValue();
       day2 = this.getDay().getValue();
       while(i<=n) {
           if(!new Year(year2).isLeapYear()) {
               this.getDay().mon_maxnum[1]=28;
           }
           else this.getDay().mon_maxnum[1]=29;
            if(day2-1>0) {
                day2--;
            }
                
            else if(month2-1>=1)
            {
                month2--;
                day2=this.getDay().mon_maxnum[month2-1];
            }
            else 
            {
                year2--;
                month2=12;
                day2=31;
            }
            i++;
       }
      return new DateUtil(year2,month2,day2);
      }
    //求2个日期之间的天数
     public int getDaysofDates(DateUtil date){
          int i,j,d = 0;
          int year1=0,month1=0,day1=0;
          int year2=0,month2=0,day2=0;
          DateUtil newDate1 = new DateUtil(year2,month2,day2);
          DateUtil newDate2 = new DateUtil(year2,month2,day2);
     if (this.equalTwoDates(date)) {
         return 0;
     }
     else if (!this.compareDates(date)) {
         year1=this.getDay().getMonth().getYear().getValue();
         month1=this.getDay().getMonth().getValue();
         day1=this.getDay().getValue();
         year2=date.getDay().getMonth().getYear().getValue();
         month2=date.getDay().getMonth().getValue();
         day2=date.getDay().getValue();
     }
     else {
         year2=this.getDay().getMonth().getYear().getValue();
         month2=this.getDay().getMonth().getValue();
         day2=this.getDay().getValue();
         year1=date.getDay().getMonth().getYear().getValue();
         month1=date.getDay().getMonth().getValue();
         day1=date.getDay().getValue();
     }
     for(i=year2+1;i<year1;i++) {
         newDate2.getDay().getMonth().getYear().yearIncrement();
          if(newDate2.getDay().getMonth().getYear().isLeapYear()) 
              d=d+366;
          else d=d+365;
     }
     if (year1!=year2) {
         for(j=month2+1;j<=12;j++){
           if(new Year(year2).isLeapYear()){
               this.getDay().mon_maxnum[1]=29;
            }
             d=d+this.getDay().mon_maxnum[j-1];
         }
         d+=this.getDay().mon_maxnum[month2-1]-day2;
         for(j=1;j<month1;j++){
            if(newDate1.getDay().getMonth().getYear().isLeapYear()){
                this.getDay().mon_maxnum[1]=29;
            }
             d+=this.getDay().mon_maxnum[j-1];
         }
            d+=day1;
     }
     else if(year2==year1&&month2!=month1){
         for(j=month2+1;j<=month1-1;j++){
           if(new Year(year2).isLeapYear()){
               this.getDay().mon_maxnum[1]=29;
           }
             d+=this.getDay().mon_maxnum[j-1];
         }
         d+=day1+this.getDay().mon_maxnum[month2-1]-day2;
     }
     else if(year1==year2&&month1==month2)
         d=day1-day2;
     return d;
      }
}

Main类:

主函数还是和之前一样,只在输出格式上改动一下。

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.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.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(fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else{
            System.out.println("Wrong Format");
            System.exit(0);
        }        
    }
}

题目集四 7-6

参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

应用程序共测试三个功能:

求下n天
求前n天
求两个日期相差的天数
注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

输入格式:
有三种输入方式(以输入的第一个数字划分[1,3]):

1 year month day n //测试输入日期的下n天
2 year month day n //测试输入日期的前n天
3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
输出格式:
当输入有误时,输出格式如下:
Wrong Format
当第一个数字为1且输入均有效,输出格式如下:
year1-month1-day1 next n days is:year2-month2-day2
当第一个数字为2且输入均有效,输出格式如下:
year1-month1-day1 previous n days is:year2-month2-day2
当第一个数字为3且输入均有效,输出格式如下:
The days between year1-month1-day1 and year2-month2-day2 are:值

这次不像7-5一般,是Year和Month关联、Month和Day关联、Day和DateUtil关联、最后Main和DateUtil关联,依次关联。7-6是直接Day、Month、Year、Main与DateUtil关联,这样在引用year和month不用那么麻烦,同时year、month、day相互没有关联。

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.showDate() + " 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.showDate() + " 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{
    Year year;
    Month month;
    Day day;
    int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
    //默认构造方法
    DateUtil(){
        
    }
    //带参构造方法
    DateUtil(int y,int m,int d){
        this.year = new Year(y);
        this.month = new Month(m);
        this.day = new Day(d);
    }
    //year getter
    Year getYear() {
        return year;
    }
    //year setter
    void setYear(Year year) {
        this.year = year;
    }
    //month getter
    Month getMonth() {
        return this.month;
    }
    //month setter
    void setMonth(Month month ) {
        this.month = month;
    }
    //day getter
    Day getDay() {
        return day;
    }
    //day setter
    void setDay(Day d) {
        this.day = d;
    }
    //日期复位(1)
    void resetMin() {
        this.day = new Day(1);
    }
    //日期设为该为的最大值
    void resetMax() {
        this.day = new Day(mon_maxnum[month.value-1]);
    }
    //检查数据合法性
    boolean checkInputValidity() {
        boolean a = false;
           if(month.value<=12&&month.value>=1) {
                if(this.getYear().isLeapYear())
                    mon_maxnum[1]=29;
        if(this.getDay().getValue()>=1&&this.getDay().getValue()<=mon_maxnum[month.getValue()-1]&&this.getMonth().validate()&&this.getYear().validate()) {
            a=true;
        }
        else a=false;
           }
        return a;
    }
    //比较2个日期的大小
     boolean compareDates(DateUtil date) {
         boolean a;
        if(date.getYear().getValue()<this.getYear().getValue())
            a = false;
        else if(date.getYear().getValue()==this.getYear().getValue()&&date.getMonth().getValue()<this.getMonth().getValue())
            a = false;
        else if(date.getYear().getValue()==this.getYear().getValue()&&date.getMonth().getValue()==this.getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue())
            a = false;
        else
            a = true;
        return a;
    }
    //判断2个日期是否相等
     boolean equalTwoDates(DateUtil date){
         boolean a;
        if(this.getDay().getValue()==date.getDay().getValue()&&this.getMonth().getValue()==date.getMonth().getValue()&& this.getYear().getValue()==date.getYear().getValue())
            a = true;
        else
            a = false;
        return a;
    }
     //日期值格式化
      String showDate(){
         return this.getYear().getValue()+"-"+this.getMonth().getValue()+"-"+this.getDay().getValue();
     }   
      //求下n天
   public DateUtil getNextNDays(int n){
       int year2 = 0,month2 = 0,day2 = 0;
       int i = 1;
       year2 = this.getYear().getValue();
       month2 = this.getMonth().getValue();
       day2 = this.getDay().getValue();
       while(i<=n) {
           if(!new Year(year2).isLeapYear()) {
               this.mon_maxnum[1]=28;
           }
           else this.mon_maxnum[1]=29;
            if(day2+1<=this.mon_maxnum[month2-1])
                day2++;
            else if(month2+1<=12)
            {
                month2++;
                day2=1;
            }
            else 
            {
                year2++;
                month2=1;
                day2=1;
            }
            i++;
       }
      return new DateUtil(year2,month2,day2);
   }
      //求前n天
      public DateUtil getPreviousNDays(int n){
       int year2 = 0,month2 = 0,day2 = 0;
       int i = 1;
       year2 = this.getYear().getValue();
       month2 = this.getMonth().getValue();
       day2 = this.getDay().getValue();
       while(i<=n) {
           if(!new Year(year2).isLeapYear()) {
               this.mon_maxnum[1]=28;
           }
           else this.mon_maxnum[1]=29;
            if(day2-1>0) {
                day2--;
            }
                
            else if(month2-1>=1)
            {
                month2--;
                day2=this.mon_maxnum[month2-1];
            }
            else 
            {
                year2--;
                month2=12;
                day2=31;
            }
            i++;
       }
      return new DateUtil(year2,month2,day2);
      }
      //求2个日期之间的天数
      public int getDaysofDates(DateUtil date){
          int i,j,d = 0;
          int year1=0,month1=0,day1=0;
          int year2=0,month2=0,day2=0;
          DateUtil newDate1 = new DateUtil(year2,month2,day2);
          DateUtil newDate2 = new DateUtil(year2,month2,day2);
     if (this.equalTwoDates(date)) {
         return 0;
     }
     else if (!this.compareDates(date)) {
         year1=this.getYear().getValue();
         month1=this.getMonth().getValue();
         day1=this.getDay().getValue();
         year2=date.getYear().getValue();
         month2=date.getMonth().getValue();
         day2=date.getDay().getValue();
     }
     else {
         year2=this.getYear().getValue();
         month2=this.getMonth().getValue();
         day2=this.getDay().getValue();
         year1=date.getYear().getValue();
         month1=date.getMonth().getValue();
         day1=date.getDay().getValue();
     }
     for(i=year2+1;i<year1;i++) {
         newDate2.getYear().yearIncrement();
          if(newDate2.getYear().isLeapYear()) 
              d=d+366;
          else d=d+365;
     }
     if (year1!=year2) {
         for(j=month2+1;j<=12;j++){
           if(new Year(year2).isLeapYear()){
               this.mon_maxnum[1]=29;
            }
             d=d+this.mon_maxnum[j-1];
         }
         d+=this.mon_maxnum[month2-1]-day2;
         for(j=1;j<month1;j++){
            if(newDate1.getYear().isLeapYear()){
                this.mon_maxnum[1]=29;
            }
             d+=this.mon_maxnum[j-1];
         }
            d+=day1;
     }
     else if(year2==year1&&month2!=month1){
         for(j=month2+1;j<=month1-1;j++){
           if(new Year(year2).isLeapYear()){
               this.mon_maxnum[1]=29;
           }
             d+=this.mon_maxnum[j-1];
         }
         d+=day1+this.mon_maxnum[month2-1]-day2;
     }
     else if(year1==year2&&month1==month2)
         d=day1-day2;
     return d;
      }
}
class Day{
    int value;
    //默认构造方法
    Day(){
        
    }
    //带参构造方法
    Day(int Value){
        this.value = Value;
    }
    //value getter
    int getValue() {
        return this.value;
    }
    //value setter
    void setValue (int value) {
        this.value = value;
    }
    //日期加1
    void dayIncrement() {
        this.value = this.value + 1;
    }
    //日期减1
    void dayReduction() {
        this.value = this.value - 1;
    }
}

class Month{
    int value;
    //无参构造方法
    Month(){
        
    }
    //带参构造方法
    Month(int Value){
        this.value = Value;
    }
    //value getter
    int getValue() {
        return this.value;
    }
    //value setter
    void setValue (int value) {
        this.value = value;
    }
    //月份复位(1)
    void resetMin() {
        this.value = 1;
    }
    //月份设置为12
    void reserMax() {
        this.value = 12;
    }
    //校验数据合法性
    boolean validate() {
        boolean a;
        if(value >= 1 && value <= 12) {
            a = true;
        }
        else a = false;
        return a;
    }
    //月份加一
    void monthIncrement() {
        this.value = this.value + 1;
    }
    //月份减一
    void monthReduction() {
        this.value = this.value - 1;
    }
}

class Year{
    int value;
    //默认构造方法
    Year(){
        
    }
    //带参构造方法
    Year(int value){
        this.value = value;
    }
    //value getter
    int getValue() {
        return this.value;
    }
     //value setter
    void setValue (int value) {
        this.value = value;
    }
   //判断是否为闰年
     public boolean isLeapYear() {
            boolean a;
            if((value % 4 == 0 && value % 100 != 0) || value % 400 == 0)
                a = true;
            else a = false;
            return a;
    }
    //检验数据是否合法
    boolean validate() {
        boolean a;
        if(value >= 1820 && value <= 2020) {
            a = true;
        }
        else a = false;
        return a;
    }
    //年份加一
    void yearIncrement() {
        this.value = this.value + 1;
    }
    //年份减一
    void yearReduction() {
        this.value = this.value - 1;
    }
}

但这次较之前在实现3个方法时,改变了方法,例如后2天,采用了一天一天加,n天就n次循环,如果该天在该年的最后一天,则该年加1,月份和天数都变为1,如果该天在该月的最后一天,则天数变为1,月份加1,可以采用年,月,日数值加一的方法,还有数值变为1和上月最大天数的方法,但是在做题目的时候没有用,是直接采用year++或者day=1的方法去增加数值或者改变数值。在做这道题的时候我是先从year、month和year3个类方法入手,因为DateUtil这个类许多的方法需要用到这3个类的方法,其次这3个类都是独立存在的,都只与DateUtil有依赖的关系,所以改动会更加容易一些,这3个类改完之后,DateUtil类改动最大的我认为是年月日数值的表示,以及对象的构造方法,在方法整体结构上与上一题没有太大差异。

对于5-1和7-1,在答案上我没有做出来,在创造解决问题的方法没有完全做出来,加上我认为这2道题在整体上,关系是比较紧密的,这2个方法没出来,可能导致整体没办法实现,虽然我没有做出来,但是在eclipse上也花费了大量的时间,在其中我也了解到了许多方法,更加熟练的理解如何根据一个需求去创造类,以及更加熟练去的去分析一个类一个方法,但可能在实现方法上存在一些欠缺,这也让更加明白了自己的不足之处和自己应该努力的方向。

(3)踩坑心得

1.在7-5中有参创造Month函数时,直接用

year = yearValue;

这样会导致编译错误因为year是Year类型的变量,而yearValue是整型的数值,不能直接用=去赋值;

year = (Year)yearValue;

之后尝试用强制转换,但明显不合理;

this.year = new Year(yearValue);

之后想到year是Year类型的变量,而Year里有构造对象的方法。

2.在7-5中在引用年份的值时一开始直接用this.year后来用this.getYear().getValue().

year2 = this.year;
year2 = this.getYear().getValue();

应该按照层层递进的方法读取year的值

year2 = this.getDay().getMonth().getYear().getValue();

3.在之前的日期类设计中是用isLeapYear(year2)进行闰年的判断,在这次设计中一开始我直接用year2.isLeapYear(),后来发现这个是Year类里面的方法,得用

New Year(Year2)先去创建对象,再去引用isLeapYear()这个方法。

new Year(year2).isLeapYear()

 4.在判断数据是否合理时,总是在月份上判断失误,因为在判断日数据是否合理时,采用的是一个存了12个数字的数组,如果月份大于12或者为负数是,数组超限等等错误就会出来,所以在判断日数据是否合理时,加了一个if语句来阻止这种错误的发生。

           if(month.value<=12&&month.value>=1) {
                if(this.getYear().isLeapYear())
                    mon_maxnum[1]=29;
        if(this.getDay().getValue()>=1&&this.getDay().getValue()<=mon_maxnum[month.getValue()-1]&&this.getMonth().validate()&&this.getYear().validate()) {
            a=true;
        }
        else a=false;
           }

 

(4)改进建议

 我觉得我编码了类中,有许多好的方法但是我并没有去用,换句话说并没有第一时间想到去使用所设计的方法,导致许多代码重复写,导致很累赘,给自己增加负担,就像是日期类设计,2道题明明都会在Year类,Day类、Month类写了一些数值增加数值减少以及数值化1,数值化为最大的方法,但是在实现求前n天、后n天、2个日期之间天数这3个方法时仍然自己重新写一遍代码,重复多余,其次自己写代码,一开始并没有条理性,总是不加思考就去写,还好先在eclipse上写,能清楚的告诉我错在哪里,不然要想好久,其次在引用类方法里面的数值时,一开始不懂得怎么去引用,但是现在更加明白和理解,。

(5)总结

在这三次题目集中我认为我收获最大的就是在写代码时不能把什么东西放在一个类里面,根据特性的不同,设计多个类,在利用关联、聚合等关系来达到实现方法的目的。这样可以减少类与类之间的高依赖性,同时增加了程序的高复用性,类的重用性。其次更加了解LocalDate类中函数的使用以及正则表达式的使用,明白里如何用一个函数将一个数组排序,如何用函数将一个字符串按特定的结构分割。我对正则表达式的运用不够熟练只知道初步使用,稍微难一点,需要灵活运用时,就无从下手。对于LocalDate类和integer类中许多方法应该深入了解。对于课程、实验和作业方面,我认为比较好的,在课后适当的布置作业,可以提高我们的学习的积极性,加速我们吸收课堂的知识,我认为可以多组织一些课上的活动。