题目集4~6总结

一.前言

1.题目集4:

(1)     知识点:正则表达式,聚合,继承

(2)     题量:虽然只有三个题,但难度较大,所以相对来说比较合适

(3)     难度:因为对正则表达式掌握还不熟练,以及对聚合和继承的理解还不到位,所以此次题目集难度偏难;但主要是第一题较复杂,第二题只要看懂类图,理解各个类的关系之后便大致可以写出。

2.题目集5:

(1)     知识点:字符串分割,排序,正则表达式,继承

(2)     题量:虽然有5个题,但难度不大,所以相对合适

(3)     难度:此次题目集大多为之前做过的题,除了第四题难度较大以外,其他题难度都偏小。

3.题目集6:

(1)     知识点:正则表达式,继承,多态,接口

(2)     题量:虽然有5个题,但难度适中,所以相对合适

(3)     难度:此题目集大多为基本的正则表达式的运用和基本的排序,除了最后两道题以外难度都不大,且最后两道题也是之前做过的题目的变形,题目大同小异,所以难度适中。

二.设计与分析:

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

      题目集4(7-2)日期问题面向对象设计(聚合一):

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

类图.jpg

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

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用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且输入均有效,输出格式如下:
    天数值

测试结果:

 

 

 

 

 

 

 

 

        分析:此题只要看懂类图则难度不大,若没看懂可能有些绕。我刚开始做这题时,也是没有看懂类图,有些烧脑,但后来慢慢理解发现其实很简单。

          在这里主要运用了聚合关系(【聚合关系】:是整体与部分的关系,且部分可以离开整体而单独存在。如车和轮胎是整体和部分的关系,轮胎离开车仍然可以存在。聚合关系是关联关系的一种,是强的关联关系;关联和聚合在语法上无法区分,必须考察具体的逻辑关系。)更多类的关系点击这里哦

        在理清类与类的关系后,其难点在与其功能的实现。

  1. 求下n天:首先要判断当前年份是否是闰年,如果是闰年则二月为29天,若不是则28天。然后调用day.dayIncrement()方法进行日期的增1,如果增1后达到了当前月份的最大天数,则月份增1,并使用day.resetMin()方法进行日期复位成1号,如果月份增1后变为13,则年份增1 ,并使用day.month.resetMin()进行月份复位成1月,直到增加了指定的n天。(PS:此题测试点有考虑到一个效率问题,所以要在方法开始时增加

    int t=n/146097;
    day.month.year.value= day.month.year.value+t*400;这两行代码,其中146097是400年的总天数,如果n超过146097,则进行缩减来提搞效率)

  2. 求前n天:大致思路同上。
  3. 求两个日期相差的天数:我的大致思路是,把年月日拆开进行计算,先算年与年之间相差的天数m(这里要注意闰年的问题),而后将每个日期的月份所包含的天数进行计算再加上日的天数得到n1,n2,将得到的n1,n2进行求差(根据大小关系判断是n1-n2还是n2-n1)然后再加上m即可。
  1 import java.util.Scanner;
  2 
  3 class Dateutil{
  4     Day day;
  5     int flag=0;
  6     public Dateutil() {
  7         
  8     }
  9     public Dateutil(int d,int m,int y) {
 10         this.day = new Day(y,m,d);
 11         /*day.month.year.value=y;
 12            day.month.value=m;
 13            day.value=d;*/
 14             
 15     }
 16     public void setDay(Day d)
 17     {
 18         this.day=d;
 19         
 20     }
 21     public Day getDay()
 22     {
 23         return day;
 24     }
 25     
 26     public boolean checkInputValidity()
 27     {
 28         if(day.validate()&&day.month.validate()&&day.month.year.validate())
 29             return true;
 30         else
 31             return false;
 32     }
 33     public boolean compareDates(Dateutil date)
 34     {
 35         if(day.month.year.value>date.day.month.year.value)
 36             return true;
 37         else if(day.month.year.value==date.day.month.year.value)
 38         {
 39             if(day.month.value>date.day.month.value)
 40                 return true;
 41             else if(day.month.value==date.day.month.value)
 42             {
 43                 if(day.value>date.day.value)
 44                    return true;
 45                 else if(day.value<date.day.value)
 46                    return false;
 47             }
 48             else 
 49             {
 50                 return false;
 51             }
 52         }
 53         else 
 54         {
 55             return false;
 56         }
 57         return false;
 58     }
 59     public boolean equalTwoDates(Dateutil date)
 60     {
 61         if(day.month.year.value==date.day.month.year.value&&day.month.value==date.day.month.value&&day.value==date.day.value)
 62             return true;
 63         else
 64             return false;
 65     }
 66     public String showDate()
 67     {
 68         String abc=null;
 69         abc=String.format("%d-%d-%d",day.month.year.value,day.month.value,day.value);
 70         return abc;
 71     }
 72     public Dateutil getNextNDays(int n)
 73     {
 74           int i=0;
 75           while(i!=n)
 76           {
 77               if(day.month.year.isLeapYear())
 78                 {
 79                  day.mon_maxnum[1]=29;
 80                 }
 81                 else
 82                 {
 83                     day.mon_maxnum[1]=28;
 84                 }
 85            day.dayIncrement();
 86            if(day.value==day.mon_maxnum[day.month.value-1]+1)
 87            {
 88                day.month.value++;
 89                day.resetMin();
 90            }
 91            if(day.month.value==13)
 92            {
 93                day.month.year.value++;
 94                day.month.resetMin();
 95            }
 96            i++;
 97           }
 98         return null;
 99     }
100     public Dateutil getpreviousNDays(int n)
101     { 
102         
103        int i=0;
104        while(i!=n)
105        {
106            if(day.month.year.isLeapYear())
107               {
108                day.mon_maxnum[1]=29;
109               }
110               else
111               {
112                   day.mon_maxnum[1]=28;
113               }
114         day.dayReduction();
115         if(day.value==0)
116         {
117            day.month.value--;
118 
119              if(day.month.value==0)
120              {
121                  day.month.year.value--;
122                  day.month.resetMax();
123              }
124            day.resetMax();
125         }
126         i++;
127        }
128         return null;
129     }
130     public int getDaysofDates(Dateutil date)
131     {
132         int x=0,y1 = 0,y2=0,z1=0,z2=0,m=0,n=0;
133         
134         if(equalTwoDates(date))
135         {
136             return 0;
137         }
138         else
139         {
140                   //2020.1.1
141                 //2019.12.31
142                 if(compareDates(date)) {
143                     //前大
144                     x=Math.abs(day.month.year.value-date.day.month.year.value);
145                     m=x/4;
146                     if (date.day.month.year.value == 1900 && !date.compareDates(new Dateutil(1900,2,28))) {
147                         m--;
148                     }
149                     for(int i=0;i<day.month.value-1;i++)
150                     {
151                         y1=y1+day.mon_maxnum[i];
152                     }
153                     for(int i=0;i<date.day.month.value-1;i++)
154                     {
155                         y2=y2+date.day.mon_maxnum[i];
156                     }
157                     z1=y1+day.value;//1
158                     z2=y2+date.day.value;//365
159                     n=365*x+m+(z1-z2);
160                     //System.out.print(z1 + "&" + z2);
161                 }else {
162                     x=Math.abs(day.month.year.value-date.day.month.year.value);
163                     m=x/4;
164 
165                     if (day.month.year.value == 1900 && !compareDates(new Dateutil(1900,2,28))) {
166                         m--;
167                     }
168                     for(int i=0;i<day.month.value-1;i++)
169                     {
170                         y1=y1+day.mon_maxnum[i];
171                     }
172                     for(int i=0;i<date.day.month.value-1;i++)
173                     {
174                         y2=y2+date.day.mon_maxnum[i];
175                     }
176                     z1=y1+day.value;//365
177                     z2=y2+date.day.value;//1
178                     n=365*x+m+(z2-z1);
179                 }
180                 
181             
182         }
183         return n;
184     }
185 }
186 
187 class Day
188 {
189     int value;
190     Month month;
191     int mon_maxnum[]= {31,28,31,30,31,30,31,31,30,31,30,31};
192     public Day() {
193         
194     }
195     
196     public Day(int yearValue,int monthValue,int dayValue) {
197                this.month=new Month(yearValue,monthValue);
198                //month.year.value=yearValue;
199                //month.value=monthValue;
200                this.value=dayValue;
201                
202     
203     }
204     public void setValue(int value)
205     {
206         this.value=value;
207     }
208     public int getValue()
209     {
210         return value;
211     }
212     public void setMonth(Month value)
213     {
214         this.month=value;
215     }
216     public Month getMonth()
217     {
218         return month;
219     }
220    public void  resetMin()
221    {
222        value=1;
223    }
224    public void  resetMax()
225    {
226        value=mon_maxnum[month.value-1];
227    }
228    public boolean validate()
229    {
230         if(value<1||value>31)
231             return false;
232         else 
233         {
234             if(month.year.isLeapYear())
235             {
236               if((month.value==2&&value>29)||(month.value==4&&value>30)||(month.value==6&&value>30)||(month.value==9&&value>30)||(month.value==11&&value>30))
237                   return false;
238               
239             }
240             else
241             {
242               if((month.value==2&&value>28)||(month.value==4&&value>30)||(month.value==6&&value>30)||(month.value==9&&value>30)||(month.value==11&&value>30))
243                   return false;
244             }
245         }
246         return true;
247    }
248    public void dayIncrement()
249    {
250        value=value+1;
251    }
252    public void dayReduction()
253    {
254        value=value-1;
255    }
256    
257 }
258 class Month
259 {
260     int value;
261     Year year;
262     public Month()
263     {
264         
265     }
266     public Month(int yearValue,int monthValue)
267     {
268         this.year= new Year(yearValue);
269         //year.value=yearValue;
270         this.value=monthValue;
271     }
272     public void setValue(int value)
273     {
274         this.value=value;
275     }
276     public int getValue()
277     {
278         return value;
279     }
280     public void setYear(Year year)
281     {
282         this.year=year;
283     }
284     public Year getyear()
285     {
286         return year;
287     }
288     public void  resetMin()
289     {
290         value=1;
291     }
292     public void  resetMax()
293     {
294         value=12;
295     }
296     public boolean validate()
297     {
298         if(value<1||value>12)
299             return false;
300         else
301              return true;
302     }
303     public void monthIncrement()
304     {
305         value=value+1;
306     }
307     public void monthReduction()
308     {
309         value=value-1;
310     } 
311 }
312 class Year{
313     int value;
314     public Year()
315     {}
316     public Year(int value)
317     {
318         this.value=value;
319     }
320     public void setvalue(int value)
321     {
322         this.value=value;
323     }
324     public int getvalue()
325     {
326         return value;
327     }
328     
329      public  boolean isLeapYear()
330         {
331             if((value%4==0)&&(value%100!=0)||value%400==0)
332             return true;
333             return false;
334         }
335      public  boolean validate()
336         {
337             
338             if(value<1900||value>2050)
339                 return false;
340             else
341                 return true;
342         }
343      public void yearIncrement()
344      {
345          value = value+1;
346      }
347      public void yearReduction()
348      {
349          value = value-1;
350      }
351 }
352 public class Main {
353     public static void main(String[] args) {
354         Scanner in =  new Scanner(System.in);
355         int m;
356         m=in.nextInt();
357         switch(m)
358         {
359         case 1:
360             int year=in.nextInt();
361             int month=in.nextInt();
362             int day=in.nextInt();
363             int n=in.nextInt();
364             Dateutil abc =new Dateutil(day,month,year);
365             if(abc.checkInputValidity())
366             {
367                 abc.getNextNDays(n);
368                 System.out.println(abc.showDate());
369             }
370             else
371             {
372                 System.out.println("Wrong Format");
373             }
374             break;
375         case 2:
376             int year2=in.nextInt();
377             int month2=in.nextInt();
378             int day2=in.nextInt();
379             int n2=in.nextInt();
380             Dateutil abc2 =new Dateutil(day2,month2,year2);
381             if(abc2.checkInputValidity())
382             {
383                 abc2.getpreviousNDays(n2);
384                 System.out.println(abc2.showDate());
385             }
386             else
387             {
388                 System.out.println("Wrong Format");
389             }
390             break;
391         case 3:
392             int year3=in.nextInt();
393             int month3=in.nextInt();
394             int day3=in.nextInt();
395             int year4=in.nextInt();
396             int month4=in.nextInt();
397             int day4=in.nextInt();
398             Dateutil abc3 = new Dateutil(day3,month3,year3);
399             Dateutil date = new Dateutil(day4,month4,year4);
400             if(abc3.checkInputValidity()&&date.checkInputValidity())
401             {
402             System.out.println(abc3.getDaysofDates(date));
403             }
404             else
405             {
406                 System.out.println("Wrong Format");
407             }
408             break;
409         default:
410            System.out.println("Wrong Format");
411         }
412     }
413     }

 

 题目集5(7-5)日期问题面向对象设计(聚合二):

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

