我的第二次Blog
1.前言
2.设计与分析
3.采坑心得
4.改进建议
5.总结
(1)前言
在题目集4~6中,知识点从正则表达式、继承、聚合等java设计越来越深入了解Java的语法,每一次题目集都有一两道题目要特别深入地了解Java语法知识才能完成。每次题目集的题量也不多,刚刚好能够覆盖Java的基础语法知识以及每一个难点,后两次的题目集中难度偏高,但是只要肯钻研Java语法,也是可以完成的。
(2)设计与分析
①题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较
题目集4(7-2)

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

输入格式:
有三种输入方式(以输入的第一个数字划分[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:值
代码如下
1 import java.util.*; 2 public class Main { 3 4 public static void main(String[] args) { 5 Scanner input=new Scanner(System.in); 6 int year,month,day; 7 int n=input.nextInt(); 8 switch(n) { 9 case 1://测试输入日期的下n天 10 int n1; 11 year=input.nextInt(); 12 month=input.nextInt(); 13 day=input.nextInt(); 14 n1=input.nextInt(); 15 DateUtil d3; 16 d3=new DateUtil(day,month,year); 17 if(d3.checkInputValidity()==false) { 18 System.out.print("Wrong Format"); 19 } 20 else { 21 d3.getNextDays(n1); 22 System.out.print(year+"-"+month+"-"+day+" next "+n1+" days is:"+d3.day.month.year.value+"-"+d3.day.month.value+"-"+d3.day.value); 23 } 24 break; 25 case 2://测试输入日期的前n天 26 int n2; 27 year=input.nextInt(); 28 month=input.nextInt(); 29 day=input.nextInt(); 30 n2=input.nextInt(); 31 DateUtil d4; 32 d4=new DateUtil(day,month,year); 33 if(d4.checkInputValidity()==false) { 34 System.out.print("Wrong Format"); 35 } 36 else { 37 d4.getPreviousNDays(n2); 38 System.out.print(year+"-"+month+"-"+day+" previous "+n2+" days is:"+d4.day.month.year.value+"-"+d4.day.month.value+"-"+d4.day.value); 39 } 40 break; 41 case 3://测试两个日期之间相差的天数 42 int year1,month1,day1,year2,month2,day2; 43 year1=input.nextInt(); 44 month1=input.nextInt(); 45 day1=input.nextInt(); 46 year2=input.nextInt(); 47 month2=input.nextInt(); 48 day2=input.nextInt(); 49 DateUtil d1,d2; 50 d1=new DateUtil(day1,month1,year1); 51 d2=new DateUtil(day2,month2,year2); 52 if(d1.checkInputValidity()==false||d2.checkInputValidity()==false) { 53 System.out.print("Wrong Format"); 54 } 55 else { 56 System.out.print("The days between "+year1+"-"+month1+"-"+day1+" and "+year2+"-"+month2+"-"+day2+" are:"+d1.getDaysofDates(d2)); 57 } 58 break; 59 default: 60 String s=input.nextLine(); 61 System.out.print("Wrong Format"); 62 break; 63 } 64 } 65 } 66 67 class Day{ 68 int value; 69 Month month=new Month(); 70 int[] mon_maxnum= {31,28,31,30,31,30,31,31,30,31,30,31}; 71 Day(){ 72 if(month.year.isLeapYear()==true)//闰年 73 { 74 mon_maxnum[1]=29; 75 } 76 } 77 Day(int yearValue,int monthValue,int dayValue){ 78 value=dayValue; 79 month.value=monthValue; 80 month.year.value=yearValue; 81 if(month.year.isLeapYear()==true)//闰年 82 { 83 mon_maxnum[1]=29; 84 } 85 } 86 int getValue(){ 87 return value; 88 } 89 void setValue(int value) 90 { 91 this.value=value; 92 } 93 Month getMonth() { 94 return month; 95 } 96 void setMonth(Month value) { 97 month=value; 98 } 99 void resetMin() { 100 value=1; 101 } 102 void resetMax() { 103 value=mon_maxnum[month.value-1]; 104 } 105 boolean validate() { 106 if(month.year.isLeapYear()==true)//闰年 107 { 108 mon_maxnum[1]=29; 109 }else 110 { 111 mon_maxnum[1]=28; 112 } 113 if(value<1||value>mon_maxnum[month.value-1]) { 114 return false; 115 } 116 return true; 117 } 118 void dayIncrement() { 119 value++; 120 if(month.year.isLeapYear()==true)//闰年 121 { 122 mon_maxnum[1]=29; 123 }else 124 { 125 mon_maxnum[1]=28; 126 } 127 if(value>mon_maxnum[month.value-1]) { 128 resetMin(); 129 month.monthIncrement(); 130 } 131 } 132 void dayRedduction() { 133 value--; 134 if(month.year.isLeapYear()==true)//闰年 135 { 136 mon_maxnum[1]=29; 137 }else 138 { 139 mon_maxnum[1]=28; 140 } 141 if(value<1) { 142 month.monthReduction(); 143 resetMax(); 144 } 145 } 146 } 147 148 class Month{ 149 int value; 150 Year year=new Year(); 151 Month(){ 152 153 } 154 Month(int yearValue,int monthValue){ 155 value=monthValue; 156 year.value=yearValue; 157 } 158 int getValue() { 159 return value; 160 } 161 void setValue(int value) { 162 this.value=value; 163 } 164 Year getYear() { 165 return year; 166 } 167 void serYear(Year year) { 168 this.year.value=year.value; 169 } 170 void resetMin() { 171 value=1; 172 173 } 174 void resetMax() { 175 value=12; 176 177 } 178 boolean validate() { 179 if(value<1||value>12) { 180 return false; 181 } 182 else { 183 return true; 184 } 185 } 186 void monthIncrement() { 187 value++; 188 if(value>12) { 189 resetMin(); 190 year.yearIncrement(); 191 } 192 } 193 void monthReduction() { 194 value--; 195 if(value<1) { 196 resetMax(); 197 year.yearReauction(); 198 } 199 } 200 201 } 202 203 class DateUtil{ 204 Day day =new Day(); 205 DateUtil(){ 206 207 } 208 DateUtil(int d,int m,int y){ 209 day.value=d; 210 day.month.value=m; 211 day.month.year.value=y; 212 } 213 Day getDay() { 214 return day; 215 } 216 void setDay(Day d) { 217 day=d; 218 } 219 boolean checkInputValidity() {//校验数据的合法性 220 if(day.month.year.validate()==false||day.month.validate()==false||day.validate()==false) { 221 return false; 222 } 223 else { 224 return true; 225 } 226 } 227 boolean compareDates(DateUtil date) {//day>date 返回true 228 if(day.month.year.value>date.day.month.year.value) { 229 return true; 230 } 231 else 232 if(day.month.year.value<date.day.month.year.value) { 233 return false; 234 } 235 else { 236 if(day.month.value>date.day.month.value) { 237 return true; 238 } 239 else 240 if(day.month.value<date.day.month.value) { 241 return false; 242 } 243 else { 244 if(day.value>date.day.value) { 245 return true; 246 } 247 else { 248 return false; 249 } 250 } 251 } 252 } 253 boolean equalTwoDates(DateUtil date) {//判断两个日期是否相等 254 if(day.value==date.day.value&&day.month.value==date.day.month.value&&day.month.year.value==date.day.month.year.value) { 255 return true; 256 } 257 else { 258 return false; 259 } 260 } 261 // String showDate() { 262 // 263 // } 264 DateUtil getNextDays(int n) {//求下n天 265 DateUtil d1=new DateUtil(1,1,day.month.year.value+1); 266 int m=getDaysofDates(d1); 267 //System.out.println(m); 268 if(n-m>0) { 269 n=n-m; 270 int temp=day.month.year.value+1; 271 int flag=365; 272 if (temp % 4 == 0 && temp % 100 != 0 || temp % 400 == 0) { 273 flag= 366; 274 } else { 275 flag= 365; 276 } 277 while(n-flag>0){ 278 n=n-flag; 279 temp++; 280 if (temp % 4 == 0 && temp % 100 != 0 || temp % 400 == 0) { 281 flag= 366; 282 } else { 283 flag= 365; 284 } 285 } 286 day.month.year.value=temp; 287 day.month.value=1; 288 day.value=1; 289 290 for(int i=0;i<n;i++) { 291 day.dayIncrement(); 292 } 293 } 294 return null; 295 } 296 DateUtil getPreviousNDays(int n) {//求前n天 297 // for(int i=0;i<n;i++) { 298 // day.dayRedduction(); 299 // } 300 301 DateUtil d1=new DateUtil(31,12,day.month.year.value-1); 302 int m=getDaysofDates(d1); 303 //System.out.println(m); 304 if(n-m>0) { 305 n=n-m; 306 int temp=day.month.year.value-1; 307 int flag=365; 308 if (temp % 4 == 0 && temp % 100 != 0 || temp % 400 == 0) { 309 flag= 366; 310 } else { 311 flag= 365; 312 } 313 while(n-flag>0){ 314 n=n-flag; 315 temp--; 316 if (temp % 4 == 0 && temp % 100 != 0 || temp % 400 == 0) { 317 flag= 366; 318 } else { 319 flag= 365; 320 } 321 } 322 day.month.year.value=temp; 323 day.month.value=12; 324 day.value=31; 325 326 for(int i=0;i<n;i++) { 327 day.dayRedduction(); 328 } 329 } 330 return null; 331 } 332 333 int getDaysofDates(DateUtil date) {// 求两个日期之差 334 if (equalTwoDates(date) == true) { 335 return 0; 336 } 337 int sum = 0; 338 int[] mon_maxnum = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 339 if (compareDates(date) == true) {//day>date 340 int temp = date.day.month.year.value; 341 for (temp = temp + 1;temp<day.month.year.value; temp++) { 342 if (temp % 4 == 0 && temp % 100 != 0 || temp % 400 == 0) { 343 sum = sum + 366; 344 } else { 345 sum = sum + 365; 346 } 347 } 348 if(date.day.month.year.value==day.month.year.value) { 349 if(date.day.month.value==day.month.value) { 350 return day.value-date.day.value; 351 } 352 if (date.day.month.year.value % 4 == 0 && date.day.month.year.value % 100 != 0 353 || date.day.month.year.value % 400 == 0) { 354 mon_maxnum[1] = 29; 355 } 356 else { 357 mon_maxnum[1] = 28; 358 } 359 360 for(int i=date.day.month.value;i<day.month.value-1;i++) { 361 sum=sum+mon_maxnum[i]; 362 } 363 sum=sum+mon_maxnum[date.day.month.value-1]-date.day.value+day.value; 364 return sum; 365 } 366 if (day.month.year.value % 4 == 0 && day.month.year.value % 100 != 0 || day.month.year.value % 400 == 0) { 367 mon_maxnum[1] = 29; 368 } 369 for (int i = 0; i < day.month.value-1; i++) { 370 sum = sum + mon_maxnum[i]; 371 } 372 sum = sum + day.value; 373 if (date.day.month.year.value % 4 == 0 && date.day.month.year.value % 100 != 0 374 || date.day.month.year.value % 400 == 0) { 375 mon_maxnum[1] = 29; 376 } 377 else { 378 mon_maxnum[1] = 28; 379 } 380 for (int i = date.day.month.value ; i < 12; i++) { 381 sum = sum + mon_maxnum[i]; 382 } 383 sum = sum + mon_maxnum[date.day.month.value-1] - date.day.value; 384 return sum; 385 } else {//day<date 386 int temp = day.month.year.value; 387 for (temp = temp + 1;temp<date.day.month.year.value; temp++) { 388 if (temp % 4 == 0 && temp % 100 != 0 || temp % 400 == 0) { 389 sum = sum + 366; 390 } else { 391 sum = sum + 365; 392 } 393 } 394 395 if(day.month.year.value==date.day.month.year.value) { 396 if(day.month.value==date.day.month.value) { 397 return date.day.value-day.value; 398 } 399 if (day.month.year.value % 4 == 0 && day.month.year.value % 100 != 0 400 || day.month.year.value % 400 == 0) { 401 mon_maxnum[1] = 29; 402 } 403 else { 404 mon_maxnum[1] = 28; 405 } 406 407 for(int i=day.month.value;i<date.day.month.value-1;i++) { 408 sum=sum+mon_maxnum[i]; 409 } 410 sum=sum+mon_maxnum[day.month.value-1]-day.value+date.day.value; 411 return sum; 412 } 413 if (date.day.month.year.value % 4 == 0 && date.day.month.year.value % 100 != 0 || date.day.month.year.value % 400 == 0) { 414 mon_maxnum[1] = 29; 415 } 416 else { 417 mon_maxnum[1] = 28; 418 } 419 for (int i = 0; i < date.day.month.value-1; i++) { 420 sum = sum + mon_maxnum[i]; 421 } 422 sum = sum + date.day.value; 423 if (day.month.year.value % 4 == 0 && day.month.year.value % 100 != 0 424 || day.month.year.value % 400 == 0) { 425 mon_maxnum[1] = 29; 426 } 427 else { 428 mon_maxnum[1] = 28; 429 } 430 for (int i=day.month.value; i <12; i++) { 431 sum = sum + mon_maxnum[i]; 432 } 433 sum = sum + mon_maxnum[day.month.value-1] - day.value; 434 return sum; 435 } 436 } 437 } 438 class Year{ 439 int value; 440 Year(){ 441 442 } 443 Year(int value){ 444 this.value=value; 445 } 446 int getValue() 447 { 448 return value; 449 } 450 void setValue(int value) { 451 this.value=value; 452 } 453 boolean isLeapYear() { 454 if(value % 4 == 0 && value % 100 != 0 || value % 400 == 0){ 455 return true; 456 }else{ 457 return false; 458 } 459 } 460 boolean validate() { 461 if(value<1820||value>2020) { 462 return false; 463 } 464 else { 465 return true; 466 } 467 } 468 void yearIncrement() { 469 value++; 470 } 471 void yearReauction() { 472 value--; 473 } 474 }
第一个函数圈复杂度图如下

第二个圈复杂度如下

通过比较,两个题目的圈复杂度相差不大,但是明显第二个的算两个日期之差的方法更优,在第二个题目中,提交第一个题目的源码会被报算两个日期之差超时,第二个则是优化了算两个日期之差的算法,减少运行时间,以求正确答案。
②题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)
编写程序,实现图形类的继承,并定义相应类对象并进行测试。
- 类Shape,无属性,有一个返回0.0的求图形面积的公有方法
public double getArea();//求图形面积 - 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
- 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
- 类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法
public double getVolume();//求球体积 - 类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法
public double getVolume();//求立方体体积 - 注意:
- 每个类均有构造方法,且构造方法内必须输出如下内容:
Constructing 类名 - 每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
- 输出的数值均保留两位小数
主方法内,主要实现四个功能(1-4): 从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积; 从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积; 从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积; 从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;
假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出Wrong Format
输入格式:
共四种合法输入
- 1 圆半径
- 2 矩形宽、长
- 3 球半径
- 4 立方体宽、长、高
输出格式:
按照以上需求提示依次输出
代码如下
1 import java.util.*; 2 import java.text.DecimalFormat; 3 public class Main { 4 public static void main(String[] args) { 5 Scanner input=new Scanner(System.in); 6 DecimalFormat df = new DecimalFormat("0.00"); 7 int num=input.nextInt(); 8 switch(num) { 9 case 1: 10 double radius=input.nextDouble(); 11 Circle a=new Circle(); 12 a.setRadius(radius); 13 System.out.print("Circle's area:"+df.format(a.getArea())); 14 break; 15 case 2: 16 double width=input.nextDouble(); 17 double length=input.nextDouble(); 18 Rectangle b=new Rectangle(); 19 b.setWidth(width); 20 b.setLength(length); 21 System.out.print("Rectangle's area:"+df.format(b.getArea())); 22 break; 23 case 3: 24 double radius1=input.nextDouble(); 25 Ball c=new Ball(); 26 c.setRadius(radius1); 27 System.out.println("Ball's surface area:"+df.format(c.getArea())); 28 System.out.println("Ball's volume:"+df.format(c.getVolume())); 29 break; 30 case 4: 31 double width1=input.nextDouble(); 32 double length1=input.nextDouble(); 33 double height=input.nextDouble(); 34 Box d=new Box(); 35 d.setHeight(height); 36 d.setLength(length1); 37 d.setWidth(width1); 38 System.out.println("Box's surface area:"+df.format(d.getArea())); 39 System.out.println("Box's volume:"+df.format(d.getVolum())); 40 break; 41 default: 42 System.out.print("Wrong Format"); 43 } 44 } 45 } 46 47 class Shape { 48 Shape(){ 49 50 } 51 public double getArea() { 52 return 1; 53 } 54 public void show() { 55 System.out.println("Constructing Shape"); 56 } 57 } 58 59 class Circle extends Shape { 60 private double radius; 61 double pai=3.141593; 62 Circle(){ 63 64 } 65 public double getRadius() { 66 return radius; 67 } 68 public void setRadius(double radius) { 69 if(validate(radius)==false) { 70 System.out.print("Wrong Format"); 71 System.exit(0); 72 } 73 this.radius = radius; 74 } 75 public double getArea() { 76 show(); 77 return radius*radius*pai; 78 } 79 public boolean validate(double n) { 80 if(n<=0) { 81 return false; 82 } 83 else 84 return true; 85 } 86 public void show() { 87 super.show(); 88 System.out.println("Constructing Circle"); 89 } 90 } 91 92 class Rectangle extends Shape { 93 private double width,length; 94 95 public double getWidth() { 96 return width; 97 } 98 99 public void setWidth(double width) { 100 if(validate(width)==false) { 101 System.out.print("Wrong Format"); 102 System.exit(0); 103 } 104 this.width = width; 105 } 106 107 public double getLength() { 108 return length; 109 } 110 111 public void setLength(double length) { 112 if(validate(length)==false) { 113 System.out.print("Wrong Format"); 114 System.exit(0); 115 } 116 this.length = length; 117 } 118 public double getArea() { 119 show(); 120 return width*length; 121 } 122 public boolean validate(double n) { 123 if(n<=0) { 124 return false; 125 } 126 else 127 return true; 128 } 129 public void show() { 130 super.show(); 131 System.out.println("Constructing Rectangle"); 132 } 133 } 134 135 class Ball extends Circle { 136 double pai=3.141593; 137 Ball(){ 138 139 } 140 public double getRadius() { 141 return super.getRadius(); 142 } 143 public void setRadius(double radius) { 144 if(validate(radius)==false) { 145 System.out.print("Wrong Format"); 146 System.exit(0); 147 } 148 super.setRadius(radius); 149 } 150 public double getArea() { 151 show(); 152 return 4*Math.PI*super.getRadius()*super.getRadius(); 153 } 154 public double getVolume() { 155 return 4*Math.PI*super.getRadius()*super.getRadius()*super.getRadius()/3; 156 } 157 public void show() { 158 super.show(); 159 System.out.println("Constructing Ball"); 160 } 161 } 162 163 class Box extends Rectangle{ 164 private double height; 165 public double getHeight() { 166 return height; 167 } 168 public void setHeight(double height) { 169 if(validate(height)==false) { 170 System.out.print("Wrong Format"); 171 System.exit(0); 172 } 173 this.height = height; 174 } 175 public double getArea() { 176 show(); 177 return super.getLength()*super.getWidth()*2+super.getLength()*height*2+super.getWidth()*height*2; 178 } 179 public double getVolum() { 180 return super.getLength()*super.getWidth()*height; 181 } 182 public boolean validate(double n) { 183 if(n<=0) { 184 return false; 185 } 186 else 187 return true; 188 } 189 public void show() { 190 super.show(); 191 System.out.println("Constructing Box"); 192 } 193 }
题目集6(7-5)
掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。
从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。
输出格式:
- 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出
Wrong Format。 - 如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):
- 各个图形的面积;
- 所有图形的面积总和;
- 排序后的各个图形面积;
- 再次所有图形的面积总和。
代码如下
1 import java.util.*; 2 import java.text.DecimalFormat; 3 public class Main { 4 public static void main(String[] args) { 5 Scanner input=new Scanner(System.in); 6 ArrayList<Double> sum1 = new ArrayList<Double>(); 7 ArrayList<Double> sum2 = new ArrayList<Double>(); 8 ArrayList<Double> sum3 = new ArrayList<Double>(); 9 int a=input.nextInt(); 10 int b=input.nextInt(); 11 int c=input.nextInt(); 12 if(a<0||b<0||c<0) { 13 System.out.print("Wrong Format"); 14 System.exit(0); 15 } 16 int i=0; 17 for(i=0;i<a;i++) { 18 Circle cir=new Circle(); 19 sum1.add(0.0); 20 double radius=input.nextDouble(); 21 cir.setRadius(radius); 22 sum1.set(i,cir.getArea()); 23 } 24 for(i=0;i<b;i++) { 25 Rectangle rec=new Rectangle(); 26 double width=input.nextDouble(); 27 double length=input.nextDouble(); 28 rec.setWidth(width); 29 rec.setLength(length); 30 sum2.add(0.0); 31 sum2.set(i, rec.getArea()); 32 } 33 for(i=0;i<c;i++) { 34 Triangle tri=new Triangle(); 35 double a1=input.nextDouble(); 36 double b1=input.nextDouble(); 37 double c1=input.nextDouble(); 38 tri.setRadius(a1, b1, c1); 39 sum3.add(0.0); 40 sum3.set(i, tri.getArea()); 41 } 42 System.out.println("Original area:"); 43 int k=0; 44 double temp=0; 45 double[] num=new double[a+b+c]; 46 for(i=0;i<a;i++) { 47 System.out.print(String.format("%.2f", sum1.get(i))+" "); 48 num[k++]=sum1.get(i); 49 } 50 51 for(i=0;i<b;i++) { 52 System.out.print(String.format("%.2f", sum2.get(i))+" "); 53 num[k++]=sum2.get(i); 54 } 55 56 for(i=0;i<c;i++) { 57 System.out.print(String.format("%.2f", sum3.get(i))+" "); 58 num[k++]=sum3.get(i); 59 } 60 for(i=0;i<a+b+c;i++) { 61 temp=temp+num[i]; 62 } 63 System.out.println(); 64 System.out.println("Sum of area:"+String.format("%.2f", temp)); 65 int max=0; 66 double temp1=0; 67 for(i=0;i<a+b+c;i++) { 68 max=i; 69 for(int j=i;j<a+b+c;j++) { 70 if(num[j]<num[max]) { 71 max=j; 72 } 73 } 74 temp1=num[i]; 75 num[i]=num[max]; 76 num[max]=temp1; 77 } 78 System.out.println("Sorted area:"); 79 for(i=0;i<a+b+c;i++) { 80 System.out.print(String.format("%.2f", num[i])+" "); 81 } 82 System.out.println(); 83 System.out.println("Sum of area:"+String.format("%.2f", temp)); 84 } 85 } 86 abstract class Shape { 87 abstract double getArea(); 88 abstract boolean validate(double n) ; 89 90 } 91 class Circle extends Shape { 92 private double radius; 93 Circle(){ 94 } 95 public double getRadius() { 96 return radius; 97 } 98 public void setRadius(double radius) { 99 if(validate(radius)==false) { 100 System.out.print("Wrong Format"); 101 System.exit(0); 102 } 103 this.radius = radius; 104 } 105 public double getArea() { 106 return radius*radius*Math.PI; 107 } 108 boolean validate(double n) { 109 if(n<=0) { 110 return false; 111 } 112 else 113 return true; 114 } 115 } 116 class Rectangle extends Shape { 117 private double width,length; 118 Rectangle(){ 119 } 120 public double getWidth() { 121 return width; 122 } 123 124 public void setWidth(double width) { 125 if(validate(width)==false) { 126 System.out.print("Wrong Format"); 127 System.exit(0); 128 } 129 this.width = width; 130 } 131 132 public double getLength() { 133 return length; 134 } 135 136 public void setLength(double length) { 137 if(validate(length)==false) { 138 System.out.print("Wrong Format"); 139 System.exit(0); 140 } 141 this.length = length; 142 } 143 public double getArea() { 144 return width*length; 145 } 146 boolean validate(double n) { 147 if(n<=0) { 148 return false; 149 } 150 else 151 return true; 152 } 153 } 154 class Triangle extends Shape { 155 double a,b,c; 156 Triangle(){ 157 } 158 public void setRadius(double a,double b,double c) { 159 if(validate(a)==false||validate(b)==false||validate(c)==false||a-b>=c||a-c>=b||b-c>=a||a+b<=c||b+c<=a||a+c<=b) { 160 System.out.print("Wrong Format"); 161 System.exit(0); 162 } 163 this.a=a; 164 this.b=b; 165 this.c=c; 166 } 167 public double getArea() { 168 double p=(a+b+c)/2; 169 return Math.pow(p*(p-a)*(p-b)*(p-c), 0.5); 170 } 171 boolean validate(double n) { 172 if(n<=0) { 173 return false; 174 } 175 else 176 return true; 177 } 178 }
题目集6(7-6)

其中:
- GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;
- Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口
- 要求:在Main类的主方法中分别定义一个圆类对象及矩形类对象(其属性值由键盘输入),使用接口的引用分别调用圆类对象及矩形类对象的求面积的方法,直接输出两个图形的面积值。(要求只保留两位小数)
输入格式:
从键盘分别输入圆的半径值及矩形的宽、长的值,用空格分开。
输出格式:
- 如果输入的圆的半径值及矩形的宽、长的值非法(≤0),则输出
Wrong Format - 如果输入合法,则分别输出圆的面积和矩形的面积值(各占一行),保留两位小数。
代码如下
import java.util.*; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); double a=input.nextDouble(); double b=input.nextDouble(); double c=input.nextDouble(); Circle cir=new Circle(); cir.setRadius(a); Rectangle ret=new Rectangle(); ret.setWidth(b); ret.setLength(c); System.out.println(String.format("%.2f", cir.getArea())); System.out.println(String.format("%.2f", ret.getArea())); } } class Circle extends Shape { private double radius; Circle(){ } public double getRadius() { return radius; } public void setRadius(double radius) { if(validate(radius)==false) { System.out.print("Wrong Format"); System.exit(0); } this.radius = radius; } public double getArea() { return radius*radius*Math.PI; } boolean validate(double n) { if(n<=0) { return false; } else return true; } } class Rectangle extends Shape { private double width,length; Rectangle(){ } public double getWidth() { return width; } public void setWidth(double width) { if(validate(width)==false) { System.out.print("Wrong Format"); System.exit(0); } this.width = width; } public double getLength() { return length; } public void setLength(double length) { if(validate(length)==false) { System.out.print("Wrong Format"); System.exit(0); } this.length = length; } public double getArea() { return width*length; } boolean validate(double n) { if(n<=0) { return false; } else return true; } } abstract class Shape { abstract double getArea(); abstract boolean validate(double n) ; }
分析:
在这三次题目集中,
③对三次题目集中用到的正则表达式技术的分析总结
在题目集4中,水文检验使用到了正则表达式,使用到了matches,在题目集5统计Java程序中关键词的出现次数中,使用到了
import java.util.regex.Matcher;
import java.util.regex.Pattern;
在题目集6正则表达式训练-学号校验中,使用到了最简单的matches
对于正则表达式的运用java中通过Pattern和Match进行对输入的字符进行匹配,可以通过Pattern.complie(String regex)简单工厂方法创建一个正则表达式, 这就使我们使用相关的正则表达式变得方便了起来,对于相关的Pattern的三种Matcher类提供三个匹配操作方法,三个方法均返回boolean类型,当匹配到时返回true,没匹配到则返回false,还有find()对字符串进行匹配,匹配到的字符串可以在任何位置,当使用matches(),lookingAt(),find()执行匹配操作后,就可以利用以上三个方法得到更详细的信息。
对于其他对字符串的匹配,有如下几条:
a. "^\d+$"//非负整数(正整数 + 0)
b. "^[0-9]*[1-9][0-9]*$"//正整数
c. "^((-\d+)|(0+))$"//非正整数(负整数 + 0)
d. "^-[0-9]*[1-9][0-9]*$"//负整数
e. "^-?\d+$"//整数
④题目集5(7-4)中Java集合框架应用的分析总结
Java集合框架为程序员提供了预先包装的数据结构和算法来操纵他们。集合是一个对象,可容纳其他对象的引用。集合接口声明对每一种类型的集合可以执行的操作。集合框架的类和接口均在java.util包中。
任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换。ArrayList是一个动态数组,也是我们最常用的集合。它允许任何符合规则的元素插入甚至包括null。每一个ArrayList都有一个初始容量(10),该容量代表了数组的大小。随着容器中的元素不断增加,容器的大小也会随着增加。在每次向容器中增加元素的同时都会进行容量检查,当快溢出时,就会进行扩容操作。
(3)采坑心得
(1)在本次实验中,还是有许许多多的坑的,例如在题目集4(7-2)和5(7-4)中,在计算每一年的日期的时候要考虑当年是不是闰年,如果是闰年二月份有29天,而不是闰年只有28天,在计算日期之差的时候更要注意这个问题,多一天少一天答案都不对
(2)在正则表达式中,要注意空格和特殊符号的表示形式,例如 n匹配字符 n。\n 匹配换行符。序列 \\\\ 匹配 \\ ,\\( 匹配 (,要注意每种正则表达式的写法
(3)还有图形的继承和多态的使用也要符合规则。
(4)改进建议
在本次实验中,由于题目集4的7-1水文检测难度有点打,导致只拿了部分测试点的分数,没有做全对,希望自己下次可以更加认真和仔细的而去完成题目。
(5)总结
总的来说,通过这几次题目集,对于Java继承和正则表达式有了更加深刻的理解,学到了如何通过修改代码之间的关系减少圈复杂度,但是正则表达式还是缺乏一点理解,才导致那一道题目没有完全对,在以后的Java学习中,我会更加认真和仔细的对待每一次Java作业,以及在每一次Java课后都要做课堂总结和复习课堂所讲的内容,实验出真知,多写写代码提高自己的水平。

浙公网安备 33010602011771号