首页 > 编程语言 >JAVA面向对象程序设计_PTA题目集07-11总结分析

JAVA面向对象程序设计_PTA题目集07-11总结分析

时间:2023-06-23 19:22:22浏览次数:43  
标签:11 return 07 int ArrayList PTA split public String

JAVA面向对象程序设计_PTA题目集07-11总结分析

前言:

天将降大任于是人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为。所以动心忍性,增益其所不能。

随堂检测在最末浅析。

 

题目集七:

知识点:类间设计,类的设计。

题量:一题,菜单计价五。

难度:八个满分。

 

题目集八:

知识点:类间设计,类的设计。

题量:一题,成绩程序一。

难度:二十一个满分。

 

题目集九:

知识点:处理字符串的基础算法,set,map,正则表达式。

题量:一题,统计Java程序中关键字的出现字数。

难度:五分制的三分。

 

题目集十:

知识点:HashMap检索、排序,多态,类间设计,类的设计。

题量:四题。检索,排序,成绩程序2,多态。

难度:五分制的三点五。

 

题目集十一:

知识点:ArrayList排序,Comparator,自定义接口ArrayIntegerStack,定义PersonOverride类以覆盖toString与equals方法。

题量:五道题,四算法加一个成绩程序设计。

难度:不加成绩程序设计五分制2.9,加了就3.9。作为参考,成绩程序题通过人数为2。

 

设计与分析:

0701