类图.jpg

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

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用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:值

        测试结果:同上。

        此题与上题思路基本相同,不同点在与类与类之间的关系不同。

       题目集4(7-2):运用了聚合关系,部分与整体的关系密切,类与类之间的关系更为紧密,但代码实现更为繁琐。

       题目集5(7-4):直接调用相应类中的属性和方法,但代码实现更简洁容易。

  1 import java.util.Scanner;
  2 
  3 class Dateutil{
  4     Day day;
  5     int flag=0;
  6     public Dateutil() {
  7         
  8     }
  9     public Dateutil(int d,int m,int y) {
 10         this.day = new Day(y,m,d);
 11         /*day.month.year.value=y;
 12            day.month.value=m;
 13            day.value=d;*/
 14             
 15     }
 16     public void setDay(Day d)
 17     {
 18         this.day=d;
 19         
 20     }
 21     public Day getDay()
 22     {
 23         return day;
 24     }
 25     
 26     public boolean checkInputValidity()
 27     {
 28         if(day.validate()&&day.month.validate()&&day.month.year.validate())
 29             return true;
 30         else
 31             return false;
 32     }
 33     public boolean compareDates(Dateutil date)
 34     {
 35         if(day.month.year.value>date.day.month.year.value)
 36             return true;
 37         else if(day.month.year.value==date.day.month.year.value)
 38         {
 39             if(day.month.value>date.day.month.value)
 40                 return true;
 41             else if(day.month.value==date.day.month.value)
 42             {
 43                 if(day.value>date.day.value)
 44                    return true;
 45                 else if(day.value<date.day.value)
 46                    return false;
 47             }
 48             else 
 49             {
 50                 return false;
 51             }
 52         }
 53         else 
 54         {
 55             return false;
 56         }
 57         return false;
 58     }
 59     public boolean equalTwoDates(Dateutil date)
 60     {
 61         if(day.month.year.value==date.day.month.year.value&&day.month.value==date.day.month.value&&day.value==date.day.value)
 62             return true;
 63         else
 64             return false;
 65     }
 66     public String showDate()
 67     {
 68         String abc=null;
 69         abc=String.format("%d-%d-%d",day.month.year.value,day.month.value,day.value);
 70         return abc;
 71     }
 72     public Dateutil getNextNDays(int n)
 73     {
 74           int i=0;
 75           while(i!=n)
 76           {
 77               if(day.month.year.isLeapYear())
 78                 {
 79                  day.mon_maxnum[1]=29;
 80                 }
 81                 else
 82                 {
 83                     day.mon_maxnum[1]=28;
 84                 }
 85            day.dayIncrement();
 86            if(day.value==day.mon_maxnum[day.month.value-1]+1)
 87            {
 88                day.month.value++;
 89                day.resetMin();
 90            }
 91            if(day.month.value==13)
 92            {
 93                day.month.year.value++;
 94                day.month.resetMin();
 95            }
 96            i++;
 97           }
 98         return null;
 99     }
100     public Dateutil getpreviousNDays(int n)
101     { 
102         
103        int i=0;
104        while(i!=n)
105        {
106            if(day.month.year.isLeapYear())
107               {
108                day.mon_maxnum[1]=29;
109               }
110               else
111               {
112                   day.mon_maxnum[1]=28;
113               }
114         day.dayReduction();
115         if(day.value==0)
116         {
117            day.month.value--;
118 
119              if(day.month.value==0)
120              {
121                  day.month.year.value--;
122                  day.month.resetMax();
123              }
124            day.resetMax();
125         }
126         i++;
127        }
128         return null;
129     }
130     public int getDaysofDates(Dateutil date)
131     {
132         int x=0,y1 = 0,y2=0,z1=0,z2=0,m=0,n=0;
133         
134         if(equalTwoDates(date))
135         {
136             return 0;
137         }
138         else
139         {
140                   //2020.1.1
141                 //2019.12.31
142                 if(compareDates(date)) {
143                     //前大
144                     x=Math.abs(day.month.year.value-date.day.month.year.value);
145                     m=x/4;
146                     if (date.day.month.year.value == 1900 && !date.compareDates(new Dateutil(1900,2,28))) {
147                         m--;
148                     }
149                     for(int i=0;i<day.month.value-1;i++)
150                     {
151                         y1=y1+day.mon_maxnum[i];
152                     }
153                     for(int i=0;i<date.day.month.value-1;i++)
154                     {
155                         y2=y2+date.day.mon_maxnum[i];
156                     }
157                     z1=y1+day.value;//1
158                     z2=y2+date.day.value;//365
159                     n=365*x+m+(z1-z2);
160                     //System.out.print(z1 + "&" + z2);
161                 }else {
162                     x=Math.abs(day.month.year.value-date.day.month.year.value);
163                     m=x/4;
164 
165                     if (day.month.year.value == 1900 && !compareDates(new Dateutil(1900,2,28))) {
166                         m--;
167                     }
168                     for(int i=0;i<day.month.value-1;i++)
169                     {
170                         y1=y1+day.mon_maxnum[i];
171                     }
172                     for(int i=0;i<date.day.month.value-1;i++)
173                     {
174                         y2=y2+date.day.mon_maxnum[i];
175                     }
176                     z1=y1+day.value;//365
177                     z2=y2+date.day.value;//1
178                     n=365*x+m+(z2-z1);
179                 }
180                 
181             
182         }
183         return n;
184     }
185 }
186 
187 class Day
188 {
189     int value;
190     Month month;
191     int mon_maxnum[]= {31,28,31,30,31,30,31,31,30,31,30,31};
192     public Day() {
193         
194     }
195     
196     public Day(int yearValue,int monthValue,int dayValue) {
197                this.month=new Month(yearValue,monthValue);
198                //month.year.value=yearValue;
199                //month.value=monthValue;
200                this.value=dayValue;
201                
202     
203     }
204     public void setValue(int value)
205     {
206         this.value=value;
207     }
208     public int getValue()
209     {
210         return value;
211     }
212     public void setMonth(Month value)
213     {
214         this.month=value;
215     }
216     public Month getMonth()
217     {
218         return month;
219     }
220    public void  resetMin()
221    {
222        value=1;
223    }
224    public void  resetMax()
225    {
226        value=mon_maxnum[month.value-1];
227    }
228    public boolean validate()
229    {
230         if(value<1||value>31)
231             return false;
232         else 
233         {
234             if(month.year.isLeapYear())
235             {
236               if((month.value==2&&value>29)||(month.value==4&&value>30)||(month.value==6&&value>30)||(month.value==9&&value>30)||(month.value==11&&value>30))
237                   return false;
238               
239             }
240             else
241             {
242               if((month.value==2&&value>28)||(month.value==4&&value>30)||(month.value==6&&value>30)||(month.value==9&&value>30)||(month.value==11&&value>30))
243                   return false;
244             }
245         }
246         return true;
247    }
248    public void dayIncrement()
249    {
250        value=value+1;
251    }
252    public void dayReduction()
253    {
254        value=value-1;
255    }
256    
257 }
258 class Month
259 {
260     int value;
261     Year year;
262     public Month()
263     {
264         
265     }
266     public Month(int yearValue,int monthValue)
267     {
268         this.year= new Year(yearValue);
269         //year.value=yearValue;
270         this.value=monthValue;
271     }
272     public void setValue(int value)
273     {
274         this.value=value;
275     }
276     public int getValue()
277     {
278         return value;
279     }
280     public void setYear(Year year)
281     {
282         this.year=year;
283     }
284     public Year getyear()
285     {
286         return year;
287     }
288     public void  resetMin()
289     {
290         value=1;
291     }
292     public void  resetMax()
293     {
294         value=12;
295     }
296     public boolean validate()
297     {
298         if(value<1||value>12)
299             return false;
300         else
301              return true;
302     }
303     public void monthIncrement()
304     {
305         value=value+1;
306     }
307     public void monthReduction()
308     {
309         value=value-1;
310     } 
311 }
312 class Year{
313     int value;
314     public Year()
315     {}
316     public Year(int value)
317     {
318         this.value=value;
319     }
320     public void setvalue(int value)
321     {
322         this.value=value;
323     }
324     public int getvalue()
325     {
326         return value;
327     }
328     
329      public  boolean isLeapYear()
330         {
331             if((value%4==0)&&(value%100!=0)||value%400==0)
332             return true;
333             return false;
334         }
335      public  boolean validate()
336         {
337             
338             if(value<1900||value>2050)
339                 return false;
340             else
341                 return true;
342         }
343      public void yearIncrement()
344      {
345          value = value+1;
346      }
347      public void yearReduction()
348      {
349          value = value-1;
350      }
351 }
352 public class Main {
353     public static void main(String[] args) {
354         Scanner in =  new Scanner(System.in);
355         int m;
356         m=in.nextInt();
357         switch(m)
358         {
359         case 1:
360             int year=in.nextInt();
361             int month=in.nextInt();
362             int day=in.nextInt();
363             int n=in.nextInt();
364             Dateutil abc =new Dateutil(day,month,year);
365             if(abc.checkInputValidity())
366             {
367                 abc.getNextNDays(n);
368                 System.out.println(abc.showDate());
369             }
370             else
371             {
372                 System.out.println("Wrong Format");
373             }
374             break;
375         case 2:
376             int year2=in.nextInt();
377             int month2=in.nextInt();
378             int day2=in.nextInt();
379             int n2=in.nextInt();
380             Dateutil abc2 =new Dateutil(day2,month2,year2);
381             if(abc2.checkInputValidity())
382             {
383                 abc2.getpreviousNDays(n2);
384                 System.out.println(abc2.showDate());
385             }
386             else
387             {
388                 System.out.println("Wrong Format");
389             }
390             break;
391         case 3:
392             int year3=in.nextInt();
393             int month3=in.nextInt();
394             int day3=in.nextInt();
395             int year4=in.nextInt();
396             int month4=in.nextInt();
397             int day4=in.nextInt();
398             Dateutil abc3 = new Dateutil(day3,month3,year3);
399             Dateutil date = new Dateutil(day4,month4,year4);
400             if(abc3.checkInputValidity()&&date.checkInputValidity())
401             {
402             System.out.println(abc3.getDaysofDates(date));
403             }
404             else
405             {
406                 System.out.println("Wrong Format");
407             }
408             break;
409         default:
410            System.out.println("Wrong Format");
411         }
412     }
413     }

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

  1. 题目集4(7-3)图形继承 :

 

