java第二次大作业Blog

前言

题目集4 、 5、 6 主要涉及了关于java最主要的三大特性——封装、继承、多态的知识点。旨在通过实际代码编程深刻理解java三大主要特性的编写方式及优点益处。
三次作业题量较少,但难度显然有所提升,涉及到了较多之前未使用过的类、方法。

设计与分析

①题目集4 7-2 和题目集5 7-4 日期类聚合设计的优劣比较

源码

题目集4 7-2

  1   1 import java.util.*;
  2   2 
  3   3 public class Main {
  4   4         public static void main(String args[]) {
  5   5             int[] str = Input.in();
  6   6             if(str!=null)
  7   7             {
  8   8                 switch(str[0])
  9   9                 {
 10  10                 case 1:
 11  11                     DateUtil d = new DateUtil(str[3],str[2],str[1]);
 12  12                     if(!d.checkInputValidity())
 13  13                         {System.out.print("Wrong Format");break;}
 14  14                     d = d.getNextNDays(str[4]);
 15  15                     System.out.print(d.showDate());
 16  16                     break;
 17  17                 case 2:
 18  18                     DateUtil dd = new DateUtil(str[3],str[2],str[1]);
 19  19                     if(!dd.checkInputValidity())
 20  20                         {System.out.print("Wrong Format");break;}
 21  21                     dd = dd.getPreviousNDays(str[4]);
 22  22                     System.out.print(dd.showDate());
 23  23                     break;
 24  24                 case 3:
 25  25                     DateUtil d1 = new DateUtil(str[6],str[5],str[4]);
 26  26                     DateUtil d2 = new DateUtil(str[3],str[2],str[1]);
 27  27                     if(!d1.checkInputValidity()||!d2.checkInputValidity())
 28  28                         {System.out.print("Wrong Format");break;}
 29  29                     int sum = d1.getDaysofDates(d2);
 30  30                     System.out.print(sum);
 31  31                     break;
 32  32                     default:
 33  33                 }
 34  34                     
 35  35             }
 36  36         }
 37  37 }
 38  38 class DateUtil {
 39  39     private Day day;
 40  40     
 41  41     DateUtil(){
 42  42         
 43  43     }
 44  44     DateUtil(int d,int n,int y){
 45  45         this.day = new Day(y,n,d);
 46  46     }
 47  47     
 48  48     Day getDay() {
 49  49         return this.day;
 50  50     }
 51  51     void setDay(Day day) {
 52  52         this.day = day;
 53  53     }
 54  54     
 55  55     boolean checkInputValidity() {
 56  56         //初始顺序是,日,月,年   会出现bug 因为月如果越界,无法判断,有bug
 57  57         if(this.day.getMonth().getYear().validate()&&this.day.getMonth().validate()&&this.day.validate())
 58  58             return true;
 59  59         else
 60  60             return false;
 61  61         
 62  62     }
 63  63     boolean compareDates(DateUtil date) {
 64  64         //大 返回真 小 返回假
 65  65         if(this.day.getMonth().getYear().getValue()>date.day.getMonth().getYear().getValue())
 66  66             return true;
 67  67         else if(this.day.getMonth().getYear().getValue()<date.day.getMonth().getYear().getValue())
 68  68             return false;
 69  69         else
 70  70         {
 71  71             if(this.day.getMonth().getValue()>date.getDay().getMonth().getValue())
 72  72                 return true;
 73  73             else if(this.day.getMonth().getValue()<date.getDay().getMonth().getValue())
 74  74                 return false;
 75  75             else
 76  76             {
 77  77                 if(this.day.getValue()>date.getDay().getValue())
 78  78                     return true;
 79  79                 else
 80  80                     return false;
 81  81             }
 82  82         }
 83  83     }
 84  84     boolean equalTwoDates(DateUtil date) {
 85  85         if(this.day.getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue()
 86  86                 &&this.day.getMonth().getValue()==date.getDay().getMonth().getValue()
 87  87                 &&this.day.getValue()==date.getDay().getValue())
 88  88             return true;
 89  89         else
 90  90             return false;
 91  91     }
 92  92     String showDate() {
 93  93         String s = this.day.getMonth().getYear().getValue()+"-"+this.day.getMonth().getValue()+"-"+this.day.getValue();
 94  94         return s;
 95  95     }
 96  96     DateUtil getNextNDays(int n) {
 97  97         for(;n>0;n--)
 98  98         {
 99  99             if(this.day.getMonth().getYear().isLeapYear())
100 100                 this.day.mon_maxnum[1] = 29;
101 101             else
102 102                 this.day.mon_maxnum[1] = 28;
103 103             this.day.dayIncrease();
104 104             
105 105         }
106 106         return this;
107 107     }
108 108     DateUtil getPreviousNDays(int n) {
109 109         
110 110         for(;n>0;n--)
111 111         {
112 112             if(this.day.getMonth().getYear().isLeapYear())
113 113                 this.day.mon_maxnum[1] = 29;
114 114             else
115 115                 this.day.mon_maxnum[1] = 28;
116 116             this.day.dayReduction();
117 117             
118 118             
119 119         }
120 120         return this;
121 121     }
122 122   
123 123          int getDaysofDates(DateUtil date) {
124 124         int sum = 0;
125 125              //一开始是,用年-年,月-月,日-日,可是这样是假定大日期的年月日一定比小日期都大,所以有bug,因为不一定
126 126         while(!this.equalTwoDates(date))
127 127         {
128 128             if(this.compareDates(date))
129 129             {
130 130                 if(date.day.getMonth().getYear().isLeapYear())
131 131                     date.day.mon_maxnum[1] = 29;
132 132                 else
133 133                     date.day.mon_maxnum[1] = 28;
134 134                 date.day.dayIncrease();
135 135             }
136 136             else
137 137             {
138 138                 if(this.day.getMonth().getYear().isLeapYear())
139 139                     this.day.mon_maxnum[1] = 29;
140 140                 else
141 141                     this.day.mon_maxnum[1] = 28;
142 142                 this.day.dayIncrease();
143 143             }
144 144             sum++;
145 145         }
146 146         return sum;
147 147     }
148 148     
149 149 }
150 150 class Day {
151 151         private int value;
152 152         private Month month;
153 153         int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
154 154         
155 155         Day(){
156 156             
157 157         }
158 158         Day(int yearValue,int monthValue,int dayValue){
159 159             this.month = new Month(yearValue,monthValue);
160 160             this.value = dayValue;
161 161         }
162 162         
163 163         int getValue() {
164 164             return this.value;
165 165         }
166 166         void setValue(int value) {
167 167             this.value = value;
168 168         }
169 169         Month getMonth() {
170 170             return this.month;
171 171         }
172 172         void setMonth(Month month)
173 173         {
174 174             this.month = month;
175 175         }
176 176         
177 177         void resetMin() {
178 178             this.value = 1;
179 179         }
180 180         void resetMax() {
181 181             this.value = this.mon_maxnum[this.month.getValue()-1];
182 182         }
183 183         boolean validate() {
184 184             if(this.month.getYear().isLeapYear())
185 185             {
186 186                 this.mon_maxnum[1]=29;
187 187             }
188 188             if(this.value<1||this.value>this.mon_maxnum[this.month.getValue()-1])
189 189                 return false;
190 190             else
191 191                 return true;
192 192         }
193 193         void dayIncrease() {
194 194             if(this.value<this.mon_maxnum[this.month.getValue()-1])
195 195                 this.value += 1;
196 196             else
197 197             {
198 198                 this.resetMin();
199 199                 this.month.monthIncrease();
200 200             }
201 201         }
202 202         void dayReduction() {
203 203             if(this.value>1)
204 204                 this.value -=1;
205 205             else
206 206             {
207 207                 this.month.monthReduction();
208 208             //    validate();
209 209                 this.resetMax();
210 210             }
211 211         }
212 212 }
213 213 class Month {
214 214         private int value;
215 215         private Year year;
216 216         
217 217         Month(){
218 218             
219 219         }
220 220         Month(int yearValue,int monthValue){
221 221             this.year = new Year(yearValue);
222 222             this.value = monthValue;
223 223         }
224 224         
225 225         int getValue() {
226 226             return this.value;
227 227         }
228 228         void setValue(int value) {
229 229             this.value = value;
230 230         }
231 231         Year getYear() {
232 232             return this.year;
233 233         }
234 234         void setYear(Year year) {
235 235             this.year = year;
236 236         }
237 237         
238 238         void resetMin() {
239 239             this.value = 1;
240 240         }
241 241         void resetMax() {
242 242             this.value = 12;
243 243         }
244 244         boolean validate() {
245 245             if(this.value>=1&&this.value<=12)
246 246                 return true;
247 247             else
248 248                 return false;
249 249         }
250 250         void monthIncrease() {
251 251             if(this.value<12)
252 252                 this.value += 1;
253 253             else 
254 254             {
255 255                 this.resetMin();
256 256                 this.year.yearIncrease();
257 257             }
258 258         }
259 259         void monthReduction() {
260 260             if(this.value>1)
261 261                 this.value -= 1;
262 262             else
263 263             {
264 264                 this.resetMax();
265 265                 this.year.yearReduction();
266 266             }
267 267         }
268 268 }
269 269 class Year {
270 270         private int value;
271 271         
272 272         Year(){
273 273             
274 274         }
275 275         Year(int value){
276 276             this.value = value;
277 277         }
278 278         
279 279         int getValue() {
280 280             return this.value;
281 281         }
282 282         void setValue(int value) {
283 283             this.value = value;
284 284         }
285 285         boolean isLeapYear() {
286 286             boolean juge = true ;
287 287             if(this.value%4==0&&this.value%100!=0||this.value%400==0)
288 288             {
289 289                 juge = true;
290 290             }
291 291             else
292 292             {
293 293                 juge = false;
294 294             }
295 295             return juge;
296 296         }
297 297         boolean validate() {
298 298             if(this.value<1900||this.value>2050)
299 299                 return false;
300 300             else
301 301                 return true;
302 302         }
303 303         void yearIncrease() {
304 304             this.value += 1;
305 305         }
306 306         void yearReduction() {
307 307             this.value -= 1;
308 308         }
309 309 }
310 310 class Input {
311 311 
312 312     static int[] in() {
313 313         Scanner in = new Scanner(System.in);
314 314         int flag;
315 315         int[] str1 = new int[5];
316 316         int[] str2 = new int[7];
317 317         flag = in.nextInt();
318 318         if(flag==1||flag == 2)
319 319         {
320 320             str1[0] = flag;
321 321             str1[1] = in.nextInt();
322 322             str1[2] = in.nextInt();
323 323             str1[3] = in.nextInt();
324 324             str1[4] = in.nextInt();
325 325             return str1;
326 326         }
327 327         else if(flag==3)
328 328         {
329 329             str2[0] = flag;
330 330             str2[1] = in.nextInt();
331 331             str2[2] = in.nextInt();
332 332             str2[3] = in.nextInt();
333 333             str2[4] = in.nextInt();
334 334             str2[5] = in.nextInt();
335 335             str2[6] = in.nextInt();
336 336             return str2;
337 337         }
338 338         else
339 339             {System.out.print("Wrong Format");return null;}
340 340         
341 341         
342 342         
343 343     }
344 344     
345 345 }
题目集4(7-2)

 

 踩坑心得:

  踩坑的地方就是在计算前几天和相差天数的地方,因为之前已经写过好几次关于日期的代码了,所以警惕性降低。

  根据样例输出的结果死活就是对不上。

  最后debug的时候发现,问题就出现在闰年和平年对日期天数数组 mon_maxnum 的修改上。

  以 getPreviousNDays()函数为例,因为两个关于计算的函数都是踩的同一个坑,我就不多加赘述了。

  在代码112行,可以看到,我是以一天为单位计算的,在每次进入减一天方法的时候,都会先判断一下,重置数组的值。之前,我是没有多加考虑这个地方的,一开始根本没有加入对数组不同情况下的判断。

  后来意识到可能是这个地方出的bug,我又对是否是闰年加入了判断,将数组改值。调试一下,发现还是有问题。当然是的,问题就在后面是平年的时候我也没有写条件把数组改回来。然后之后一直用闰年计算。

  所以在这个地方,因为天数数组已经相当于是一个全局变量了,所以每次都必须把平年闰年的判断都考虑进去。

