ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

BLOG

2022-06-16 23:00:12  阅读:169  来源: 互联网

标签:return String double ArrayList BLOG new public


一:前言 对于近期的学习:主要是进行了pta的作业的学习,由于近期有同学反应java的作业太多,所以我们的老师将pta后三次题目集的难度大大降低了,第七次题目集是因为要用到之前的那些图形有关的代码,而之前的图形题目因为未完全做出,而导致了第七次pta作业的很多问题,但是最后三次的pta作业由于老师大大的降低了其难度,所以我们的分数普遍都更高一点,也就导致了这几次pta作业的题量、难度等都大大的降低了,而这次blog的主要是就是分析八九十三次题目集中的电信资费题目。   二:设计与分析 7-1 电信计费系列1-座机计费 源码如下: + View Code

  类图如下:

 

 题目分析:

本道题目由于老师已经给出了类图,所以第一步的事情就是将所有给出的类给补全,补全之后,在去主函数中,分别利用正则表达式去判断每一段输入的字符串,之后进行容器的添加,计算容器中的花费金额。除此之外,本道题目由于采用的是多个类之间的关系,所以要将每一个类与类之间的关系理顺。

踩坑心得:本道题目的正则表达式是一个非常有难度的地方,由于本人非常菜鸡所以询问了同学。

7-2 多态测试