编写程序,实现图形类的继承,并定义相应类对象并进行测试。

  1. 类Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积
  2. 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
  3. 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
  4. 类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法public double getVolume();//求球体积
  5. 类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法public double getVolume();//求立方体体积
  6. 注意:
  • 每个类均有构造方法,且构造方法内必须输出如下内容:Constructing 类名
  • 每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
  • 输出的数值均保留两位小数

主方法内,主要实现四个功能(1-4): 从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积; 从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积; 从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积; 从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;

假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出Wrong Format

输入格式:

共四种合法输入

  • 1 圆半径
  • 2 矩形宽、长
  • 3 球半径
  • 4 立方体宽、长、高

输出格式:

按照以上需求提示依次输出

测试结果;

 

 

 

 

    分析:此题主要运用了封装与继承。若能基本掌握继承,则本题难度不大。

              继承:(点击此处了解更多)         

                      1.继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。

                       2.继承的作用:通过继承可以快速创建新的类,实现代码的重用,提高程序的可维护性,节省大量创建新类的时间,提高开发效率和开发质量。

               首先,定义一个Shape类作为父类,之后定义的Circle,Rectangle都继承自Shape类并定义各自的私有属性和方法,而后的类Ball,继承自Circle,类Box,继承自Rectangle并定义或继承各自的私有属性和方法。

       PS :1.由于会在计算中使用Π,所以在使用时不能直接使用3.14来计算,而是用Math.PI来进行计算。

                 2.结果会保留两位小数,所以在计算球的体积时应使用4 / 3.0 * Math.PI * getradius() * getradius() * getradius()而不是4 / 3 * Math.PI * getradius() * getradius() * getradius()来计算。

  1 import java.math.BigInteger;
  2 import java.text.DecimalFormat;
  3 import java.util.Scanner;
  4 import java.util.regex.Matcher;
  5 import java.util.regex.Pattern;
  6 
  7 class Shape {
  8     public Shape() {
  9         System.out.println("Constructing Shape");
 10     }
 11 
 12     public double getArea() {
 13         return 0.0;
 14 
 15     }
 16 }
 17 
 18 class Circle extends Shape {
 19     private double radius;
 20 
 21     public Circle(double radius) {
 22         this.radius = radius;
 23         System.out.println("Constructing Circle");
 24     }
 25 
 26     
 27     public void setradius(double radius) {
 28         this.radius = radius;
 29     }
 30 
 31     public double getradius() {
 32         return radius;
 33     }
 34 
 35     public double getArea() {
 36         double CircleArea;
 37         CircleArea = Math.PI * getradius() * getradius();
 38         return CircleArea;
 39     }
 40 }
 41 
 42 class Rectangle extends Shape {
 43     public Rectangle(double width, double length) {
 44         this.width = width;
 45         this.length = length;
 46         System.out.println("Constructing Rectangle");
 47     }
 48 
 49     private double width;
 50     private double length;
 51 
 52     public void setwidth(double width) {
 53         this.width = width;
 54     }
 55 
 56     public double getwidth() {
 57         return width;
 58     }
 59 
 60     public void setlength(double length) {
 61         this.length = length;
 62     }
 63 
 64     public double getlength() {
 65         return length;
 66     }
 67 
 68     public double getArea() {
 69 
 70         double Rectangle;
 71         Rectangle = getwidth() * getlength();
 72         return Rectangle;
 73     }
 74 }
 75 
 76 class Ball extends Circle {
 77     public Ball(double radius) {
 78         super(radius);
 79         System.out.println("Constructing Ball");
 80     }
 81 
 82     public double getArea() {
 83 
 84         double BallArea;
 85         BallArea = 4 * Math.PI * getradius() * getradius();
 86         return BallArea;
 87     }
 88 
 89     public double getVolume() {
 90         double BallVolume;
 91         BallVolume = 4 / 3.0 * Math.PI * getradius() * getradius() * getradius();
 92         return BallVolume;
 93     }
 94 }
 95 
 96 class Box extends Rectangle {
 97     public Box(double width, double length, double height) {
 98         super(width, length);
 99         this.height = height;
100         System.out.println("Constructing Box");
101     }
102 
103     private double height;
104 
105     public void setheight(double height) {
106         this.height = height;
107     }
108 
109     public double getheight() {
110         return height;
111     }
112 
113     public double getArea() {
114         double BoxArea;
115         BoxArea = (getwidth() * getlength() + getwidth() * getheight() + getlength() * getheight()) * 2;
116         return BoxArea;
117     }
118 
119     public double getVolume() {
120         double BoxVolume;
121         BoxVolume = getwidth() * getlength() * getheight();
122         return BoxVolume;
123     }
124 }
125 
126 public class Main {
127     public static void main(String[] args) {
128         Scanner in = new Scanner(System.in);
129         int n = in.nextInt();
130         int flag = 0;
131         DecimalFormat df = new DecimalFormat("0.00");
132         switch (n) {
133         case 1:
134             double radius1 = in.nextDouble();
135             if (radius1 <= 0) {
136                 flag = 1;
137                 break;
138             }
139 
140             Circle circle = new Circle(radius1);
141 
142             circle.setradius(radius1);
143             System.out.println("Circle's area:" + df.format(circle.getArea()));
144             break;
145         case 2:
146             double width1 = in.nextDouble();
147             double length1 = in.nextDouble();
148             if (width1 <= 0 || length1 <= 0) {
149                 flag = 1;
150                 break;
151             }
152 
153             Rectangle rectangle = new Rectangle(width1, length1);
154 
155             rectangle.setwidth(width1);
156             rectangle.setlength(length1);
157             System.out.println(
158                     "Rectangle's area:" + df.format(rectangle.getArea()));
159             break;
160         case 3:
161             double radius2 = in.nextDouble();
162             if (radius2 <= 0) {
163                 flag = 1;
164                 break;
165             }
166 
167             Ball ball = new Ball(radius2);
168 
169             ball.setradius(radius2);
170             System.out.println("Ball's surface area:" + df.format(ball.getArea()));
171             System.out.println("Ball's volume:" + df.format(ball.getVolume()));
172             break;
173         case 4:
174             double width2 = in.nextDouble();
175             double length2 = in.nextDouble();
176             double height = in.nextDouble();
177             if (width2 <= 0 || length2 <= 0 || height <= 0) {
178                 flag = 1;
179                 break;
180             }
181 
182             Box box = new Box(width2, length2, height);
183 
184             box.setwidth(width2);
185             box.setlength(length2);
186             box.setheight(height);
187             System.out.println(
188                     "Box's surface area:" + df.format(box.getArea()));
189             System.out.println(
190                     "Box's volume:" + df.format(box.getVolume()));
191             break;
192 
193         default:
194             flag = 1;
195             break;
196         }
197         if (flag == 1)
198             System.out.println("Wrong Format");
199 
200     }
201 }

 

