四、五、六次PTA作业总结


 

(1).前言

  第一次作业:

     知识点:考察java的调用Array、Hashset、LocalDate、ChronoUnit类中的方法,以及基本的语法,还有对一些小数位数的处理。

     题量:7道题。

     难度:除第一题除外,其他题都较为简单。

  第二次作业:

     知识点:前四题考察java的正则表达式的运用,即利用正则表达式来进行字符和数字的判断;后面两题则是日期问题面向对象设计的迭代版本,主要考察对聚合的理解。

     题量:6道题

     难度:56两题较难,其他题目较简单。

  第三次作业:

    知识点:java的基本语法实现,除题目要求的类之外,以及自己设计的类。

    题量:1道题

    难度:,由于多重迭代,导致难度较大。

 


(2).设计与分析

             题目集(5):

            7-5:

                        需求分析:本题为上次日期类的迭代版本,不过要按照新的类间关系来写。即如下图的关系:

 

 

 

   类图说明:如图,Main,Day,Month,Year之间不断两两聚合,即不像上一次那样把year,month,day三个属性写在一个类里。

全部代码:

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 && choice <= 3){
        if (choice == 1) { // test getNextNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

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

            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.getNextNDays(n).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(day, month, year);

            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(day, month, year);
            DateUtil toDate = new DateUtil(anotherDay, anotherMonth, anotherYear);

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






 
class DateUtil{                    //DateUtil类
    private Day day;
    public DateUtil(){             //定义构造函数
        
    }
    
    public DateUtil( int d,int m,int y ){
        this.day = new Day( y,m,d );
    }
    public Day getDay(){             //setter和getter方法
         return day;
    }
    public void setDay(Day d){
        this.day = d;
    }
    
    public boolean checkInputValidity(){        //检测日期是否合法
        if(day.validate() && this.getDay().getMonth().validate() && this.getDay().getMonth().getYear().validate()){
            return true;
        }else return false;
    }
    
    public boolean compareDates(DateUtil date){           //比较两个日期的先后
        if(this.getDay().getMonth().getYear().getValue() > date.getDay().getMonth().getYear().getValue())
            return false;
        else if(this.getDay().getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue() && this.getDay().getMonth().getValue() > date.getDay().getMonth().getValue() )
            return false;
        else if(this.getDay().getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue() && this.getDay().getMonth().getValue() == date.getDay().getMonth().getValue() && this.getDay().getValue() > date.getDay().getValue() )
            return false;
        else
            return true;
    }
    
    public boolean equaltwoDates(DateUtil date){           //判断两个日期是否相等
        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()){
            return true;
        }else return false;
    }
    
    public String showDate(){                   //输出结果
        return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue();
    }
    
    public DateUtil getNextNDays(int n){              //计算下n天的方法
        int y = this.getDay().getMonth().getYear().getValue();
        int m = this.getDay().getMonth().getValue();
        int d = this.getDay().getValue();
        int i = 0,j = 0;
        int a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        
        while(n>365){
            if(new Year(y).isLeapYear() && m <= 2){
                if(m == 2 && d == 29 ){
                    m = 3;
                    d = 1;
                }
                y ++;
                n -= 366;
            }else if(new Year(y+1).isLeapYear() && m > 2){
                y ++;
                n -= 366;
            }else {
                y ++;
                n -= 365;
            }
        }
        for( i = 0 ; i < n ; i++)
        {
            d++;
            if( new Year(y).isLeapYear() && m == 2){
                if(d > 29){
                    m = 3;
                    d = 1;
                }
            }else if( d > a[m] ){
                if( m == 12 && d ==32){
                    y++;
                    m = 1;
                    d = 1;
                }else{
                    m ++;
                    d = 1;
                }

            }
            
        }
        return new DateUtil( d,m,y );
    }
     
    public DateUtil getPreviousNDays(int n){              //计算前n天的方法
        int y = this.getDay().getMonth().getYear().getValue();
        int m = this.getDay().getMonth().getValue();
        int d = this.getDay().getValue();
        int i = 0,j = 0;
        int a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        
        while(n>365){
           if(new Year(y).isLeapYear() && m > 2 ){
                y--;
                n-=366;
            }else if(new Year(y-1).isLeapYear() && m <= 2){
                y--;
                n-=366;
            }else{
                y--;
                n-=365;
            }
       }
        for( i = 0 ; i < n ; i++)
        {
            d--;
           if(d <= 0){
               m--;
               if(m <= 0){
                   m = 12;
                   y--;
               }
               if(new Year(y).isLeapYear() && m == 2){
                   d = 29;
               }else{
                   d = a[m];
               }
           }
        }
        return new DateUtil( d,m,y );
    }
    
    public int getDaysofDates(DateUtil date){        //计算两个给定日期的日期差
        int cnt1 = 0;
        int cnt2 = 0;
        int a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        
        for(int i = 1 ; i < this.getDay().getMonth().getYear().getValue() ; i++)
        {
            if(new Year(i).isLeapYear()){
                cnt1 += 366;
            }else cnt1 += 365;
        }
        for(int i = 1 ; i < this.getDay().getMonth().getValue() ; i++)
        {
            if(new Year(this.getDay().getMonth().getYear().getValue()).isLeapYear() && i==2){
                cnt1 += 29;
            }else cnt1 += a[i];
        }
        cnt1 = cnt1 + this.getDay().getValue();
        
        
        for(int i = 1 ; i < date.getDay().getMonth().getYear().getValue() ; i++)
        {
            if(new Year(i).isLeapYear()){
                cnt2 += 366;
            }else cnt2 += 365;
        }
        for(int i = 1 ; i < date.getDay().getMonth().getValue() ; i++)
        {
            if(new Year(date.getDay().getMonth().getYear().getValue()).isLeapYear() && i == 2){
                cnt2 += 29;
            }else cnt2 += a[i];
        }
        cnt2 = cnt2 + date.getDay().getValue();
        return (Math.abs(cnt1-cnt2));
    }
    
    
}
 
class Year{                 //Year类
    private int value;
    public Year(){           //构造方法
        
    }
    
    public Year(int value){      //setter和getter类
        this.value = value;
    }
    
    public int getValue(){
        return value;
    }
    
    public 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;
    }
      
    public boolean validate(){            //判断日期的合法性
        if( value >=1900 && value <= 2050){
            return true;
        }else return false;
    }
    
    public void yearIncrement(){           //年份加一
        value++;
    }
    
    public void yearReduction(){           //年份减一
        value--;
    }
}
 