类图:

 

 源码:

  1 import java.text.ParseException;
  2 import java.text.SimpleDateFormat;
  3 import java.util.*;
  4 public class Main {
  5     public static void main(String[] args) throws ParseException {
  6         Scanner in = new Scanner(System.in);
  7 
  8         Menu menu = new Menu();
  9         Order order = new Order();
 10         Record record = new Record();
 11         Client client = new Client();
 12         Dish dish = new Dish();
 13         LocalDate localDate = new LocalDate();
 14         Table table = new Table();
 15         Time time = new Time();
 16 
 17         int i = 0;
 18         int tableindex = 0;
 19         int endindex = 0;
 20         String tableLine;
 21 
 22 
 23         ArrayList<String> information = new ArrayList<>();
 24         ArrayList<String> addMenu = new ArrayList<>();
 25         ArrayList<String> orderDish = new ArrayList<>();
 26         ArrayList<String> endpart = new ArrayList<>();
 27 
 28 
 29         {//将输入数据处理成几部分
 30             //总处理输入
 31 
 32             String temp;
 33             while (true) {
 34                 temp = in.nextLine();//暂时存放信息
 35                 if (temp.equals("end")) {
 36                     break;
 37                 }//end在数组里
 38                 information.add(temp);//每行信息存入数组
 39                 i++;
 40             }
 41             //分出菜谱部分
 42             for (int j = 0; j < information.size(); j++) {
 43                 if (information.get(j).startsWith("table")) {
 44                     tableindex = j;
 45                     break;//table开始说明到了桌号行,结束
 46                 } else {
 47                     addMenu.add(information.get(j));
 48                 }
 49             }
 50             //分出桌号部分
 51             tableLine = information.get(tableindex);
 52             //分出点菜部分
 53             int length = information.size() - addMenu.size();
 54             for (int j = tableindex + 1; j < length; j++) {
 55 //                if (information.get(j) == null) {
 56 //                    break;
 57 //                }
 58                 if (information.get(j).equals("end")
 59                         || information.get(j).startsWith("delete")) {
 60                     endindex = j;
 61                     break;
 62                 } else {
 63                     orderDish.add(information.get(j));
 64                 }
 65             }
 66             //分出删除及结尾部分
 67             int length1 = information.size() - addMenu.size() - orderDish.size();
 68             for (int j= endindex; j < length1 ; j ++) {
 69                 endpart.add(information.get(j));
 70                 if (endpart.get(j).equals("ene")) {
 71                     break;
 72                 }
 73             }
 74         }
 75         {//处理菜谱的记录
 76             for (int j = 0; j < addMenu.size(); j++) {
 77                 if (addMenu.get(j) == null) {
 78                     break;
 79                 }
 80                 if (addMenu.get(j).split(" ").length > 2) {
 81                     //System.out.println(addMenu[j].split(" ").length);
 82                     dish.setName(addMenu.get(j).split(" ")[0]);
 83                     dish.setTaste(addMenu.get(j).split(" ")[1]);
 84                     dish.setUnit_price(Integer.parseInt(addMenu.get(j).split(" ")[2]));
 85                     dish.setKind(addMenu.get(j).split(" ")[3]);
 86 
 87                     menu.addDish(dish.getName(), dish.getUnit_price(), dish.getKind(), dish.getTaste());
 88                 } else {
 89                     //System.out.println(addMenu[j]);
 90                     dish.setName(addMenu.get(j).split(" ")[0]);
 91                     dish.setUnit_price(Integer.parseInt(addMenu.get(j).split(" ")[1]));
 92                     dish.setTaste("无");//无指定口味
 93                     dish.setKind("Normal");//非特色菜,普通
 94                 }
 95             }
 96 //            for (int j = 0; j < addMenu.size(); j ++) {
 97 //                System.out.println(addMenu.get(j));
 98 //            }//存进去且存对了
 99 //            System.out.println(tableLine);//存进去且存对了
100 //            for (int j = 0; j < orderDish.size(); j ++) {
101 //                System.out.println(orderDish.get(j));
102 //            }//没存进去,空的
103 //            for (int j = 0; j < endpart.size(); j ++) {
104 //                System.out.println(endpart.get(j));
105 //            }//存成菜谱和桌子行了,存错了
106 
107         }
108         {//处理桌号行的信息
109             //这一行信息都应该存进去再判断
110             table.setTable(Integer.parseInt(
111                     tableLine.split(" ")[1]));//存桌号
112 
113             client.setName(tableLine.split(" ")[3]);
114            // System.out.println(client.getName());
115             client.setPhonenum(tableLine.split(" ")[4]);
116             //字符串转日期
117             SimpleDateFormat datestring = new SimpleDateFormat("yyyy/MM/dd");
118             localDate.setDate(datestring.parse(
119                     tableLine.split(" ")[5]));//存日期
120             time.setTime(tableLine.split(" ")[6]);//存时间
121             if(time.TimeValidity(time.getTime())){
122                 System.out.println("table " + table.getTable() + ":");
123             }
124             if (!time.TimeValidity(time.getTime())) {
125                 System.out.println("table " + table.getTable() +
126                         " out of opening hours");
127                 System.exit(0);
128             }
129             if (!client.nameValidity()) {
130                 System.out.println("wrong format");
131             }
132             if (!client.phonenumValitidy()) {
133                 System.out.println("wrong format");
134             }
135         }
136         {//处理点菜行的信息
137             for (int j = tableindex + 1; j < orderDish.size(); j++) {
138                 if (orderDish.get(j) == null) {
139                     break;
140                 }
141                 if (menu.searchDish(orderDish.get(j).split(" ")[1]).getName()
142                         == null) {//返回为空,则查找到了对应的菜,即菜不存在
143                     System.out.println(orderDish.get(j).split(" ")[1] +
144                             " does not exist");
145                 } else {
146                     if (orderDish.get(j).length() > 4) {//有口味菜,最后一个数字是口味度
147                         order.addARecord(Integer.parseInt(orderDish.get(j).split(" ")[0]),
148                                 orderDish.get(j).split(" ")[1],
149                                 Integer.parseInt(orderDish.get(j).split(" ")[2]),
150                                 Integer.parseInt(orderDish.get(j).split(" ")[3]),
151                                 Integer.parseInt(orderDish.get(j).split(" ")[4]));
152                         //本条加入的菜记为record1,方便后续取用
153                         Record record1 = order.addARecord(Integer.parseInt(orderDish.get(j).split(" ")[0]),
154                                 orderDish.get(j).split(" ")[1],
155                                 Integer.parseInt(orderDish.get(j).split(" ")[2]),
156                                 Integer.parseInt(orderDish.get(j).split(" ")[3]),
157                                 Integer.parseInt(orderDish.get(j).split(" ")[4]));
158 
159                         if (!order.tasteValitidy(menu.searchDish
160                                 (orderDish.get(j).split(" ")[1]))) {//口味度在范围之外
161                             if (menu.searchDish(orderDish.get(j).split(" ")[1]).
162                                     getTaste().equals("川菜")) {
163                                 System.out.println("spicy num out of rang :" +
164                                         Integer.parseInt(orderDish.get(j).split(" ")[4]));
165                             }
166                             if (menu.searchDish(orderDish.get(j).split(" ")[1]).
167                                     getTaste().equals("晋菜")) {
168                                 System.out.println("acidity num out of rang :" +
169                                         Integer.parseInt(orderDish.get(j).split(" ")[4]));
170                             }
171                             if (menu.searchDish(orderDish.get(j).split(" ")[1]).
172                                     getTaste().equals("浙菜")) {
173                                 System.out.println("sweetness num out of rang :" +
174                                         Integer.parseInt(orderDish.get(j).split(" ")[4]));
175                             }
176                         } else {//口味度不在范围之外
177                             System.out.println(record1.getOrderNum() + " "
178                                     + record1.getD().getName() + " " +
179                                     record1.getD().getPrice(record1.getPortion()) * record1.getNum());
180                         }
181                     } else {
182                         order.addARecord(Integer.parseInt(orderDish.get(j).split(" ")[0]),
183                                 orderDish.get(j).split(" ")[1],
184                                 Integer.parseInt(orderDish.get(j).split(" ")[2]),
185                                 Integer.parseInt(orderDish.get(j).split(" ")[3]),
186                                 -1);//没口味度,记为-1
187                         Record record1 = order.addARecord(Integer.parseInt(orderDish.get(j).split(" ")[0]),
188                                 orderDish.get(j).split(" ")[1],
189                                 Integer.parseInt(orderDish.get(j).split(" ")[2]),
190                                 Integer.parseInt(orderDish.get(j).split(" ")[3]),
191                                 -1);
192                         //记作record1,方便后续取用
193 
194                         System.out.println(record1.getOrderNum() + " "
195                                 + record1.getD().getName() + " " +
196                                 record1.getD().getPrice(record1.getPortion()) * record1.getNum());
197                     }
198                 }
199 
200             }
201 
202         }
203         {//可能会有删除行
204             boolean flagNum = true;
205             for (int j = endindex; j < endpart.size(); j++) {
206                 if (endpart.get(j) == null) {
207                     break;
208                 }
209                 if (endpart.get(j).startsWith("delete")) {
210                     flagNum = false;
211                 }
212                 for (int k = 0; k < orderDish.size(); k++) {
213                     if (orderDish.get(k).split(" ")[0].
214                             equals(endpart.get(j).split(" ")[1])) {//序号存在
215                         flagNum = true;
216                     }
217                 }
218 
219                 if (!flagNum) {
220                     System.out.println("delete error");
221                 } else if (flagNum) {
222                     order.delARecordByOrderNum(record.getOrderNum());
223                 }
224 
225             }
226             {//处理完后输出
227                 System.out.print("table " + table.getTable() + ": "
228                         + order.getTotalPrice(time) +
229                         order.getTotalPrice(time) //??折上折?
230                 );
231                 //区分各个菜类再输出
232                 for (int j = 0; j < orderDish.size(); j++) {
233                     if (order.records.get(i).getD().getTaste().equals("川菜")) {
234                         System.out.println(" 川菜 " + record.getTastelever() +
235                                 " " + order.tasteCharLever(record.getD()));
236                     }
237                     if (order.records.get(i).getD().getTaste().equals("晋菜")) {
238                         System.out.println(" 晋菜 " + record.getTastelever() +
239                                 " " + order.tasteCharLever(record.getD()));
240                     }
241                     if (order.records.get(i).getD().getTaste().equals("浙菜")) {
242                         System.out.println(" 浙菜 " + record.getTastelever() +
243                                 " " + order.tasteCharLever(record.getD()));
244                     }
245                 }
246                 System.out.println(client.getName() + " " + client.getPhonenum() +
247                         " " + order.getTotalPrice(time));//第二个可能是折上折的价钱
248             }
249         }
250 
251     }
252 }
253 
254 
255 class LocalDate {
256     private Date date = new Date();//eg.2023/15/16
257     //private String date;
258     Date date1 = new Date(2022, 1,1);
259     Date date2 = new Date(2023,12,31);
260     public LocalDate() {
261         // TODO Auto-generated constructor stub
262     }
263 
264     public void LocalDate(Date date) {
265         this.date = date;
266     }
267     public Date getDate() {
268         return date;
269     }
270 
271     public void setDate(Date date) {
272         this.date = date;
273     }
274 
275 //    public boolean dateValidity(Date date) {
276 //        if (date.after(date1) && date.before(date2)){
277 //            return true;
278 //        }
279 //        else {
280 //            return false;
281 //        }
282 //    }
283     public int transformWeekday(Date date) {//日期转换为周几
284         int[] weekDays = {1, 2, 3, 4, 5, 6, 7};
285         Calendar cal = Calendar.getInstance();
286         cal.setTime(date);
287         int weekday = cal.get(Calendar.DAY_OF_WEEK) - 1;
288         if (weekday < 0){
289             weekday = 0;
290         }
291         return weekDays[weekday];
292     }
293 }
294 
295 //菜品类:对应菜谱上一道菜的信息。
296  class Dish {
297     private String name;//菜品名称
298     private int unit_price; //单价
299     private String kind;//是否是特色菜
300     private String taste;//口味
301     private int tastelever;//口味度
302     public Dish() {
303     }
304     public void Dish(String name, int unit_price) {
305         this.name = name;
306         this.unit_price = unit_price;
307     }
308 
309     public String getName() {
310         return name;
311     }
312 
313     public void setName(String name) {
314         this.name = name;
315     }
316 
317     public String getTaste() {
318         return taste;
319     }
320 
321     public void setTaste(String taste) {
322         this.taste = taste;
323     }
324 
325     public int getTastelever() {
326         return tastelever;
327     }
328 
329     public void setTastelever(int tastelever) {
330         this.tastelever = tastelever;
331     }
332 
333     public int getUnit_price() {
334         return unit_price;
335     }
336 
337     public void setUnit_price(int unit_price) {
338         this.unit_price = unit_price;
339     }
340 
341     public void setKind(String kind) {
342         this.kind = kind;
343     }
344 
345     public String getKind() {
346         return kind;
347     }
348 
349     public int getPrice(int portion) {//一个菜确定大小后的单价
350         if(portion == 2) {
351             unit_price = (int) (Math.ceil(unit_price * 1.5));
352         }
353         else if(portion == 3) {
354             unit_price = (int) (Math.ceil(unit_price * 2.0));
355         }
356         return unit_price;
357     }//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
358 //    public boolean priceValidity(int unit_price) {
359 //        if (unit_price <= 0 || unit_price >= 300) {
360 //            return false;
361 //        }
362 //        else return true;
363 //    }
364 
365 }
366 
367 //菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
368 class Menu {
369 
370     //Dish[] dish;
371     ArrayList<Dish> dishs = new ArrayList<>();//菜品数组,保存所有菜品信息
372 
373     public Menu() {
374     }
375     public void Menu(ArrayList dishs) {
376         this.dishs = dishs;
377     }
378 
379     public void addDish(String dishName, int unit_price, String kind, String taste) {
380         Dish dishsNew = new Dish();
381         dishsNew.setName(dishName);
382         dishsNew.setUnit_price(unit_price);
383         dishsNew.setKind(kind);
384         dishsNew.setTaste(taste);
385         dishs.add(dishsNew);
386     }//添加一道菜品信息
387     public Dish searchDish(String dishName) {//比较字符串
388         Dish dishSearched = new Dish();
389         for (int i = 0; i < dishs.size(); i ++){
390             if (dishName.equals(dishs.get(i).getName())) {
391                 dishSearched.setName(dishName);
392                 dishSearched.setKind(dishs.get(i).getKind());
393                 dishSearched.setUnit_price(dishs.get(i).getUnit_price());
394                 break;
395             }
396         }
397         return dishSearched;
398     }//根据菜名在菜谱中查找菜品信息,返回Dish对象。
399 
400 
401 }
402 
403 //点菜记录类:保存订单上的一道菜品记录
404 class Record {
405     private int orderNum;//序号
406     public int num;//份数
407     private Dish d = new Dish();//菜品
408     private int portion;//份额(1/2/3代表小/中/大份)
409     private int tastelever;
410 
411     public Record(){
412     }
413     public void Record(int orderNum, int num, Dish d, int portion, int tastelever) {
414         this.d = d;
415         this.num = num;
416         this.portion = portion;
417         this.orderNum = orderNum;
418         this.tastelever = tastelever;
419     }
420 
421     public Dish getD() {
422         return d;
423     }
424 
425     public void setD(Dish d) {
426         this.d = d;
427     }
428 
429     public int getNum() {
430         return num;
431     }
432 
433     public void setNum(int num) {
434         this.num = num;
435     }
436 
437     public void setTastelever(int tastelever) {
438         this.tastelever = tastelever;
439     }
440 
441     public int getTastelever() {
442         return tastelever;
443     }
444 
445     public int getOrderNum() {
446         return orderNum;
447     }
448 
449     public void setOrderNum(int orderNum) {
450         this.orderNum = orderNum;
451     }
452 
453     public int getPortion() {
454         return portion;
455     }
456 
457     public void setPortion(int portion) {
458         this.portion = portion;
459     }
460 
461     public int getPrice() {
462         return d.getPrice(portion);
463     }//计价,计算本条记录的价格//份额后价格
464 
465 //    public int numValidity(int num) {//份数合法
466 //        if(num > 15 || num < 0){
467 //            return 0;
468 //        }
469 //        return 1;
470 //    }
471 //    public int portionValidity(int portion, Dish d) {
472 //        if (d.getKind().equals("T") && (portion == 1 || portion == 3)) {
473 //            return 1;
474 //        }
475 //        else if((portion < 1 || portion > 3) && portion <= 9) {
476 //            return 0;
477 //        }
478 //        else if(portion > 9) {
479 //            return -1;
480 //        }
481 //        return 1;
482 //    }
483 }
484 
485 // 订单类:保存用户点的所有菜的信息。
486 class Order {
487     //Record[] records;
488     ArrayList<Record> records = new ArrayList<>();//保存订单上每一道的记录
489 
490     private int tastelever;//口味度平均值
491 
492     public int getTastelever() {
493         return tastelever;
494     }
495 
496     public void setTastelever(int tastelever) {
497         this.tastelever = tastelever;
498     }
499 
500     Menu menu = new Menu();
501     Client client = new Client();
502 
503     public Order() {
504     }
505 
506     public void Order(ArrayList records, int tastelever) {
507         this.records = records;
508         this.tastelever = tastelever;
509     }
510 
511     public int getTotalPrice(Time time) {
512         double totalprice = 0;
513         int total;
514         int size = records.size();
515         for (int i = 0; i < size; i++) {
516             totalprice = totalprice
517                     + records.get(i).getPrice()
518                     * time.TimeDishCount(time.getTime(), records.get(i).getD());
519         }
520         total = (int) totalprice;
521         return total;
522     }//计算订单的总价
523 
524     public Record addARecord(int orderNum, String dishName,
525                              int portion, int num, int tastelever) {
526         Record recordNew = new Record();
527         recordNew.setOrderNum(orderNum);
528         recordNew.setD(menu.searchDish(dishName));
529         recordNew.setPortion(portion);
530         recordNew.setNum(num);
531         recordNew.setTastelever(tastelever);
532         records.add(recordNew);
533         return recordNew;
534     }//添加一条菜品信息到订单中。
535 
536     public void delARecordByOrderNum(int orderNum) {
537         Record recordNew = new Record();
538         for (int i = 0; i < records.size(); i++) {
539             if (orderNum == records.get(i).getOrderNum()) {
540                 recordNew = records.get(i);
541             }
542         }
543         records.remove(recordNew);
544     }//根据序号删除一条记录
545 
546     public boolean findRecordByNum(int orderNum) {
547         //或者遍历数组找有没有orderNum
548         boolean flag = false;
549         for (int i = 0; i < records.size(); i++) {
550             if (orderNum == records.get(i).getOrderNum()) {
551                 flag = true;
552             }
553         }
554         return flag;
555     }//根据序号查找一条记录;
556 
557     public boolean tasteValitidy(Dish dish) {
558         for (int i = 0; i < records.size(); i++) {
559             if (records.get(i).getD().getTaste().equals("川菜")) {
560                 int sum0 = 0, t = 0;
561                 for (int j = 0; j < records.size(); j++) {
562                     sum0 = sum0 + records.get(i).getD().getTastelever();
563                     t = j;
564                 }
565                 setTastelever(Math.round((float) sum0 / t));
566                 if (getTastelever() < 0 || getTastelever() > 5) {
567                     return false;
568                 }
569             } else if (dish.getTaste().equals("晋菜")) {
570                 int sum1 = 0, t = 0;
571                 for (int j = 0; j < records.size(); j++) {
572                     sum1 = sum1 + records.get(i).getD().getTastelever();
573                     t = j;
574                 }
575                 setTastelever(Math.round((float) sum1 / t));
576                 if (getTastelever() < 0 || getTastelever() > 4) {
577                     return false;
578                 }
579             } else if (dish.getTaste().equals("浙菜")) {
580                     int sum2 = 0, t = 0;
581                     for (int j = 0; j < records.size(); j++) {
582                         sum2 = sum2 + records.get(i).getD().getTastelever();
583                         t = j;
584                     }
585                     setTastelever(Math.round((float) sum2 / t));
586                     if (getTastelever() < 0 || getTastelever() > 3) {
587                         return false;
588                     }
589                 }
590             }
591         return true;
592         }
593 
594 
595 
596     public String tasteCharLever(Dish dish) {//辣酸甜程度值
597         for (int i = 0; i < records.size(); i++) {
598             if (records.get(i).getD().getTaste().equals("川菜")) {
599                 int sum0 = 0, t = 0;
600                 for (int j = 0; j < records.size(); j ++) {
601                     sum0 = sum0 + records.get(i).getD().getTastelever();
602                     t = j;
603                 }
604                 setTastelever(Math.round((float) sum0 / t));
605                 if (getTastelever() == 0) {
606                     return "不辣";
607                 } else if (getTastelever() == 1) {
608                     return "微辣";
609                 } else if (getTastelever() == 2) {
610                     return "稍辣";
611                 } else if (getTastelever() == 3) {
612                     return "辣";
613                 } else if (getTastelever() == 4) {
614                     return "很辣";
615                 } else if (getTastelever() == 5) {
616                     return "爆辣";
617                 }
618             } else if (dish.getTaste().equals("晋菜")) {
619                 int sum1 = 0, t = 0;
620                 for (int j = 0; j < records.size(); j ++) {
621                     sum1 = sum1 + records.get(i).getD().getTastelever();
622                     t = j;
623                 }
624                 setTastelever(Math.round((float) sum1 / t));
625                 if (getTastelever() == 0) {
626                     return "不酸";
627                 } else if (getTastelever() == 1) {
628                     return "微酸";
629                 } else if (getTastelever() == 2) {
630                     return "稍酸";
631                 } else if (getTastelever() == 3) {
632                     return "酸";
633                 } else if (getTastelever() == 4) {
634                     return "很酸";
635                 }
636             } else if (dish.getTaste().equals("浙菜")) {
637                 int sum2 = 0, t = 0;
638                 for (int j = 0; j < records.size(); j ++) {
639                     sum2 = sum2 + records.get(i).getD().getTastelever();
640                     t = j;
641                 }
642                 setTastelever(Math.round((float) sum2 / t));
643                 if (getTastelever() == 0) {
644                     return "不甜";
645                 } else if (getTastelever() == 1) {
646                     return "微甜";
647                 } else if (getTastelever() == 2) {
648                     return "稍甜";
649                 } else if (getTastelever() == 3) {
650                     return "甜";
651                 }
652             }
653         }
654         return null;
655     }
656 }
657 class Table {
658     int table;
659 
660     public Table() {
661     }
662     public void Table() {
663         this.table = table;
664     }
665     public int getTable() {
666         return table;
667     }
668 
669     public void setTable(int table) {
670         this.table = table;
671     }
672 //    public int tableValidity(int table) {
673 //        if(table < 1 || table > 55){
674 //            return 0;
675 //        }
676 //        return 1;
677 //    }
678 }
679 
680 class Time {
681     private String time;
682 
683     public Time() {
684     }
685 
686 
687     public void Time() {
688         this.time = time;
689     }
690 
691     public void setTime(String time) {
692         this.time = time;
693     }
694 
695     public String getTime() {
696         return time;
697     }
698 
699     public boolean TimeValidity(String time) {//正确的不用改
700         String[] times = time.split("/");
701         LocalDate localDate = new LocalDate();
702         if ((localDate.transformWeekday(localDate.getDate()) >= 1
703                 && localDate.transformWeekday(localDate.getDate()) <= 5) &&
704                 ((Integer.parseInt(times[0]) == 10 && Integer.parseInt(times[1]) >= 30)
705                         || (Integer.parseInt(times[0]) > 10 && Integer.parseInt(times[0]) < 14)
706                         || (Integer.parseInt(times[1]) <= 30) && Integer.parseInt(times[0]) == 14)) {
707             return true;
708         }//周一至周五中午,六折
709         else if ((localDate.transformWeekday(localDate.getDate()) >= 1
710                 && localDate.transformWeekday(localDate.getDate()) <= 5) &&
711                 ((Integer.parseInt(times[0]) >= 17 && Integer.parseInt(times[0]) < 20)
712                         || (Integer.parseInt(times[0]) == 20 && Integer.parseInt(times[1]) <= 30))) {
713             return true;
714         }//周一至周五晚上,八折
715         else if ((localDate.transformWeekday(localDate.getDate()) >= 6)
716                 && ((Integer.parseInt(times[0]) == 9 && Integer.parseInt(times[1]) >= 30)
717                 || (Integer.parseInt(times[0]) > 9 && Integer.parseInt(times[0]) < 21)
718                 || (Integer.parseInt(times[0]) == 21 && Integer.parseInt(times[1]) <= 30))) {
719             return true;
720         }//周末营业时间,全价
721         else return false;
722     }
723 
724     public double TimeDishCount(String time, Dish dish) {
725         //不同营业时间普通和特色菜的折扣
726         String[] times = time.split("/");
727         LocalDate localDate = new LocalDate();
728         if ((localDate.transformWeekday(localDate.getDate()) >= 1
729                 && localDate.transformWeekday(localDate.getDate()) <= 5) &&
730                 ((Integer.parseInt(times[0]) == 10 && Integer.parseInt(times[1]) >= 30)
731                         || (Integer.parseInt(times[0]) > 10 && Integer.parseInt(times[0]) < 14)
732                         || (Integer.parseInt(times[1]) <= 30) && Integer.parseInt(times[0]) == 14)) {
733             if (dish.getKind().equals("T")) {
734                 return 0.7;
735             } else return 1;
736         }//周一至周五
737         else if (((localDate.transformWeekday(localDate.getDate()) >= 1)
738                 && (localDate.transformWeekday(localDate.getDate()) <= 5)) &&
739                 (((Integer.parseInt(times[0]) >= 17) && (Integer.parseInt(times[0]) < 20))
740                         || ((Integer.parseInt(times[0]) == 20) && (Integer.parseInt(times[1]) <= 30)))) {
741             if (dish.getKind().equals("T")) {
742                 return 0.7;
743             } else return 1;
744         }//周一至周五晚上,八折
745         else if ((localDate.transformWeekday(localDate.getDate()) >= 6)
746                 && ((Integer.parseInt(times[0]) == 9 && Integer.parseInt(times[1]) >= 30)
747                 || (Integer.parseInt(times[0]) > 9 && Integer.parseInt(times[0]) < 21)
748                 || (Integer.parseInt(times[0]) == 21 && Integer.parseInt(times[1]) <= 30))) {
749             return 1.0;
750         }//周末营业时间,全价
751         else return 0;
752     }
753 }
754 class Client {
755     private String name;//客户姓名
756     private String phonenum;//手机号
757 
758     public String getName() {
759         return name;
760     }
761 
762     public void setName(String name) {
763         this.name = name;
764     }
765 
766     public String getPhonenum() {
767         return phonenum;
768     }
769 
770     public void setPhonenum(String phonenum) {
771         this.phonenum = phonenum;
772     }
773     Client () {
774     }
775     Client(String name, String phonenum) {
776         this.name = name;
777         this.phonenum = phonenum;
778     }
779     public boolean nameValidity() {
780         if(getName().length() > 10) {
781             return false;
782         }
783         else return true;
784     }
785     public boolean phonenumValitidy() {
786         if (getPhonenum().startsWith("180")
787         || getPhonenum().startsWith("181")
788         || getPhonenum().startsWith("189")
789         || getPhonenum().startsWith("133")
790         || getPhonenum().startsWith("135")
791         || getPhonenum().startsWith("136")) {
792             return true;
793         }
794         else return false;
795     }
796 }

 