2.题目集6(7-5)图形继承与多态:

掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。

2021-OO第06次作业-5指导书V1.0.pdf

输入格式:

从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。

输出格式:

  1. 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出Wrong Format
  2. 如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):
  • 各个图形的面积;
  • 所有图形的面积总和;
  • 排序后的各个图形面积;
  • 再次所有图形的面积总和。

 测试结果:

 

 

 

 

 

 

分析:

类图如下:

 

            根据类图我们可以明白类与类之间的关系,之后再书写代码

            此题各个类的继承与上题大致相同并在继承的基础上增加了多态,容易踩坑的地方在于对三角形合理性的判断(此判断在上个博客中有提到)

多态:(想了解更多点击此处)          

1.多态是继封装、继承之后,面向对象的第三大特性。

2.多态现实意义理解:

       现实事物经常会体现出多种形态,如学生,学生是人的一种,则一个具体的同学张三既是学生也是人,即出现两种形态。                                                                                                            Java作为面向对象的语言,同样可以描述一个事物的多种形态。如Student类继承了Person类,一个Student的对象便既是Student,又是Person。

3.多态体现为父类引用变量可以指向子类对象。

4.前提条件:必须有子父类关系。

    注意:在使用多态后的父类引用变量调用方法时,会调用子类重写后的方法。