class Month{                      //Month类
    private int value;
    private Year year;
    
    public Month(){                //构造方法
        
    }
    
    public Month(int yearValue,int monthValue){
        this.year = new Year(yearValue);
        this.value = monthValue;
    }
    
    public int getValue(){                     //setter和getter类
        return value;
    }
    
    public void setValue( int value ){
        this.value = value;
    }
    
    public Year getYear(){
        return year;
    }
    
    public void setYear(Year year){
        this.year = year;
    }
    
    public void resetMin(){             //重置日期
        value = 1;
    }
    public void resetMax(){
        value = 12;
    }
    
    public boolean validate(){             //检测日期合法性
        if(value >= 1 && value <= 12)
            return true;
        else 
            return false;
    }
    
    public void monthIncrement(){
        value++;
    }
    
    public void monthReduction(){
        value--;
    }
}

class Day{
    private int value;  
    private Month month;
    int[] mon_maxnum = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    
    public Day(){                        //构造方法
        
    }
    
    public Day(int yearValue,int monthValue,int dayValue){
        this.value = dayValue;
        this.month = new Month(yearValue,monthValue);
    }
    
    
    public int getValue(){                 //getter和setter类
        return value;
    }
    
    public void setValue( int value ){
        this.value = value;
    }
    
    public Month getMonth(){
        return month;
    }
    
    public void setMonth(Month month){
        this.month = month;
    }
    
    public void resetMin(){
        value = 1;
    }
    
    public void resetMax(){
        value = mon_maxnum[month.getValue()];
    }
    
    public boolean validate(){                      //检测日期合法性
        if(this.getMonth().getYear().isLeapYear())
            mon_maxnum[2] = 29;
        if(value>=1 && value <= mon_maxnum[month.getValue()])
            return true;
        else
            return false;

    }
    
    public void dayIncrement(){
        value++;
    }
    