分析:

还是菜单。

自己写真的稀巴烂。回顾了一下之前写的代码发现一个一直忽略了的点,有些set我没有用或者没有用对,以及类中方法的返回类型设置有误。又去逛了下博客园别人的代码,决定学习先统一输入后拆分为不同部分,再分别做判断。规避了之前连拆分部分都没做好的问题,各类各方法编写存在的问题让笔者坚定了做不来题问题出在笔者身上而非题目身上的观点。

 

0901

源码:

  1 import java.util.*;
  2 import java.util.regex.*;
  3 
  4 public class Main {
  5     public static void main(String[] args) {
  6         Scanner in = new Scanner(System.in);
  7 
  8         Map<String, Integer> map = new TreeMap<>();
  9         Set<String> set = new TreeSet<>(Arrays.asList(
 10                 "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue",
 11                 "default", "do", "double", "else", "enum", "extends", "false", "final", "finally", "float", "for", "goto",
 12                 "if", "implements", "import", "instanceof", "int", "interface",  "long", "native", "new", "null", "package",
 13                 "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized",
 14                 "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while"
 15         ));
 16 
 17         for (String value: set) {// map初始化
 18             map.put(value, 0);
 19         }
 20 
 21         String temp;// 临时
 22         ArrayList<String> information = new ArrayList<>();// 所有输入
 23         
 24         do {
 25             temp = in.nextLine().trim();
 26             information.add(temp);
 27         }while(!temp.equals("exit"));
 28         if (information.size() == 1) {
 29             System.out.println("Wrong Format");
 30         }
 31         else {
 32             dealPrint(information, map);
 33         }
 34     }
 35     private static void dealPrint(ArrayList<String> information,  Map<String, Integer> map) {
 36         ArrayList<String> infor = dealStrings(information);
 37         ArrayList<String> dealedInfor = new ArrayList<>();
 38 
 39         Pattern pattern = Pattern.compile(   //"\\|\\{|}|;|\\.|\\(|\\)|\\[|]|,|\"|\\\\");
 40                 "[,. *'();\\[\\]{}]");
 41         for (String value: infor) {
 42             Matcher matcher = pattern.matcher(value);
 43             for (int i = 0; i < value.length(); i ++) {
 44                 if (matcher.find()) {// 所有符号替换为空格,放进新信息列
 45                     value = value.replace(matcher.group(), " ");
 46                 }
 47             }
 48             dealedInfor.add(value);
 49         }
 50         for (String value: dealedInfor) {
 51             for (String str: value.split(" ")) {
 52                 if (map.containsKey(str)) {
 53                     map.replace(str, map.get(str) + 1);
 54                 }
 55             }
 56         }
 57         map.forEach((key, value) -> {
 58             if (value != 0){
 59                 System.out.println(value + "\t" + key);
 60             }
 61         });
 62     }
 63     private static ArrayList<String> dealStrings(ArrayList<String> information) {
 64         ArrayList<String> infro = dealExplanatory(information);
 65         ArrayList<String> dealedInfor = new ArrayList<>();
 66         for (String value: infro) {// 去掉""
 67             value = value.replaceAll("\"(.*?)\"", " ");
 68             dealedInfor.add(value);
 69         }
 70         return dealedInfor;
 71     }
 72     private static ArrayList<String> dealExplanatory(ArrayList<String> information) {
 73         ArrayList<String> dealedInfor = new ArrayList<>();
 74         int temp = 0;
 75         for (String value: information) {
 76             if (value.contains("//")) {// 去掉“//”
 77                 value = value.split("//")[0];// value.contain("//") || 后面有关键字就考虑不到了
 78             }
 79             dealedInfor.add(value);
 80         }// 到这里是正确的
 81         for (int i = 0; i < dealedInfor.size(); i ++) {
 82             if(dealedInfor.get(i).equals("")) {
 83                 dealedInfor.remove(i);
 84             }
 85             if (dealedInfor.get(i).startsWith("/*")) {
 86                 dealedInfor.remove(i);// 移除之后,list的容量变了,每行信息对应的标号也变了
 87                 temp = i;
 88                 for (int j = 0; j < dealedInfor.size(); j ++) {
 89                     dealedInfor.remove(temp);
 90                     if (dealedInfor.get(temp).contains("*/")) {// 这行的情况
 91                         dealedInfor.remove(temp);
 92                         break;
 93                     }
 94                 }
 95                 i = temp;
 96             }
 97         }
 98         return dealedInfor;
 99     }
100 }

 