5.多态的定义与使用格式

                   定义格式:父类类型 变量名=new 子类类型();

6.理解:

               多态是同一个行为具有多个不同表现形式或形态的能力。

               多态就是同一个接口,使用不同的实例而执行不同操作

  1 import java.text.DecimalFormat;
  2 import java.util.Scanner;
  3 import java.util.regex.Matcher;
  4 import java.util.regex.Pattern;
  5 class shape
  6 {
  7     public shape()
  8     {
  9         
 10     }
 11     public double getarea()
 12     {
 13         return 0.0;
 14     }
 15 }
 16 class Circle extends shape
 17 {
 18     public Circle(double r)
 19     {
 20         this.r=r;
 21     }
 22     
 23     private double r;
 24     public void setr(double r)
 25     {
 26         this.r=r;
 27     }
 28     public double getr()
 29     {
 30         return r;
 31     }
 32     
 33     public double getArea()
 34     {
 35         double Circlearea=Math.PI*r*r;
 36         return Circlearea;
 37     }
 38 }
 39 class Rectangle extends shape
 40 {
 41     public Rectangle(double width, double length) {
 42         this.width = width;
 43         this.length = length;
 44     }
 45 
 46     private double width;
 47     private double length;
 48     public void setwidth(double width) {
 49         this.width = width;
 50     }
 51 
 52     public double getwidth() {
 53         return width;
 54     }
 55 
 56     public void setlength(double length) {
 57         this.length = length;
 58     }
 59 
 60     public double getlength() {
 61         return length;
 62     }
 63 
 64     public double getArea() {
 65         double Rectanglearea = getwidth() * getlength();
 66         return Rectanglearea;
 67     }
 68     
 69 }
 70 class Triangle extends shape
 71 {
 72     public Triangle(double a,double b,double c)
 73     {
 74         this.a=a;
 75         this.b=b;
 76         this.c=c;
 77     }
 78     private double a;
 79     private double b;
 80     private double c;
 81     public void seta(double a) {
 82         this.a = a;
 83     }
 84 
 85     public double geta() {
 86         return a;
 87     }
 88     public void setb(double b) {
 89         this.b = b;
 90     }
 91 
 92     public double getb() {
 93         return b;
 94     }
 95     public void setc(double c) {
 96         this.c = c;
 97     }
 98 
 99     public double getc() {
100         return c;
101     }
102     
103     public double getArea() {
104         double p=(a+b+c)/2.0;
105         double Trianglearea=Math.sqrt(p*(p-a)*(p-b)*(p-c));
106         return Trianglearea;
107     }
108     public boolean check()
109     {
110         if((a+b>c&&a+c>b&&b+c>a)&&(Math.abs(a-b)<c&&Math.abs(a-c)<b&&Math.abs(b-c)<a))
111         return true;
112         else
113         return false;
114     }
115 
116 }
117 public class Main {
118     public static void main(String[] args) {
119         Scanner in = new Scanner(System.in);
120         int Circlenum=in.nextInt();
121         int Rectanglenum=in.nextInt();
122         int Trianglenum=in.nextInt();
123         
124         int flag=0;
125         double  []Circlearea = new double[1000];
126         double  []Rectanglearea = new double[1000];
127         double  []Trianglearea = new double[1000];
128         if(Circlenum<0||Rectanglenum<0||Trianglenum<0)
129         {
130             System.out.println("Wrong Format");
131         }
132         else
133         {
134         for(int i=0;i<Circlenum;i++)                       //
135         {
136             double r=in.nextDouble();
137             if(r<=0)
138             {
139                 System.out.println("Wrong Format");
140                 flag=1;
141                 break;
142             }
143             else
144             {
145             Circle circle = new Circle(r);
146             Circlearea[i]=circle.getArea();
147             }
148         }
149         if(flag==0)
150         {
151         for(int i=0;i<Rectanglenum;i++)             //矩形
152         {
153             double width=in.nextDouble();
154             double length=in.nextDouble();
155             if(width<0||length<0)
156             {
157                 System.out.println("Wrong Format");
158                 flag=1;
159                 break;
160             }
161             else
162             {
163                 Rectangle rectangle = new Rectangle(width,length);
164                 Rectanglearea[i]=rectangle.getArea();
165             }
166         }
167         }
168         if(flag==0)
169         {
170             for(int i=0;i<Trianglenum;i++)                        //三角形
171             {
172                 double a=in.nextDouble();
173                 double b=in.nextDouble();
174                 double c=in.nextDouble();
175                 if(a<0||b<0||c<0)
176                 {
177                     System.out.println("Wrong Format");
178                     flag=1;
179                     break;
180                 }
181                 else
182                 {
183                     Triangle triangle = new Triangle(a,b,c);
184                     if(triangle.check())
185                     {
186                     Trianglearea[i]=triangle.getArea();
187                     }
188                     else
189                     {
190                     flag=1;
191                     System.out.println("Wrong Format");
192                     break;
193                     }
194                 }
195             }
196         }
197         }
198         if(flag==0)
199         {
200         DecimalFormat df = new DecimalFormat("0.00");
201         double []shape = new double[Circlenum+Rectanglenum+Trianglenum]; 
202         double num1=0,num2=0,num3=0;
203         System.out.println("Original area:");
204         if(Circlenum==0&&Rectanglenum==0&&Trianglenum==0)
205         {
206             System.out.println("");
207         }
208         for(int i=0;i<Circlenum;i++)
209         {
210             shape[i]=Circlearea[i];
211             System.out.print(df.format(Circlearea[i])+" ");
212             num1=num1+Circlearea[i];
213         }
214         for(int i=0;i<Rectanglenum;i++)
215         {
216             shape[i+Circlenum]=Rectanglearea[i];
217             if(Trianglenum==0&&i==Rectanglenum-1)
218             System.out.println(df.format(Rectanglearea[i])+" ");
219             else
220             {
221                 System.out.print(df.format(Rectanglearea[i])+" ");
222             }
223             num2=num2+Rectanglearea[i];
224         }
225         for(int i=0;i<Trianglenum;i++)
226         {
227             shape[i+Rectanglenum+Circlenum]=Trianglearea[i];
228             if(i==Trianglenum-1)
229             System.out.println(df.format(Trianglearea[i])+" ");
230             else
231             {
232             System.out.print(df.format(Trianglearea[i])+" ");    
233             }
234             num3=num3+Trianglearea[i];
235         }
236         System.out.println("Sum of area:"+df.format((num1+num2+num3)));
237         System.out.println("Sorted area:");
238         if(Circlenum==0&&Rectanglenum==0&&Trianglenum==0)
239         {
240             System.out.println("");
241         }
242         for(int i=0;i<shape.length-1;i++)
243         {
244             for(int j=0;j<shape.length-1-i;j++)
245             {
246                 if(shape[j]>shape[j+1])
247                 {
248                 double t=shape[j];
249                 shape[j]=shape[j+1];
250                 shape[j+1]=t;
251                 }
252             }
253         }
254         for(int i=0;i<shape.length;i++)
255         {
256             if(i==shape.length-1)
257             System.out.println(df.format(shape[i])+" ");
258             else
259             System.out.print(df.format(shape[i])+" ");
260         }
261         System.out.println("Sum of area:"+df.format((num1+num2+num3)));
262         
263     }
264     }
265 }

 