改进建议:

在这里先贴上我在SourceMonitor计算出的圈复杂度

   

 

可以看到平均圈复杂度最高的就是Main函数了...这里就截取了Main函数的主图

 

从每个类的圈复杂度可以看出来,类的结构这样的分法应该是对的,最高的就是Main主类,10的圈复杂度。

不过正常来说,一般Main里面应是圈复杂度最小的。Main在这里明显还被赋予了别的比较复杂的职能,显然在这里还可以多分出一个处理类,把这个职责分出去。

如此才符合“单一职责”原则。

 

PowerDesign生成类图如下:

 

 

 

可以看出这里有多重的组合关系,之后会与后面的题目集5 7-4一起进行比较详细的比较叙述。

 

 

源码

题目集5 7-4

  1   1 import java.util.*;
  2   2 public class Main {
  3   3 
  4   4     public static void main(String[] args) {
  5   5         // TODO Auto-generated method stub
  6   6                 int[] str = Input.in();
  7   7                 if(str!=null)
  8   8                 {
  9   9                     switch(str[0])
 10  10                     {
 11  11                     case 1:
 12  12                         DateUtil d = new DateUtil(str[1],str[2],str[3]);
 13  13                         DateUtil tempd;
 14  14                         if(!d.checkInputValidity())
 15  15                             {System.out.print("Wrong Format");break;}
 16  16                         tempd = d.getNextDays(str[4]);
 17  17                         System.out.print(d.showDate()+" next "+str[4]+" days is:"+tempd.showDate());
 18  18                         break;
 19  19                     case 2:
 20  20                         DateUtil dd = new DateUtil(str[1],str[2],str[3]);
 21  21                         DateUtil tempdd;
 22  22                         if(!dd.checkInputValidity())
 23  23                             {System.out.print("Wrong Format");break;}
 24  24                         tempdd = dd.getPreviousDays(str[4]);
 25  25                         System.out.print(dd.showDate()+" previous "+str[4]+" days is:"+tempdd.showDate());
 26  26                         break;
 27  27                     case 3:
 28  28                         DateUtil d1 = new DateUtil(str[4],str[5],str[6]);
 29  29                         DateUtil d2 = new DateUtil(str[1],str[2],str[3]);
 30  30                         if(!d1.checkInputValidity()||!d2.checkInputValidity())
 31  31                             {System.out.print("Wrong Format");break;}
 32  32                         int sum = d1.getDaysofDates(d2);
 33  33                         System.out.print("The days between "+d2.showDate()+" and "+d1.showDate()+" are:"+sum);
 34  34                         break;
 35  35                         default:
 36  36                     }
 37  37                         
 38  38                 }
 39  39     }
 40  40 
 41  41 }
 42  42 class Input {
 43  43 
 44  44     static int[] in() {
 45  45         Scanner in = new Scanner(System.in);
 46  46         int flag;
 47  47         int[] str1 = new int[5];
 48  48         int[] str2 = new int[7];
 49  49         flag = in.nextInt();
 50  50         if(flag==1||flag == 2)
 51  51         {
 52  52             str1[0] = flag;
 53  53             str1[1] = in.nextInt();
 54  54             str1[2] = in.nextInt();
 55  55             str1[3] = in.nextInt();
 56  56             str1[4] = in.nextInt();
 57  57             return str1;
 58  58         }
 59  59         else if(flag==3)
 60  60         {
 61  61             str2[0] = flag;
 62  62             str2[1] = in.nextInt();
 63  63             str2[2] = in.nextInt();
 64  64             str2[3] = in.nextInt();
 65  65             str2[4] = in.nextInt();
 66  66             str2[5] = in.nextInt();
 67  67             str2[6] = in.nextInt();
 68  68             return str2;
 69  69         }
 70  70         else
 71  71             {System.out.print("Wrong Format");return null;}
 72  72         
 73  73         
 74  74         
 75  75     }
 76  76 }
 77  77 class Year {
 78  78 
 79  79     private int value;
 80  80     
 81  81     //构造函数
 82  82     Year(){
 83  83         
 84  84     }
 85  85     Year(int value){
 86  86         this.value = value;
 87  87     }
 88  88     
 89  89     public int getValue() {
 90  90         return value;
 91  91     }
 92  92     public void setValue(int value) {
 93  93         this.value = value;
 94  94     }
 95  95     
 96  96     boolean isLeapYear() {
 97  97         boolean juge = true ;
 98  98         if(this.value%4==0&&this.value%100!=0||this.value%400==0)
 99  99         {
100 100             juge = true;
101 101         }
102 102         else
103 103         {
104 104             juge = false;
105 105         }
106 106         return juge;
107 107     }
108 108     
109 109     boolean validate() {
110 110         if(this.value<1820||this.value>2020)
111 111             return false;
112 112         else
113 113             return true;
114 114     }
115 115     
116 116     void yearIncrement() {
117 117         this.value += 1;
118 118     }
119 119     
120 120     void yearReduction() {
121 121         this.value -= 1;
122 122     }
123 123 }
124 124 class Month {
125 125 
126 126     private int value;
127 127     
128 128     //构造函数
129 129     Month(){
130 130         
131 131     }
132 132     Month(int value){
133 133         this.value = value;
134 134     }
135 135     
136 136     
137 137     public int getValue() {
138 138         return value;
139 139     }
140 140     public void setValue(int value) {
141 141         this.value = value;
142 142     }
143 143     
144 144     void resetMin() {
145 145         this.value = 1;
146 146     }
147 147     
148 148     void resetMax() {
149 149         this.value = 12;
150 150     }
151 151     
152 152     boolean validate() {
153 153         if(this.value>=1&&this.value<=12)
154 154             return true;
155 155         else
156 156             return false;
157 157     }
158 158     
159 159     void monthIncrement() {
160 160         this.value += 1;
161 161     }
162 162     
163 163     void monthReduction() {
164 164         this.value -= 1;
165 165     }
166 166 }
167 167 class Day {
168 168         
169 169         private int value;
170 170         
171 171         //构造函数
172 172         Day(){
173 173             
174 174         }
175 175         Day(int value){
176 176             this.value = value;
177 177         }
178 178         
179 179         
180 180         public int getValue() {
181 181             return value;
182 182         }
183 183         public void setValue(int value) {
184 184             this.value = value;
185 185         }
186 186         
187 187         void dayIncrement() {
188 188                 this.value += 1;
189 189         }
190 190         
191 191         void dayReduction() {
192 192             this.value -= 1;
193 193         }
194 194 }
195 195 class DateUtil {
196 196 
197 197     private Year year;
198 198     private Month month;
199 199     private Day day;
200 200     private int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31}; 
201 201     
202 202     //构造函数
203 203     DateUtil(){
204 204         
205 205     }
206 206     DateUtil(int y,int m,int d){
207 207         this.year = new Year(y);
208 208         this.month = new Month(m);
209 209         this.day = new Day(d);
210 210     }
211 211     
212 212     
213 213     public Year getYear() {
214 214         return year;
215 215     }
216 216     public void setYear(Year year) {
217 217         this.year = year;
218 218     }
219 219     public Month getMonth() {
220 220         return month;
221 221     }
222 222     public void setMonth(Month month) {
223 223         this.month = month;
224 224     }
225 225     public Day getDay() {
226 226         return day;
227 227     }
228 228     public void setDay(Day day) {
229 229         this.day = day;
230 230     }
231 231     
232 232     
233 233     void setDayMin() {
234 234         this.day.setValue(1);
235 235     }
236 236     void setDayMax() {
237 237         if(this.year.isLeapYear())
238 238             mon_maxnum[1] = 29;
239 239         else
240 240             mon_maxnum[1] = 28;
241 241         
242 242         this.day.setValue(mon_maxnum[this.month.getValue()-1]);
243 243     }
244 244     
245 245     boolean checkInputValidity() {
246 246         if(this.year.validate()&&this.month.validate())
247 247         {
248 248             if(this.year.isLeapYear())
249 249                 mon_maxnum[1] = 29;
250 250             else
251 251                 mon_maxnum[1] = 28;
252 252             if(this.day.getValue()<=this.mon_maxnum[this.month.getValue()-1]&&this.day.getValue()>=1)
253 253                 return true;
254 254         }
255 255         return false;    
256 256     }
257 257     
258 258     DateUtil getNextDays(int n) {
259 259         DateUtil date = new DateUtil(this.year.getValue(),this.month.getValue(),this.day.getValue());
260 260         for(;n>0;n--)
261 261         {
262 262         if(date.year.isLeapYear())
263 263             date.mon_maxnum[1] = 29;
264 264         else
265 265             date.mon_maxnum[1] = 28;
266 266         
267 267            date.day.dayIncrement();
268 268            if(date.day.getValue() > date.mon_maxnum[date.month.getValue()-1])
269 269            {
270 270                date.month.monthIncrement();
271 271                if(date.month.getValue() > 12)
272 272                {
273 273                    date.year.yearIncrement();
274 274                    date.month.resetMin();
275 275                }
276 276                date.setDayMin();
277 277            }
278 278         }
279 279         return date;
280 280     }
281 281     DateUtil getPreviousDays(int n) {
282 282         DateUtil date = new DateUtil(this.year.getValue(),this.month.getValue(),this.day.getValue());
283 283         for(;n>0;n--)
284 284         {
285 285             if(date.year.isLeapYear())
286 286                 date.mon_maxnum[1] = 29;
287 287             else
288 288                 date.mon_maxnum[1] = 28;
289 289             date.day.dayReduction();
290 290             if(date.day.getValue()==0)
291 291             {
292 292                 date.month.monthReduction();
293 293                 if(date.month.getValue() == 0)
294 294                 {
295 295                     date.month.resetMax();
296 296                     date.year.yearReduction();
297 297                 }
298 298                 date.setDayMax();
299 299             }
300 300         }
301 301         return date;
302 302     }
303 303     boolean compareDates(DateUtil date) {
304 304         
305 305         if(this.year.getValue()>date.year.getValue())
306 306             return true;
307 307         else if(this.year.getValue() == date.year.getValue())
308 308         {
309 309             if(this.month.getValue()>date.month.getValue())
310 310                 return true;
311 311             else if(this.month.getValue() == date.month.getValue())
312 312             {
313 313                 if(this.day.getValue()>date.day.getValue())
314 314                     return true;
315 315             }
316 316         }
317 317         return false;   //默认不相等
318 318     }
319 319     boolean equalTwoDates(DateUtil date) {
320 320         if(this.year.getValue() == date.year.getValue() && this.month.getValue() == date.month.getValue() && this.day.getValue() == date.day.getValue())
321 321             return true;
322 322         else
323 323             return false;
324 324     }
325 325     int getDaysofDates(DateUtil date) {            
326 326         int sum = 0;
327 327         DateUtil temp = new DateUtil(this.year.getValue(),this.month.getValue(),this.day.getValue());
328 328         DateUtil temp2 = new DateUtil(date.year.getValue(),date.month.getValue(),date.day.getValue());
329 329         while(!temp.equalTwoDates(temp2))
330 330         {
331 331             if(temp.compareDates(temp2))
332 332             {
333 333                 
334 334                 temp2 = temp2.getNextDays(1);
335 335             }
336 336             else
337 337             {
338 338                 
339 339                 temp = temp.getNextDays(1);
340 340             }
341 341             sum++;
342 342         }
343 343         return sum;
344 344     }
345 345     String showDate() {
346 346         String date = "" + this.year.getValue() + "-" +this.month.getValue() + "-" +this.day.getValue();
347 347         return date;
348 348     }
349 349 }
题目集5(7-4)

 