分析:

按照要求使用了set和map,set存放所有关键字,再储存到map里以键对值匹配出现次数。调试中出现的问题有
1.正则表达式的使用
2.处理两种注释,其中处理/**/时循环去除其中内容修改多次
3.去除字符串内容
4.依据题给样例测试通过了测试点,自测了几份代码也与idea的结果符合,但题给测试点过不了

5.重新多调了几份代码,把逻辑上的缺漏修正。且和室友沟通,发现问题出在没有理解清楚题目,经室友解释后程序运行正确,但代码长度过长……真的笔者想不出来几十行就能干完的是什么品种的大学生啊。

 

1001

源码:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        String perLine;
        Map<String, String> studentsInfor = new HashMap<>();
        do{
           perLine = in.nextLine();
           if (perLine.split(" ").length == 3) {
                String key = perLine.split(" ")[0];
                String value = perLine.split(" ")[1] +" " + perLine.split(" ")[2];
                studentsInfor.put(key, value);
           }
        }while(!perLine.equals("end"));

        String searchedID;

        searchedID = in.nextLine();

        if (studentsInfor.containsKey(searchedID)) {
            System.out.println(searchedID + " " + studentsInfor.get(searchedID));
        }
        else {
            System.out.println("The student " + searchedID + " does not exist");
        }
    }
}

 

分析:本题是针对HashMap的训练。较为简单,基于题目所给提示“学号是每个学生的唯一识别号,互不相同“,可知可将学号作为检索学生的关键,将学号存储为键值,使用HashMap中的containKey()函数即可。

 

1002

源码:

 1 import java.util.*;
 2 import java.util.stream.Collectors;
 3 
 4 public class Main {
 5     public static void main(String[] args) {
 6         Scanner in = new Scanner(System.in);
 7 
 8         String perLine;
 9         Map<Integer, String> stuInforMap = new HashMap<>();
10         do{
11             perLine = in.nextLine();
12             if (perLine.split(" ").length == 3) {
13                 int key = Integer.valueOf(perLine.split(" ")[0]);
14                 String value = perLine.split(" ")[1] +" " + perLine.split(" ")[2];
15                 stuInforMap.put(key, value);
16             }
17         }while(!perLine.equals("end"));
18 
19 
20 //        stuInforMap.entrySet().stream().sorted(
21 //                (e1, e2) -> e2.getKey().compareTo(e1.getKey())).forEach(System.out::println);
22 //        Map<Integer, String> sortedMap = stuInforMap.entrySet()
23 //                .stream() // 做不到降序
24 //                .sorted(Map.Entry.comparingByKey())
25 //                .collect(Collectors
26 //                        .toMap(
27 //                                Map.Entry::getKey,
28 //                                Map.Entry::getValue,
29 //                                (e1, e2) -> e1,
30 //                                LinkedHashMap::new));
31 
32         List<Map.Entry<Integer, String>> list = new ArrayList<>(stuInforMap.entrySet());
33         Collections.sort(list, new Comparator<Map.Entry<Integer, String>>() {
34             public int compare(Map.Entry<Integer, String> e1,
35                                Map. Entry<Integer, String> e2) {
36                 return e2.getKey().compareTo(e1.getKey());
37             }
38         });
39 
40         for (int i = 0; i < list.size(); i ++) {
41             System.out.println(list.get(i).getKey() + " " + list.get(i).getValue());
42         }
43     }
44 }

 

分析:利用HashMap实现排序,存储数据方法同1001。难处在于实现降序排序,查询资料获得了多种方法并一一进行了尝试。仅仅使用Map的方法未能实现降序,故选择了借助List的方法以实现算法。

 

1101

源码:

 1 import java.util.*;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner in = new Scanner(System.in);
 6 
 7         String temp;
 8         ArrayList<String> students = new ArrayList<>();
 9 
10         do {
11             temp = in.nextLine();
12             students.add(temp);
13         } while(!temp.equals("end"));
14 
15         Map<String, Integer> map = new HashMap<>();
16         int math;
17         int physcial;
18         int sum;
19         String stuInfor;
20 
21         for (String per: students) {
22             if (per.equals("end")) {
23                 break;
24             }
25             math = Integer.parseInt(per.split(" ")[2]);
26             physcial = Integer.parseInt(per.split(" ")[3]);
27             sum = math + physcial;
28             stuInfor = per.split(" ")[0] + " " + per.split(" ")[1];
29             map.put(stuInfor, sum);
30         }
31 
32         List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
33         Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
34             @Override
35             public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
36                 return o2.getValue().compareTo(o1.getValue());
37             }
38         });
39         for (int i = 0; i < list.size(); i ++) {
40             System.out.println(list.get(i).getKey() + " " + list.get(i).getValue());
41         }
42 
43     }
44 }

 

分析:

基于1001和1002的分支变式。结合使用即可,更改的地方是将键值存储为“学号+英文空格+姓名”,对值存储为总成绩。另一种思路是另开一个Student类,将学号、姓名、单科及总成绩都以Student类表示,键对值存储此类。基于这几次练习,笔者注意到Map类的使用关键就是键对值的选择存放,刚上手时尤其陌生,但熟悉后能正常操作。

 

1103

源码:

  1 import java.util.*;
  2 
  3 public class Main {
  4     public static void main(String[] args) {
  5         Scanner in = new Scanner(System.in);
  6 
  7         int num = Integer.parseInt(in.nextLine());
  8         ArrayList<String> identities = new ArrayList<>();
  9         String temp;
 10 
 11         for (int i = 0; i < num; i ++) {
 12             temp = in.nextLine();
 13             identities.add(temp);
 14         }// 身份证全输入
 15         String choice ;//= in.nextLine();
 16         do {
 17             choice = in.nextLine();
 18             if (choice.equals("sort1")) {
 19                 Sort1(identities);
 20             }
 21             else if (choice.equals("sort2")) {
 22                 Sort2(identities);
 23             }
 24             else {
 25                 System.out.println("exit");
 26                 System.exit(0);// 退出
 27             }
 28             //choice = in.nextLine();
 29         }while (in.hasNext());
 30 
 31     }
 32     private static void Sort1(ArrayList<String> list) {
 33         ArrayList<DateSort1> dateSort1s = new ArrayList<>();
 34         for (String per: list) {
 35             DateSort1 dateSort1 = new DateSort1();
 36             dateSort1.setDate(Integer.parseInt(per.substring(6, 14)));
 37             dateSort1s.add(dateSort1);
 38         }
 39         dateSort1s.sort(new DateSort1());
 40         for (DateSort1 per: dateSort1s) {
 41             System.out.println(String.valueOf(per.getDate()).substring(0, 4)
 42             + "-" + String.valueOf(per.getDate()).substring(4, 6)
 43             + "-" + String.valueOf(per.getDate()).substring(6, 8));
 44         }
 45     }
 46     private static void Sort2(ArrayList<String> list) {
 47         ArrayList<DateSort2> dateSort2s = new ArrayList<>();
 48         for (String per: list) {
 49             DateSort2 dateSort2 = new DateSort2();
 50             dateSort2.setDate(Integer.parseInt(per.substring(6, 14)));
 51             dateSort2.setInfor(per);
 52             dateSort2s.add(dateSort2);
 53         }
 54         dateSort2s.sort(new DateSort2());
 55         for (DateSort2 per: dateSort2s) {
 56             System.out.println(per.getInfor());
 57         }
 58     }
 59 }
 60 class DateSort2 implements Comparator<DateSort2> {
 61     DateSort2() {
 62 
 63     }
 64     DateSort2(int date, String infor) {
 65         this.date = date;
 66         this.infor = infor;
 67     }
 68     private int date;
 69     private String infor;
 70 
 71     public void setInfor(String infor) {
 72         this.infor = infor;
 73     }
 74 
 75     public String getInfor() {
 76         return infor;
 77     }
 78 
 79     public void setDate(int date) {
 80         this.date = date;
 81     }
 82 
 83     public int getDate() {
 84         return date;
 85     }
 86 
 87     @Override
 88     public int compare(DateSort2 o1, DateSort2 o2) {
 89         return Integer.compare(o1.date, o2.getDate());
 90     }
 91 }
 92 class DateSort1 implements Comparator<DateSort1> {
 93     DateSort1() {
 94 
 95     }
 96 
 97     private int date;
 98     DateSort1(int date) {
 99         this.date = date;
100     }
101 
102     public void setDate(int date) {
103         this.date = date;
104     }
105 
106     public int getDate() {
107         return date;
108     }
109 
110     @Override
111     public int compare(DateSort1 o1, DateSort1 o2) {
112         return Integer.compare(o1.date, o2.getDate());
113     }
114 }