源码如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 import java.util.ArrayList; import java.util.Scanner; public class Main {           public static void main(String[] args) {         ArrayList<Container> a = new ArrayList<Container>();         cube cu;         cylinder cy;         Scanner input = new Scanner(System.in);         double count = input.nextDouble();         for(int i = 0 ; i < count; i++) {                           String sc = input.next();             if(sc.equals("cube")) {                 cu = new cube(input.nextDouble());                 a.add(cu);             }             if(sc.equals("cylinder")) {                 cy = new cylinder(input.nextDouble(),input.nextDouble());                 a.add(cy);             }         }         double s =  Container.sumofArea(a.toArray(new Container[a.size()]));         double v = Container.sumofVolume(a.toArray(new Container[a.size()]));         System.out.printf("%.2f\n",s);         System.out.printf("%.2f",v);               } }   interface Container {     public static final double pi=3.1415926;     public abstract double area();     public abstract double volume();     static double sumofArea(Container c[]) {         double s = 0;         for(int i = 0 ; i < c.length ; i++) {             s = s + c[i].area();         }           return s;     }           static double sumofVolume(Container c[]) {         double v = 0;         for(int i = 0 ; i < c.length ; i++) {             v = v + c[i].volume();         }         return v;     }   } class cube implements Container {     private double a;           public cube(double a) {         super();         this.a = a;     }       @Override     public double area() {         double s = 0;         s = 6 * a * a;         // TODO Auto-generated method stub         return s;     }       @Override     public double volume() {         double v = 0;         v = a * a * a;         // TODO Auto-generated method stub         return v;     }               } class cylinder implements Container {     private double r;     private double h;       public cylinder(double r, double h) {         super();         this.r = r;         this.h = h;     }       @Override     public double area() {         double s = 0;         s = 2 * pi * r * h + pi * r * r * 2;         // TODO Auto-generated method stub         return s;     }       @Override     public double volume() {         double v = 0;         v = pi * r * r * h;         // TODO Auto-generated method stub         return v;     }   }

  题目分析:本道题目并没有什么难度,所要求的东西均在题目中给出,只要一步一步的按照题目理一遍就能将本道题目写出。

 

7-1 电信计费系列2-手机+座机计费

源码如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.Scanner;   public class Main{     public static void main(String []args) throws ParseException {                          Scanner input = new Scanner(System.in);         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");         ArrayList<User> arr = new ArrayList<>();         int flag = 0;         String sc = input.nextLine();         String[] a ;         //z1         while (!sc.equals("end")){             //z1             if(sc.matches("[u][-](0791)[0-9]{7,8}[ ][0]")){                 a = sc.substring(2,sc.length()).split("\\s");                                   for(int i = 0;i < arr.size();i++){                     if(Objects.equals(arr.get(i).getNumber(), a[0])) {                         flag = 1;                     }                 }                 if(flag == 0) {                     arr.add(new User(a[0]));                     flag = 0;                 }             }             //s1             else if(sc.matches("u-1[0-9]{10} [1]")) {                  a = sc.substring(2,sc.length()).split("\\s");                  for(int i = 0;i < arr.size();i++){                      if(Objects.equals(arr.get(i).getNumber(), a[0])) {                          flag = 1;                      }                  }                  if(flag == 0) {                     arr.add(new User(a[0]));                     flag = 0;                  }             }             //z1,z2            if(sc.matches("t-0\\d{9,11}\\s0\\d{9,11}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){                 a = sc.substring(2,sc.length()).split("\\s");                 for(int i=0 ; i< arr.size() ; i++){                     if(arr.get(i).number.equals(a[0])){                         if(a[1].startsWith("0791")){                             arr.get(i).userRecord.addCallingInCityRecords(new CallRecord(a[2]+" "+a[3],a[4]+" "+a[5]));                             arr.get(i).setUserRecord(arr.get(i).userRecord);                         }                         else if(a[1].substring(0,4).matches("(079\\d|0701)")){                             arr.get(i).userRecord.addCallingInProvinceRecords(new CallRecord(a[2]+" "+a[3],a[4]+" "+a[5]));                             arr.get(i).setUserRecord(arr.get(i).userRecord);                         }                         else{                             arr.get(i).userRecord.addCallingInLandRecords(new CallRecord(a[2]+" "+a[3],a[4]+" "+a[5]));                             arr.get(i).setUserRecord(arr.get(i).userRecord);                         }                     }                 }             }             //z1,s2             else if(sc.matches("t-(0[0-9]{3}|020)\\d{7,9} \\d{11} (\\d{3}|\\d{4})\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){                 a = sc.substring(2,sc.length()).split("\\s");                 for (int i=0;i< arr.size();i++){                     if (arr.get(i).number.equals(a[0])){                         if (a[2].startsWith("0791")){                             arr.get(i).userRecord.addCallingInCityRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));                         }                         else if (a[2].matches("(079\\d|0701)")){                             arr.get(i).userRecord.addCallingInProvinceRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));                         }                         else {                             arr.get(i).userRecord.addCallingInLandRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));                         }                     }                     if (arr.get(i).number.equals(a[1])){                         if (!a[2].matches("(079\\d|0701)"))                             arr.get(i).userRecord.addAnswerInLandRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));                     }                 }             }             //s1,s2             else if (sc.matches("t-\\d{11} (\\d{3}|\\d{4}) \\d{11} (\\d{3}|\\d{4})\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){                 a = sc.substring(2,sc.length()).split("\\s");                  for (int i=0;i< arr.size();i++){                      if (arr.get(i).number.equals(a[0])){                          if (a[1].equals("0791")) {                              if (a[3].matches("0791"))                                  arr.get(i).userRecord.addCallingInCityRecords(new CallRecord(a[4] + " " + a[5], a[6] + " " + a[7]));                              else if (a[3].matches("(079\\d|0701)"))                                  arr.get(i).userRecord.addCallingInProvinceRecords(new CallRecord(a[4] + " " + a[5], a[6] + " " + a[7]));                              else                                  arr.get(i).userRecord.addCallingInLandRecords(new CallRecord(a[4] + " " + a[5], a[6] + " " + a[7]));                          }                          else if (a[1].matches("(0701|079\\d)")){                              arr.get(i).userRecord.addCallingInProvinceRoamRecords(new CallRecord(a[4] + " " + a[5], a[6] + " " + a[7]));                          }                          else                              arr.get(i).userRecord.addCallingInLandRoamRecords(new CallRecord(a[4] + " " + a[5], a[6] + " " + a[7]));                      }                      if (arr.get(i).number.equals(a[2])){                          if (!a[3].matches("(079\\d|0701)"))                              arr.get(i).userRecord.addAnswerInLandRecords(new CallRecord(a[4] + " " + a[5], a[6] + " " + a[7]));                      }                  }             }             //s1,z2             else if (sc.matches( "t-\\d{11} (020|0\\d{3}) \\d{10,13}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d" )){                 a = sc.substring(2,sc.length()).split("\\s");                 for (int i=0;i< arr.size();i++){                     if (arr.get(i).number.equals(a[0])){                         if (a[1].equals("0791")){                             if (a[2].matches("0791\\d{6,8}"))                                 arr.get(i).userRecord.addCallingInCityRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));                             else if (a[2].matches("(079\\d|0701)\\d{6,8}"))                                 arr.get(i).userRecord.addCallingInProvinceRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));                             else                                 arr.get(i).userRecord.addCallingInLandRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));                         }                         else if (a[1].matches("(0701|0790|079[2-9])")){                             arr.get(i).userRecord.addCallingInProvinceRoamRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));                         }                         else                             arr.get(i).userRecord.addCallingInLandRoamRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));                     }                 }             }             sc = input.nextLine();         }                   List<User> list = new ArrayList<>(arr);         Collections.sort(list);                   for (User e : list){             System.out.print(e.getNumber()+" ");             System.out.print(new DecimalFormat("0.0#").format(e.calCost()));             System.out.print(" ");             System.out.print(new DecimalFormat("0.0#").format(e.calBalance()));             System.out.println();         }         } }   class User implements Comparable<User> {     UserRecords userRecord = new UserRecords();     double balance = 100;     ChargeMode chargeMode;     String number;     public User(){       }     public User(String number) {         this.number = number;     }       public double calBalance(){         if(number.matches("(0791)\\d{6,8}"))             return balance-calCost()-20;         return balance-calCost()-15;     }     public double calCost(){         LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();         LandPhoneInIandRule landPhoneInIandRule = new LandPhoneInIandRule();         LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();         PhoneInCityRule phoneInCityRule = new PhoneInCityRule();         PhoneInIandRule phoneInIandRule = new PhoneInIandRule();         PhoneInProvinceRule phoneInProvinceRule = new PhoneInProvinceRule();         CallPhoneInIandRoamRule callphoneInIandRoamRule = new CallPhoneInIandRoamRule();         CallPhoneInProvinceRoamRule callphoneInProvinceRoamRule = new CallPhoneInProvinceRoamRule();         AnswerPhoneInLandRoamRule answerPhoneInLandRoamRule = new AnswerPhoneInLandRoamRule();         if(number.matches("(0791)\\d{6,8}")) {             return landPhoneInIandRule.calCost(userRecord.getCallingInLandRecords())                     +landPhoneInCityRule.calCost(userRecord.getCallingInCityRecords())                     +landPhoneInProvinceRule.calCost(userRecord.getCallingInProvinceRecords());         }         return phoneInIandRule.calCost(userRecord.getCallingInLandRecords())                 +phoneInCityRule.calCost(userRecord.getCallingInCityRecords())                 +phoneInProvinceRule.calCost(userRecord.getCallingInProvinceRecords())                 +answerPhoneInLandRoamRule.calCost(userRecord.getAnswerInLandRecords())                 +callphoneInIandRoamRule.calCost(userRecord.getCallingInLandRoamRecords())                 +callphoneInProvinceRoamRule.calCost(userRecord.getCallingInProvinceRoamRecords());     }           public UserRecords getUserRecord(){         return userRecord;     }           public void setUserRecord(UserRecords userRecord){         this.userRecord = userRecord;     }           public double getBalance(){         return balance;     }           public ChargeMode getChargeMode(){         return chargeMode;     }           public void setChargeMode(ChargeMode chargeMode){         this.chargeMode = chargeMode;     }           public String getNumber(){         return number;     }           public void setNumber(String number){         this.number = number;     }           public int compareTo(User o) {         return this.getNumber().compareTo(o.getNumber());     }   } abstract class ChargeMode{     ArrayList<ChargeRule> list = new ArrayList<ChargeRule>();     public ArrayList<ChargeRule> getChargeRule(){         return list;     }     public void setChargeRule(ArrayList<ChargeRule> chargeRule){         this.list = list;     }     public double calCost(UserRecords userRecords){         return 0;     }     public double getMonthlyRent(){         return 20;     } } class UserRecords{     ArrayList<CallRecord> callingInProvinceRoamRecords =  new ArrayList<CallRecord>();     ArrayList<CallRecord> callingInLandRoamRecords =  new ArrayList<CallRecord>();     ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();     ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();     ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();     ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();     ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();     ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();     ArrayList<MessageRecord> sendMessageRecord = new ArrayList<MessageRecord>();     ArrayList<MessageRecord> receiveMessageRecord = new ArrayList<MessageRecord>();           public void addCallingInProvinceRoamRecords(CallRecord callRecord){         callingInProvinceRoamRecords.add(callRecord);     }           public void addCallingInLandRoamRecords(CallRecord callRecord){         callingInLandRoamRecords.add(callRecord);     }           public void addCallingInCityRecords(CallRecord callRecord){         callingInCityRecords.add(callRecord);     }           public void addCallingInProvinceRecords(CallRecord callRecord){         callingInProvinceRecords.add(callRecord);     }           public void addCallingInLandRecords(CallRecord callRecord){         callingInLandRecords.add(callRecord);     }           public void addAnswerInCityRecords(CallRecord callRecord){         answerInCityRecords.add(callRecord);     }           public void addAnswerInProvinceRecords(CallRecord callRecord){         answerInProvinceRecords.add(callRecord);     }           public void addAnswerInLandRecords(CallRecord callRecord){         answerInLandRecords.add(callRecord);     }           public void addSendMessageRecord(MessageRecord sendMessageRecord){         this.sendMessageRecord.add(sendMessageRecord);     }           public void addReceiveMessageRecord(MessageRecord receiveMessageRecord){         this.receiveMessageRecord.add(receiveMessageRecord);     }           public ArrayList<MessageRecord> getSendMessageRecord(){         return sendMessageRecord;     }           public ArrayList<MessageRecord> getReceiveMessageRecord(){         return receiveMessageRecord;     }           public ArrayList<CallRecord> getCallingInCityRecords(){         return callingInCityRecords;     }           public ArrayList<CallRecord> getCallingInProvinceRecords(){         return callingInProvinceRecords;     }           public ArrayList<CallRecord> getCallingInLandRecords(){         return callingInLandRecords;     }           public ArrayList<CallRecord> getAnswerInCityRecords(){         return answerInCityRecords;     }           public ArrayList<CallRecord> getAnswerInProvinceRecords(){         return answerInProvinceRecords;     }           public ArrayList<CallRecord> getAnswerInLandRecords(){         return answerInLandRecords;     }           public ArrayList<CallRecord> getCallingInProvinceRoamRecords(){         return  callingInProvinceRoamRecords;     }           public ArrayList<CallRecord> getCallingInLandRoamRecords(){         return callingInLandRoamRecords;     }     } class LandlinePhoneCharging extends ChargeMode{     double monthlyRent = 20;       public double calCost(UserRecords userRecords){         return 0;     }           public double getMonthlyRent(){         return monthlyRent;     } } class CommunicationRecord{     String callingNumber;     String answerNumber;           public String getCallingNumber(){         return callingNumber;     }           public void setCallingNumber(String callingNumber){         this.callingNumber = callingNumber;     }           public String getAnswerNumber(){         return answerNumber;     }           public void setAnswerNumber(String answerNumber){         this.answerNumber = answerNumber;     } }   class CallRecord extends ChargeRule{     Date startTime;     Date endTime;     String callingAddressAreaCode;     String AnswerAddressAreaCode;       public CallRecord(String startTime,String endTime) throws ParseException {         this.startTime =  new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(startTime);         this.endTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(endTime);     }       public Date getStartTime(){         return startTime;     }           public void setStartTime(Date startTime){         this.startTime = startTime;     }           public Date getEndTime(){         return endTime;     }           public void setEndTime(Date endTime){         this.endTime = endTime;     }           public String getCallingAddressAreaCode(){         return callingAddressAreaCode;     }           public void setCallingAddressAreaCode(String callingAddressAreaCode){         this.callingAddressAreaCode = callingAddressAreaCode;     }       public String getAnswerAddressAreaCode() {         return AnswerAddressAreaCode;     }       public void setAnswerAddressAreaCode(String answerAddressAreaCode) {         this.AnswerAddressAreaCode = answerAddressAreaCode;     }       }   class MessageRecord extends ChargeRule{     String message;       public String getMessage() {         return message;     }       public void setMessage(String message) {         this.message = message;     } }   abstract class ChargeRule{   }   abstract class CallChargeRule extends ChargeRule{     public double calCost(ArrayList<CallRecord>callRecords){         return 0;     } }   class LandPhoneInCityRule extends CallChargeRule{     public double calCost(ArrayList<CallRecord> callRecords){         double cost = 0;         double  minutes;         for (CallRecord e: callRecords){             minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double60000);             cost = cost + 0.1 * minutes;         }         return cost;     } }   class LandPhoneInProvinceRule extends CallChargeRule{     public double calCost(ArrayList<CallRecord> callRecords){         double cost = 0;         double  minutes;         for (CallRecord e: callRecords){             minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double60000);             cost = cost + 0.3 * minutes;         }         return cost;     } }   class LandPhoneInIandRule extends CallChargeRule{     public double calCost(ArrayList<CallRecord> callRecords){         double cost = 0;         double  minutes;         for (CallRecord e: callRecords){             minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double60000);             cost = cost + 0.6 * minutes;         }         return cost;     } }   class PhoneInCityRule extends CallChargeRule{     public double calCost(ArrayList<CallRecord> callRecords){         double cost = 0;         double  minutes;         for (CallRecord e: callRecords){             minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double60000);             cost = cost + 0.1 * minutes;         }         return cost;     } }   class PhoneInProvinceRule extends CallChargeRule{     public double calCost(ArrayList<CallRecord> callRecords){         double cost = 0;         double  minutes;         for (CallRecord e: callRecords){             minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double60000);             cost = cost + 0.2 * minutes;         }         return cost;     } }   class PhoneInIandRule extends CallChargeRule{     public double calCost(ArrayList<CallRecord> callRecords){         double cost = 0;         double  minutes;         for (CallRecord e: callRecords){             minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double60000);             cost = cost + 0.3 * minutes;         }         return cost;     } }   class CallPhoneInIandRoamRule extends CallChargeRule{     public double calCost(ArrayList<CallRecord> callRecords){         double cost = 0;         double  minutes;         for (CallRecord e: callRecords){             minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double60000);             cost = cost + 0.6 * minutes;         }         return cost;     } }   class CallPhoneInProvinceRoamRule extends CallChargeRule{     public double calCost(ArrayList<CallRecord> callRecords){         double cost = 0;         double  minutes;         for (CallRecord e: callRecords){             minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double60000);             cost = cost + 0.3 * minutes;         }         return cost;     } }   class AnswerPhoneInLandRoamRule extends CallChargeRule{     public double calCost(ArrayList<CallRecord> callRecords){         double cost = 0;         double  minutes;         for (CallRecord e: callRecords){             minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double60000);             cost = cost + 0.3 * minutes;         }         return cost;     } }

  类图如下:

 

 

 

 题目分析:本道题目在之前的那道题目上进行添加一些算钱的类,依然如同上题一样利用正则表达式来判别输入的字符串之后在进行相应的开户等操作,本道题目由于有上一题的参考,所以添加几个算钱的类,并且在总的算钱的类里面更改一下算钱方式即可。

 

采坑心得:本道题目的关于年份的判断出了一点问题,经多次修改为修改正确,放弃了。

7-2 sdut-Collection-sort--C~K的班级(II)

源码如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner;   public class Main {     public static void main(String[] args) {         Scanner input = new Scanner (System.in);         int n = input.nextInt();         String a = input.nextLine();         ArrayList<String> list = new ArrayList<String>();         while(n-- != 0) {             a = input.nextLine();             if(list.contains(a)) {                 continue;             }             else {                 list.add(a);             }         }         System.out.println(list.size());                   List<String> b = new ArrayList<>(list);         Collections.sort(list);                   for(String c : list) {             System.out.println(c);         }               } }

  题目分析:本道题目的难度在于用了一个contains()函数去判断是否有重复的数据输入,之后排序一下输出即可。

7-3 阅读程序,按照题目需求修改程序

源码如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner;     //1、导入相关包   //定义员工类 class Employee implements Comparable<Employee>{       private String name;     private int age;       public Employee() {         super();     }       public Employee(String name, int age) {         super();         this.name = name;         this.age = age;     }       public String getName() {         return name;     }       public void setName(String name) {         this.name = name;     }       public int getAge() {         return age;     }       public void setAge(int age) {         this.age = age;     }       @Override     public int compareTo(Employee o) {         // TODO Auto-generated method stub         return 0;     } }   //主函数 public class Main {       public static void main(String[] args) {                   ArrayList<Employee> list = new ArrayList<Employee>();                 // 1、创建有序集合对象      // 创建3个员工元素对象         for (int i = 0; i < 3; i++) {             Scanner sc = new Scanner(System.in);             String employeeName = sc.nextLine();             int employeeAge = sc.nextInt();                           Employee employee = new Employee(employeeName, employeeAge);             list.add(employee);         }                    List<Employee> c = new ArrayList<>(list);          Collections.sort(list);                     for(Employee a : list) {              System.out.println(a.getName() + "---" + a.getAge());          }                                             /*      // 2、创建迭代器遍历集合                 Iterator it;                                   //3、遍历                 while (it.hasNext()) {                     //4、集合中对象未知,向下转型                     Employee e =   (Employee)it.next();                     System.out.println(e.getName() + "---" + e.getAge());                 }           */     }   }

  题目分析:本道题目由于我未使用题目给出的迭代器方法,所以本题不做分析。

7-1 电信计费系列3-短信计费

 源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Scanner;   public class Main {           public static void main(String[] args) throws ParseException {         Scanner input = new Scanner(System.in);         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");         ArrayList<User> users = new ArrayList<>();         String sc = input.nextLine();         String[] a ;         while (!sc.equals("end")){             int j = 0;             if(sc.matches("u-1[0-9]{10} [3]")){                 a = sc.substring(2).split("\\s");                 if (users.size() == 0){                     users.add(new User(a[0],new MessageCharging()));}                 else {                     for (int i = 0; i < users.size(); i++) {                         if (a[0].equals(users.get(i).getNumber())) {                             j = 1;                             break;                         }                     }                     if (j == 0 && users.size() != 0)                         users.add(new User(a[0],new MessageCharging()));                 }             }             else if(sc.matches("m-1\\d{10} 1\\d{10} (\\w|\\,|\\.| )+") || sc.matches("m-1\\d{10} 1\\d{10}")){                 a = sc.substring(2).split("\\s");                 for (User e:users){                     if (a[0].equals(e.getNumber())) {                         e.getUserRecords().addSendMessageRecord(new MessageRecord(sc.substring(26,sc.length())));                     }                 }             }             sc = input.nextLine();         }                   Collections.sort(users);                   for (User e:users) {             System.out.print(e.getNumber()+" ");             System.out.printf("%.1f",e.calCost());             System.out.print(" ");             System.out.printf("%.1f",e.calBalance());             System.out.println();         }     } }   class User implements Comparable<User> {     UserRecords userRecords = new UserRecords();     double balance = 100;     ChargeMode chargeMode;     ChargePhone chargePhone;     String number;     public User(){       }       public User(String s, LandlinePhoneCharging landlinePhoneCharging) {         this.number = s;     }       public User(String s, MobilePhoneCharging mobilePhoneCharging) {         this.number = s;     }       public User(String s, MessageCharging messageCharging) {         this.number = s;     }       public double calBalance(){         return balance-calCost();     }       public double calCost() {         MessageChargeRule messageChargeRule = new MessageChargeRule();         return messageChargeRule.calCost(userRecords.sendMessageRecord);     }           public UserRecords getUserRecords(){         return userRecords;     }           public void setUserRecords(UserRecords userRecords){         this.userRecords = userRecords;     }           public double getBalance(){         return balance;     }           public ChargeMode getChargeMode(){         return chargeMode;     }           public void setChargeMode(ChargeMode chargeMode){         this.chargeMode = chargeMode;     }           public String getNumber(){         return number;     }       public ChargePhone getChargePhone() {         return chargePhone;     }       public void setNumber(String number){         this.number = number;     }           public int compareTo(User o) {         return this.getNumber().compareTo(o.getNumber());     }   }   abstract class ChargeMode{     ArrayList<ChargeRule> list = new ArrayList<ChargeRule>();     public ArrayList<ChargeRule> getChargeRule(){         return list;     }     public void setChargeRule(ArrayList<ChargeRule> chargeRule){         this.list = list;     }     public double calCost(UserRecords userRecords){         return 0;     }     public double getMonthlyRent(){         return 0;     } }   abstract class ChargePhone{     ArrayList<ChargeRule> list = new ArrayList<ChargeRule>();     public ArrayList<ChargeRule> getChargeRule(){         return list;     }     public void setChargeRule(ArrayList<ChargeRule> chargeRule){         this.list = list;     }     public double calCost(UserRecords userRecords){         return 0;     }     public double getMonthlyRent(){         return 15;     } }   abstract class ChargeMessage{     ArrayList<ChargeRule> list = new ArrayList<ChargeRule>();     public ArrayList<ChargeRule> getChargeRule(){         return list;     }     public void setChargeRule(ArrayList<ChargeRule> chargeRule){         this.list = list;     }     public double calCost(UserRecords userRecords){         return 0;     }     public double getMonthlyRent(){         return 0;     } }   class UserRecords{     ArrayList<MessageRecord> sendMessageRecord = new ArrayList<MessageRecord>();     ArrayList<MessageRecord> receiveMessageRecord = new ArrayList<MessageRecord>();           public void addSendMessageRecord(MessageRecord sendMessageRecord){         this.sendMessageRecord.add(sendMessageRecord);     }           public void addReceiveMessageRecord(MessageRecord receiveMessageRecord){         this.receiveMessageRecord.add(receiveMessageRecord);     }           public ArrayList<MessageRecord> getSendMessageRecord(){         return sendMessageRecord;     }           public ArrayList<MessageRecord> getReceiveMessageRecord(){         return receiveMessageRecord;     }       }   class LandlinePhoneCharging extends ChargeMode{     double monthlyRent = 20;       public double calCost(UserRecords userRecords){         return 0;     }     public double getMonthlyRent(){         return monthlyRent;     } }   class MobilePhoneCharging extends ChargePhone{     double monthlyRent = 15;       public double calCost(UserRecords userRecords){         return 0;     }     public double getMonthlyRent(){         return monthlyRent;     } }   class MessageCharging extends ChargeMessage{     double monthlyRent = 0;       public double calCost(UserRecords userRecords){         return 0;     }     public double getMonthlyRent(){         return monthlyRent;     } }   class CommunicationRecord{     String callingNumber;     String answerNumber;           public String getCallingNumber(){         return callingNumber;     }           public void setCallingNumber(String callingNumber){         this.callingNumber = callingNumber;     }           public String getAnswerNumber(){         return answerNumber;     }           public void setAnswerNumber(String answerNumber){         this.answerNumber = answerNumber;     } }   class CallRecord extends CommunicationRecord{     Date startTime;     Date endTime;     String callingAddressAreaCode;     String AnswerAddressAreaCode;       public CallRecord(String startTime,String endTime) throws ParseException {         this.startTime =  new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(startTime);         this.endTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(endTime);     }       public Date getStartTime(){         return startTime;     }     public void setStartTime(Date startTime){         this.startTime = startTime;     }     public Date getEndTime(){         return endTime;     }     public void setEndTime(Date endTime){         this.endTime = endTime;     }     public String getCallingAddressAreaCode(){         return callingAddressAreaCode;     }     public void setCallingAddressAreaCode(String callingAddressAreaCode){         this.callingAddressAreaCode = callingAddressAreaCode;     }       public String getAnswerAddressAreaCode() {         return AnswerAddressAreaCode;     }       public void setAnswerAddressAreaCode(String answerAddressAreaCode) {         this.AnswerAddressAreaCode = answerAddressAreaCode;     } }   class MessageRecord extends CommunicationRecord{     String message;       public MessageRecord(String message) {         this.message = message;     }       public String getMessage() {         return message;     }       public void setMessage(String message) {         this.message = message;     } }   class SendMessageRule extends MessageChargeRule{     @Override     public double calCost(ArrayList<MessageRecord> messageRecords) {         return super.calCost(messageRecords);     } }   abstract class ChargeRule{   }   abstract class CallChargeRule {           public double calCost(ArrayList<CallRecord>callRecords){         return 0;     }       }   class MessageChargeRule extends ChargeRule{           public double calCost(ArrayList<MessageRecord> messageRecords) {         double cost = 0;         double count = 0;                   for (int i = 0; i < messageRecords.size(); i++) {             double a = messageRecords.get(i).message.length();                           while (a > 0){                 count++;                 a = a - 10;             }         }                   if(count <= 3){             cost = 0.1 * count;         }         else if(count > 3 && count <= 5){             cost = 0.3 0.2 * (count - 3);         }         else if(count > 5){             cost = 0.7 0.3 * ( count - 5) ;         }                   return cost;     } }

  

类图如下:

 

 

 

 题目分析:

本道题目由于和之前收费并无关系,所以可以将之前很多的代码进行删除,修改,最后进行一个关于短信的收费计算即可,由于其类之间的关系基本没有变化,所以只需要在main函数里面进行修改即可。

采坑心得:

本道题目关于如何将短信收费的计算做好是非常重要的。

7-2 编写一个类Shop(商店)、内部类InnerCoupons(内部购物券)

源码如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 import java.util.Scanner;     public class Main {     public static void main(String[] args) {         Scanner input = new Scanner(System.in);         Shop myshop = new Shop(input.nextInt());         Shop.InnerCoupons in = myshop.new InnerCoupons();         in.buy();     } }   class Shop{     InnerCoupons coupons50 = new InnerCoupons(50);     InnerCoupons coupons100 = new InnerCoupons(100);     private int milkCount;           public Shop() {         super();     }       public Shop(int milkCount) {         super();         this.milkCount = milkCount;     }       public int getMilkCount() {         return milkCount;     }       public void setMilkCount(int milkCount) {         this.milkCount = milkCount;     }           public InnerCoupons getCoupons50() {         return coupons50;     }       public void setCoupons50(InnerCoupons coupons50) {         this.coupons50 = coupons50;     }       public InnerCoupons getCoupons100() {         return coupons100;     }       public void setCoupons100(InnerCoupons coupons100) {         this.coupons100 = coupons100;     }       class InnerCoupons{         int value;                   public InnerCoupons(int value) {             super();             this.value = value;         }                   public InnerCoupons() {                       }           public void buy() {             System.out.println("使用了面值为50的购物券进行支付");             setMilkCount(milkCount - coupons50.value / 50);             System.out.println("牛奶还剩" + getMilkCount() + "箱");             System.out.println("使用了面值为100的购物券进行支付");             setMilkCount(milkCount - coupons100.value / 50);             System.out.println("牛奶还剩" + getMilkCount() + "箱");         }               }             }

  题目分析:

本次题目的难点在于内部类的使用,其余并没有什么难度。

 

7-3 动物发声模拟器(多态)

源码如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 public class Main {   public static void main(String[] args) {               Cat cat = new Cat();        Dog dog = new Dog();              Goat goat = new Goat();        speak(cat);        speak(dog);        speak(goat);   }   //定义静态方法speak()  public static void speak(Animal animal) {       System.out.print(animal.getAnimalClass()+"的叫声:");       animal.shout();   }   }     //定义抽象类Animal abstract class Animal{     public String getAnimalClass() {         return null;     }     public void shout() {               } } //基于Animal类,定义猫类Cat,并重写两个抽象方法 class Cat extends Animal{     @Override     public String getAnimalClass() {         return "猫";     }     @Override     public void shout() {         System.out.println("喵喵");     } } //基于Animal类,定义狗类Dog,并重写两个抽象方法 class Dog extends Animal{     @Override     public String getAnimalClass() {         return "狗";     }     @Override     public void shout() {         System.out.println("汪汪");     } } //基于Animal类,定义山羊类Goat,并重写两个抽象方法 class Goat extends Animal{     @Override     public String getAnimalClass() {         return "山羊";     }     @Override     public void shout() {         System.out.println("咩咩");     } }

题目分析:本道题目进行的简单的多肽使用,并没有什么难度。

采坑心得:获取动物名的时候记得设置String’函数。

 

四:改进建议

对于第八次题目集的作业:资费题目由于给出了类图,并没有什么需要改进的地方。

 

对于第九次题目集第五次作业:资费题目中由于正则表达式出了点错误,所以尽量避免使用正则表达式或者将正则表达式修改正确。

 

对于第十次题目集第二次作业:资费题目未出现什么大的问题,并不需要修改。

 

 

五:总结

通过这几次题目集的练习,从最初对java的简单认知到现在对java有了一定的认知,算是完成了对于java这门课程的入门,但是题目的难度越来越大于,对于自身的能力的提升也应跟随上脚步。并且这几次题目对于正则表达式的训练需要加强,对于类与类之间关系需要有较为清晰的认知,这几次的pta作业由于老师降低了难度,但是对我来说题目的难度依旧是非常的大。除此之外,最近开始了java的实训,其中将学习使用swing去制作界面,所以接下来的java学习更加不能松懈,需要不断的接受新知识来扩充自己,增强自己的能力。

标签:return,String,double,ArrayList,BLOG,new,public
来源: https://www.cnblogs.com/c257819735/p/16383982.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有