踩坑心得:

很明显,这也是一个日期类的题,同样的,自以为“身经百战”的我以为可以无比丝滑的完成整个题..结果...只能说还是太年轻了。

 

 

 

 

会运行超时主要是因为我使用的是一天天的暴力模拟方式,大神说这样的循环大概1s跑1e8次,测试点主要测试int的最大值,2e9,跑下来理论值需要20s,这题给的限制时间在10s左右,妥妥的超时了。

恍然大悟后,我重新eclipse上测试了一遍我的源代码,在eclipse上大概要跑5s差不多吧...总之是很慢很慢了,因为一般来说,在eclipse上跑代码基本等待时间也就1s左右了吧...于是我依照建议将循环改为了以月计数(其实年也可以,但我想取一个折中的),eclipse上跑起来确实是快了许多,所以一般来说,在eclipse上跑代码可能最好也不要超过3s把...超过的话,大概就要向办法改进了。(在改了之后几乎等待时间可不计)

改进建议:

写日期类时,以日计数,虽然思路简单,写起来容易,但禁不起较大数据的打击。最好是把单位调大一点,若是数据再大,就以年计。

关于代码结构方面

 

 从圈复杂度上看,也是大体偏低的情况,而Main函数的高,同样给出Main类的具体分析

 

与题目集4的日期类差不多是相同的问题,Main类里可能存在多职责的问题。需要把相应职责分出去。