分析:

身份证排序,依据题目给的显然提示,写两个方法即sort1和sort2.又经室友帮助,为这两种排序新设计两个类,即DateSort1和DateSort2,以实现比较接口Comparator<DateSort1>和Comparator<DateSort2>。

其中出现的问题及解决方法:

1.未设计清楚实现多个选择的循环输入:经过多次改变choice = in.nextLine()的位置并进行调试即可确认。

2.compare方法的覆盖:在实现接口的抬头加上<DateSort1><DateSort2>即可。

3.生日大小的比较:第一反应是之前写日期系列题时的判断方法,分年月日再一层一层比较,写了一半发现直接按年月日八位数的值来比大小就可,遂改。

 

1104

源码:

  1 import java.util.Arrays;
  2 import java.util.Scanner;
  3 
  4 public class Main {
  5     public static void main(String[] args) {
  6         Scanner in = new Scanner(System.in);
  7 
  8 
  9         int num = in.nextInt();
 10         ArrayIntergerStack arrayIntergerStack = new ArrayIntergerStack(num);
 11 
 12         int numIn = in.nextInt();
 13         for (int i = 0; i < numIn; i ++) {
 14             int temp = in.nextInt();
 15             //arrayIntergerStack.push(temp);
 16             System.out.println(arrayIntergerStack.push(temp));
 17         }
 18 
 19         System.out.println(arrayIntergerStack.peek() + ","
 20                 + arrayIntergerStack.empty() + ","
 21                 + arrayIntergerStack.size());
 22         System.out.println(Arrays.toString(arrayIntergerStack.getIntegers()));
 23 
 24         int numOut = in.nextInt();
 25         for (int i = 0; i < numOut; i ++) {
 26             System.out.println(arrayIntergerStack.pop());
 27         }
 28         System.out.println(arrayIntergerStack.peek() + ","
 29                 + arrayIntergerStack.empty() + ","
 30                 + arrayIntergerStack.size());
 31         System.out.println(Arrays.toString(arrayIntergerStack.getIntegers()));
 32 
 33 
 34     }
 35 }
 36 interface IntegerStack {
 37     public Integer push(Integer item);
 38 //如果item为null,则不入栈直接返回null。如果栈满,也返回null。如果插入成功,返回item。
 39 
 40     public Integer pop();   //出栈,如果为空,则返回null。出栈时只移动栈顶指针,相应位置不置为null
 41     public Integer peek();  //获得栈顶元素,如果为空,则返回null.
 42     public boolean empty(); //如果为空返回true
 43     public int size();      //返回栈中元素个数
 44 }
 45 class ArrayIntergerStack implements IntegerStack {
 46     ArrayIntergerStack() {
 47 
 48     }
 49     private Integer[] integers;
 50     private int max, index = 0;
 51     public ArrayIntergerStack(int num) {
 52         integers = new Integer[num];
 53         max = num;
 54     }
 55     ArrayIntergerStack(Integer[] integers, int max, int index) {
 56         this.integers = integers;
 57         this.max = max;
 58         this.index = index;
 59     }
 60 
 61     public Integer[] getIntegers() {
 62         return integers;
 63     }
 64 
 65     @Override
 66     public Integer push(Integer item) {
 67         if (item == null || index == max) {
 68             return null;
 69         }
 70         else {
 71             integers[index] = item;
 72             index++;
 73             return item;
 74         }
 75     }
 76 
 77     @Override
 78     public Integer pop() {
 79         if (index == 0) {
 80             return null;
 81         }
 82         else {
 83             index --;
 84             return integers[index];
 85         }
 86     }
 87 
 88     @Override
 89     public Integer peek() {
 90         if (index == 0) {
 91             return null;
 92         }
 93         else return integers[index - 1];// !!!!
 94     }
 95 
 96     @Override
 97     public boolean empty() {
 98         if (index == 0) {
 99             return true;
100         }
101         else return false;
102     }
103 
104     @Override
105     public int size() {
106         return index;
107     }
108 }

 

分析:

自定义接口ArrayIntegerStack。哇我真的一开始拿到写了两下品了两下是完全写不来。主方法是正常写,但ArrayIntegerStack一开始完全是无从下手。依照题给,方法只有push有参数且第一步为“输入n,建立可包含n个元素的ArrayIntegerStack对象”,可推断ArrayIntegerStack类中应有一个数组属性和一个int的num属性。但为方便初始化

ArrayIntegerStack故将其放作ArrayIntergerStack arrayIntergerStack = new ArrayIntergerStack(num)。

接着以各方法的需求推出别的需要的属性以及方法的书写。其中peek()方法存在的问题是返回值的数组位置,基于初始值和别的方法使用的值,peek()的返回的数组位置应该是index-1。

 

 

1105

源码:

  1 import java.util.ArrayList;
  2 import java.util.Arrays;
  3 import java.util.Scanner;
  4 
  5 public class Main {
  6     public static void main(String[] args) {
  7         Scanner in = new Scanner(System.in);
  8         int n1 = Integer.parseInt(in.nextLine());
  9         int n2 = Integer.parseInt(in.nextLine());
 10 
 11         ArrayList<PersonOverride> personOverrides = new ArrayList<>();
 12         ArrayList<PersonOverride> personOverrides1 = new ArrayList<>();
 13         ArrayList<String> infor = new ArrayList<>();
 14         String t;
 15         int i = 0;
 16 
 17         do {
 18             t = in.nextLine();
 19             infor.add(t);
 20             i ++;
 21         } while (i != n2);
 22 
 23         for (int j = 0; j < n1; j ++) {
 24             personOverrides.add(new PersonOverride());
 25         }
 26 
 27 
 28         for (String temp: infor) {
 29             boolean flag = true;
 30             String name = temp.split(" ")[0];
 31             int age = Integer.parseInt(temp.split(" ")[1]);
 32             boolean gender = Boolean.parseBoolean(temp.split(" ")[2]);
 33             PersonOverride personOverride = new PersonOverride(
 34                     name, age, gender
 35             );
 36             for (PersonOverride per: personOverrides1) {
 37                 if (per.equals(personOverride)) {
 38                     flag = false;
 39                     break;
 40                 }
 41             }
 42             if (flag) {
 43                 personOverrides1.add(personOverride);
 44             }
 45         }
 46 
 47         if (personOverrides.isEmpty()) {
 48             System.out.println("default-1-true");
 49         }
 50         else {
 51             for (PersonOverride per: personOverrides) {
 52                 System.out.println(per.toString());
 53             }
 54         }
 55 
 56         if (personOverrides1.isEmpty()) {
 57             System.out.println("default-1-true");
 58         }
 59         else {
 60             for (PersonOverride per: personOverrides1) {
 61                 System.out.println(per.toString());
 62             }
 63         }
 64 
 65         System.out.println(personOverrides1.size());
 66         System.out.println(Arrays.toString(
 67                 PersonOverride.class.getConstructors()));
 68     }
 69 }
 70 class PersonOverride {
 71     private final String name;
 72     private final int age;
 73     private final boolean gender;
 74     public PersonOverride() {
 75         this.name = "default";
 76         this.age = 1;
 77         this.gender = true;
 78     }
 79     public PersonOverride(String name, int age, boolean gender) {
 80         this.age = age;
 81         this.gender = gender;
 82         this.name = name;
 83     }
 84 
 85 
 86 
 87     @Override
 88     public String toString() {
 89         return name + "-" + age + "-" + gender;
 90     }
 91 
 92     @Override
 93     public boolean equals(Object o) {
 94         if (this == o) {
 95             return true;
 96         }
 97         else if (o == null || getClass() != o.getClass()) {
 98             return false;
 99         }
100         PersonOverride that = (PersonOverride) o;
101         return that.age == age &&
102                 that.gender == gender &&
103                 that.name.equals(name);
104     }
105 }

 

分析:

覆盖。定义PersonOverride类并覆盖其toString与equals方法。

这题是啊啊写起来感觉很好上手诶难点不多诶都能写诶,一运行一调试天哪就稀巴烂,题目读了很多遍把每一种可能的意思都推了一遍,不确定的又试了一遍才确定这题应该有的思路,非常不知道是笔者的理解能力出了问题还是——基于这题定价五分好吧那确实是笔者个人的问题。

遇见的问题及解决方法:

1.没有理解题意:读题more and more,书读百遍其意自现把两划不到的题读得快要背下来了又试来试去终于搞懂这题的意思。侧面反映了笔者的才疏学浅甚至完全无才。

2.equals(Object o)方法覆盖的书写:对笔者来说是个很新鲜的行当新鲜到哇塞没有这题getClass()根本没有用到过,像所有翻书的时候会用上但是考试时死都想不起来的高数公式,说谢谢PTA日常题目集可以翻书。笔者一开始写的时候只返回了三个都相等的ture情况而忽略了false和this == o的ture情况,求助室友,求助互联网,遂改之。

 

0801-1003-1102

类图:

 

