1、前言
本三次题目集的知识点个人认为主要有以下知识点(包括但不限于):
①程序中聚合的使用
②关于类中继承的使用
③对于代码内正则表达式的运用
④对象数组的使用
⑤Java中对于接口的使用
⑥Java内多态的用法
这三套题目集的话题量是逐步增大的,至于难度的话,个人感觉由于题量的增大,难度被逐步分散到不同的题目中去了,程序没有之前那么难写出来,还算是比较适合的。并且从出的题目上来看老师是想重点要让我们去摸透正则表达式的使用,对程序中的聚合与继承熟悉和使用以及尝试去使用关于多态这个方面的知识。
2、设计与分析
(1)题目集4(7-2)与题目集5(7-4)比较
①题目集4(7-2)
参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:
应用程序共测试三个功能:
有三种输入方式(以输入的第一个数字划分[1,3]):
1 //package buhuichucuo; 2 //import java.util. 3 import java.util.Scanner; 4 public class Main 5 { 6 public static void main(String[] args) 7 { 8 Scanner inn = new Scanner(System.in); 9 int year = 0; 10 int month = 0; 11 int day = 0; 12 13 int n = inn.nextInt(); 14 if(n==1) 15 { 16 int x = 0; 17 year = Integer.parseInt(inn.next()); 18 month = Integer.parseInt(inn.next()); 19 day = Integer.parseInt(inn.next()); 20 21 DateUtil date = new DateUtil(year, month, day); 22 23 if (!date.checkInputValidity()) 24 { 25 System.out.print("Wrong Format"); 26 System.exit(0); 27 } 28 x = inn.nextInt(); 29 if(x<0) 30 { 31 System.out.print("Wrong Format"); 32 System.exit(0); 33 } 34 System.out.print(date.getNextNDays(x).showDate()); 35 } 36 if(n==2) 37 { 38 int m = 0; 39 year = Integer.parseInt(inn.next()); 40 month = Integer.parseInt(inn.next()); 41 day = Integer.parseInt(inn.next()); 42 43 DateUtil date = new DateUtil(year, month, day); 44 45 if (!date.checkInputValidity()) { 46 System.out.print("Wrong Format"); 47 System.exit(0); 48 } 49 50 m = inn.nextInt(); 51 52 if (m < 0) { 53 System.out.print("Wrong Format"); 54 System.exit(0); 55 } 56 57 System.out.print(date.getPreviousNDays(m).showDate()); 58 } 59 if(n==3) 60 { 61 year = Integer.parseInt(inn.next()); 62 month = Integer.parseInt(inn.next()); 63 day = Integer.parseInt(inn.next()); 64 65 int anotherYear = Integer.parseInt(inn.next()); 66 int anotherMonth = Integer.parseInt(inn.next()); 67 int anotherDay = Integer.parseInt(inn.next()); 68 69 DateUtil fromDate = new DateUtil(year, month, day); 70 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 71 72 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) 73 { 74 System.out.print(fromDate.getDaysofDates(toDate)); 75 } 76 else 77 { 78 System.out.print("Wrong Format"); 79 System.exit(0); 80 } 81 } 82 //else 83 //{ 84 // System.out.println("Wrong Format"); 85 // System.exit(0); 86 //} 87 } 88 } 89 90 91 92 93 class DateUtil{ 94 Day day; 95 DateUtil(){ 96 } 97 DateUtil(int d,int m,int y) 98 { 99 this.day = new Day(d,m,y); 100 } 101 102 public Day getDay()//导出日期 103 { 104 return this.day; 105 } 106 public void setDay(Day day)//设置日期 107 { 108 this.day = day; 109 } 110 /// 111 public boolean checkInputValidity()//检查数据合法性 112 { 113 if(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&day.validate()) 114 { 115 return true; 116 } 117 else 118 { 119 return false; 120 } 121 } 122 public boolean compareDates(DateUtil date)//比较两个日期的大小 123 { 124 if (date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue()) 125 { 126 return false; 127 } 128 else if (date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue() 129 &&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue()) 130 { 131 return false; 132 } 133 if (date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue() 134 &&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue() 135 &&date.getDay().getValue()<this.getDay().getValue()) 136 { 137 return false; 138 } 139 else 140 { 141 return true; 142 } 143 } 144 public boolean equalTwoDates(DateUtil date)//判定两个日期是否相等 145 { 146 if (this.getDay().getValue()==date.getDay().getValue() 147 && this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue() 148 && this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()) 149 { 150 return true; 151 } 152 else 153 { 154 return false; 155 } 156 } 157 /// 158 public String showDate()//日期值格式化 159 { 160 String s=this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue(); 161 return s; 162 } 163 /// 164 public DateUtil getNextNDays(int n)//求下n天 165 { 166 int arr[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 167 int year = this.getDay().getMonth().getYear().getValue(); 168 int month = this.getDay().getMonth().getValue(); 169 int day = this.getDay().getValue(); 170 171 for(int i=0;i<n;i++) 172 { 173 174 if(day==arr[month-1])//如果天数到达本月最后一天 175 { 176 if(month==12)//如果月份到达最后一月 177 { 178 year++; 179 month=1; 180 day=1; 181 } 182 else//若不是最后一月 183 { 184 month++; 185 day=1; 186 } 187 } 188 else//如果不是一个月的最后一天 189 { 190 day++; 191 } 192 if((year%4==0&&year%100!=0)||year%400==0) 193 { 194 arr[1]=29; 195 } 196 else 197 { 198 arr[1]=28; 199 } 200 } 201 return new DateUtil(year, month, day); 202 } 203 public DateUtil getPreviousNDays(int n)//求前n天 204 { 205 int arr[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 206 int year = this.getDay().getMonth().getYear().getValue(); 207 int month = this.getDay().getMonth().getValue(); 208 int day = this.getDay().getValue(); 209 for(int i=0;i<n;i++) 210 { 211 if(day==1) 212 { 213 if(month==1)//如果月份到达一月 214 { 215 year--; 216 month=12; 217 day=31; 218 } 219 else////若不是一月 220 { 221 month--; 222 day=arr[month-1]; 223 } 224 } 225 else 226 { 227 day--; 228 } 229 if((year%4==0&&year%100!=0)||year%400==0) 230 { 231 arr[1]=29; 232 } 233 else 234 { 235 arr[1]=28; 236 } 237 } 238 return new DateUtil(year, month, day); 239 } 240 /// 241 public int getDaysofDates(DateUtil date)//求两个日期之间的天数 242 { 243 DateUtil pred = this; 244 DateUtil nextd = date; 245 if (this.equalTwoDates(date)) { 246 return 0; 247 } 248 else if (!this.compareDates(date)) { 249 pred = date; 250 nextd = this; 251 } 252 int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 253 int i,j,d = 0; 254 for(i=pred.getDay().getMonth().getYear().getValue()+1;i<nextd.getDay().getMonth().getYear().getValue();i++) { 255 d=d+365; 256 if(new Year(i).isLeapYear()) 257 d++; 258 } 259 if (pred.getDay().getMonth().getYear().getValue()!=nextd.getDay().getMonth().getYear().getValue()) { 260 for(j=pred.getDay().getMonth().getValue()+1;j<=12;j++) 261 d=d+arr[j]; 262 d+=arr[pred.getDay().getMonth().getValue()]-pred.getDay().getValue(); 263 for(j=1;j<nextd.getDay().getMonth().getValue();j++) 264 d+=arr[j]; 265 d+=nextd.getDay().getValue(); 266 if(pred.getDay().getMonth().getYear().isLeapYear()&&pred.getDay().getMonth().getValue()<=2) 267 d++; 268 if (nextd.getDay().getMonth().getYear().isLeapYear()&&nextd.getDay().getMonth().getValue()>2) { 269 d++; 270 } 271 } 272 else if(pred.getDay().getMonth().getYear().getValue()==nextd.getDay().getMonth().getYear().getValue()&&pred.getDay().getMonth().getValue()!=nextd.getDay().getMonth().getValue()){ 273 for(j=pred.getDay().getMonth().getValue()+1;j<=nextd.getDay().getMonth().getValue()-1;j++) 274 d+=arr[j]; 275 d+=arr[pred.getDay().getMonth().getValue()]-pred.getDay().getValue(); 276 d+=nextd.getDay().getValue(); 277 if(pred.getDay().getMonth().getYear().isLeapYear()&&pred.getDay().getMonth().getValue()<=2) 278 d++; 279 } 280 else if(pred.getDay().getMonth().getYear().getValue()==nextd.getDay().getMonth().getYear().getValue()&&pred.getDay().getMonth().getValue()==nextd.getDay().getMonth().getValue()){ 281 d=nextd.getDay().getValue()-pred.getDay().getValue(); 282 } 283 return d; 284 } 285 } 286 287 class Day{ 288 int value; 289 Month month; 290 int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31}; 291 Day(){ 292 } 293 Day(int yearValue,int monthValue,int dayValue) 294 { 295 this.month = new Month(yearValue,monthValue,dayValue); 296 this.value = dayValue; 297 } 298 /// 299 public int getValue() 300 { 301 return value; 302 } 303 public void setValue(int value) 304 { 305 this.value = value; 306 } 307 public Month getMonth() 308 { 309 return month; 310 } 311 public void setMonth(Month value) 312 { 313 this.month = value; 314 } 315 /// 316 public void resetMin()//日期复位 317 { 318 value = 1; 319 } 320 public void resetMax()//日期设为月最大值 321 { 322 value = mon_maxnum[month.getValue()-1]; 323 } 324 /// 325 public boolean validate()//检验日期合法性 326 { 327 if(this.getMonth().getYear().isLeapYear()) 328 mon_maxnum[1]++; 329 if(1<=value&&mon_maxnum[month.getValue()-1]>=value) 330 return true; 331 return false; 332 } 333 /// 334 public void dayIncrement()//日期增一 335 { 336 value++; 337 } 338 public void dayReduction()//日期减一 339 { 340 value--; 341 } 342 } 343 344 class Month{ 345 int value; 346 Year year; 347 Month(){ 348 } 349 Month(int yearValue,int monthValue,int dayValue) 350 { 351 this.value = monthValue; 352 this.year =new Year(yearValue); 353 } 354 public int getValue() 355 { 356 return this.value; 357 } 358 public void setValue(int value) 359 { 360 this.value = value; 361 } 362 /// 363 public Year getYear() 364 { 365 return year; 366 } 367 public void setYear(Year year) 368 { 369 this.year = year; 370 } 371 /// 372 public void resetMin()//月份复位 373 { 374 this.value = 1; 375 } 376 public void resetMax()//月份设置为12 377 { 378 this.value = 12; 379 } 380 /// 381 public boolean validate()//校验数据合法性 382 { 383 if(value<1||value>12) 384 { 385 return false; 386 } 387 else 388 { 389 return true; 390 } 391 } 392 /// 393 public void monthIncrement()//月份增一 394 { 395 this.value++; 396 } 397 public void monthReduction()//月份减一 398 { 399 this.value--; 400 } 401 } 402 403 class Year{ 404 int value; 405 Year(){ 406 } 407 Year(int value) 408 { 409 this.value = value; 410 } 411 /// 412 public int getValue() 413 { 414 return value; 415 } 416 public void setValue(int value) 417 { 418 this.value = value; 419 } 420 /// 421 public boolean isLeapYear()//判断是否为闰年 422 { 423 if((value%4==0&&value%100!=0)||value%400==0) 424 { 425 return true; 426 } 427 else 428 { 429 return false; 430 } 431 } 432 public boolean validate()//判断数据合法性 433 { 434 if(this.value>2020||this.value<1820) 435 { 436 return false; 437 } 438 else 439 { 440 return true; 441 } 442 } 443 /// 444 public void yearIncrement()//年份增一 445 { 446 this.value++; 447 } 448 public void yearReduction()//年份减一 449 { 450 this.value--; 451 } 452 }
②题目集5(7-5)
参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:
应用程序共测试三个功能:
有三种输入方式(以输入的第一个数字划分[1,3]):
1 //package test5; 2 //package buhuichucuo; 3 //import java.util. 4 import java.util.Scanner; 5 public class Main 6 { 7 public static void main(String[] args) 8 { 9 Scanner inn = new Scanner(System.in); 10 int year = 0; 11 int month = 0; 12 int day = 0; 13 14 int n = inn.nextInt(); 15 if(n==1) 16 { 17 int x = 0; 18 year = Integer.parseInt(inn.next()); 19 month = Integer.parseInt(inn.next()); 20 day = Integer.parseInt(inn.next()); 21 22 DateUtil date = new DateUtil(year, month, day); 23 24 if (!date.checkInputValidity()) 25 { 26 System.out.print("Wrong Format"); 27 System.exit(0); 28 } 29 x = inn.nextInt(); 30 if(x<0) 31 { 32 System.out.print("Wrong Format"); 33 System.exit(0); 34 } 35 System.out.print(year+"-"+month+"-"+day+" next "+x+" days is:"+date.getNextNDays(x).showDate()); 36 } 37 if(n==2) 38 { 39 int m = 0; 40 year = Integer.parseInt(inn.next()); 41 month = Integer.parseInt(inn.next()); 42 day = Integer.parseInt(inn.next()); 43 44 DateUtil date = new DateUtil(year, month, day); 45 46 if (!date.checkInputValidity()) { 47 System.out.print("Wrong Format"); 48 System.exit(0); 49 } 50 51 m = inn.nextInt(); 52 53 if (m < 0) { 54 System.out.print("Wrong Format"); 55 System.exit(0); 56 } 57 58 System.out.print(year+"-"+month+"-"+day+" previous "+m+" days is:"+date.getPreviousNDays(m).showDate()); 59 } 60 if(n==3) 61 { 62 year = Integer.parseInt(inn.next()); 63 month = Integer.parseInt(inn.next()); 64 day = Integer.parseInt(inn.next()); 65 66 int anotherYear = Integer.parseInt(inn.next()); 67 int anotherMonth = Integer.parseInt(inn.next()); 68 int anotherDay = Integer.parseInt(inn.next()); 69 70 DateUtil fromDate = new DateUtil(year, month, day); 71 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 72 73 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) 74 { 75 System.out.print("The days between "+year+"-"+month+"-"+day+" and "+anotherYear+"-"+anotherMonth+"-"+anotherDay+" are:"+fromDate.getDaysofDates(toDate)); 76 } 77 else 78 { 79 System.out.print("Wrong Format"); 80 System.exit(0); 81 } 82 } 83 //else 84 //{ 85 // System.out.println("Wrong Format"); 86 // System.exit(0); 87 //} 88 } 89 } 90 91 92 93 94 class DateUtil{ 95 Day day; 96 DateUtil(){ 97 } 98 DateUtil(int d,int m,int y) 99 { 100 this.day = new Day(d,m,y); 101 } 102 103 public Day getDay()//导出日期 104 { 105 return this.day; 106 } 107 public void setDay(Day day)//设置日期 108 { 109 this.day = day; 110 } 111 /// 112 public boolean checkInputValidity()//检查数据合法性 113 { 114 if(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&day.validate()) 115 { 116 return true; 117 } 118 else 119 { 120 return false; 121 } 122 } 123 public boolean compareDates(DateUtil date)//比较两个日期的大小 124 { 125 if (date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue()) 126 { 127 return false; 128 } 129 else if (date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue() 130 &&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue()) 131 { 132 return false; 133 } 134 if (date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue() 135 &&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue() 136 &&date.getDay().getValue()<this.getDay().getValue()) 137 { 138 return false; 139 } 140 else 141 { 142 return true; 143 } 144 } 145 public boolean equalTwoDates(DateUtil date)//判定两个日期是否相等 146 { 147 if (this.getDay().getValue()==date.getDay().getValue() 148 && this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue() 149 && this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()) 150 { 151 return true; 152 } 153 else 154 { 155 return false; 156 } 157 } 158 /// 159 public String showDate()//日期值格式化 160 { 161 String s=this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue(); 162 return s; 163 } 164 /// 165 public DateUtil getNextNDays(int n)//求下n天 166 { 167 int arr[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 168 int year = this.getDay().getMonth().getYear().getValue(); 169 int month = this.getDay().getMonth().getValue(); 170 int day = this.getDay().getValue(); 171 172 for(int i=0;i<n;i++) 173 { 174 175 if(day==arr[month-1])//如果天数到达本月最后一天 176 { 177 if(month==12)//如果月份到达最后一月 178 { 179 year++; 180 month=1; 181 day=1; 182 } 183 else//若不是最后一月 184 { 185 month++; 186 day=1; 187 } 188 } 189 else//如果不是一个月的最后一天 190 { 191 day++; 192 } 193 if((year%4==0&&year%100!=0)||year%400==0) 194 { 195 arr[1]=29; 196 } 197 else 198 { 199 arr[1]=28; 200 } 201 } 202 return new DateUtil(year, month, day); 203 } 204 public DateUtil getPreviousNDays(int n)//求前n天 205 { 206 int arr[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 207 int year = this.getDay().getMonth().getYear().getValue(); 208 int month = this.getDay().getMonth().getValue(); 209 int day = this.getDay().getValue(); 210 for(int i=0;i<n;i++) 211 { 212 if(day==1) 213 { 214 if(month==1)//如果月份到达一月 215 { 216 year--; 217 month=12; 218 day=31; 219 } 220 else////若不是一月 221 { 222 month--; 223 day=arr[month-1]; 224 } 225 } 226 else 227 { 228 day--; 229 } 230 if((year%4==0&&year%100!=0)||year%400==0) 231 { 232 arr[1]=29; 233 } 234 else 235 { 236 arr[1]=28; 237 } 238 } 239 return new DateUtil(year, month, day); 240 } 241 /// 242 public int getDaysofDates(DateUtil date)//求两个日期之间的天数 243 { 244 DateUtil pred = this; 245 DateUtil nextd = date; 246 if (this.equalTwoDates(date)) { 247 return 0; 248 } 249 else if (!this.compareDates(date)) { 250 pred = date; 251 nextd = this; 252 } 253 int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 254 int i,j,d = 0; 255 for(i=pred.getDay().getMonth().getYear().getValue()+1;i<nextd.getDay().getMonth().getYear().getValue();i++) { 256 d=d+365; 257 if(new Year(i).isLeapYear()) 258 d++; 259 } 260 if (pred.getDay().getMonth().getYear().getValue()!=nextd.getDay().getMonth().getYear().getValue()) { 261 for(j=pred.getDay().getMonth().getValue()+1;j<=12;j++) 262 d=d+arr[j]; 263 d+=arr[pred.getDay().getMonth().getValue()]-pred.getDay().getValue(); 264 for(j=1;j<nextd.getDay().getMonth().getValue();j++) 265 d+=arr[j]; 266 d+=nextd.getDay().getValue(); 267 if(pred.getDay().getMonth().getYear().isLeapYear()&&pred.getDay().getMonth().getValue()<=2) 268 d++; 269 if (nextd.getDay().getMonth().getYear().isLeapYear()&&nextd.getDay().getMonth().getValue()>2) { 270 d++; 271 } 272 } 273 else if(pred.getDay().getMonth().getYear().getValue()==nextd.getDay().getMonth().getYear().getValue()&&pred.getDay().getMonth().getValue()!=nextd.getDay().getMonth().getValue()){ 274 for(j=pred.getDay().getMonth().getValue()+1;j<=nextd.getDay().getMonth().getValue()-1;j++) 275 d+=arr[j]; 276 d+=arr[pred.getDay().getMonth().getValue()]-pred.getDay().getValue(); 277 d+=nextd.getDay().getValue(); 278 if(pred.getDay().getMonth().getYear().isLeapYear()&&pred.getDay().getMonth().getValue()<=2) 279 d++; 280 } 281 else if(pred.getDay().getMonth().getYear().getValue()==nextd.getDay().getMonth().getYear().getValue()&&pred.getDay().getMonth().getValue()==nextd.getDay().getMonth().getValue()){ 282 d=nextd.getDay().getValue()-pred.getDay().getValue(); 283 } 284 return d; 285 } 286 } 287 288 class Day{ 289 int value; 290 Month month; 291 int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31}; 292 Day(){ 293 } 294 Day(int yearValue,int monthValue,int dayValue) 295 { 296 this.month = new Month(yearValue,monthValue,dayValue); 297 this.value = dayValue; 298 } 299 /// 300 public int getValue() 301 { 302 return value; 303 } 304 public void setValue(int value) 305 { 306 this.value = value; 307 } 308 public Month getMonth() 309 { 310 return month; 311 } 312 public void setMonth(Month value) 313 { 314 this.month = value; 315 } 316 /// 317 public void resetMin()//日期复位 318 { 319 value = 1; 320 } 321 public void resetMax()//日期设为月最大值 322 { 323 value = mon_maxnum[month.getValue()-1]; 324 } 325 /// 326 public boolean validate()//检验日期合法性 327 { 328 if(this.getMonth().getYear().isLeapYear()) 329 mon_maxnum[1]++; 330 if(1<=value&&mon_maxnum[month.getValue()-1]>=value) 331 return true; 332 return false; 333 } 334 /// 335 public void dayIncrement()//日期增一 336 { 337 value++; 338 } 339 public void dayReduction()//日期减一 340 { 341 value--; 342 } 343 } 344 345 class Month{ 346 int value; 347 Year year; 348 Month(){ 349 } 350 Month(int yearValue,int monthValue,int dayValue) 351 { 352 this.value = monthValue; 353 this.year =new Year(yearValue); 354 } 355 public int getValue() 356 { 357 return this.value; 358 } 359 public void setValue(int value) 360 { 361 this.value = value; 362 } 363 /// 364 public Year getYear() 365 { 366 return year; 367 } 368 public void setYear(Year year) 369 { 370 this.year = year; 371 } 372 /// 373 public void resetMin()//月份复位 374 { 375 this.value = 1; 376 } 377 public void resetMax()//月份设置为12 378 { 379 this.value = 12; 380 } 381 /// 382 public boolean validate()//校验数据合法性 383 { 384 if(value<1||value>12) 385 { 386 return false; 387 } 388 else 389 { 390 return true; 391 } 392 } 393 /// 394 public void monthIncrement()//月份增一 395 { 396 this.value++; 397 } 398 public void monthReduction()//月份减一 399 { 400 this.value--; 401 } 402 } 403 404 class Year{ 405 int value; 406 Year(){ 407 } 408 Year(int value) 409 { 410 this.value = value; 411 } 412 /// 413 public int getValue() 414 { 415 return value; 416 } 417 public void setValue(int value) 418 { 419 this.value = value; 420 } 421 /// 422 public boolean isLeapYear()//判断是否为闰年 423 { 424 if((value%4==0&&value%100!=0)||value%400==0) 425 { 426 return true; 427 } 428 else 429 { 430 return false; 431 } 432 } 433 public boolean validate()//判断数据合法性 434 { 435 if(this.value>2020||this.value<1820) 436 { 437 return false; 438 } 439 else 440 { 441 return true; 442 } 443 } 444 /// 445 public void yearIncrement()//年份增一 446 { 447 this.value++; 448 } 449 public void yearReduction()//年份减一 450 { 451 this.value--; 452 } 453 }
题目集4(7-2)与题目集5(7-5)比较:
题目集4 7-2的图如下:
题目集5 7-5的图如下:
由图分析可知,题目集5中的7-5题的代码复杂度比题目集4中的7-2要高。因此,从代码的复写度来说题目集4中的7-2在实际生活中比题目集5中的7-5题更加方便进行修改以及使用。
所以,本人更加倾向于使用题目集4中7-2的代码。
(2)题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)
由图分析:
题目集4(7-3)
题目集6(7-5)
题目集6(7-6)
据图分析可知:
①题目集4(7-3)中思路主要是:首先声明一个shape类,里面放一个getArea的方法。然后再写继承了父类Shape的子类Circle类和Rectangle类,并且在这两个类中写按照题目要求
来的构造方法和一些方法,之后再写一个继承Rectangle类的子类Box和一个继承Circle类的子类Ball类,并且同样按照题目需求来书写相应的构造方法和一些方法。
②题目集6(7-5)中,我参考了题目集4(7-3)中的一些思路,通过更改了一些子类,删除了不需要的一些类以及按照题目上所给的需求修改了一下主方法的逻辑顺序和试着增添了一些
新学习的对象数组等知识在代码内。
③题目集6(7-6)中,同样是参考了题目集4(7-4)中的一些思路,对主方法的逻辑进行了一些修改,将输出格式更改后加入了几个接口即可完成题目的需求。
(3)对三次题目集中用到的正则表达式技术的分析总结
正则表达式的主要使用对象是文本,可以使用正则表达式指定想要匹配的字符串规则,然后来进行匹配、查找、替换那些符合指定规则的文本。
总体上来说,正则表达式可以对指定的文本实现一下功能:①匹配验证 ②查找和替换
另外,我在写代码去学习正则表达式的时候偶然间看到了别人在博客上总结的关于正则表达式中常使用的情况,个人觉得还是挺方便使用的,如下所示:
匹配内容 | 正则表达式 |
---|---|
QQ号码 | [1-9]\d{4,} |
中国大陆固定电话号码 | (\d{3,4}-)?\d{7,8} |
中国大陆手机号码 | 1\d{10} |
中国大陆邮政编码 | \d{6} |
汉字 | [\u4e00-\u9fa5] |
中文及全角标点符号 | [\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee] |
不含abc的单词 | (?=\w+)(?!abc) |
正整数 | [1-9]+ |
负整数 | -[1-9]+ |
非负整数(正整数+0) | [1-9]+ |
非正整数(负整数+0) | -[1-9]+ |
整数+0 | -?[1-9]+ |
正浮点数 | \d+.\d+ |
负浮点数 | -\d+.\d+ |
浮点数 | -?\d+.\d+ |
再次说明,实际情况中需要我们根据具体情况再这些正则表达式的首部和尾部加上相应的边界符,如:^, $, \A, \Z, \b, \B等
3、踩坑心得
在完成这三次题目集的时候,还是跟上次一样碰到了许许多多的问题,如下所示:
①自己在学习继承的时候没有注意到一个子类只能继承一个父类,因而走了许多弯路。
②我在写代码的时候,忽略了当需要调用父类中的方法时,调用方法就必须要写在第一行上(致使自己又一头雾水地改了大半天代码也没能找出编译器指出的自己的错误的地方所在)
4、改进建议
在写类的继承的题目(题目集6的7-5、7-6)的时候可以自己再优化一下代码,这两题的代码由于部分参考的之前题目集4(7-3)的代码,有一些比较繁琐无用的
代码可以删去。然后在题目集6的7-5中可以简化下代码,如:若有可以使用的Java内置的一些方法,可以将自己写的作用相同的代码替换掉使代码更加简洁方便检
查(如直接使用Java.util.Arrays中内置的数组内元素排序的方法简化代码)
5、总结
①在写与正则表达式有关的题目的时候由于不够熟练,还是需要加强使用正则表达式的熟练度。
②在一个方法中,当需要调用父类中的方法时,调用方法必须要写在第一行上,否则编译器会报错。
③在这几次题目集中,我学习到了正则表达式,类的继承,多态与接口,对象数组等使用方法以及对这些知识的实践。
④建议:希望在出题的时候能多给几个测试样例让我们进行程序的测试与检验。
原文:https://www.cnblogs.com/kbhqq351643289/p/14705717.html