代码对应类图

 

 

关于题目集4 7-2 和题目集5 7-1的日期类代码比较分析

首先从类图就能很清晰的看到,显然题目集4的代码是组合关系而题目集5采用的是聚合关系。在写代码的时候也能感觉出来,明显组合关系比较难以理解,

但易于使用,只要搞清楚了其中的关系,还是很好写的。题目集5的聚合关系虽然很好理解,但写起来就明显感觉不如组合关系用的顺手(也可能是先写的

组合所以产生的错觉?)。在圈复杂度上,二者区别几乎可以忽略不计。如果一般来写的话,大部分可能下意识就会用聚合关系而不会用到组合,毕竟这里

组合也搞了两层。总之感觉,用到组合的话,在设计思路上花的时间可能会多一点,在后面写代码的时候就比较丝滑,聚合的话,设计上很容易就懂了,但

在写代码的时候可能要关注的点就会多一点。个人其实比较倾向于使用组合关系,主要感觉组合关系的封装做的比较好,在进行日期计算的时候就可以明显

感觉出来。

 

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

 

源码

题目集4 7-3

  1   1 import java.util.*;
  2   2 import java.math.*;
  3   3 class Input {
  4   4         
  5   5     static double[] in() {
  6   6         Scanner in = new Scanner(System.in);
  7   7         double[] datum1 = new double[2];
  8   8         double[] datum2 = new double[3];
  9   9         double[] datum3 = new double[2];
 10  10         double[] datum4 = new double[4];
 11  11         int flag;
 12  12         flag = in.nextInt();
 13  13         switch(flag) {
 14  14         case 1:
 15  15             datum1[0] = flag;
 16  16             datum1[1] = in.nextDouble();
 17  17             return datum1;
 18  18         case 2:
 19  19             datum2[0] = flag;
 20  20             datum2[1] = in.nextDouble();
 21  21             datum2[2] = in.nextDouble();
 22  22             return datum2;
 23  23         case 3:
 24  24             datum3[0] = flag;
 25  25             datum3[1] = in.nextDouble();
 26  26             return datum3;
 27  27         case 4:
 28  28             datum4[0] = flag;
 29  29             datum4[1] = in.nextDouble();
 30  30             datum4[2] = in.nextDouble();
 31  31             datum4[3] = in.nextDouble();
 32  32             return datum4;
 33  33             default:
 34  34                 System.out.print("Wrong Format");
 35  35         }
 36  36         return null;
 37  37     }
 38  38     
 39  39     static boolean vali(double[] datum) {
 40  40         int len = datum.length;
 41  41         for(int i = 0;i<len;i++) {
 42  42             if(datum[i]<=0.0)
 43  43                 {System.out.print("Wrong Format");return false;}
 44  44         }
 45  45         return true;
 46  46     }
 47  47 }
 48  48 class Shape {
 49  49         
 50  50     Shape(){
 51  51         System.out.println("Constructing Shape");
 52  52     }
 53  53     public double getArea() {
 54  54         double area;
 55  55         area = 0.0;
 56  56         return area;
 57  57     }
 58  58 }
 59  59 class Rectangle extends Shape {
 60  60                 private double width;
 61  61                 private double length;
 62  62                 
 63  63                 Rectangle(){
 64  64                     System.out.println("Constructing Rectangle");
 65  65                 }
 66  66                 double getWidth() {
 67  67                     return this.width;
 68  68                 }
 69  69                 void setWidth(double w) {
 70  70                     this.width = w;
 71  71                 }
 72  72                 double getLength() {
 73  73                     return this.length;
 74  74                 }
 75  75                 void setLength(double l) {
 76  76                     this.length = l;
 77  77                 }
 78  78                 
 79  79                 public double getArea() {
 80  80                     double area;
 81  81                     area = this.length*this.width;
 82  82                     return area;
 83  83                 }
 84  84 }
 85  85 class Box extends Rectangle {
 86  86             private double height;
 87  87             
 88  88             Box(){
 89  89                 System.out.println("Constructing Box");
 90  90             }
 91  91             void setHeight(double h) {
 92  92                 this.height = h;
 93  93             }
 94  94             double getHeight() {
 95  95                 return this.height;
 96  96             }
 97  97             public double getArea() {
 98  98                  double area;
 99  99                  area = this.getLength()*this.getWidth()*2+this.getLength()*this.height*2+this.getWidth()*this.height*2;
100 100                  return area;
101 101             }
102 102             
103 103             public double getVolume() {
104 104                 double volume;
105 105                 volume = this.getLength()*this.getWidth()*this.height;
106 106                 return volume;
107 107             }
108 108 }
109 109 class Ball extends Circle {
110 110         
111 111     Ball(){
112 112         System.out.println("Constructing Ball");
113 113     }
114 114     public double getArea() {
115 115         double area;
116 116         area = 4*Math.PI*this.getRadius()*this.getRadius();
117 117         return area;
118 118     }
119 119     public double getVolume() {
120 120         double volume;
121 121         volume = (4.0/3)*Math.PI*this.getRadius()*this.getRadius()*this.getRadius();
122 122         return volume;
123 123     }
124 124 }
125 125 class Circle extends Shape {
126 126         private double radius;
127 127         
128 128         Circle(){
129 129             System.out.println("Constructing Circle");
130 130         }
131 131         double getRadius() {
132 132             return this.radius;
133 133         }
134 134         void setRadius(double r) {
135 135             this.radius = r;
136 136         }
137 137         public double getArea() {
138 138             double area;
139 139             area = Math.PI*this.radius*this.radius;
140 140             return area;
141 141         }
142 142 }
143 143 
144 144 public class Main {
145 145 
146 146     public static void main(String[] args) {
147 147         // TODO Auto-generated method stub
148 148     //        Scanner in = new Scanner(System.in);
149 149             double[] datum = Input.in();
150 150             if(datum!=null)
151 151             {
152 152                 if(Input.vali(datum))
153 153                 {
154 154                 //    Shape shape = new Shape();
155 155                     if(datum[0]==1)
156 156                     {
157 157                         Circle c = new Circle();
158 158                         c.setRadius(datum[1]);
159 159                         System.out.print("Circle's area:"+String.format("%.2f",c.getArea()));
160 160                     }
161 161                     if(datum[0]==2)
162 162                     {
163 163                         Rectangle r = new Rectangle();
164 164                         r.setWidth(datum[1]);
165 165                         r.setLength(datum[2]);
166 166                         System.out.print("Rectangle's area:"+String.format("%.2f", r.getArea()));
167 167                     }
168 168                     if(datum[0]==3)
169 169                     {
170 170                 //        Circle c1 = new Circle();
171 171                         Ball b = new Ball();
172 172                         b.setRadius(datum[1]);
173 173                         System.out.println("Ball's surface area:"+String.format("%.2f",b.getArea()));
174 174                         System.out.print("Ball's volume:"+String.format("%.2f", b.getVolume()));
175 175                     }
176 176                     if(datum[0]==4)
177 177                     {
178 178                 //        Rectangle r1 = new Rectangle();
179 179                         Box box = new Box();
180 180                         box.setWidth(datum[1]);
181 181                         box.setLength(datum[2]);
182 182                         box.setHeight(datum[3]);
183 183                         System.out.println("Box's surface area:"+String.format("%.2f", box.getArea()));
184 184                         System.out.print("Box's volume:"+String.format("%.2f", box.getVolume()));
185 185                     }
186 186                     
187 187                     }
188 188                 }
189 189             }
190 190     }
题目集4 7-3

 

 踩坑心得:

  这个题只要严格按照老师给的类去写,基本不会出现什么问题。