源码:

  1 import java.util.*;
  2 
  3 public class Main {
  4     public static void main(String[] args) {
  5         Scanner in = new Scanner(System.in);
  6 
  7         DealInfor dealInfor = new DealInfor();
  8 
  9         String temp;
 10         ArrayList<String> inforAll = new ArrayList<>();
 11         ArrayList<String> courInfor = new ArrayList<>();
 12         ArrayList<String> stuSelInfor = new ArrayList<>();
 13 
 14         do {
 15             temp = in.nextLine();
 16             inforAll.add(temp);
 17         } while (!temp.equals("end"));// 所有信息录入
 18 
 19         for (String item : inforAll) {
 20             if (item.split(" ").length == 3) {
 21                 courInfor.add(item);
 22             }// 课程信息
 23             else if (item.split(" ").length != 3) {
 24                 stuSelInfor.add(item);
 25             } // 学生选课信息
 26             else break;// end也会存进去
 27         }
 28         dealInfor.dealCourInfor(courInfor);
 29         dealInfor.dealStuSelInfor(stuSelInfor);
 30     }
 31 }
 32 class DealInfor {
 33     DealInfor() {
 34     }
 35 
 36     School school = new School();
 37     // 输出课程的格式错误,正确则加入课程列表
 38     public void dealCourInfor(ArrayList<String> courInfor) {
 39 
 40         Map<String, Integer> tempMap = new HashMap<>();
 41         ArrayList<Course> tempCourss = new ArrayList<>();
 42 
 43         String tempName;
 44         String tempNature;
 45         String tempAss;
 46         for (String str: courInfor) {
 47             Course dealingCour = new Course();
 48             tempName = str.split(" ")[0];
 49             tempNature = str.split(" ")[1];
 50             tempAss = str.split(" ")[2];
 51             if (!dealingCour.CourNameValitidy(tempName)) {
 52                 System.out.println("wrong format");
 53             }
 54             else if (!dealingCour.JudgeNA(tempNature, tempAss)) {
 55                 System.out.println(tempName + " : course type & access mode mismatch");
 56             }
 57             else {
 58                 dealingCour.setCourName(tempName);
 59                 dealingCour.setNature(tempNature);
 60                 dealingCour.setAssMent(tempAss);
 61             }
 62             tempCourss.add(dealingCour);
 63             tempMap.put(tempName, 0);
 64             // 课程列表存错了——已经调好
 65         }
 66         school.setAllCour(tempCourss);
 67         school.setCouInfor(tempMap);
 68     }
 69 
 70     public void dealStuSelInfor(ArrayList<String> StuSelInfor) {
 71         AssScore assScore = new AssScore();
 72         ExamScore examScore = new ExamScore();
 73         ExperiScore experiScore = new ExperiScore();
 74         ArrayList<Course> couAL = new ArrayList<>();
 75         ArrayList<Integer> stuScoreTemp = new ArrayList<>();
 76         Map<Integer, String> tempStuMap = new HashMap<>();// 所有学生信息和成绩
 77 
 78         String tempID;
 79         String tempStuName;
 80         String tempSelCou;
 81         for (String per: StuSelInfor) {
 82             Student student = new Student();
 83 
 84             if (per.equals("end")){
 85                 break;
 86             }
 87             else {
 88                 tempID = per.split(" ")[0];
 89                 tempStuName = per.split(" ")[1];
 90                 tempSelCou = per.split(" ")[2];
 91                 if (!student.IDValidity(tempID) || !student.NameValidity(tempStuName)) {
 92                     System.out.println("wrong format");
 93                 }
 94                 else if (!school.getCouInfor().containsKey(tempSelCou)) {
 95                     System.out.println(tempID + " " +
 96                             tempStuName + " :" + tempSelCou + " doed not exist");
 97                 }
 98                 else {
 99                     if (per.split(" ").length == 4) {// 正确为考察
100                         if (!searchCourse(tempSelCou, school.getAllCour()).
101                                 getAssMent().equals("考察")) {// 不匹配
102                             System.out.println(tempID + " " + tempStuName +
103                                     " : access mode mismatch");
104                             tempStuMap.put(Integer.valueOf(tempID),
105                                     tempStuName + " " + 0
106                                             + " " + searchCourse(tempSelCou, school.getAllCour()).getCourName());
107                         }
108                         else if (!assScore.ScoreValidity(0,
109                                 Integer.parseInt(per.split(" ")[3]), null)) {
110                             System.out.println("wrong format");
111                             tempStuMap.put(Integer.valueOf(tempID),
112                                     tempStuName + " " + 0
113                                             + " " + searchCourse(tempSelCou, school.getAllCour()).getCourName());
114                         }
115                         else {
116                             int tempScore = (int)Integer.parseInt(per.split(" ")[3]);
117                             // 学生基本信息set进学生
118                             student.setID(tempID);
119                             student.setName(tempStuName);
120                             // 计算学生一门或多门课的总评————拉个列表
121                             couAL.add(searchCourse(tempSelCou, school.getAllCour()));
122                             student.setSelectedCourse(couAL);
123                             stuScoreTemp.add(tempScore);
124                             // 对应课程设置学生成绩列表
125                             searchCourse(tempSelCou, school.getAllCour()).setStuScore(stuScoreTemp);
126                             tempStuMap.put(Integer.valueOf(tempID),
127                                     tempStuName + " " + per.split(" ")[3]
128                                             + " "+ per.split(" ")[3]
129                                             + " " + searchCourse(tempSelCou, school.getAllCour()).getCourName());
130                         }
131                     }
132                     else if (per.split(" ").length == 5) {// 正确为考试
133 
134                         if (!searchCourse(tempSelCou, school.getAllCour()).
135                                 getAssMent().equals("考试")) {// 不匹配
136                             System.out.println(tempID + " " + tempStuName +
137                                     " : access mode mismatch");
138                             tempStuMap.put(Integer.valueOf(tempID),
139                                     tempStuName + " " + 0
140                                             + " " + searchCourse(tempSelCou, school.getAllCour()).getCourName());
141                         }
142                         else if (!examScore.ScoreValidity(Integer.parseInt(per.split(" ")[3]),
143                                 Integer.parseInt(per.split(" ")[4]), null)) {
144                             System.out.println("wrong format");
145                             tempStuMap.put(Integer.valueOf(tempID),
146                                     tempStuName + " " + 0
147                                             + " " + searchCourse(tempSelCou, school.getAllCour()).getCourName());
148                         }
149                         else {// right———— 储存,计算并输出成绩
150                             int tempNormalScore = Integer.parseInt(per.split(" ")[3]);
151                             int tempFinalScore = Integer.parseInt(per.split(" ")[4]);
152                             int tempGeneral = examScore.generalScore(tempNormalScore,
153                                     tempFinalScore,null);
154 
155                             student.setID(tempID);
156                             student.setName(tempStuName);
157                             // 临时课程列表里加入一位学生选了的课
158                             couAL.add(searchCourse(tempSelCou, school.getAllCour()));
159                             // set到学生里
160                             student.setSelectedCourse(couAL);
161                             stuScoreTemp.add(tempGeneral);
162                             searchCourse(tempSelCou, school.getAllCour()).setStuScore(stuScoreTemp);
163                             tempStuMap.put(Integer.valueOf(tempID),
164                                     tempStuName + " " +  tempNormalScore +
165                                             " " + tempFinalScore + " " + tempGeneral
166                                             + " " + searchCourse(tempSelCou, school.getAllCour()).getCourName());
167                         }
168                     }
169                     else {// 正确为实验
170                         ArrayList<Integer> tempExperiScore = new ArrayList<>();
171                         for (int i = 4; i < per.split(" ").length; i ++) {
172                             tempExperiScore.add(Integer.valueOf(per.split(" ")[i]));
173                         }
174                         if (!experiScore.ScoreValidity(0, 0, tempExperiScore) ||
175                         !experiScore.TimesJudge(Integer.parseInt(per.split(" ")[3]),
176                                 tempExperiScore)) {
177                             System.out.println("wrong format");
178 //                            tempStuMap.put(Integer.valueOf(tempID),
179 //                                    tempStuName + " " + 0
180 //                                            + " " + searchCourse(tempSelCou, school.getAllCour()).getCourName());
181                         }
182                         else if ( !searchCourse(tempSelCou, school.getAllCour()).
183                                 getAssMent().equals("实验")) {
184                             System.out.println(tempID + " " + tempStuName
185                                     + " : access mode mismatch");
186                             tempStuMap.put(Integer.valueOf(tempID),
187                                     tempStuName + " " + 0
188                                             + " " + searchCourse(tempSelCou, school.getAllCour()).getCourName());
189                         }
190                         else {// right———— 计算并输出成绩
191                             int tempGeneral = (int)experiScore.generalScore(0, 0,
192                                     tempExperiScore) / Integer.parseInt(per.split(" ")[3]);
193 
194                             student.setID(tempID);
195                             student.setName(tempStuName);
196                             searchCourse(tempSelCou, school.getAllCour()).setScore(tempGeneral); // 加入一门课的总评成绩
197                             couAL.add(searchCourse(tempSelCou, school.getAllCour()));// 课程列表
198                             student.setSelectedCourse(couAL);// 课程列表set回去
199                             stuScoreTemp.add(tempGeneral);// 学生成绩列表
200                             searchCourse(tempSelCou, school.getAllCour()).setStuScore(stuScoreTemp);
201                             tempStuMap.put(Integer.valueOf(tempID),
202                                     tempStuName + " " + tempGeneral
203                                             + " " + searchCourse(tempSelCou, school.getAllCour()).getCourName());
204                         }
205                     }
206                 }
207             }
208         }// 出了for循环_学生输出
209         sortStu(tempStuMap);
210         //课程输出
211         sortCourse(courAverDiffer(tempStuMap));
212         //班级输出
213         sortClas(clasAverDivid(tempStuMap));
214     }
215     // 各排各的序,输出
216     public Map<String, String> courAverDiffer(Map<Integer, String> map) {
217         Map<String, String> courseAverMap = new HashMap<>();
218         String clasName = null;
219         for (Map.Entry<Integer, String> entry: map.entrySet()) {// 存不同的课程
220             if (entry.getValue().split(" ").length == 3) {// 实验
221                 clasName = entry.getValue().split(" ")[2];
222             }
223             else if (entry.getValue().split(" ").length == 4) {// 考察
224                 clasName = entry.getValue().split(" ")[3];
225             }
226             else if (entry.getValue().split(" ").length == 5) {// 考试
227                 clasName = entry.getValue().split(" ")[4];
228             }
229             if (!courseAverMap.containsKey(clasName)) {
230                 courseAverMap.put(clasName, "0");
231             }
232         }
233 
234 
235         for (Map.Entry<String, String> entry: courseAverMap.entrySet()) {// 课程
236             int sumExperi = 0, numExperi = 0;
237             int sumAss = 0, numAss = 0;
238             int sumNormalExam = 0, sumFinallExam = 0, sumExam = 0, numExam = 0;
239             String str1 = null, str2 = null, str3 = null;
240             for (Map.Entry<Integer, String> entry1: map.entrySet()) {// 学生
241 
242                 if (entry1.getValue().split(" ").length == 3){// 实验
243                     if (entry1.getValue().split(" ")[2].equals(
244                             entry.getKey())) {
245                         sumExperi = sumExperi + Integer.parseInt(entry1.getValue().split(" ")[1]);
246                         numExperi ++;
247                     }
248                 }
249                 else if (entry1.getValue().split(" ").length == 4) {// 考察
250                         if (entry1.getValue().split(" ")[3].equals(
251                                 entry.getKey())) {
252                             sumAss = sumAss + Integer.parseInt(entry1.getValue().split(" ")[1]);
253                             numAss ++;
254                         }
255                 }
256                 else if (entry1.getValue().split(" ").length == 5) {// 考试
257                         if (entry1.getValue().split(" ")[4].equals(
258                                 entry.getKey())) {
259                             sumNormalExam = sumNormalExam + Integer.parseInt(entry1.getValue().split(" ")[1]);
260                             sumFinallExam = sumFinallExam + Integer.parseInt(entry1.getValue().split(" ")[2]);
261                             sumExam = sumExam + Integer.parseInt(entry1.getValue().split(" ")[3]);
262                             numExam ++;
263                         }
264                 }
265             }
266             if (numExperi != 0 ){
267                 str1 = String.valueOf((int)sumExperi / numExperi);
268                 entry.setValue(str1);
269             }
270             else if (numAss != 0 ) {
271                 str2 = String.valueOf((int)(sumAss / numAss) + " " + (int)(sumAss / numAss));
272                 entry.setValue(str2);
273             }
274             else if (numExam != 0 ) {
275                 str3 = String.valueOf((int)(sumNormalExam / numExam) + " "
276                         + (int)(sumFinallExam / numExam) + " " + (int)(sumExam / numExam));
277                 entry.setValue(str3);
278             }
279         }
280 
281 
282 
283         return courseAverMap;
284     }
285     public Map<Integer, Integer> clasAverDivid(Map<Integer, String> map) {// 用总学生信息的map,分班,计算平均分
286         Map<Integer, Integer> tempMap = new HashMap<>();
287 
288         for (Map.Entry<Integer, String> entry: map.entrySet()) {// 存不同的班号
289             int clasID = entry.getKey() / 100;
290             if (!school.getClasInfor().containsKey(clasID)) {
291                 tempMap.put(clasID, 0);
292             }
293         }
294         for (Map.Entry<Integer, Integer> entry1: tempMap.entrySet()) {// 存了班号的
295             int sum = 0, num = 0;
296             for (Map.Entry<Integer, String> entry: map.entrySet()) {// 学生信息
297                 if (entry.getKey() / 100 == entry1.getKey()) {// 学生属于这个班
298                     if (entry.getValue().split(" ").length == 3) {
299                         sum = sum + Integer.parseInt(entry.getValue().split(" ")[1]);
300                     }
301                     else if (entry.getValue().split(" ").length == 4) {
302                         sum = sum + Integer.parseInt(entry.getValue().split(" ")[2]);
303                     }
304                     else if (entry.getValue().split(" ").length == 5) {
305                         sum = sum + Integer.parseInt(entry.getValue().split(" ")[3]);
306                     }
307                     num ++;
308                 }
309             }
310             entry1.setValue((int)sum / num);
311         }
312         return tempMap;
313     }
314     // 班级和学生排序方法一样,输入的map不一样
315     public void sortStu(Map<Integer, String> map) {
316         List<Map.Entry<Integer, String>> list = new ArrayList<>(map.entrySet());
317         list.sort(Map.Entry.comparingByKey());
318         for (Map.Entry<Integer, String> integerStringEntry : list) {
319             if (Integer.parseInt(integerStringEntry.getValue().split(" ")[1]) != 0) {
320                 if (integerStringEntry.getValue().split(" ").length == 3) {
321                     System.out.println(integerStringEntry.getKey() + " " +
322                             integerStringEntry.getValue().split(" ")[0] + " " +
323                             integerStringEntry.getValue().split(" ")[1]);
324                 }
325                 else if (integerStringEntry.getValue().split(" ").length == 4) {
326                     System.out.println(integerStringEntry.getKey() + " " +
327                             integerStringEntry.getValue().split(" ")[0] + " " +
328                             integerStringEntry.getValue().split(" ")[2]);
329                 }
330                 else if (integerStringEntry.getValue().split(" ").length == 5) {
331                     System.out.println(integerStringEntry.getKey() + " " +
332                             integerStringEntry.getValue().split(" ")[0] + " " +
333                             integerStringEntry.getValue().split(" ")[3]);
334                 }
335 
336             } else {
337                 System.out.println(integerStringEntry.getKey() + " "
338                         + integerStringEntry.getValue().split(" ")[0]
339                         + " did not take any exams");
340             }
341         }
342     }
343     public void sortClas(Map<Integer, Integer> map) {
344         List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
345         list.sort(Map.Entry.comparingByKey());
346         for (Map.Entry<Integer, Integer> integerIntegerEntry : list) {
347             if (integerIntegerEntry.getValue() != 0) {
348                 System.out.println(integerIntegerEntry.getKey() +
349                         " " + integerIntegerEntry.getValue());
350             }
351             else {
352                 System.out.println(integerIntegerEntry.getKey() +
353                         " has no grades yet");
354             }
355         }
356     }
357 
358     public void sortCourse(Map<String, String> map) {// 按key的字符顺序来拍,英文大于汉字巴拉巴拉
359         List<Map.Entry<String, String>> list = new ArrayList<>(map.entrySet());
360         list.sort(Map.Entry.comparingByKey());
361         for (Map.Entry<String, String> stringIntegerEntry : list) {
362             if (!stringIntegerEntry.getValue().equals("0")) {
363                 System.out.println(stringIntegerEntry.getKey() +
364                         " " + stringIntegerEntry.getValue());
365             }
366             else {
367                 System.out.println(stringIntegerEntry.getKey() +
368                         " has no grades yet");
369             }
370         }
371     }
372     public Course searchCourse(String courName, ArrayList<Course> courses) {
373         for (Course one: courses) {
374             if (one.getCourName().equals(courName)) {
375                 return one;
376             }
377         }
378         return null;
379     }
380 }
381 class School {
382     School() {
383 
384     }
385     // 有多个班级时便于排序输出平均分
386     private Map<Integer, String> clasInfor = new HashMap();
387     // 多个课程,排序输出课程平均分
388     private Map<String, Integer> couInfor = new HashMap();
389     // 看课程列表里有没有这课
390     private ArrayList<Course> allCour = new ArrayList<>();
391     // 开一个学生列表,学生全放进去
392     private Map<Integer, String> stuMap = new HashMap<>();
393     private List<Map.Entry<Integer, String>> stuList = new ArrayList<>(stuMap.entrySet());
394     School(Map<Integer, String> clasInfor,
395            Map<String, Integer> couInfor,
396            ArrayList<Course> allCour,
397            Map<Integer, String> stuMap,
398            List<Map.Entry<Integer, String>> stuList) {
399         this.clasInfor = clasInfor;
400         this.couInfor = couInfor;
401         this.allCour = allCour;
402         this.stuMap = stuMap;
403         this.stuList = stuList;
404     }
405 
406     public List<Map.Entry<Integer, String>> getStuList() {
407         return stuList;
408     }
409 
410     public void setStuList(List<Map.Entry<Integer, String>> stuList) {
411         this.stuList = stuList;
412     }
413 
414     public Map<Integer, String> getStuMap() {
415         return stuMap;
416     }
417 
418     public void setStuMap(Map<Integer, String> stuMap) {
419         this.stuMap = stuMap;
420     }
421 
422     public void setAllCour(ArrayList<Course> allCour) {
423         this.allCour = allCour;
424     }
425 
426     public ArrayList<Course> getAllCour() {
427         return allCour;
428     }
429 
430     public Map<Integer, String> getClasInfor() {
431         return clasInfor;
432     }
433 
434     public void setClasInfor(Map<Integer, String> clasInfor) {
435         this.clasInfor = clasInfor;
436     }
437 
438     public Map<String, Integer> getCouInfor() {
439         return couInfor;
440     }
441 
442     public void setCouInfor(Map<String, Integer> couInfor) {
443         this.couInfor = couInfor;
444     }
445 
446 }
447 class Course{
448     Course() {
449 
450     }
451     private String courName;
452     private String nature;// 课程性质
453     private String assMent;// 课程考察方式
454     private int score;// 一门课单科期末总评
455     private ArrayList<Integer> stuScore = new ArrayList<>();// 选了这门课的学生,用来算课程平均分
456 
457     Course(ArrayList<Integer> stuScore,
458            String nature, String assMent,
459            String courName,
460            int score) {
461         this.stuScore = stuScore;
462         this.nature = nature;
463         this.assMent = assMent;
464         this.courName = courName;
465         this.score = score;
466     }
467 
468     public void setScore(int score) {
469         this.score = score;
470     }
471 
472     public int getScore() {
473         return score;
474     }
475 
476     public ArrayList<Integer> getStuScore() {
477         return stuScore;
478     }
479 
480     public void setStuScore(ArrayList<Integer> stuScore) {
481         this.stuScore = stuScore;
482     }
483 
484     public void setNature(String nature) {
485         this.nature = nature;
486     }
487 
488     public String getNature() {
489         return nature;
490     }
491 
492     public void setAssMent(String assMent) {
493         this.assMent = assMent;
494     }
495 
496     public String getAssMent() {
497         return assMent;
498     }
499 
500     public void setCourName(String courName) {
501         this.courName = courName;
502     }
503 
504     public String getCourName() {
505         return courName;
506     }
507 
508     public boolean JudgeNA(String nature, String assMent) {
509         return (nature.equals("必修") && (assMent.equals("") ||
510                 assMent.equals("考试")))
511                 || (nature.equals("选修") && (assMent.equals("考试") ||
512                 assMent.equals("考察")))
513                 || (nature.equals("实验") && assMent.equals("实验"));
514     }
515     public boolean CourNameValitidy(String courName) {
516         return courName.length() < 10;
517     }
518 }
519 abstract class Score {
520     Score() {
521     }
522 
523     protected int normal;
524     protected int finall;
525     protected int expriNum;
526     protected ArrayList<Integer> perExperi = new ArrayList<>();
527 
528     Score(int normal, int finall, ArrayList<Integer> perExperi, int expriNum) {
529         this.normal = normal;
530         this.finall = finall;
531         this.perExperi = perExperi;
532         this.expriNum = expriNum;
533     }
534 
535     public void setExpriNum(int expriNum) {
536         this.expriNum = expriNum;
537     }
538 
539     public int getExpriNum() {
540         return expriNum;
541     }
542 
543 
544     public ArrayList<Integer> getPerExperi() {
545         return perExperi;
546     }
547 
548     public void setPerExperi(ArrayList<Integer> perExperi) {
549         this.perExperi = perExperi;
550     }
551 
552     public void setFinall(int finall) {
553         this.finall = finall;
554     }
555 
556     public int getFinall() {
557         return finall;
558     }
559 
560     public void setNormal(int normal) {
561         this.normal = normal;
562     }
563 
564     public int getNormal() {
565         return normal;
566     }
567 
568     public abstract int generalScore(int normal, int finall, ArrayList<Integer> perExperi);
569 
570     public abstract boolean ScoreValidity(int normal, int finall, ArrayList<Integer> perExperi);
571 }
572 class ExamScore extends Score{
573     ExamScore() {
574         super();
575     }
576 
577     @Override
578     public int generalScore(int normal, int finall, ArrayList<Integer> perExperi) {
579         return (int)(normal * 0.3 + finall * 0.7);
580     }
581 
582     @Override
583     public boolean ScoreValidity(int normal, int finall, ArrayList<Integer> perExperi) {
584         return normal >= 0 && normal <= 100 && finall >= 0 && finall <= 100;
585 
586     }
587 }
588 class AssScore extends Score {
589     AssScore() {
590         super();
591     }
592     @Override
593     public int generalScore(int normal, int finall, ArrayList<Integer> perExperi) {
594         return finall;
595     }
596 
597     @Override
598     public boolean ScoreValidity(int normal, int finall, ArrayList<Integer> perExperi) {
599         return normal >= 0 && normal <= 100 && finall >= 0 && finall <= 100;
600     }
601 }
602 class ExperiScore extends Score {
603     ExperiScore(){
604         super();
605     }
606     @Override
607     public int generalScore(int normal, int finall, ArrayList<Integer> perExperi) {
608         int sum = 0;
609         for (Integer per: perExperi) {
610             sum = sum + per;
611         }
612         return sum;
613     }
614 
615     @Override
616     public boolean ScoreValidity(int normal, int finall, ArrayList<Integer> perExperi) {
617         boolean flag = true;
618         for (Integer per: perExperi) {
619             flag = per >= 0 && per <= 100;
620         }
621         return flag;
622     }
623 
624     public boolean TimesJudge(int expriNum, ArrayList<Integer> perExperi) {
625         // 输出都是格式错误
626         return expriNum == perExperi.size()
627                 && (expriNum >= 4 && expriNum <= 9);
628     }
629 }
630 class Student {
631     Student() {
632     }
633     private String ID;
634     private String name;
635     private ArrayList<Course> selectedCourse = new ArrayList<>();// 学生选了的课
636 
637     Student(String ID, String name, ArrayList<Course> selectedCourse) {
638         this.ID = ID;
639         this.name = name;
640         this.selectedCourse = selectedCourse;
641     }
642 
643     public String getID() {
644         return ID;
645     }
646 
647     public void setID(String ID) {
648         this.ID = ID;
649     }
650 
651     public String getName() {
652         return name;
653     }
654 
655     public void setName(String name) {
656         this.name = name;
657     }
658 
659     public ArrayList<Course> getSelectedCourse() {
660         return selectedCourse;
661     }
662 
663     public void setSelectedCourse(ArrayList<Course> selectedCourse) {
664         this.selectedCourse = selectedCourse;
665     }
666     public boolean IDValidity(String ID) {
667         return ID.length() == 8;
668     }
669     public boolean NameValidity(String name) {
670         return name.length() <= 10;
671     }
672 }
673 class Clas {
674     Clas() {
675 
676     }
677     private ArrayList<Student> stuMember = new ArrayList<>();// 班级里的学生,算班级平均分
678     Clas(ArrayList<Student> stuMember) {
679         this.stuMember = stuMember;
680     }
681 
682     public void setStuMember(ArrayList<Student> stuMember) {
683         this.stuMember = stuMember;
684     }
685 
686     public ArrayList<Student> getStuMember() {
687         return stuMember;
688     }
689 }

 