    public void dayReduction(){
        value--;
    }
    
}

 

 

            源码分析:

                 本次题目大体上方法和上一迭代版本相似,只不过因为year,month,day,属性在分开的类里,但实际使用时有时需用到其他类的属性如month需用到year,day则需要用到year和month,故在Year,Month,Day类中都相应的添加了对应的getter和setter方法,例如我要引用year的值,即要用getDay().getMonth().getYear().getValue()来表示,引用month的值,要用getDay().getMonth().getValue()来表示,引用day的值,要用getDay().getValue()来表示;所以在计算输入日期的下n天或是上n天,由于原本的值不能去改变,所以引入了新的变量y、m、d,给他们赋予year、month、day的值,再用y,m,d的值来进行n天的计算,最后输出结果,另外,再检测日期合法性的部分,和之前不一样的是,之前是在一个方法里完成检测返回ture或false,本题则是在Year类中检测年份的合法性,Month类中检测月份的合法性,再在Day类中检测日期合法性的同时&&上以上两个条件,同时成立才满足合法的条件。此外还有一个小细节,与上次不同的是,这次题目输出格式不同,许稍加改动。

 

         

 

 

       7-6:

                       需求分析:

 

                                      本题要求大体和习题集(5)中的7-5大体相同,也同为聚合,但聚合的方式不同,故类图也就不一样,如图,题目要求按照如图来进行设计加以改造:

 

 

 

 