3.题目集6(7-6)实现图形接口及多态性 :

编写程序,使用接口及类实现多态性,类图结构如下所示:

类图.jpg

其中:

  • GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;
  • Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口
  • 要求:在Main类的主方法中分别定义一个圆类对象及矩形类对象(其属性值由键盘输入),使用接口的引用分别调用圆类对象及矩形类对象的求面积的方法,直接输出两个图形的面积值。(要求只保留两位小数)

输入格式:

从键盘分别输入圆的半径值及矩形的宽、长的值,用空格分开。

输出格式:

  • 如果输入的圆的半径值及矩形的宽、长的值非法(≤0),则输出Wrong Format
  • 如果输入合法,则分别输出圆的面积和矩形的面积值(各占一行),保留两位小数。

测试结果:

 

 

 

 

        分析:此题运用了接口,首先我们需要了解接口是什么?

                  接口:(点击此处了解更多)

                           Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能)。

                  基本了解接口后,在根据类图所提供的思路,此题并不难,其大致的思路与前面两题相同,只是使用的技术不同

             

 1 import java.text.DecimalFormat;
 2 import java.util.Scanner;
 3 
 4 interface GetArea
 5 {
 6      double getArea();
 7 }
 8 class Circle implements GetArea
 9 {
10     private double radius;
11     public void setRadius(double radius)
12     {
13         this.radius=radius;
14     }
15 
16     public double getRadius() {
17         return radius;
18     }
19     public Circle(double radius)
20     {
21         this.radius=radius;
22     }
23     public Circle(){}
24 
25     @Override
26     public double getArea() {
27         return Math.PI*radius*radius;
28     }
29 }
30 
31 class Rectangle implements GetArea
32 {
33     private double width;
34     private double length;
35 
36     public void setWidth(double width) {
37         this.width = width;
38     }
39 
40     public double getWidth() {
41         return width;
42     }
43 
44     public void setLength(double length) {
45         this.length = length;
46     }
47 
48     public double getLength() {
49         return length;
50     }
51 
52     public Rectangle(double width,double length)
53     {
54         this.length=length;
55         this.width=width;
56     }
57     public Rectangle(){}
58 
59     @Override
60     public double getArea() {
61         return width*length;
62     }
63 }
64 public class Main {
65     public static void main(String[] args) {
66             Scanner in = new Scanner(System.in);
67             double radius=in.nextDouble();
68             double width=in.nextDouble();
69             double length=in.nextDouble();
70             Circle circle = new Circle(radius);
71             Rectangle rectangle= new Rectangle(width,length);
72             DecimalFormat df = new DecimalFormat("0.00");
73             if(radius<=0||width<=0||length<=0)
74             {
75                 System.out.println("Wrong Format");
76             }
77             else {
78                 System.out.println(df.format(circle.getArea()));
79                 System.out.println(df.format(rectangle.getArea()));
80                 }
81             
82             
83     }
84 }

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

 常用语法参考:

1.String.matches() :matches()是全部匹配,是将整个输入串与模式匹配,如果要验证一个输入的数据是否为数字类型或其他类型,一般要用matches()。如果匹配则为true,否则为false;

2.String.find:  find()方法是部分匹配,是查找输入串中与模式匹配的子串,如果该匹配的串有组还可以使用group()函数。如果匹配则为true,否则为false;

3.当使用matches(),lookingAt(),find()执行匹配操作后,就可以利用以上三个方法得到更详细的信息. 

start()返回匹配到的子字符串在字符串中的索引位置. 
end()返回匹配到的子字符串的最后一个字符在字符串中的索引位置. 
group()返回匹配到的子字符串 

④题目集5(7-4)统计Java程序中关键词的出现次数 中Java集合框架应用的分析总结:

     编写程序统计一个输入的Java源码中关键字(区分大小写)出现的次数。说明如下:

  • Java中共有53个关键字(自行百度)
  • 从键盘输入一段源码,统计这段源码中出现的关键字的数量
  • 注释中出现的关键字不用统计
  • 字符串中出现的关键字不用统计
  • 统计出的关键字及数量按照关键字升序进行排序输出
  • 未输入源码则认为输入非法

输入格式:

输入Java源码字符串,可以一行或多行,以exit行作为结束标志

