Pta题目集4~6
作者:互联网
一,各题目集分析
1.题目集4(7-2)、题目集5(7-4)两种日期类聚合设计的优劣比较
(1)对比两题目集
题目集4(7-2):
题目:设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] ,
应用程序共测试三个功能:
- 求下n天
- 求前n天
- 求两个日期相差的天数
注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)
类图:
输入样例
3 2014 2 14 2020 6 14
输出样例:
2312
题目集5(7-5):
题目:同题目集4(7-2)
类图:
输入样例:
3 2014 2 14 2020 6 14
输出样例:
The days between 2014-2-14 and 2020-6-14 are:2312
题目集4(7-2)我所输入的代码:
1 //package demo; 2 3 import java.util.Scanner; 4 5 public class Main { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Date b = new Date(); 10 Scanner a = new Scanner(System.in); 11 int n = a.nextInt(); 12 if(n==1) { 13 b.setyear1(a.nextInt()); 14 b.setmonth1(a.nextInt()); 15 b.setday1(a.nextInt()); 16 b.getnextDate(a.nextInt()); 17 } 18 else if(n==2) { 19 b.setyear1(a.nextInt()); 20 b.setmonth1(a.nextInt()); 21 b.setday1(a.nextInt()); 22 b.getagoDate(a.nextInt()); 23 } 24 else if(n==3) { 25 b.setyear1(a.nextInt()); 26 b.setmonth1(a.nextInt()); 27 b.setday1(a.nextInt()); 28 b.setyear2(a.nextInt()); 29 b.setmonth2(a.nextInt()); 30 b.setday2(a.nextInt()); 31 b.getdayDate(); 32 } 33 34 } 35 36 } 37 38 class Date{ 39 private int year1 = 0; 40 private int month1 = 0; 41 private int day1 = 0; 42 private int year2 = 0; 43 private int month2 = 0; 44 private int day2 = 0; 45 46 public Date(){ 47 } 48 public Date(int year1,int month1,int day1,int year2,int month2,int day2) { 49 this.year1=year1; 50 this.month1=month1; 51 this.day1=day1; 52 this.year2=year2; 53 this.month2=month2; 54 this.day2=day2; 55 } 56 public int getyear1() { 57 return year1; 58 } 59 public void setyear1(int year) { 60 this.year1 = year; 61 } 62 public int getmonth1() { 63 return month1; 64 } 65 public void setmonth1(int month) { 66 this.month1 = month; 67 } 68 public int getday1() { 69 return day1; 70 } 71 public void setday1(int day) { 72 this.day1=day; 73 } 74 public int getyear2() { 75 return year2; 76 } 77 public void setyear2(int year) { 78 this.year2 = year; 79 } 80 public int getmonth2() { 81 return month2; 82 } 83 public void setmonth2(int month) { 84 this.month2 = month; 85 } 86 public int getday2() { 87 return day2; 88 } 89 public void setday2(int day) { 90 this.day2=day; 91 } 92 public boolean isleapYear1(int year) { 93 boolean a = false; 94 if(this.year1>=1900&&this.year1<=2050) { 95 a=true; 96 return a; 97 } 98 else 99 return a; 100 } 101 public boolean checkInputValidity1() { 102 boolean a = true; 103 if(this.month1<1||this.month1>12) 104 a = false; 105 if(day1<1) 106 a=false; 107 if((this.year1%4==0&&this.year1%100!=0)||this.year1%400==0) { 108 if(this.month1==1||this.month1==3||this.month1==5||this.month1==7||this.month1==8||this.month1==10||this.month1==12) { 109 if(this.day1>31) 110 a=false; 111 } 112 else 113 if(this.month1==4||this.month1==6||this.month1==9||this.month1==11) { 114 if(this.day1>30) { 115 a=false; 116 } 117 } 118 else 119 if(this.month1==2) { 120 if(this.day1>29) { 121 a=false; 122 } 123 } 124 } 125 else { 126 if(this.month1==1||this.month1==3||this.month1==5||this.month1==7||this.month1==8||this.month1==10||this.month1==12) { 127 if(this.day1>31) { 128 a=false; 129 } 130 } 131 else 132 if(this.month1==4||this.month1==6||this.month1==9||this.month1==11) { 133 if(this.day1>30) { 134 a=false; 135 } 136 } 137 else 138 if(this.month1==2) { 139 if(this.day1>28) { 140 a=false; 141 } 142 } 143 } 144 return a; 145 } 146 147 148 public boolean isleapYear2(int year) { 149 boolean a = false; 150 if(this.year2>=1900&&this.year2<=2050) { 151 a=true; 152 return a; 153 } 154 else 155 return a; 156 } 157 public boolean checkInputValidity2() { 158 boolean a = true; 159 if(this.month2<1||this.month2>12) 160 a = false; 161 if(day2<1) 162 a=false; 163 if((this.year2%4==0&&this.year2%100!=0)||this.year2%400==0) { 164 if(this.month2==1||this.month2==3||this.month2==5||this.month2==7||this.month2==8||this.month2==10||this.month2==12) { 165 if(this.day2>31) 166 a=false; 167 } 168 else 169 if(this.month2==4||this.month2==6||this.month2==9||this.month2==11) { 170 if(this.day2>30) { 171 a=false; 172 } 173 } 174 else 175 if(this.month2==2) { 176 if(this.day2>29) { 177 a=false; 178 } 179 } 180 } 181 else { 182 if(this.month2==1||this.month2==3||this.month2==5||this.month2==7||this.month2==8||this.month2==10||this.month2==12) { 183 if(this.day2>31) { 184 a=false; 185 } 186 } 187 else 188 if(this.month2==4||this.month2==6||this.month2==9||this.month2==11) { 189 if(this.day2>30) { 190 a=false; 191 } 192 } 193 else 194 if(this.month2==2) { 195 if(this.day2>28) { 196 a=false; 197 } 198 } 199 } 200 return a; 201 } 202 203 204 public void getnextDate(int n){ 205 boolean a,b; 206 a= isleapYear1(this.year1); 207 b= checkInputValidity1(); 208 if(a!=false&&b!=false) { 209 this.day1=this.day1+n; 210 do { 211 if((this.year1%4==0&&this.year1%100!=0)||this.year1%400==0) { 212 if(this.month1==1||this.month1==3||this.month1==5||this.month1==7||this.month1==8||this.month1==10||this.month1==12) { 213 if(this.day1>31) { 214 if(this.month1!=12) { 215 this.month1=this.month1+1; 216 } 217 else{ 218 this.year1=this.year1+1; 219 this.month1=1; 220 } 221 this.day1=this.day1-31; 222 } 223 if(this.day1<=31) 224 break; 225 } 226 else 227 if(this.month1==4||this.month1==6||this.month1==9||this.month1==11) { 228 if(this.day1>30) { 229 this.month1=this.month1+1; 230 this.day1=this.day1-30; 231 } 232 if(this.day1<=30) 233 break; 234 } 235 else 236 if(this.month1==2) { 237 if(this.day1>29) { 238 this.month1=this.month1+1; 239 this.day1=this.day1-29; 240 } 241 if(this.day1<=29) 242 break; 243 } 244 } 245 else { 246 if(this.month1==1||this.month1==3||this.month1==5||this.month1==7||this.month1==8||this.month1==10||this.month1==12) { 247 if(this.day1>31) { 248 if(this.month1!=12) { 249 this.month1=this.month1+1; 250 } 251 else { 252 this.year1=this.year1+1; 253 this.month1=1; 254 } 255 this.day1=this.day1-31; 256 } 257 if(this.day1<=31) 258 break; 259 } 260 else 261 if(this.month1==4||this.month1==6||this.month1==9||this.month1==11) { 262 if(this.day1>30) { 263 this.month1=this.month1+1; 264 this.day1=this.day1-30; 265 } 266 if(this.day1<=30) 267 break; 268 } 269 else 270 if(this.month1==2) { 271 if(this.day1>28) { 272 this.month1=this.month1+1; 273 this.day1=this.day1-28; 274 } 275 if(this.day1<=28) 276 break; 277 } 278 } 279 }while(true); 280 System.out.println(this.year1+"-"+this.month1+"-"+this.day1); 281 } 282 else 283 System.out.println("Wrong Format"); 284 } 285 286 287 public void getagoDate(int n) { 288 boolean a,b; 289 a= isleapYear1(this.year1); 290 b= checkInputValidity1(); 291 if(a!=false&&b!=false) { 292 this.day1=this.day1-n; 293 do { 294 if((this.year1%4==0&&this.year1%100!=0)||this.year1%400==0) { 295 if(this.month1==5||this.month1==7||this.month1==10||this.month1==12) { 296 if(this.day1<0) { 297 this.month1=this.month1-1; 298 this.day1=30+this.day1; 299 } 300 } 301 else 302 if(this.month1==2||this.month1==4||this.month1==6||this.month1==8||this.month1==9||this.month1==11) { 303 if(this.day1<0) { 304 this.month1=this.month1-1; 305 this.day1=31+this.day1; 306 } 307 } 308 else 309 if(this.month1==3) { 310 if(this.day1<0) { 311 this.month1=this.month1-1; 312 this.day1=29+this.day1; 313 } 314 } 315 else if(this.month1==1) { 316 if(this.day1<0) { 317 this.year1=this.year1-1; 318 this.month1=12; 319 this.day1=31+this.day1; 320 } 321 } 322 } 323 else { 324 if(this.month1==5||this.month1==7||this.month1==10||this.month1==12) { 325 if(this.day1<0) { 326 this.month1=this.month1-1; 327 this.day1=30+this.day1; 328 } 329 } 330 else 331 if(this.month1==2||this.month1==4||this.month1==6||this.month1==8||this.month1==9||this.month1==11) { 332 if(this.day1<0) { 333 this.month1=this.month1-1; 334 this.day1=31+this.day1; 335 } 336 } 337 else 338 if(this.month1==3) { 339 if(this.day1<0) { 340 this.month1=this.month1-1; 341 this.day1=28+this.day1; 342 } 343 } 344 else if(this.month1==1) { 345 if(this.day1<0) { 346 this.year1=this.year1-1; 347 this.month1=12; 348 this.day1=31+this.day1; 349 } 350 } 351 } 352 }while(this.day1<0); 353 System.out.println(this.year1+"-"+this.month1+"-"+this.day1); 354 } 355 else 356 System.out.println("Wrong Format"); 357 } 358 359 public void getdayDate() { 360 boolean a,b,c,d; 361 int m = 0,sum1 = 0,sum2 = 0; 362 int[] n= {0,31,28,31,30,31,30,31,31,30,31,30,31}; 363 a=isleapYear1(this.year1); 364 b=isleapYear2(this.year2); 365 c=checkInputValidity1(); 366 d=checkInputValidity2(); 367 if(a!=false&&b!=false&&c!=false&&d!=false) { 368 if((this.year1%4==0&&this.year1%100!=0)||this.year1%400==0) n[2]=29; 369 for(int i=1;i<this.month1;i++) sum1=sum1+n[i]; 370 sum1=sum1+this.day1; 371 if((this.year2%4==0&&this.year2%100!=0)||this.year2%400==0) n[2]=29; 372 for(int i=1;i<this.month2;i++) sum2=sum2+n[i]; 373 sum2=sum2+this.day2; 374 if(this.year1<this.year2) { 375 if((this.year1%4==0&&this.year1%100!=0)||this.year1%400==0) m=366-sum1; 376 else m=365-sum1; 377 m=m+sum2; 378 for(;this.year1<this.year2-1;this.year1++) { 379 if((this.year1%4==0&&this.year1%100!=0)||this.year1%400==0) m=m+366; 380 else m=m+365; 381 } 382 } 383 else if(this.year1==this.year2) { 384 if(sum1>=sum2) m=sum1-sum2; 385 else m=sum2-sum1; 386 } 387 else if(this.year1>this.year2) { 388 if((this.year2%4==0&&this.year2%100!=0)||this.year2%400==0) m=366-sum2; 389 else m=365-sum2; 390 m=m+sum1; 391 for(;this.year2<this.year1-1;this.year2++) { 392 if((this.year2%4==0&&this.year2%100!=0)||this.year2%400==0) m=m+366; 393 else m=m+365; 394 } 395 } 396 System.out.println(m); 397 } 398 else 399 System.out.println("Wrong Format"); 400 } 401 402 }
题目集5(7-5)我所输入的代码:
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 int year = 0; 7 int month = 0; 8 int day = 0; 9 10 int choice = input.nextInt(); 11 12 if (choice == 1) { // test getNextNDays method 13 int m = 0; 14 year = Integer.parseInt(input.next()); 15 month = Integer.parseInt(input.next()); 16 day = Integer.parseInt(input.next()); 17 18 DateUtil date = new DateUtil(year, month, day); 19 20 if (!date.checkInputValidity()) { 21 System.out.println("Wrong Format"); 22 System.exit(0); 23 } 24 25 m = input.nextInt(); 26 27 if (m < 0) { 28 System.out.println("Wrong Format"); 29 System.exit(0); 30 } 31 32 System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:"); 33 System.out.println(date.getNextNDays(m).showDate()); 34 } else if (choice == 2) { // test getPreviousNDays method 35 int n = 0; 36 year = Integer.parseInt(input.next()); 37 month = Integer.parseInt(input.next()); 38 day = Integer.parseInt(input.next()); 39 40 DateUtil date = new DateUtil(year, month, day); 41 42 if (!date.checkInputValidity()) { 43 System.out.println("Wrong Format"); 44 System.exit(0); 45 } 46 47 n = input.nextInt(); 48 49 if (n < 0) { 50 System.out.println("Wrong Format"); 51 System.exit(0); 52 } 53 54 System.out.print( 55 date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:"); 56 System.out.println(date.getPreviousNDays(n).showDate()); 57 } else if (choice == 3) { //test getDaysofDates method 58 year = Integer.parseInt(input.next()); 59 month = Integer.parseInt(input.next()); 60 day = Integer.parseInt(input.next()); 61 62 int anotherYear = Integer.parseInt(input.next()); 63 int anotherMonth = Integer.parseInt(input.next()); 64 int anotherDay = Integer.parseInt(input.next()); 65 66 DateUtil fromDate = new DateUtil(year, month, day); 67 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 68 69 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 70 System.out.println("The days between " + fromDate.showDate() + 71 " and " + toDate.showDate() + " are:" 72 + fromDate.getDaysofDates(toDate)); 73 } else { 74 System.out.println("Wrong Format"); 75 System.exit(0); 76 } 77 } 78 else{ 79 System.out.println("Wrong Format"); 80 System.exit(0); 81 } 82 } 83 } 84 85 class DateUtil { 86 private int year; 87 private int month; 88 private int day; 89 90 public DateUtil(int year, int month, int day) { 91 this.year = year; 92 this.month = month; 93 this.day = day; 94 } 95 96 public DateUtil(){} 97 98 public void setYear(int year) { 99 this.year = year; 100 } 101 102 public void setMonth(int month) { 103 this.month = month; 104 } 105 106 public void setDay(int day) { 107 this.day = day; 108 } 109 110 public int getYear() { 111 return year; 112 } 113 114 public int getMonth() { 115 return month; 116 } 117 118 public int getDay() { 119 return day; 120 } 121 122 private final int[] DAY_OF_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 123 124 private int getDayOfMonth(int year, int month) { 125 int days = DAY_OF_MONTH[month - 1]; 126 if (month == 2 && isLeapYear(year)) { 127 days = 29; 128 } 129 return days; 130 } 131 132 public boolean checkInputValidity()//检测输入的年、月、日是否合法 133 { 134 if (year < 1820 || year > 2020) return false; 135 if (month < 1 || month > 12) return false; 136 // int _day = this.getDayOfMonth(year, month); 137 // return day <= _day; 138 return day >= 1 && day <= 31; 139 } 140 141 public boolean isLeapYear(int year)//判断year是否为闰年 142 { 143 return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; 144 } 145 146 147 public DateUtil getNextNDays(int n)//取得year-month-day的下n天日期 148 { 149 int year = this.year; 150 int month = this.month; 151 int day = this.day; 152 // day = Math.min(day, this.getDayOfMonth(year, month)); 153 for (int i = 0; i < n; i++) { 154 day++; 155 if (day > getDayOfMonth(year, month)) { 156 day = 1; 157 month++; 158 if (month > 12) { 159 month = 1; 160 year++; 161 } 162 } 163 } 164 return new DateUtil(year, month, day); 165 } 166 167 public DateUtil getPreviousNDays(int n)//取得year-month-day的前n天日期 168 { 169 int year = this.year; 170 int month = this.month; 171 int day = this.day; 172 for (int i = 0; i < n; i++) { 173 day--; 174 while (day < 1) { 175 month--; 176 if (month < 1) { 177 month = 12; 178 year--; 179 } 180 day += getDayOfMonth(year, month); 181 } 182 } 183 return new DateUtil(year, month, day); 184 } 185 186 public boolean compareDates(DateUtil date)//比较当前日期与date的大小(先后) 187 { 188 if (this.year > date.year) return true; 189 if (this.year == date.year) { 190 if (this.month > date.month) return true; 191 if (this.month == date.month) { 192 if (this.day >= date.day) return true; 193 } 194 } 195 return false; 196 } 197 198 public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等 199 { 200 if (date != null) { 201 if (year == date.year && month == date.month && day == date.day) { 202 return true; 203 } 204 } 205 return false; 206 } 207 208 private static final int[] mon = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; 209 public int getDaysofDates(DateUtil date)//求当前日期与date之间相差的天数 210 { 211 DateUtil dateUtil1 = this; // 小 212 DateUtil dateUtil2 = date; // 大 213 if (this.compareDates(date)) { 214 dateUtil1 = date; 215 dateUtil2 = this; 216 } 217 218 int days; 219 int leapYearNum = 0; 220 for (int i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) { 221 if (isLeapYear(i)) { 222 leapYearNum++; 223 } 224 } 225 226 days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum; 227 228 int d1 = mon[dateUtil1.getMonth() - 1] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0); 229 int d2 = mon[dateUtil2.getMonth() - 1] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0); 230 return days - d1 + d2; 231 } 232 233 public String showDate()//以“year-month-day”格式返回日期值 234 { 235 return year + "-" + month + "-" + day; 236 } 237 }
分析:很明显从类图可以看出题目集4(7-2)的函数调用很单一是由main函数调用DateUtil,然后在由DateUtil调用year,year调用Month,month调用day来实现这个功能;但是题目集5(7-5)则是由main函数调用DateUtil函数,再由DateUtil函数去调用year,month,day函数来实现功能。就这两个函数实现功能的原理来说,明显题目集5(7-5)跟符合现实更方便使用,并且更稳定不易出错。
2,题目集4(7-3)、题目集6(7-5、7-6)三种渐进式图形继承设计的思路与技术运用(封装、继承、多态、接口等)
(1)题目集4(7-3)
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub int n; double r,w,l,h; Scanner a = new Scanner(System.in); n = a.nextInt(); switch(n) { case 1: r=a.nextDouble(); if(r<=0) System.out.println("Wrong Format"); else { Circle b=new Circle(); b.setRadius(r); System.out.println("Circle's area:"+String.format("%.2f",b.getArea())); } break; case 2: w = a.nextDouble(); l = a.nextDouble(); if(w<=0||l<=0) System.out.println("Wrong Format"); else { Rectangle b=new Rectangle(); b.setWidth(w); b.setLength(l); System.out.println("Rectangle's area:"+String.format("%.2f",b.getArea())); } break; case 3: r=a.nextDouble(); if(r<=0) System.out.println("Wrong Format"); else { Ball b=new Ball(); b.setRadius(r); System.out.println("Ball's surface area:"+String.format("%.2f",b.getArea())); System.out.println("Ball's volume:"+String.format("%.2f",b.getVolume())); } break; case 4: w = a.nextDouble(); l = a.nextDouble(); h = a.nextDouble(); if(w<=0||l<=0||h<=0) System.out.println("Wrong Format"); else { Box b=new Box(); b.setWidth(w); b.setLength(l); b.setHeight(h); System.out.println("Box's surface area:"+String.format("%.2f",b.getArea())); System.out.println("Box's volume:"+String.format("%.2f",b.getVolume())); } break; default: System.out.println("Wrong Format"); } } } class Shape{ public double getArea(){//求图形面积 return 0.0; } Shape(){ System.out.println("Constructing Shape"); } } class Circle extends Shape{//圆 private double radius;//半径 Circle(){ System.out.println("Constructing Circle"); } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double getArea() { double s=0; s=this.radius*this.radius*Math.PI; return s; } } class Rectangle extends Shape{//矩形 private double width = 0; private double length = 0; Rectangle(){ System.out.println("Constructing Rectangle"); } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getArea() { double s = 0; s = length*width; return s; } } class Ball extends Circle{ Ball(){ System.out.println("Constructing Ball"); } public double getArea() { double s = 0; //s=super.getRadius()*super.getRadius()*Math.PI*4; s=4*super.getArea(); //System.out.println("Ball's Area:"+s); return s; } public double getVolume(){//求球体积 double v = 0; v=getRadius()*getRadius()*getRadius()*Math.PI*4/3.0; //v=getArea()/3*getRadius(); //System.out.println("Ball's Volume:"+v); return v; } } class Box extends Rectangle{ private double height; public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } Box(){ System.out.println("Constructing Box"); } public double getArea() { double s = 0; s=getLength()*getWidth()*2+this.height*getLength()*2+this.height*getWidth()*2; //System.out.println("Box's Area:"+s); return s; } public double getVolume(){//求立方体体积 double v = 0; v=super.getArea()*height; //System.out.println("Box's Volume:"+v): return v; } }
(2)题目集6(7-5,7-6)
<1>题目集6(7-5)
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub int n1 = 0,n2 = 0,n3 = 0; double r,w,l,side1,side2,side3; double[] d = new double[]; boolean f = true; boolean g = true; boolean h = true; boolean k = true; Scanner a = new Scanner(System.in); n1 = a.nextInt(); n2 = a.nextInt(); n3 = a.nextInt(); double[] c = new double[n1+n2+n3]; double sum = 0; if(n1+n2+n3<0) f = false; for(int i = 0;i<n1;i++) { r = a.nextDouble(); if(r<=0) { g = false; break; } Circle b = new Circle(); b.setRadius(r); c[i] = b.getArea(); } for(int i = 0;i<n2;i++) { w = a.nextDouble(); l = a.nextDouble(); if(w<=0||l<=0) { h = false; break; } Rectangle b = new Rectangle(); b.setLength(l); b.setWidth(w); c[n1+i] = b.getArea(); } for(int i = 0;i<n3;i++) { side1 = a.nextDouble(); side2 = a.nextDouble(); side3 = a.nextDouble(); if(side1>=side2+side3||side2>=side1+side3||side3>=side1+side2||side1<0||side2<0||side3<0) { k = false; break; } Triangle b = new Triangle(); b.setSide1(side1); b.setSide2(side2); b.setSide3(side3); c[n1+n2+i] = b.getArea(); } if(f==true&&g==true&&h==true&&k==true) { int size = c.length; for (int i = 0;i<c.length;i++){ if (c[i] == 0){ for (int j = i;j<c.length-1;j++){ c[j] = c[j+1]; } i--; size--; } } System.out.println("Original area:"); for(int i = 0;i<size;i++) { System.out.printf(String.format("%.2f ",c[i])); sum = sum+c[i]; } System.out.println("\nSum of area:"+String.format("%.2f",sum)); for (int i=0;i<size-1;i++){ for (int j=i+1;j<size;j++){ if(c[i]>c[j]){ double temp = c[i]; c[i] = c[j]; c[j] = temp; } } } System.out.println("Sorted area:"); for(int i = 0;i<size;i++) { System.out.printf(String.format("%.2f ",c[i])); } System.out.println("\nSum of area:"+String.format("%.2f",sum)); } else System.out.println("Wrong Format"); } } class Shape{ public double getArea(){//求图形面积 return 0.0; } Shape(){ } } class Circle extends Shape{//圆 private double radius;//半径 Circle(){ } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double getArea() { double s=0; s=this.radius*this.radius*Math.PI; return s; } } class Rectangle extends Shape{//矩形 private double width = 0; private double length = 0; Rectangle(){ } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getArea() { double s = 0; s = length*width; return s; } } class Triangle extends Shape{ private double side1 = 0; private double side2 = 0; private double side3 = 0; Triangle(){ } public double getSide1() { return side1; } public void setSide1(double side1) { this.side1 = side1; } public double getSide2() { return side2; } public void setSide2(double side2) { this.side2 = side2; } public double getSide3() { return side3; } public void setSide3(double side3) { this.side3 = side3; } public double getArea() { double s = 0; double p = 0; p = (this.side1+this.side2+this.side3)/2; s = (Math.sqrt(p*(p-this.side1)*(p-this.side2)*(p-this.side3))); return s; } }
<2>题目集6(7-6)
代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub //int n; double r,w,l; Scanner a = new Scanner(System.in); r=a.nextDouble(); w=a.nextDouble(); l=a.nextDouble(); if(r<=0||w<=0||l<=0) System.out.println("Wrong Format"); else { Circle b=new Circle(); b.setRadius(r); System.out.println(String.format("%.2f",b.getArea())); Rectangle c=new Rectangle(); c.setLength(l); c.setWidth(w); System.out.println(String.format("%.2f",c.getArea())); } } } class GetArea{ GetArea(){//求图形面积 } } class Circle extends GetArea{//圆 private double radius;//半径 //Circle(double radius); Circle(){ } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double getArea() { double s=0; s=this.radius*this.radius*Math.PI; return s; } } class Rectangle extends GetArea{//矩形 private double width = 0; private double length = 0; Rectangle(){ } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getArea() { double s = 0; s = length*width; return s; } }
类图:
分析:这几道题目主要是考查对封装,接口,多态以及继承的,继承几乎在每段代码都能看到,因为我们可以通过继承来修改父类的一些函数,使它跟适合我们需要他完成的功能;而封装是实现面向对象程序设计的第一步,封装就是将数据或函数等集合在一个个的单元中(我们称之为类)被封装的对象通常被称为抽象数据类型,封装可以保护或者防止代码(数据)被我们无意中破坏,在面向对象程序设计中数据被看作是一个中心的元素并且和使用它的函数结合的很密切,从而保护它不被其它的函数意外的修改;接口本质上是抽象方法,在接口中指定的方法没有默认的实现,每个包含接口的类必需实现所有的方法,它描述可属于任何类或结构的一组相关行为;多态首先是建立在继承的基础上的,先有继承才能有多态,多态是指不同的子类在继承父类后分别都重写覆盖了父类的方法,即父类同一个方法,在继承的子类中表现出不同的形式,多态成立的另一个条件是在创建子类时候必须使用父类new子类的方式。
3,正则表达式:
"[1-9][0-9]{4,14}"检查一段5-10位开头不为0的数组
"^([0-9]||[A-Z]||[a+z]){4}$"检查一段由四位数字或者字母(包含大小写)组成的字符串
flag=g.matches("2020");h.matches("1[1-7]")||h.matches("61")||h.matches("7[1-3]")||h.matches("8[1-2]")b.matches("0[1-9]")||b.matches("[1-4][0-9]")检查学号,且学号前4位为2020,班级为11-17,61,71-73,81-82,在 班级内的学号(序号)为01-40.
4,题目集5(7-4)中Java集合框架应用的分析总结(这道题我没有写,不会写)
二,踩坑心得
1,正则表达式的书写不规范导致实现不了功能或编译错误,还是需要去熟悉正则表达式的原理才能对他的使用更加得心应手。
2,对各个函数之间的关系理不清,继承,接口会弄错,因该要在写之前就把各个函数的关系理清再开始写。
3,对封装和接口不熟悉,经常会因为封装不对或者接口设置不当而出现问题。
三,改进建议
要想办法删除一些比较繁杂的无用的代码,让代码更简洁精炼。还要继续学习正则表达式,要做到能够完全自己写出一整段正则表达式,并且能够运行且完整的实现所需要的功能。
四,总结
通过本次的学习,我对JAVA的了解更深入了,自学了一部分的正则表达式的运用,了解并使用了类的基本概念,如何创建类,学会了类的继承,了解了类的多态与接口。在正则表达式这方面还要继续学习,还有封装和接口同样需要去学习。
标签:题目,int,month1,Pta,month,year,public,day 来源: https://www.cnblogs.com/1113456a/p/14722311.html