分析:

进阶版。第一版给了参考类图照着写,观摩着类图开始感慨菜单没给类图自己也设计得稀巴烂还敢写实属是赶鸭子上架以及无知者无畏。这版非常善解人意地在同次题目集里给了HashMap的检索和排序以及多态,利于笔者有一个考量的局部以及框架的更新,这题笔者取得的分数相较前几次确实取得了进步,感谢室友的帮助以及笔者本人时间的消耗。

没有算法,基本的分区再导入各个方法处理,值得注意仍然是类间设计。笔者编写此次代码前借了同学的类图进行参考,显然学习到的是,给的参考类图只是参考。故自己更改了一些类以及类间设计。

 

踩坑心得:

1.人不可能不犯错的,由收敛性以大及小,写代码是不可能不踩坑的。踩坑不是事故,知错不改屡犯旧错才是事故。

2.旧坑几乎踏平,故此不再重提。新发现的坑是代码的连带问题。修正了一个测试错误A后仍然存在错误,debug后发现和A同属于一条思路下,即错误思路下仅仅修正了最利于发现的一个问题而忽略了其他。属于总体思路问题而非代码耦合度问题。

3.这五次及第十第十一次中间夹杂的随堂检测反映出的问题,算法是稀巴烂的一点都不会。以及笔者写代码的速度是真的慢,直接表现为随堂测验中有人做出了第七题而笔者仅仅是在下课时堪堪将这题读完。