改进建议:

SourceMonitor

 

 这个题主要是初步认知继承的写法与多态的应用,没有什么复杂的要求,因而复杂度很低。较为简单

PowerDesigner

 

 题目集6 7-5

 

源码

题目集6 7-5

  1   1 import java.util.*;
  2   2 import java.lang.Math;
  3   3 import java.util.ArrayList;
  4   4 import java.util.Collections;
  5   5 import java.util.Comparator;
  6   6 
  7   7 public class Main {
  8   8 
  9   9     public static void main(String[] args) {
 10  10         // TODO Auto-generated method stub
 11  11                 ArrayList<Shape> list_t;
 12  12                 list_t = Input.in_character();
 13  13                 if(list_t == null)
 14  14                     return ;
 15  15                 Output.out_character(list_t);
 16  16                 
 17  17     }
 18  18 
 19  19 }
 20  20 class Input {
 21  21             
 22  22             
 23  23             static ArrayList<Shape> in_character(){
 24  24                 
 25  25                 
 26  26                  Scanner in = new Scanner(System.in);
 27  27                  ArrayList<Shape> list = new ArrayList<Shape>();
 28  28                  double side1,side2,side3;
 29  29                  
 30  30                 int cnum = in.nextInt();
 31  31                 int rnum = in.nextInt();
 32  32                 int tnum = in.nextInt();
 33  33                 if(cnum<0||rnum<0||tnum<0)
 34  34                 {System.out.print("Wrong Format");return null;} 
 35  35                  for(int i = 0;i<cnum;i++)
 36  36                  {
 37  37                      side1 = in.nextDouble();
 38  38                      Circle c = new Circle(side1);
 39  39                      if(c.validate())
 40  40                          list.add(c);
 41  41                      else
 42  42                          return null;
 43  43                  }
 44  44                  
 45  45                  for(int j = 0;j<rnum;j++)
 46  46                  {
 47  47                      side1 = in.nextDouble();
 48  48                      side2 = in.nextDouble();
 49  49                      Rectangle r = new Rectangle(side1,side2);
 50  50                      if(r.validate())
 51  51                          list.add(r);
 52  52                      else
 53  53                          return null;
 54  54                  }
 55  55                  
 56  56                  for(int k = 0;k<tnum;k++)
 57  57                  {
 58  58                      side1 = in.nextDouble();
 59  59                      side2 = in.nextDouble();
 60  60                      side3 = in.nextDouble();
 61  61                      Triangle t = new Triangle(side1,side2,side3);
 62  62                      if(t.validate())
 63  63                          list.add(t);
 64  64                      else
 65  65                          return null;
 66  66                  }
 67  67                  
 68  68                  return list;
 69  69                  
 70  70             }
 71  71 }
 72  72 class Output {
 73  73     
 74  74     static void out_character(ArrayList<Shape> list_t) {
 75  75         int len = list_t.size();
 76  76         double area = 0;
 77  77         
 78  78         System.out.println("Original area:");
 79  79         for(int i = 0;i<len;i++)
 80  80         {
 81  81             System.out.print(list_t.get(i).toString()+" ");
 82  82             area += list_t.get(i).getArea();
 83  83             
 84  84         }
 85  85         System.out.println("\nSum of area:"+String.format("%.2f", area));
 86  86         System.out.println("Sorted area:");
 87  87         
 88  88         Collections.sort(list_t,new Comparator<Shape>() {
 89  89             public int compare(Shape o1,Shape o2) {
 90  90                 return (int) (o1.getArea() - o2.getArea());
 91  91             }
 92  92         });
 93  93         
 94  94         for(int i = 0;i<len;i++)
 95  95         {
 96  96             System.out.print(list_t.get(i).toString()+" ");
 97  97         }
 98  98         System.out.print("\nSum of area:"+String.format("%.2f", area));
 99  99     }
100 100 }
101 101 class Shape {
102 102             
103 103       public double getArea() {
104 104           double area = 0.0;
105 105           return area;
106 106       }
107 107       public boolean validate() {
108 108           boolean juge = true;
109 109           return juge;
110 110       }
111 111       
112 112       public String toString() {
113 113             String s;
114 114             s = String.format("%.2f", this.getArea());
115 115             return s;
116 116         }
117 117 }
118 118 class Circle extends Shape{
119 119         private double radius;
120 120         
121 121         public double getRadius() {
122 122             return radius;
123 123         }
124 124 
125 125         public void setRadius(double radius) {
126 126             this.radius = radius;
127 127         }
128 128 
129 129         Circle(double radius){
130 130             this.radius = radius;
131 131         }
132 132         
133 133         public double getArea() {
134 134             double area;
135 135             area = Math.PI*radius*radius;
136 136             return area;
137 137         }
138 138         public boolean validate() {
139 139             if(this.radius<=0)
140 140             {System.out.print("Wrong Format");return false;}
141 141             else
142 142                 return true;
143 143         }
144 144         
145 145 }
146 146 class Rectangle extends Shape{
147 147 
148 148     private double width;
149 149     private double length;
150 150     
151 151     
152 152     public double getWidth() {
153 153         return width;
154 154     }
155 155 
156 156     public void setWidth(double width) {
157 157         this.width = width;
158 158     }
159 159 
160 160     public double getLength() {
161 161         return length;
162 162     }
163 163 
164 164     public void setLength(double length) {
165 165         this.length = length;
166 166     }
167 167 
168 168     Rectangle(double length,double width){
169 169         this.length = length;
170 170         this.width = width;
171 171     }
172 172     
173 173     public double getArea() {
174 174         double area = width*length;
175 175         return area;
176 176     }
177 177     public boolean validate() {
178 178         if(this.length<0||this.width<0)
179 179         {
180 180             System.out.print("Wrong Format");
181 181             return false;
182 182         }
183 183         else
184 184             return true;
185 185     }
186 186     
187 187 }
188 188 class Triangle extends Shape {
189 189 
190 190     private double side1;
191 191     private double side2;
192 192     private double side3;
193 193     
194 194     
195 195     
196 196     public double getSide1() {
197 197         return side1;
198 198     }
199 199 
200 200     public void setSide1(double side1) {
201 201         this.side1 = side1;
202 202     }
203 203 
204 204     public double getSide2() {
205 205         return side2;
206 206     }
207 207 
208 208     public void setSide2(double side2) {
209 209         this.side2 = side2;
210 210     }
211 211 
212 212     public double getSide3() {
213 213         return side3;
214 214     }
215 215 
216 216     public void setSide3(double side3) {
217 217         this.side3 = side3;
218 218     }
219 219 
220 220     Triangle(double side1,double side2,double side3){
221 221         this.side1 = side1;
222 222         this.side2 = side2;
223 223         this.side3 = side3;
224 224     }
225 225     
226 226     public double getArea() {
227 227         double area ;
228 228         double p = (this.side1+this.side2+this.side3)/2.0;
229 229         p = p*(p-this.side1)*(p-this.side2)*(p-this.side3);
230 230         area = Math.sqrt(p);
231 231         return area;
232 232     }
233 233     
234 234     public boolean validate() {
235 235         if(this.side1<0||this.side2<0||this.side3<0)
236 236             {System.out.print("Wrong Format");return false;}
237 237         if(this.side1+this.side2>this.side3&&this.side1+this.side3>this.side2&&this.side2+this.side3>this.side1)
238 238             return true;
239 239         else
240 240         {System.out.print("Wrong Format");return false;}
241 241     }
242 242 }
题目集6 7-5

 

 踩坑心得:

一开始,测试点“无矩形对象”测试点过不去,是因为这里在计算sum of area的时候不能直接使用之前已经保留了两位小数的数据,一定要使用原始的数据,然后再保留两位小数。这是精度问题。

关于这样的精度问题要警惕,留心一点。虽然看上去是八竿子打不着一块儿的测试点,但就是因为精度导致的问题,改了就对了。

 

改进建议:

SourceMonitor

直接拿圈复杂度最高的来说

在input类里确实是进行了很多判断,但我感觉这些也不能少的吧。也许要多看看大神的代码,模仿也是提升自我的方式。

 

题目集6 7-6

 

源码

题目集6 7-6

  1   1 import java.util.*;
  2   2 public class Main {
  3   3 
  4   4     public static void main(String[] args) {
  5   5         // TODO Auto-generated method stub
  6   6             Scanner input = new Scanner(System.in);
  7   7             double radius,length,width;
  8   8             
  9   9             radius = input.nextDouble();
 10  10             length = input.nextDouble();
 11  11             width = input.nextDouble();
 12  12             
 13  13             Circle c = new Circle(radius);
 14  14             if(!c.validate())
 15  15                 return ;
 16  16             Rectangle r = new Rectangle(length,width);
 17  17             if(!r.validate())
 18  18                 return ;
 19  19             System.out.println(String.format("%.2f", c.getArea()));
 20  20             System.out.print(String.format("%.2f", r.getArea()));
 21  21     }
 22  22 
 23  23 }
 24  24 interface GetArea {
 25  25             
 26  26          default double getArea() {
 27  27             double n = 0.0;
 28  28             return n;
 29  29         }
 30  30          
 31  31          default boolean validate() {
 32  32              return true;
 33  33          }
 34  34 }
 35  35 class Circle implements GetArea{
 36  36 
 37  37     private double radius;
 38  38     
 39  39     Circle(){
 40  40         
 41  41     }
 42  42     Circle(double radius){
 43  43         this.radius = radius;
 44  44     }
 45  45 
 46  46     public double getRadius() {
 47  47         return radius;
 48  48     }
 49  49 
 50  50     public void setRadius(double radius) {
 51  51         this.radius = radius;
 52  52     }
 53  53     
 54  54     public double getArea() {
 55  55         double area;
 56  56         area = Math.PI*this.radius*this.radius;
 57  57         return area;
 58  58     }
 59  59     
 60  60     public boolean validate() {
 61  61         if(this.radius<=0)
 62  62         {System.out.print("Wrong Format");return false;}
 63  63         else
 64  64             return true;
 65  65     }
 66  66     
 67  67 }
 68  68 class Rectangle implements GetArea{
 69  69 
 70  70     private double width;
 71  71     private double length;
 72  72     
 73  73     Rectangle(double length,double width){
 74  74         this.length = length;
 75  75         this.width = width;
 76  76     }
 77  77     Rectangle(){
 78  78         
 79  79     }
 80  80     public double getWidth() {
 81  81         return width;
 82  82     }
 83  83     public void setWidth(double width) {
 84  84         this.width = width;
 85  85     }
 86  86     public double getLength() {
 87  87         return length;
 88  88     }
 89  89     public void setLength(double length) {
 90  90         this.length = length;
 91  91     }
 92  92     
 93  93     public double getArea() {
 94  94         double area;
 95  95         area = this.length*this.width;
 96  96         return area;
 97  97     }
 98  98     public boolean validate() {
 99  99         if(this.length<=0||this.width<=0)
100 100         {System.out.print("Wrong Format");return false;}
101 101         else
102 102             return true;
103 103     }
104 104 }
题目集6 7-6

 

 踩坑心得:

这道题和之前题目集4给的题性质差不多,就是对接口的基本应用,比较简单。

 

改进建议:

SourceMonitor

 从代码行数也可以看出来,这道题基本没什么,就是很基础不过的接口使用。

 

 分析三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)

  题目集4 7-3

  7-3 设计将不同的职责(输入,输出等)封装在了类里。使用了继承,抽象出一个Shape类,定义了计算面积的方法,

  其他的图形都继承了Shape类,同时也从Shape继承了计算面积的方法,又使用了多态,重写了计算面积的方法,在

   进行调用的时候根据调用的对象不同也会执行不同的计算面积的方法。

  题目集6 7-5

 7-5 在类设计方面和7-3的差别不大,但使用了ArrayList、Collections等类,写代码时更加简洁,排序也整得比较简单了。

  题目集6 7-6

  除了之前的继承和多态,又加上了接口getArea。接口相当于是一种身份,可以做某种行为的身份,比如如果你是VIP那你

  获得的额外的许可就是通过“VIP”这个“接口”获得的。

 

 ③对三次题目集用到的正则表达式进行分析总结

  三次表达式中用到正则表达式主要是进行模糊匹配。

  使用正则表达式所用的基本需要的类库  java.util.regex.*;

  使用正则表达式的一般形式

 第一,Pattern p = Pattern.compile("你的正则表达式");

   第二,Matcher m = p.matcher(待匹配的字符串);

   第三,使用m.find()或m.maches()检测是否匹配成功

  以题目集6 7-1 为例 进行QQ号校验 

 1 class Main{
 2     public static void main(String args[]){
 3         Scanner input = new Scanner(System.in);
 4         String s;
 5         s = input.nextLine();
 6         Pattern p = Pattern.compile("^[1-9][\\d]{4,14}$");
 7         Matcher m = p.matcher(s);
 8         if(m.matches())
 9             System.out.print("你输入的QQ号验证成功");
10         else
11             System.out.print("你输入的QQ号验证失败");
12     }
13 }

 

  对正则进行最基本的应用差不多就是这样了,剩下就是必须明白一些关键字的含义。

  

\d [0—9]
\D [^0—9]
\w [0—9A—Z_a—z]
\W [^0—9A—Z_a—z]
\s [\t\n\r\f]
\S [^\t\n\r\f]
^ 表示否,如果用在方括号内,^表示不想匹配的字符
. 匹配单个任意字符
[] 只有方括号里面指定的字符才参与匹配,也只能匹配单个字符
| 相当与“或”,可以匹配指定的字符,但是也只能选择其中一项进行匹配
* 0次或多次
+ 1次或多次
0次或1次
{n} 恰好n次
{n,m}