输出格式:

  • 当未输入源码时,程序输出Wrong Format
  • 当没有统计数据时,输出为空
  • 当有统计数据时,关键字按照升序排列,每行输出一个关键字及数量,格式为数量\t关键字

 

测试结果:

 

 

分析:

            此题会运用集合中的List、Set或Map一种或多种(点击这里了解更多)

          在这里我使用了Map(Map中的集合,元素是成对存在的(理解为夫妻)。每个元素由键与值两部分组成,通过键可以找对所对应的值。)

           首先是53个关键字:"abstract", "boolean", "break", "byte", "case",

 

   "catch", "char", "class", "continue", "default", "do", "double",

 

   "else", "extends", "false", "final", "finally", "float", "for",

 

   "if", "implements", "import", "instanceof", "int", "interface",

 

   "long", "native", "new", "null", "package", "private", "protected",

 

   "public", "return", "short", "static", "super", "switch",

 

   "synchronized", "this", "throw", "throws", "transient", "true",

 

   "try", "void", "volatile", "while", "const", "goto","enum","assert" ,"strictfp"

      其次考虑到是输入代码,而代码较多且长,所以在这里我使用Stringbuffer(动态字符串数组)并使用append()往动态字符串数组添加(跟“xxxx”+“yyyy”相当那个‘+’号 )之后所有的代码都存在了一个字符串数组里。

      之后再使用正则表达式(find)进行匹配,若匹配到关键字则t++,匹配完后把t作为键值(map.put(keywords[i], t))存入集合中

      PS:1.关键字中有部分相同的情况,如:do和double,throw和throws所以在进行正则表达式的书写时要在两边加上\b(String pattern = "\\b"+keywords[i]+"\\b"),否则在匹配是double中的do也会被当做do进行匹配。

               2.由于注释中也会可能出现关键字,但注释中出现的关键字并不能算,所以采用String a = all.toString().replaceAll("/\\*\\s*.*\\s*\\*/", "")来清楚注释段的代码

 

 1 import java.text.DecimalFormat;
 2 import java.util.ArrayList;
 3 import java.util.Collections;
 4 import java.util.Comparator;
 5 import java.util.Iterator;
 6 import java.util.List;
 7 import java.util.Map;
 8 import java.util.Map.Entry;
 9 import java.util.Scanner;
10 import java.util.TreeMap;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13 
14 
15 
16 public class Main {
17     
18     public static void main(String args[]) {
19         Scanner in = new Scanner(System.in);
20         Map map=new TreeMap();
21         String[] keywords = { "abstract", "boolean", "break", "byte", "case",
22 
23                    "catch", "char", "class", "continue", "default", "do", "double",
24 
25                    "else", "extends", "false", "final", "finally", "float", "for",
26 
27                    "if", "implements", "import", "instanceof", "int", "interface",
28 
29                    "long", "native", "new", "null", "package", "private", "protected",
30 
31                    "public", "return", "short", "static", "super", "switch",
32 
33                    "synchronized", "this", "throw", "throws", "transient", "true",
34 
35                    "try", "void", "volatile", "while", "const", "goto","enum","assert" ,"strictfp"};
36         
37         String abc="";
38         StringBuilder all = new StringBuilder();
39         int flag=0;
40         while( !(abc = in.nextLine()).equals("exit"))
41         {
42             flag=1;
43             all.append(abc.replaceAll("//.*", "").replaceAll("\".*\"", ""));
44         }
45         String a = all.toString().replaceAll("/\\*\\s*.*\\s*\\*/", "");
46         if(flag!=1)
47         {
48             System.out.println("Wrong Format");
49         }
50         //System.out.print(all);
51         
52         for(int i=0;i<53;i++) {
53             String pattern = "\\b"+keywords[i]+"\\b";
54             Pattern r = Pattern.compile(pattern);
55             Matcher m = r.matcher(a);
56             int t=0;
57             while(m.find())
58             {
59               t++;
60               //System.out.println(m.group());
61             }
62             if(t!=0)
63             map.put(keywords[i], t);
64         }
65         
66         sortMap(map);
67         //System.out.print("ok");
68     }
69      static void sortMap(Map<String, String> param) {
70             TreeMap<String, String> paramTreeMap = new TreeMap<>(param);
71             Iterator iterator = paramTreeMap.entrySet().iterator();
72             while (iterator.hasNext()) {
73                 String a = iterator.next().toString();
74                 System.out.println(a.split("=")[1] + "\t" + a.split("=")[0]);
75             }
76         }
77 }

 

 

 

三.总结:          

      在这几次的题目中我熟练运用了正则表达式和封装,并基本掌握了继承,多态以及接口的相关知识。但是对于多态和接口的运用相对来说不够熟练,还需要进一步加强练习

     关于教学上的意见,我觉得老师的教学很好,课堂也很精彩生动。线上作业方面,题目对我们的知识利用也有很好的帮助,并且我觉得进行实践方面的操作更能增加我们对知识的运用,增加我们的熟练度,所以我觉得每周一次的大作业和实验很有帮助,但我的个人感受就是,有些时候题目做的比较快的话,做完后就找不到题做了,想要练手但是又缺乏题目,没有题目做载体,自己也不知道从何开始,所以老师能不能整理一个单独的题目集供我们练手(自愿学习,没有时间限制的那种),不是说作业太少就是,只是想要在自己自主学习的时候有一个比较好的载体进行学习。

     除此以外,在发布pta作业,能否把每个测试点想要表面的意图有个清楚明了的阐述,每次在做题的有测试点过不去时,有些测试点并不知道其重点在哪,自己也不太清楚自己的代码有哪些漏洞,自己也难以猜测那个测试点的点在哪。

 

 

 

 

 

 

 

 

 

posted on 2021-05-01 18:13  五五三金  阅读(100)  评论(0)    收藏  举报

导航