全部代码:

  1 import java.util.Scanner;
  2 
  3 public class Main {                              //主方法
  4     public static void main(String[] args) {
  5         Scanner input = new Scanner(System.in);
  6         int year = 0;
  7         int month = 0;
  8         int day = 0;
  9 
 10         int choice = input.nextInt();
 11         if(choice >=1 && choice <= 3){
 12         if (choice == 1) { // test getNextNDays method
 13             int n = 0;
 14             year = Integer.parseInt(input.next());
 15             month = Integer.parseInt(input.next());
 16             day = Integer.parseInt(input.next());
 17 
 18             DateUtil date = new DateUtil(year, month, day);
 19 
 20             if (!date.checkInputValidity()) {
 21                 System.out.println("Wrong Format");
 22                 System.exit(0);
 23             }
 24             n = input.nextInt();
 25             if (n < 0) {
 26                 System.out.println("Wrong Format");
 27                 System.exit(0);
 28             }
 29             System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + n + " days is:");
 30             System.out.println(date.getNextNDays(n).showDate());
 31         } else if (choice == 2) { // test getPreviousNDays method
 32             int n = 0;
 33             year = Integer.parseInt(input.next());
 34             month = Integer.parseInt(input.next());
 35             day = Integer.parseInt(input.next());
 36 
 37             DateUtil date = new DateUtil(year, month, day);
 38 
 39             if (!date.checkInputValidity()) {
 40                 System.out.println("Wrong Format");
 41                 System.exit(0);
 42             }
 43             n = input.nextInt();
 44             if (n < 0) {
 45                 System.out.println("Wrong Format");
 46                 System.exit(0);
 47             }
 48             System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
 49             System.out.println(date.getPreviousNDays(n).showDate());
 50         } else if (choice == 3) {    //test getDaysofDates method
 51             year = Integer.parseInt(input.next());
 52             month = Integer.parseInt(input.next());
 53             day = Integer.parseInt(input.next());
 54 
 55             int anotherYear = Integer.parseInt(input.next());
 56             int anotherMonth = Integer.parseInt(input.next());
 57             int anotherDay = Integer.parseInt(input.next());
 58 
 59             DateUtil fromDate = new DateUtil(year, month, day);
 60             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 61 
 62             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
 63                   System.out.println("The days between " + fromDate.showDate() + " and " + toDate.showDate() + " are:" + fromDate.getDaysofDates(toDate));
 64             } else {
 65                 System.out.println("Wrong Format");
 66                 System.exit(0);
 67             }
 68         }
 69         else{
 70             System.out.println("Wrong Format");
 71             System.exit(0);
 72         }        }else{
 73             System.out.println("Wrong Format");
 74         }
 75         
 76         
 77     }
 78 }
 79 
 80 
 81 class DateUtil{                    //DateUtil类
 82     private Year year;
 83     private Month month;
 84     private Day day;
 85     int mon_maxnum[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
 86     public DateUtil(){             //定义构造函数
 87         
 88     }
 89     
 90     public DateUtil( int y,int m,int d ){
 91         this.year = new Year(y);
 92         this.month = new Month(m);
 93         this.day = new Day(d);
 94     }
 95     public Day getDay(){             //setter和getter方法
 96          return day;
 97     }
 98     public void setDay(Day day){
 99         this.day = day;
100     }
101     
102     public Year getYear(){
103         return year;
104     }
105     
106     public void setYear(Year year){
107         this.year = year;
108     }
109     
110     public Month getMonth(){
111         return month;
112     }
113     
114     public void setMonth(Month month){
115         this.month = month;
116     }
117     
118     public boolean checkInputValidity(){        //检测日期是否合法
119         if(year.validate() && month.validate()) {
120             if(year.isLeapYear() && month.getValue() ==2 ){
121                 if( day.getValue() >= 1 && day.getValue() <=29 )
122                     return true;
123                 else
124                     return false;
125             }else{
126                 if( day.getValue() >= 1 && day.getValue() <= mon_maxnum[month.getValue()] )
127                     return true;
128                 else
129                     return false;
130             }
131         }else return false;
132     }
133     
134     public boolean compareDates(DateUtil date){           //比较两个日期的先后
135         if(this.getYear().getValue() > date.getYear().getValue())
136             return false;
137         else if(this.getYear().getValue() == date.getYear().getValue() && this.getMonth().getValue() > date.getMonth().getValue() )
138             return false;
139         else if(this.getYear().getValue() == date.getYear().getValue() && this.getMonth().getValue() == date.getMonth().getValue() && this.getDay().getValue() > date.getDay().getValue() )
140             return false;
141         else
142             return true;
143     }
144     
145     public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等
146     {
147         if(this.year != date.getYear())
148             return false;
149         else if(this.day != date.getDay())
150             return false;
151         else if(this.month != date.getMonth())
152             return false;
153         else
154             return true;
155     }
156     
157     public String showDate(){                   //输出结果
158         return(year.getValue()+"-"+month.getValue()+"-"+day.getValue());
159     }
160     
161     public DateUtil getNextNDays(int n){              //计算下n天的方法
162         int y = this.getYear().getValue();
163         int m = this.getMonth().getValue();
164         int d = this.getDay().getValue();
165         int i = 0,j = 0;
166         int a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
167         
168         while(n>365){
169             if(new Year(y).isLeapYear() && m <= 2){
170                 if(m == 2 && d == 29 ){
171                     m = 3;
172                     d = 1;
173                 }
174                 y ++;
175                 n -= 366;
176             }else if(new Year(y+1).isLeapYear() && m > 2){
177                 y ++;
178                 n -= 366;
179             }else {
180                 y ++;
181                 n -= 365;
182             }
183         }
184         for( i = 0 ; i < n ; i++)
185         {
186             d++;
187             if( new Year(y).isLeapYear() && m == 2){
188                 if(d > 29){
189                     m = 3;
190                     d = 1;
191                 }
192             }else if( d > a[m] ){
193                 if( m == 12 && d ==32){
194                     y++;
195                     m = 1;
196                     d = 1;
197                 }else{
198                     m ++;
199                     d = 1;
200                 }
201 
202             }
203             
204         }
205         return new DateUtil( y,m,d );
206     }
207      
208     public DateUtil getPreviousNDays(int n){              //计算前n天的方法
209         int y = this.getYear().getValue();
210         int m = this.getMonth().getValue();
211         int d = this.getDay().getValue();
212         int i = 0,j = 0;
213         int a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
214         
215         while(n>365){
216            if(new Year(y).isLeapYear() && m > 2 ){
217                 y--;
218                 n-=366;
219             }else if(new Year(y-1).isLeapYear() && m <= 2){
220                 y--;
221                 n-=366;
222             }else{
223                 y--;
224                 n-=365;
225             }
226        }
227         for( i = 0 ; i < n ; i++)
228         {
229             d--;
230            if(d <= 0){
231                m--;
232                if(m <= 0){
233                    m = 12;
234                    y--;
235                }
236                if(new Year(y).isLeapYear() && m == 2){
237                    d = 29;
238                }else{
239                    d = a[m];
240                }
241            }
242         }
243         return new DateUtil( y,m,d );
244     }
245     
246     public int getDaysofDates(DateUtil date){        //计算两个给定日期的日期差
247         int cnt1 = 0;
248         int cnt2 = 0;
249         int a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
250         
251         for(int i = 1 ; i < this.getYear().getValue() ; i++)
252         {
253             if(new Year(i).isLeapYear()){
254                 cnt1 += 366;
255             }else cnt1 += 365;
256         }
257         for(int i = 1 ; i < this.getMonth().getValue() ; i++)
258         {
259             if(new Year(this.getYear().getValue()).isLeapYear() && i==2){
260                 cnt1 += 29;
261             }else cnt1 += a[i];
262         }
263         cnt1 = cnt1 + this.getDay().getValue();
264         
265         
266         for(int i = 1 ; i < date.getYear().getValue() ; i++)
267         {
268             if(new Year(i).isLeapYear()){
269                 cnt2 += 366;
270             }else cnt2 += 365;
271         }
272         for(int i = 1 ; i < date.getMonth().getValue() ; i++)
273         {
274             if(new Year(date.getYear().getValue()).isLeapYear() && i == 2){
275                 cnt2 += 29;
276             }else cnt2 += a[i];
277         }
278         cnt2 = cnt2 + date.getDay().getValue();
279         return (Math.abs(cnt1-cnt2));
280     }
281     
282     
283 }
284  
285 class Year{                 //Year类
286     private int value;
287     public Year(){           //构造方法
288         
289     }
290     
291     public Year(int value){      //setter和getter类
292         this.value = value;
293     }
294     
295     public int getValue(){
296         return value;
297     }
298     
299     public void setValue(int value){
300         this.value = value;
301     }
302     
303     public boolean isLeapYear(){      ///判断闰年
304         if( (value % 4 == 0 && value % 100 != 0) ||value % 400 == 0){
305             return true;
306         }else  return false;
307     }
308       
309     public boolean validate(){            //判断日期的合法性
310         if( value >=1820 && value <= 2020){
311             return true;
312         }else return false;
313     }
314     
315     public void yearIncrement(){           //年份加一
316         value++;
317     }
318     
319     public void yearReduction(){           //年份减一
320         value--;
321     }
322 }
323  
324 class Month{                      //Month类
325     private int value;
326     public Month(){                //构造方法
327         
328     }
329     
330     public Month(int value){
331         this.value = value;
332     }
333     
334     public int getValue(){                     //setter和getter类
335         return value;
336     }
337     
338     public void setValue( int value ){
339         this.value = value;
340     }
341     
342 
343     
344     public void resetMin(){             //重置日期
345         value = 1;
346     }
347     public void resetMax(){
348         value = 12;
349     }
350     
351     public boolean validate(){             //检测日期合法性
352         if(value >=1 && value <=12)
353             return true;
354         else 
355             return false;
356     }
357     
358     public void monthIncrement(){
359         value++;
360     }
361     
362     public void monthReduction(){
363         value--;
364     }
365 }
366 
367 class Day{
368     private int value;  
369     public Day(){                        //构造方法
370         
371     }
372     
373     public Day(int value){
374         this.value = value;
375     }
376     
377     
378     public int getValue(){                 //getter和setter类
379         return value;
380     }
381     
382     public void setValue( int value ){
383         this.value = value;
384     }
385 
386     public void dayIncrement(){
387         value++;
388     }
389     
390     public void dayReduction(){
391         value--;
392     }
393     
394 }

 

 源码分析:

          本题和7-5一样,Year、Month、Day类都分开存在,只不过它们都是各自和DateUtil聚合,各自没有关联关系,所以再调用Year、Month、day变量时又不一样,其他方法大体一样,只是把部分原来在Year、Month、Day类中的方法移到了DateUtil类中。

 

 

习题集(6):

           7-1:

                      需求分析:本题是习题集四7-1的迭代版本,在原来的基础上添加了一些东西,在输入的要求上以及条件判断上和输出的内容也有改变,

全部代码:

  1 import java.util.Scanner;
  2 
  3 public class Main {                              //主方法
  4     public static void main(String[] args) {
  5         Scanner input = new Scanner(System.in);
  6         int year = 0;
  7         int month = 0;
  8         int day = 0;
  9 
 10         int choice = input.nextInt();
 11         if(choice >=1 && choice <= 3){
 12         if (choice == 1) { // test getNextNDays method
 13             int n = 0;
 14             year = Integer.parseInt(input.next());
 15             month = Integer.parseInt(input.next());
 16             day = Integer.parseInt(input.next());
 17 
 18             DateUtil date = new DateUtil(year, month, day);
 19 
 20             if (!date.checkInputValidity()) {
 21                 System.out.println("Wrong Format");
 22                 System.exit(0);
 23             }
 24             n = input.nextInt();
 25             if (n < 0) {
 26                 System.out.println("Wrong Format");
 27                 System.exit(0);
 28             }
 29             System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + n + " days is:");
 30             System.out.println(date.getNextNDays(n).showDate());
 31         } else if (choice == 2) { // test getPreviousNDays method
 32             int n = 0;
 33             year = Integer.parseInt(input.next());
 34             month = Integer.parseInt(input.next());
 35             day = Integer.parseInt(input.next());
 36 
 37             DateUtil date = new DateUtil(year, month, day);
 38 
 39             if (!date.checkInputValidity()) {
 40                 System.out.println("Wrong Format");
 41                 System.exit(0);
 42             }
 43             n = input.nextInt();
 44             if (n < 0) {
 45                 System.out.println("Wrong Format");
 46                 System.exit(0);
 47             }
 48             System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
 49             System.out.println(date.getPreviousNDays(n).showDate());
 50         } else if (choice == 3) {    //test getDaysofDates method
 51             year = Integer.parseInt(input.next());
 52             month = Integer.parseInt(input.next());
 53             day = Integer.parseInt(input.next());
 54 
 55             int anotherYear = Integer.parseInt(input.next());
 56             int anotherMonth = Integer.parseInt(input.next());
 57             int anotherDay = Integer.parseInt(input.next());
 58 
 59             DateUtil fromDate = new DateUtil(year, month, day);
 60             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 61 
 62             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
 63                   System.out.println("The days between " + fromDate.showDate() + " and " + toDate.showDate() + " are:" + fromDate.getDaysofDates(toDate));
 64             } else {
 65                 System.out.println("Wrong Format");
 66                 System.exit(0);
 67             }
 68         }
 69         else{
 70             System.out.println("Wrong Format");
 71             System.exit(0);
 72         }        }else{
 73             System.out.println("Wrong Format");
 74         }
 75         
 76         
 77     }
 78 }
 79 
 80 
 81 class DateUtil{                    //DateUtil类
 82     private Year year;
 83     private Month month;
 84     private Day day;
 85     int mon_maxnum[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
 86     public DateUtil(){             //定义构造函数
 87         
 88     }
 89     
 90     public DateUtil( int y,int m,int d ){
 91         this.year = new Year(y);
 92         this.month = new Month(m);
 93         this.day = new Day(d);
 94     }
 95     public Day getDay(){             //setter和getter方法
 96          return day;
 97     }
 98     public void setDay(Day day){
 99         this.day = day;
100     }
101     
102     public Year getYear(){
103         return year;
104     }
105     
106     public void setYear(Year year){
107         this.year = year;
108     }
109     
110     public Month getMonth(){
111         return month;
112     }
113     
114     public void setMonth(Month month){
115         this.month = month;
116     }
117     
118     public boolean checkInputValidity(){        //检测日期是否合法
119         if(year.validate() && month.validate()) {
120             if(year.isLeapYear() && month.getValue() ==2 ){
121                 if( day.getValue() >= 1 && day.getValue() <=29 )
122                     return true;
123                 else
124                     return false;
125             }else{
126                 if( day.getValue() >= 1 && day.getValue() <= mon_maxnum[month.getValue()] )
127                     return true;
128                 else
129                     return false;
130             }
131         }else return false;
132     }
133     
134     public boolean compareDates(DateUtil date){           //比较两个日期的先后
135         if(this.getYear().getValue() > date.getYear().getValue())
136             return false;
137         else if(this.getYear().getValue() == date.getYear().getValue() && this.getMonth().getValue() > date.getMonth().getValue() )
138             return false;
139         else if(this.getYear().getValue() == date.getYear().getValue() && this.getMonth().getValue() == date.getMonth().getValue() && this.getDay().getValue() > date.getDay().getValue() )
140             return false;
141         else
142             return true;
143     }
144     
145     public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等
146     {
147         if(this.year != date.getYear())
148             return false;
149         else if(this.day != date.getDay())
150             return false;
151         else if(this.month != date.getMonth())
152             return false;
153         else
154             return true;
155     }
156     
157     public String showDate(){                   //输出结果
158         return(year.getValue()+"-"+month.getValue()+"-"+day.getValue());
159     }
160     
161     public DateUtil getNextNDays(int n){              //计算下n天的方法
162         int y = this.getYear().getValue();
163         int m = this.getMonth().getValue();
164         int d = this.getDay().getValue();
165         int i = 0,j = 0;
166         int a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
167         
168         while(n>365){
169             if(new Year(y).isLeapYear() && m <= 2){
170                 if(m == 2 && d == 29 ){
171                     m = 3;
172                     d = 1;
173                 }
174                 y ++;
175                 n -= 366;
176             }else if(new Year(y+1).isLeapYear() && m > 2){
177                 y ++;
178                 n -= 366;
179             }else {
180                 y ++;
181                 n -= 365;
182             }
183         }
184         for( i = 0 ; i < n ; i++)
185         {
186             d++;
187             if( new Year(y).isLeapYear() && m == 2){
188                 if(d > 29){
189                     m = 3;
190                     d = 1;
191                 }
192             }else if( d > a[m] ){
193                 if( m == 12 && d ==32){
194                     y++;
195                     m = 1;
196                     d = 1;
197                 }else{
198                     m ++;
199                     d = 1;
200                 }
201 
202             }
203             
204         }
205         return new DateUtil( y,m,d );
206     }
207      
208     public DateUtil getPreviousNDays(int n){              //计算前n天的方法
209         int y = this.getYear().getValue();
210         int m = this.getMonth().getValue();
211         int d = this.getDay().getValue();
212         int i = 0,j = 0;
213         int a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
214         
215         while(n>365){
216            if(new Year(y).isLeapYear() && m > 2 ){
217                 y--;
218                 n-=366;
219             }else if(new Year(y-1).isLeapYear() && m <= 2){
220                 y--;
221                 n-=366;
222             }else{
223                 y--;
224                 n-=365;
225             }
226        }
227         for( i = 0 ; i < n ; i++)
228         {
229             d--;
230            if(d <= 0){
231                m--;
232                if(m <= 0){
233                    m = 12;
234                    y--;
235                }
236                if(new Year(y).isLeapYear() && m == 2){
237                    d = 29;
238                }else{
239                    d = a[m];
240                }
241            }
242         }
243         return new DateUtil( y,m,d );
244     }
245     
246     public int getDaysofDates(DateUtil date){        //计算两个给定日期的日期差
247         int cnt1 = 0;
248         int cnt2 = 0;
249         int a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
250         
251         for(int i = 1 ; i < this.getYear().getValue() ; i++)
252         {
253             if(new Year(i).isLeapYear()){
254                 cnt1 += 366;
255             }else cnt1 += 365;
256         }
257         for(int i = 1 ; i < this.getMonth().getValue() ; i++)
258         {
259             if(new Year(this.getYear().getValue()).isLeapYear() && i==2){
260                 cnt1 += 29;
261             }else cnt1 += a[i];
262         }
263         cnt1 = cnt1 + this.getDay().getValue();
264         
265         
266         for(int i = 1 ; i < date.getYear().getValue() ; i++)
267         {
268             if(new Year(i).isLeapYear()){
269                 cnt2 += 366;
270             }else cnt2 += 365;
271         }
272         for(int i = 1 ; i < date.getMonth().getValue() ; i++)
273         {
274             if(new Year(date.getYear().getValue()).isLeapYear() && i == 2){
275                 cnt2 += 29;
276             }else cnt2 += a[i];
277         }
278         cnt2 = cnt2 + date.getDay().getValue();
279         return (Math.abs(cnt1-cnt2));
280     }
281     
282     
283 }
284  
285 class Year{                 //Year类
286     private int value;
287     public Year(){           //构造方法
288         
289     }
290     
291     public Year(int value){      //setter和getter类
292         this.value = value;
293     }
294     
295     public int getValue(){
296         return value;
297     }
298     
299     public void setValue(int value){
300         this.value = value;
301     }
302     
303     public boolean isLeapYear(){      ///判断闰年
304         if( (value % 4 == 0 && value % 100 != 0) ||value % 400 == 0){
305             return true;
306         }else  return false;
307     }
308       
309     public boolean validate(){            //判断日期的合法性
310         if( value >=1820 && value <= 2020){
311             return true;
312         }else return false;
313     }
314     
315     public void yearIncrement(){           //年份加一
316         value++;
317     }
318     
319     public void yearReduction(){           //年份减一
320         value--;
321     }
322 }
323  
324 class Month{                      //Month类
325     private int value;
326     public Month(){                //构造方法
327         
328     }
329     
330     public Month(int value){
331         this.value = value;
332     }
333     
334     public int getValue(){                     //setter和getter类
335         return value;
336     }
337     
338     public void setValue( int value ){
339         this.value = value;
340     }
341     
342 
343     
344     public void resetMin(){             //重置日期
345         value = 1;
346     }
347     public void resetMax(){
348         value = 12;
349     }
350     
351     public boolean validate(){             //检测日期合法性
352         if(value >=1 && value <=12)
353             return true;
354         else 
355             return false;
356     }
357     
358     public void monthIncrement(){
359         value++;
360     }
361     
362     public void monthReduction(){
363         value--;
364     }
365 }
366 
367 class Day{
368     private int value;  
369     public Day(){                        //构造方法
370         
371     }
372     
373     public Day(int value){
374         this.value = value;
375     }
376     
377     
378     public int getValue(){                 //getter和setter类
379         return value;
380     }
381     
382     public void setValue( int value ){
383         this.value = value;
384     }
385 
386     public void dayIncrement(){
387         value++;
388     }
389     
390     public void dayReduction(){
391         value--;
392     }
393     
394 }

 

     源码分析及反思:没有好好完成本次作业,主要问题在于自己在遇到问题时退缩,在努力了一晚上无果后看到别人也是如此,就心生了退缩之心,便不想再去写了,自己也只是写了些简单的方法,并未进入题目主要的内容中去,没有钻研精神,碰到自己不会的便不耐烦去学习,学起来也是很困难,在今后的学习中要改变这种心态,保持一颗求知的心去不断学习学习新的东西同时充实自己,还有就是不能从众,发现别人没做出来便以一种消极的心态放松自己,导致自己也做不出来,这是不可取的!

 

 


 

 

 (3)踩坑心得:

 1.习题集五的7-5中:

此处错误为非零返回,通常为部分语法有赘述和小错误或者返回值不正确,在此处为语法上有一点错误改正后便没有问题了。

 

2.习题集五的7-4中:

 

刚开始使用正则表达式时,没有掌握正确的表达字符范围,导致多次与题目要求的不符,

 

3.习题集五的7-6中:


由于是由7-5改编而来,聚合方式不同,刚开始没有改变属性的应用部分,导致多次编译错误。

 

 

错误与心得总结:

       错误差不多都是粗心导致的,以及编码不规范导致的,今后在写代码时更应该专心,以及注意代码的规范性和逻辑性。

 


 

(4).改进建议:

         在习题集五的7-5中:

 

 

 

 

求日期差的时候,我用的方法是先分别求出两个输入日期的总天数然后在对其作差并取绝对值,这样其实是走了捷径,没有用到判断两日期先后的方法,这点在后续迭代中可以做到可持续改进。

 

 

题目中给定的方法,但实际运用上没有使用,其实是走捷径了,故在此也应该运用到题目中给定的方法来进行运用。

 

 以及在习题集五7-5中:

 

求日期差的时候,我用的方法是先分别求出两个输入日期的总天数然后在对其作差并取绝对值,这样其实是走了捷径,没有用到判断两日期先后的方法,这点在后续迭代中可以做到可持续改进。

 

 


(5).总结:

           对于这一阶段的三次PTA作业,我受益匪浅,这一阶段相比上一阶段不同的是上一阶段只是按照题目所给的方法去编写代码,而这一阶段要求我们自己来设计类和方法,对个人对该问题的理解要求更加透彻,对问题的思考更加全面和缜密,当然,自身暴露出来的问题也很多,例如:由于类相较上次更多,故在类与类之间需调用相互的元素时,有时会搞不清楚如何调用,这是对方法的理解还不透彻;还有就是遇到问题时不能独自解决,往往求助同学,在这方面还需要加强自己的独立思考的能力以应对以后实际工作上的运用;在后期学习中,继续加强自己的编码能力,短期是为了通过期末考试,长期是为以后的工作学习打基础,同时在后期的学习中,应更加系统的学习结构性的东西,学习Java真正深层的东西;然而,在做习题集六的时候,由于刚开始就遇到了重重困难,导致进度停滞不前,自己便没有了写下去的信心,这不是一种好的品质,自己在今后的编码过程中,应该多思考问题,不怕困难,什么不会就去学什么,不退缩,再也不会像这次一样选择放弃,不断充实自己来面对困难!

 

posted on 2023-04-30 16:37  czdczd  阅读(30)  评论(0编辑  收藏  举报