4.读题。先前的题目较这次题目集信息量小且简单且熟悉,笔者在读题方面存在读题不细、信息提取分析不足等问题。

 

改进建议:

烦请作业布置规律一些。虽然知道老板不会按员工的工作节奏来调整项目的接洽和任务的发布,但是如果可以的话请作业布置得规律一些。

 

总结:

笔者写此篇时正值端午假期,故先祝各位端午安康。

知识细类,每个知识点都不敢说能够熟练运用,是从题目集11的覆写得到的教训,Java已给时会用的类覆写起来是死灰在抹泥浆,有种沉重又不得不的魅力。类的设计,菜单和成绩两个系列没有一道做到了卷面满分,相比于别人的进步——话说得点到为止为好。改变游戏规则的前提是征服游戏规则,不愿承认但是事实的是笔者确实到学期初仍在从本门课程的学习制度中学到新的东西,不是知识而是学习方法之类,遗憾是甚至没有摸清游戏规则。但响应国家号召,终生学习是时代所趋个人所需,努力学习并持续坚持努力学习,是身为当代青年的笔者应该要做到的。

此次blog所涉及的知识点极大地丰富了笔者的知识库。感谢指教,下期再会。

标签:11,return,07,int,ArrayList,PTA,split,public,String
From: https://www.cnblogs.com/psychewind/p/17500033.html

相关文章

  • 11月新书-《强化学习:算法与理论》分享
        分享一本由AlekhAgarwal、NanJiang和ShamM.Kakade编写的新书,《强化学习:算法与理论》。本书11月最新release出来的讲解强化学习的书籍,目前还在持续更新中,最新版本刚刚放出来,分享给大家。     与以往动辄几百页的强化学习书籍相比,本书比较精悍,目前只有80多页,内容比......
  • NC24141 [USACO 2011 Dec G]Grass Planting
    题目链接题目题目描述FarmerJohnhasNbarrenpastures(2<=N<=100,000)connectedbyN-1bidirectionalroads,suchthatthereisexactlyonepathbetweenanytwopastures.Bessie,acowwholoveshergrazingtime,oftencomplainsabouthowthereisnogr......
  • 如何在 Windows 11 中启用 Copilot
    这是一个快速教程,用于展示如何在Windows11中启用Copilot.在Windows的开发和金丝雀版本中,如果您没有以某种方式获得copilot,则可以激活/启用copilot。在这里,我将提到您必须执行的一些步骤,以便从侧边栏访问Windows11中的Copilot。但请记住,此处提到的方法仅适用于Windows11......
  • 代码随想录算法训练营第十五天| 110.平衡二叉树 (优先掌握递归) 257. 二叉树的所有路径
     110.平衡二叉树(优先掌握递归)难点:要求每个节点的左右字数的高度相减<=1,因此,需要对每个节点都进行检查,难就难在怎么获得任意节点的高度其中递归是最简单的: 1intisB_cursor(TreeNode*node,bool&isBalance)2{3if(isBalance==false)return0;4if......
  • NC20573 [SDOI2011]染色
    题目链接题目题目描述给定一棵有n个节点的无根树和m个操作,操作有2类:1、将节点a到节点b路径上所有点都染成颜色c;2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221”由3段组成:“11”、“222”和“1”。请你写一个程序依次完成这m......
  • 值得关注的全球11家开源公司
    值得关注的全球11家开源公司公司名称:Cfengine成立时间:2008年在挪威奥斯陆注册,2009年6月在美国注册。地点:奥斯陆,挪威,圣彼得堡,佛罗里达州公司产品:Cfengine3(社区版),着重进行服务器配置管理。CfengineNova(商业版),用于服务器生命周期管理软件建设......
  • PTA6-8次题目集(成绩计算系列)总结
    1.前言6-8次作业题量不大,难度较菜单计价较小,但实现过程依旧繁琐知识点输入和输出处理:你需要从用户或文件中读取输入数据(课程信息和学生成绩),然后相应地进行处理。你可以使用诸如读取控制台输入(Scanner 类)或从文件读取技术。字符串操作和解析:你需要将输入的字符串分割后提......
  • mysql的数据类型以及mysql中的int11是什么意思
    今天抽时间来讲一下mysql里的知识点,之前有不少人问过我,mysql中的int(11),这个11到底是啥意思?是11位的意思吗?你是否也想过这个问题,是否也有这个疑问?ok,今天就展开来讲一下,用通俗易懂的大白话来给你彻底搞明白一、跟你扯点二进制的小东西要讲清楚这个问题,我先来给大家科普一点计算......
  • 6-8次PTA题目集
    前言:6-8次PTA写了一份成绩统计程序,与之前的点菜系统有类似的地方,但比点菜系统更加的复杂,选课的时候需要考虑到学生与课程两个信息,涉及到字符的排序,与菜单不一样的地方在于会出现单词与汉字进行比较的情况,这就需要将汉字改成拼音再将拼音进行排序(在设计与分析中会详细描述)。这几次......
  • [WP] 攻防世界 CSFJ1070 steg没有py
    「附件」下载附件,解压后是一张图片Do_you_like_misc.png「思路」题目是steg没有py,猜测是某种python库,搜索一下stegpy。​stegpy​​是一种将编码信息隐写在图像和音频文件中的程序。使用Python编写。那就根据教程安装https://blog.51cto.com/u_15274949/5250478安装完......