n次到m次之间

  在使用时需要注意的是find和maches的区别,find是从待匹配的字符串中找到相应符合匹配的地方即可,可能有一个也有可能有多个。

  maches是要求字符必须完全符合匹配描述。

 ④对题目集5 7-4中java集合框架应用的分析和总结

  源码

  题目集5 7-4

  1 import java.util.ArrayList;
  2 import java.util.regex.Matcher;
  3 import java.util.regex.Pattern;
  4 import java.util.*;
  5 
  6 public class Main {
  7 
  8     public static void main(String[] args) {
  9         // TODO Auto-generated method stub
 10                 ArrayList<String> list;
 11                 int flag = 0;
 12                     list = Input.in();
 13             cir:    for(int i = 0;i<list.size();i++)
 14                 {
 15                 if(flag==1)
 16                 {
 17                     Pattern p3 = Pattern.compile("\\*/");
 18                     Matcher m3 = p3.matcher(list.get(i));
 19                     if(m3.find()==true)
 20                     {
 21                         if(m3.end()==list.get(i).length())
 22                             continue cir;
 23                         String s = list.get(i).substring(m3.end());
 24                         list.set(i, s);
 25                         flag = 0;
 26                     }
 27                 }
 28                   if(flag == 0)
 29                     flag = Match.search(list.get(i).toString(),flag);
 30                     
 31                 }
 32                 Output.out();
 33     }
 34 
 35 }
 36 class Input {
 37 
 38             static ArrayList<String> in(){
 39                 ArrayList<String> list = new ArrayList<String>();
 40                 Scanner input = new Scanner(System.in);
 41                 String temp = input.nextLine();
 42                 
 43                 while(!temp.trim().equals("exit"))
 44                 {
 45                     list.add(temp);
 46                     temp = input.nextLine();
 47                 }
 48                 
 49                 if(list.size()==0||list.get(0).isEmpty())
 50                     System.out.println("Wrong Format");
 51                 /*
 52                  * else { for(int i = 0;i<list.size();i++) System.out.println(list.get(i)); }
 53                  */
 54                 return list;
 55             }
 56 }
 57 class Match {
 58 
 59   static final  String[] key  = { "abstract","assert","boolean","break","byte","case","catch",
 60              "char","class","const","continue","default","do","double","else",
 61              "enum","extends","false","final","finally","float",
 62              "for","goto","if","implements","import","instanceof",
 63              "int","interface","long","native","new","null","package",
 64              "private","protected","public","return","short","static",
 65              "strictfp","super","switch","synchronized","this","throw",
 66              "throws","transient","true","try","void","volatile","while"};            
 67             
 68          static int[] result = new int[53];
 69          
 70             static int search(String s,int flag)
 71             {
 72                 
 73                 //排除注释关键词
 74                 Pattern p2 = Pattern.compile("/\\*");
 75                 Pattern p3 = Pattern.compile("\\*/");
 76                 Matcher m2 = p2.matcher(s);
 77                 Matcher m3 = p3.matcher(s);
 78                 while(m2.find()==true&&m3.find()==false)
 79                 {
 80                     if(m2.start()==0)
 81                         return 1;
 82                     s = s.substring(0, m2.start());
 83                     flag = 1;
 84                 }
 85                 while(m2.find()&&m3.find())
 86                     s = s.replaceAll("/\\*\\s.*.+\\s.*\\*/", "");
 87                 while(m2.find()==false&&m3.find()==true)
 88                 {
 89                     if(m3.end()==s.length())
 90                         return 0;
 91                     s = s.substring(m3.end());
 92                     flag = 0;
 93                 }
 94                 
 95                 Pattern p1 = Pattern.compile("//");
 96                 Matcher m1 = p1.matcher(s);
 97                 if(m1.find())
 98                 {
 99                     if(m1.start()==0)
100                         return 0;
101                     s = s.substring(0, m1.start()-1);
102                 }
103                 
104                 Pattern pa=Pattern.compile("\"(.*?)\"");
105                 Matcher ma=pa.matcher(s);
106                 while(ma.find()){
107                   s=s.replace(ma.group()," ");
108                   pa=Pattern.compile("\"(.*?)\"");
109                   ma=pa.matcher(s);
110                 }
111                 s=s.replace("["," ");
112                  s=s.replace("]"," ");
113                 s=s.replace("-","a");
114                  s=s.replace("*","a");
115                  s=s.replace("/","a");
116                  s=s.replace("+","a");
117                 s=s.replace(">","a");
118                  s=s.replace("=","a");
119                 s=s.replace("!","a");
120                  s=s.replace(":","a");
121                 s=s.replace("\\","a");
122                  s= s.replaceAll("[^a-zA-Z]", " ");
123     
124                 //匹配所有关键字
125                 for(int i = 0;i<53;i++)
126                 {
127                     Pattern p = Pattern.compile("(\\s+|^|\\(|\\{|;)"+key[i]+"(\\s+|\\.|\\}|;|\\[|$)");
128                 //    Pattern p4 = Pattern.compile("^"+key[i]+"\\s+");
129                 //    Matcher m4 = p4.matcher(s);
130                     Matcher m = p.matcher(s);
131                     while(m.find())
132                         result[i]++;
133                 }
134                 return flag;
135             }
136 }
137 class Output {
138 
139     
140     static void out() {
141         for(int i = 0;i<53;i++)
142         {
143             if(Match.result[i]>0)
144             {
145                 System.out.println(Match.result[i]+"    "+Match.key[i]);
146             }
147         }    
148     }
149 }
题目集5 7-4

 踩坑心得:

 一开始我的思路是用ArrayList把每一行都分开来判断,后来发现其他还好,就是当遇到注释(/*  */)的时候,处理就变得十分繁琐且难以实现,后来写完之后测出了bug也改不太动了...

后来问了室友,她们都是用StringBuilder把每一行的都全部拼起来,放在同一行...显然这样注释的问题就很容易解决了...关于这样导致单行注释难以判断的问题,在把所有的行拼起来的时候就可以进行判断,把//后的全部去掉再进行拼接。

在写Match的时候一开始几乎全是答案错误,后来发现很多没考虑到的点

1、双引号内的字符串不计

2、变量名可能是各种符号加上关键字的形式

3、可能有比较符号等关键字在其中一边,这时候可能它还是属于关键字的。

(第三点是将符号删去,而第二点是要判断这个位非关键字所以改为任意字母)

改进建议:

 可以看出Match的圈复杂度过高了。主要这题感觉考虑的情况好多。

还是放在一行一起判断比较简单。

 

对7-4中集合框架应用的分析和总结:

 在我自己的代码中,我使用的是ArrayList。在本题的题目描述总能知道,这里是需要不停做插入的。所以肯定是要用到ArrayList类或者用

StringBuilder类的,这样比较方便。本题检验的是关键字匹配。很明显要使用到正则表达式进行匹配。整体的框架比较简单就是输入,匹配

输出。这题主要的难点个人认为在需要关注的点比较多,就是匹配过程中可能出现的情况很多,难以汇总。其他就是使用类似hashMap之类

的,要自己去补习一下,但也比较简单,因为这里只涉及到比较简单的应用,并不复杂。

 

总结

这三次题目集显然更加深入的表现了java封装、继承、多态的特性,还加入了接口,使程序拥有了“多继承”的性质。题目越深入越是体现出

类设计的重要性,之前也进行了不同类关系之间代码的区别比较。结合上课老师教授的示例,对java的三大特性理解更加深刻,并且了解了

编写java代码的几大原则,类似Lisvoke原则、开闭原则、单一职责、依赖倒置原则...对封装职责、继承公共抽象类有了更加具象的理解。

Lisvoke主要是子类不能有父类所没有的约束

开闭原则:对修改关闭,对扩展开放,结合咖啡机的示例对一原则会有更加深入的理解,感觉有点难以描述...就是感觉明白了点什么

对java的代码设计一直在逐渐深入的讲解,老师上课的方式易于让人接收这类思维方面的难以具象化的知识点。

 

posted @ 2021-11-13 19:36  frangipane_xx  阅读(49)  评论(0)    收藏  举报