PTA题目集7~9总结
PTA题目集7~9总结
(1)前言:
一、题目集7:
知识点:
-
继承、多态的应用;
-
ArrayList泛型的应用方法
-
Comparable接口及泛型的应用
-
单一职责原则的应用
-
“开-闭”原则的应用
题量:2道
难度:四颗星
二、题目集8:
知识点:
-
类的合理设计
-
ArrayList泛型的应用方法
-
抽象类的应用方法
-
继承与多态的应用
题量:1道
难度:五颗星
三、题目集9:
知识点:
-
继承与多态的应用
-
类的合理设计
-
ArrayList泛型的应用方法
-
抽象类的应用
题量:1道
难度:五颗星
(2)设计与分析:
①题目集7(7-1)、(7-2)两道题目的递进式设计分析总结:
题目要求:
7-1题目: 图形卡片排序游戏(详见作业指导书 2020-OO第07次作业-1指导书V1.0.pdf)
7-2题目:图形卡片分组游戏 (详见作业指导书2021-OO第07次作业-2指导书V1.0.pdf)
题目一所编写代码如下:
1 import java.util.ArrayList; 2 import java.util.Iterator; 3 import java.util.Scanner; 4 import java.util.TreeSet; 5 6 public class Main { 7 public static Scanner input = new Scanner(System.in); 8 public static void main(String[] args){ 9 ArrayList<Integer> list = new ArrayList<Integer>(); 10 int num = input.nextInt(); 11 while(num != 0){ 12 if(num < 0 || num > 4){ 13 System.out.println("Wrong Format"); 14 System.exit(0); 15 } 16 list.add(num); 17 num = input.nextInt(); 18 } 19 DealCardList dealCardList = new DealCardList(list); 20 if(!dealCardList.validate()){ 21 System.out.println("Wrong Format"); 22 System.exit(0); 23 } 24 dealCardList.showResult(); 25 input.close(); 26 } 27 } 28 class DealCardList{ 29 ArrayList<Card> cardList=new ArrayList<>(); 30 DealCardList(){ 31 32 } 33 DealCardList(ArrayList<Integer> card){ 34 for(Integer integer: card){ 35 if(integer==0){ 36 break; 37 } 38 switch (integer){ 39 case 1: 40 Card card1 = new Card(new Circle(Main.input.nextDouble())); 41 card1.getShape().setShapeName("Circle"); 42 cardList.add(card1); 43 break; 44 case 2: 45 Card card2 = new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble())); 46 card2.getShape().setShapeName("Rectangle"); 47 cardList.add(card2); 48 break; 49 case 3: 50 Card card3 = new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 51 card3.getShape().setShapeName("Triangle"); 52 cardList.add(card3); 53 break; 54 case 4: 55 Card card4 = new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 56 card4.getShape().setShapeName("Trapezoid"); 57 cardList.add(card4); 58 break; 59 } 60 } 61 } 62 63 public boolean validate() { 64 for(Card card:cardList) { 65 if(!card.getShape().validate()){ 66 return false; 67 //为加上break; 68 } 69 } 70 return true; 71 } 72 public void cardSort(){ 73 TreeSet<Card> cards = new TreeSet<Card>(cardList); 74 75 for (Card card : cards) { 76 System.out.print(card.getShape()); 77 } 78 } 79 80 public double getAllArea(){ 81 double sum=0; 82 for (Card card : cardList) { 83 sum+=card.getShape().getArea(); 84 } 85 return sum; 86 } 87 88 public void showResult() { 89 System.out.println("The original list:"); 90 for (Card card : cardList) { 91 System.out.print(card.getShape()); 92 } 93 System.out.println(); 94 System.out.println("The sorted list:"); 95 cardSort(); 96 System.out.printf("\nSum of area:%.2f",getAllArea()); 97 } 98 } 99 class Card implements Comparable<Card>{ 100 private Shape shape; 101 Card(){ 102 103 } 104 Card(Shape shape){ 105 this.shape = shape; 106 } 107 public int compareTo(Card card) { 108 return -(int) (this.shape.getArea()-card.shape.getArea()); 109 } 110 public Shape getShape(){ 111 return shape; 112 } 113 public void setShape(Shape shape){ 114 this.shape = shape; 115 } 116 117 } 118 abstract class Shape{ 119 private String shapeName; 120 Shape(){ 121 122 } 123 Shape(String shapeName){ 124 this.shapeName = shapeName; 125 } 126 127 public String getShapeName() { 128 return shapeName; 129 } 130 public void setShapeName(String shapeName) { 131 this.shapeName = shapeName; 132 } 133 public abstract double getArea(); 134 public abstract boolean validate(); 135 public String toString() { 136 return getShapeName()+":"+String.format("%.2f ",getArea()); 137 } 138 } 139 class Triangle extends Shape{ 140 private double side1; 141 private double side2; 142 private double side3; 143 Triangle(){ 144 145 } 146 Triangle(double side1,double side2,double side3){ 147 this.side1 = side1; 148 this.side2 = side2; 149 this.side3 = side3; 150 } 151 152 @Override 153 public double getArea() { 154 double s = (side1 + side2 + side3) / 2; 155 double area=Math.round(Math.sqrt(s * (s - side1) * (s - side2) * (s - side3))*100.00)*0.01; 156 return area; 157 } 158 159 @Override 160 public boolean validate() { 161 return (side1+side2>side3&&side1+side3>side2&&side2+side3>side1); 162 } 163 } 164 class Trapezoid extends Shape{ 165 private double topSide; 166 private double bottomSide; 167 private double heightSide; 168 Trapezoid(){ 169 170 } 171 Trapezoid(double topSide,double bottomSide,double heightSide){ 172 this.topSide = topSide; 173 this.bottomSide = bottomSide; 174 this.heightSide = heightSide; 175 } 176 177 @Override 178 public boolean validate() { 179 return topSide>0&&heightSide>0&&bottomSide>0; 180 } 181 182 @Override 183 public double getArea() { 184 return (topSide+bottomSide)*heightSide/2; 185 } 186 } 187 class Circle extends Shape{ 188 private double radius; 189 Circle(){ 190 191 } 192 Circle(double radius){ 193 this.radius = radius; 194 } 195 196 public double getRadius() { 197 return radius; 198 } 199 200 public void setRadius(double radius) { 201 this.radius = radius; 202 } 203 204 @Override 205 public double getArea() { 206 double area = Math.round(Math.PI * radius * radius*100.00)*0.01d; 207 return area; 208 } 209 210 @Override 211 public boolean validate() { 212 return this.radius>0; 213 } 214 } 215 class Rectangle extends Shape{ 216 private double width; 217 private double length; 218 Rectangle(){ 219 220 } 221 Rectangle(double width,double length){ 222 this.width = width; 223 this.length = length; 224 } 225 public void setLength(double length) { 226 this.length = length; 227 } 228 public double getLength() { 229 return length; 230 } 231 232 public void setWidth(double width) { 233 this.width = width; 234 } 235 236 public double getWidth() { 237 return width; 238 } 239 240 @Override 241 public double getArea() { 242 return length*width; 243 } 244 245 @Override 246 public boolean validate() { 247 return length>0&&width>0; 248 } 249 }
题目一具体数据如图所示:

题目一类图设计如下:

题目二编写代码如下:
1 // import java.lang.reflect.Array; 2 import java.util.ArrayList; 3 import java.util.Scanner; 4 import java.util.TreeSet; 5 6 public class Main { 7 public static Scanner input = new Scanner(System.in); 8 public static void main(String[] args){ 9 ArrayList<Integer> list = new ArrayList<Integer>(); 10 int num = input.nextInt(); 11 while(num != 0){ 12 if(num < 0 || num > 4){ 13 System.out.println("Wrong Format"); 14 System.exit(0); 15 } 16 list.add(num); 17 num = input.nextInt(); 18 } 19 DealCardList dealCardList = new DealCardList(list); 20 if(!dealCardList.validate()){ 21 System.out.println("Wrong Format"); 22 System.exit(0); 23 } 24 dealCardList.showResult(); 25 input.close(); 26 } 27 } 28 class DealCardList<cardList> { 29 ArrayList<Card> cardList=new ArrayList<>(); 30 ArrayList<Card> circleList=new ArrayList<>(); 31 ArrayList<Card> rectangleList = new ArrayList<>(); 32 ArrayList<Card> trapezoidList = new ArrayList<>(); 33 ArrayList<Card> triangleList = new ArrayList<>(); 34 DealCardList(){ 35 36 } 37 DealCardList(ArrayList<Integer> card){ 38 for(Integer integer: card){ 39 if(integer==0){ 40 break; 41 } 42 switch (integer){ 43 case 1: 44 Card card1 = new Card(new Circle(Main.input.nextDouble())); 45 card1.getShape().setShapeName("Circle"); 46 cardList.add(card1); 47 circleList.add(card1); 48 break; 49 case 2: 50 Card card2 = new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble())); 51 card2.getShape().setShapeName("Rectangle"); 52 cardList.add(card2); 53 rectangleList.add(card2); 54 break; 55 case 3: 56 Card card3 = new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 57 card3.getShape().setShapeName("Triangle"); 58 cardList.add(card3); 59 triangleList.add(card3); 60 break; 61 case 4: 62 Card card4 = new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 63 card4.getShape().setShapeName("Trapezoid"); 64 cardList.add(card4); 65 trapezoidList.add(card4); 66 break; 67 } 68 } 69 } 70 71 public boolean validate() { 72 for(Card card:cardList) { 73 if(!card.getShape().validate()){ 74 return false; 75 //为加上break; 76 } 77 } 78 return true; 79 } 80 public void cardSort(ArrayList cardList){ 81 TreeSet<Card> cards = new TreeSet<Card>(cardList); 82 System.out.print("["); 83 for (Card card : cards) { 84 System.out.print(card.getShape()); 85 } 86 System.out.print("]"); 87 } 88 89 public double getMaxArea(){ 90 double circleSum=0,rectangleSum = 0,triangleSum = 0,trapezoidSum=0; 91 for (Card card : circleList) { 92 circleSum+=card.getShape().getArea(); 93 } 94 for (Card card1:rectangleList 95 ) { 96 triangleSum+= card1.getShape().getArea(); 97 } 98 for (Card card2:trapezoidList 99 ) { 100 trapezoidSum+=card2.getShape().getArea(); 101 } 102 for (Card card3:triangleList 103 ) { 104 triangleSum+=card3.getShape().getArea(); 105 } 106 double d=circleSum>rectangleSum?circleSum:rectangleSum; 107 double t=d>trapezoidSum?d:trapezoidSum; 108 double max = t>triangleSum?t:triangleSum; 109 return max; 110 } 111 public double getCircleArea(){ 112 double sum = 0; 113 for (Card card:circleList) { 114 sum+=card.getShape().getArea(); 115 } 116 return sum; 117 } 118 public double getRectangleArea(){ 119 double sum = 0; 120 for (Card card:rectangleList) { 121 sum+=card.getShape().getArea(); 122 } 123 return sum; 124 } 125 public double getTrapezoidArea(){ 126 double sum = 0; 127 for (Card card:trapezoidList) { 128 sum+=card.getShape().getArea(); 129 } 130 return sum; 131 } 132 public double getTriangleArea(){ 133 double sum = 0; 134 for (Card card:triangleList) { 135 sum+=card.getShape().getArea(); 136 } 137 return sum; 138 } 139 public void showResult() { 140 141 if(cardList.size()==0){ 142 143 System.out.println("Wrong Format"); 144 System.exit(0); 145 } 146 System.out.println("The original list:"); 147 System.out.print("["); 148 for (Card card : cardList) { 149 System.out.print(card.getShape()); 150 } 151 System.out.print("]"); 152 System.out.println(); 153 System.out.println("The Separated List:"); 154 System.out.print("["); 155 for (Card card : circleList) { 156 System.out.print(card.getShape()); 157 } 158 System.out.print("]"); 159 System.out.print("["); 160 for (Card card : rectangleList) { 161 System.out.print(card.getShape()); 162 } 163 System.out.print("]"); 164 System.out.print("["); 165 for (Card card : triangleList) { 166 System.out.print(card.getShape()); 167 } 168 System.out.print("]"); 169 System.out.print("["); 170 for (Card card : trapezoidList) { 171 System.out.print(card.getShape()); 172 } 173 System.out.print("]"); 174 System.out.println(); 175 System.out.println("The Separated sorted List:"); 176 cardSort(circleList); 177 cardSort(rectangleList); 178 cardSort(triangleList); 179 cardSort(trapezoidList); 180 System.out.println(); 181 System.out.printf("The max area:%.2f",getMaxArea()); 182 } 183 } 184 class Card implements Comparable<Card>{ 185 private Shape shape; 186 Card(){ 187 188 } 189 Card(Shape shape){ 190 this.shape = shape; 191 } 192 public int compareTo(Card card) { 193 return -(int) (this.shape.getArea()-card.shape.getArea()); 194 } 195 public Shape getShape(){ 196 return shape; 197 } 198 public void setShape(Shape shape){ 199 this.shape = shape; 200 } 201 202 } 203 abstract class Shape{ 204 private String shapeName; 205 Shape(){ 206 207 } 208 Shape(String shapeName){ 209 this.shapeName = shapeName; 210 } 211 212 public String getShapeName() { 213 return shapeName; 214 } 215 public void setShapeName(String shapeName) { 216 this.shapeName = shapeName; 217 } 218 public abstract double getArea(); 219 public abstract boolean validate(); 220 public String toString() { 221 return getShapeName()+":"+String.format("%.2f ",getArea()); 222 } 223 } 224 class Triangle extends Shape{ 225 private double side1; 226 private double side2; 227 private double side3; 228 Triangle(){ 229 230 } 231 Triangle(double side1,double side2,double side3){ 232 this.side1 = side1; 233 this.side2 = side2; 234 this.side3 = side3; 235 } 236 237 @Override 238 public double getArea() { 239 double s = (side1 + side2 + side3) / 2; 240 double area=Math.round(Math.sqrt(s * (s - side1) * (s - side2) * (s - side3))*100.00)*0.01; 241 return area; 242 } 243 244 @Override 245 public boolean validate() { 246 return (side1+side2>side3&&side1+side3>side2&&side2+side3>side1); 247 } 248 } 249 class Trapezoid extends Shape{ 250 private double topSide; 251 private double bottomSide; 252 private double heightSide; 253 Trapezoid(){ 254 255 } 256 Trapezoid(double topSide,double bottomSide,double heightSide){ 257 this.topSide = topSide; 258 this.bottomSide = bottomSide; 259 this.heightSide = heightSide; 260 } 261 262 @Override 263 public boolean validate() { 264 return topSide>0&&heightSide>0&&bottomSide>0; 265 } 266 267 @Override 268 public double getArea() { 269 return (topSide+bottomSide)*heightSide/2; 270 } 271 } 272 class Circle extends Shape{ 273 private double radius; 274 Circle(){ 275 276 } 277 Circle(double radius){ 278 this.radius = radius; 279 } 280 281 public double getRadius() { 282 return radius; 283 } 284 285 public void setRadius(double radius) { 286 this.radius = radius; 287 } 288 289 @Override 290 public double getArea() { 291 double area = Math.round(Math.PI * radius * radius*100.00)*0.01d; 292 return area; 293 } 294 295 @Override 296 public boolean validate() { 297 return this.radius>0; 298 } 299 } 300 class Rectangle extends Shape{ 301 private double width; 302 private double length; 303 Rectangle(){ 304 305 } 306 Rectangle(double width,double length){ 307 this.width = width; 308 this.length = length; 309 } 310 public void setLength(double length) { 311 this.length = length; 312 } 313 public double getLength() { 314 return length; 315 } 316 317 public void setWidth(double width) { 318 this.width = width; 319 } 320 321 public double getWidth() { 322 return width; 323 } 324 325 @Override 326 public double getArea() { 327 return length*width; 328 } 329 330 @Override 331 public boolean validate() { 332 return length>0&&width>0; 333 } 334 }
题目二具体数据如下图所示:

题目二类图设计如下:

题目主要是利用继承的思想,从抽象图形类设计开始,一个抽象图形类中,拥有该图形的抽象面积计算方法,在不同的图形中有属于该图形面积计算的相关方法。对于每个图形卡片,设计一个集合对所定义的图形进行存储并按照面积大小顺序进行相关排序。在第一题的基础之上,对第二题递进式地采用对各个图形进行多个输入输出并且分组。在不同图形分界处加上分割他们地标志图形中括号,再将单个图形输入输出改为多个输入输出,不同图形之间来一个顺序排列,同时不改变原来所有图形地顺序排列。两者之间地递进关系比较明显,在编写代码时基本沿袭第一次地代码,比较方便。
②题目集8和题目集9两道ATM机仿真题目的设计思路分析总结
题目要求:
题目集8: ATM机类结构设计(一) (题目详情 (pintia.cn))
题目集9:ATM机类结构设计(二)(题目详情 (pintia.cn))
题目集8类图设计:

题目集9类图设计:

题目集8是要求我们设计一个不能跨行取款,只存在借记卡账户的小型ATM机系统,通过输入相应的卡号、密码、ATM机编号和相关取款金额对账户中余额进行取款操作,也可不输入金额单纯对余额进行查询操作。相对于题目集8,题目集9的ATM机设计上增加了跨行存取款业务,同时还增加了借贷卡账户,完善了ATM机系统的功能。对于跨行取款操作,系统还将对其进行相关手续费扣除。
(3)踩坑心得:
- 由于对输入为0时输出地代码设计错误,在提交代码地过程中,第一个点始终过不了,最终设计为输出Wrong Format.时终于通过测试点。同时在第二题最后一个测试点中,当为三种卡片测试时,测试点也始终过不了,但是前面的三种卡片测试点显示又过了。
![]()
找测试点哪里出了问题真的有点摸不着头脑。
- 在编写代码过程中,对于程序的设计思路不能够很好的把握,同时不能理解如何实现各个功能,导致在编写代码过程中,写了很多没用的代码,写了删,删了又继续写,但是最终什么都没有写出来、并且浪费了时间。理解程序步骤,顺着程序的思路来,写代码前不能一股脑就直接写,否则真会什么都写不出来,题目集中每一道题都并非容易,每次我都像踩坑一样,一踩一个中。然后写出地代码在运行测试时没有一个过。
- 对于代码中要用到的知识点不能够熟练掌握,同时还不太会运用时,在写代码时如果避开要用到的知识,写代码时会特别地艰难,像图像卡片排序中用到的集合会方便一些,但是我一开始编写时并没有去利用集合而是采用数组的方式对图形进行存储,但是当我真正运用起来时,我发现自己不会用,也不知道怎么用。
- 题目集7-2是对题目集7-1的加强,ATM(二)是对ATM(一)的加强,但是在每一次的加强练习中不是能够很好的将要设计的程序完整地呈现,难度有在加大,然后会突然感觉自己什么都不会,就像是一步回到解放前。测试点也未能在规定时间完成。
- 编写代码中有一个特别致命的缺点,我在没有什么思路时立刻开始写代码,所写出来的代码质量并不好。
(4)改进建议:
- 代码难度突增有点让人措手不及,就像上学期C语言的课设题目,有点让人痛苦...... , 希望稍稍降低一点难度,难度不要从地下突然飙到云端啊。
- 提交代码时,希望对于一些极限情况的输出结果适当给予说明,有时候不太明白在一些极限情况下,所编写的代码要给予他怎样的输出,比如说是进行报错还是只用空输出就行。
- 希望老师课堂上多讲讲题目,一些例题或是网上的觉得好的题目都可以在课堂上进行讲解,尤其是一些做题的思路总结,让学生可以多学习学习老师是如何解决问题的。
- 建议老师选取做题比较好的同学在课堂上分享一下自己的做题经验以及做题的思路供大家学习参考。
- 因为可以感受到老师在课堂上对同学们讲解自己对某一问题看法表述的锻炼,我想建议老师在授课计划完成并且时间充裕之时让同学们谈谈pta及实验课作业的相关见解。
(5)总结:
第七次pta作业(图形卡片排序游戏、图形卡片分组游戏)中,让我对Java中继承、接口和复用有了进一步的了解。同时感受到封装对复用的进一步帮助、抽象方法在子类图形方法中的具体实现。
之后的两次作业(ATM设计(一)、ATM设计(二))中,我对Java中集合,关联,聚合,组合之间的关系也有了进一步的了解,同时在程序的类结构设计中应如何设计类与类之间的关系有了进一步的思考。
通过这几次的作业,自己也有发现,写代码并不是一蹴而就,首先要思考因从哪里下手,以及对于要用到的Java知识点,及如何实现所要解决的问题。这学期的JavaPTA作业有在一次比一次加大难度,会经常感觉到力不从心。
课下的代码练习还有待加强


浙公网安备 33010602011771号