题目集7~9的总结
一:前言
题目集七
1.题目集七的的知识点
掌握类的继承、多态性使用方法以及接口的应用。
2.题目集七的题量
① 图形卡片排序游戏
② 图形卡片分组游戏
3.题目集七的难度
①考虑一款适合于小学生的卡片(Card)排序游戏,其规则为随机发放一些卡片给学生,卡片分为 四种形状:圆形(Circle)、矩形(Rectangle)、三角形(Triangle)及梯形(Trapezoid),并给出各种 卡片的相应参数,要求学生能够迅速求出各卡片的面积大小然后将卡片按照其面积值从大到小进行排 序,同时求出所有卡片的面积之和。
②主要对卡片(Card)进行分组游戏,其规则为随机发放一些卡片给学生, 卡片仍然分为四种形状:圆形(Circle)、矩形(Rectangle)、三角形(Triangle)及梯形(Trapezoid), 并给出各种卡片的相应参数,要求学生首先根据卡片类型将所有卡片进行分组(一个类型分为一组, 所以最多四组),然后能够对每组内的卡片根据面积值从大到小进行排序,同时求出该组内所有卡片 的面积之和,最后求出每组卡片面积之和中的最大值。
第一题图形卡片排序游戏知识点主要是掌握类的继承、多态性使用方法以及接口的应用,第二题在第一题的基础上更加深入,需要考虑面向对象设计的“单一职责原则”,并且思考该程序是否能够符合“开-闭”原则,难度相对中等偏上。
题目集八
1.题目集八的的知识点
考察了类与类之间的调用以及迭代器遍历集合
2.题目集八的题量
ATM机类结构设计(一)
3.题目集八的难度
编写一个银行 ATM 机的模拟程序,能够完成用户的存款、取款以及查询余额功能;难度相对较大,整体思路的构建以及数据的调用和代码的编码都相对复杂。
题目集九
1.题目集九的的知识点
Java中的继承,接口和多态,各实体类之间的关系,尤其是一对多的组合关系。
2.题目集九的题量
ATM机类结构设计(二)
3.题目集九的难度
对第八次作业的拓展,知识点与其基本一致,主要在设计ATM各个类之间的关系,难度相较于上题又增大了,增加了跨行取款和透支的手续费,在代码的复杂程度上又增大了一个难度。
二:设计与分析
①题目集7(7-1)、(7-2)两道题目的递进式设计分析总结
题目集7(7-1)代码类图如下:
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 import java.util.Collections; 4 5 public class Main { 6 //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接 7 //使用Main.input.next…即可(避免采坑) 8 public static Scanner input = new Scanner(System.in); 9 10 public static void main(String[] args){ 11 ArrayList<Integer> list = new ArrayList<Integer>(); 12 int num = input.nextInt(); 13 while(num != 0){ 14 if(num < 0 || num > 4){ 15 System.out.println("Wrong Format"); 16 System.exit(0); 17 } 18 list.add(num); 19 num = input.nextInt(); 20 } 21 DealCardList dealCardList = new DealCardList(list); 22 if(!dealCardList.validate()){ 23 System.out.println("Wrong Format"); 24 System.exit(0); 25 } 26 dealCardList.showResult(); 27 input.close(); 28 } 29 } 30 31 class Card implements Comparable<Card>{ 32 33 private Shape shape; 34 35 public Card() { 36 37 } 38 public Card(Shape shape) { 39 setShape(shape); 40 } 41 public Shape getShape() { 42 return shape; 43 } 44 public void setShape(Shape shape) { 45 this.shape= shape; 46 } 47 48 @Override 49 public int compareTo(Card card) { 50 // TODO 自动生成的方法存根 51 if(shape.getArea()<card.getShape().getArea()) 52 return 1; 53 else if(shape.getArea()>card.getShape().getArea()) 54 return -1; 55 else return 0; 56 } 57 } 58 59 abstract class Shape { 60 61 private String shapeName; 62 63 public Shape() { 64 } 65 public Shape(String shapeName) { 66 setShapeName(shapeName); 67 } 68 public String getShapeName() { 69 return shapeName; 70 } 71 public void setShapeName(String shapeName) { 72 this.shapeName=shapeName; 73 } 74 public double getArea() { 75 return 0; 76 } 77 public boolean validate() { 78 return true; 79 } 80 public String toString() { 81 return getShapeName()+":"+String.format("%.2f", getArea())+" "; 82 } 83 84 } 85 86 class DealCardList { 87 88 private ArrayList<Card> cardList=new ArrayList<Card>(); 89 90 public DealCardList() { 91 } 92 public DealCardList(ArrayList<Integer> list) { 93 // TODO 自动生成的构造函数存根 94 for(Integer num : list){ 95 switch (num){ 96 case 1: this.cardList.add(new Card(new Circle(Main.input.nextDouble()))); break; 97 case 2: this.cardList.add(new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble()))); break; 98 case 3: this.cardList.add(new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()))); break; 99 case 4: this.cardList.add(new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()))); break; 100 default: break; 101 } 102 } 103 } 104 public boolean validate() { 105 for(Card card : cardList){ 106 if(!card.getShape().validate()){ 107 return false; 108 } 109 } 110 return true; 111 } 112 public void cardSort() { 113 Collections.sort(cardList); 114 } 115 public double getAllArea() { 116 double a = 0; 117 for(Card card : cardList){ 118 a += card.getShape().getArea(); 119 } 120 return a; 121 122 } 123 public void showResult() { 124 // TODO 自动生成的方法存根 125 System.out.println("The original list:"); 126 for(Card card : cardList){ System.out.print(card.getShape().toString()); } 127 cardSort(); 128 System.out.printf("\nThe sorted list:\n"); 129 for(Card card : cardList){ System.out.print(card.getShape().toString()); } 130 System.out.printf("\nSum of area:%.2f",getAllArea()); 131 } 132 } 133 134 class Circle extends Shape{ 135 private double radius; 136 public Circle() { 137 138 } 139 140 public Circle(double radius) { 141 super("Circle"); 142 this.radius = radius; 143 } 144 145 public double getRadius() { 146 return radius; 147 } 148 149 public void setRadius(double radius) { 150 this.radius = radius; 151 } 152 153 public double getArea(){ 154 double area= Math.PI*getRadius()*getRadius(); 155 return area; 156 157 } 158 public boolean validate() { 159 if(radius>0) { 160 return true; 161 }else { 162 return false; 163 } 164 165 } 166 /*public String toString() { 167 return getArea()+" "; 168 169 }*/ 170 } 171 172 class Rectangle extends Shape { 173 private double width; 174 private double length; 175 public Rectangle() { 176 177 } 178 public Rectangle(double width, double length) { 179 super("Rectangle"); 180 this.width = width; 181 this.length = length; 182 } 183 184 public double getWidth() { 185 return width; 186 } 187 public void setWidth(double width) { 188 this.width = width; 189 } 190 public double getLength() { 191 return length; 192 } 193 public void setLength(double length) { 194 this.length = length; 195 } 196 197 public double getArea(){ 198 double area = getWidth()*getLength(); 199 return area; 200 } 201 public boolean validate() { 202 if(width>0&&length>0) { 203 return true ; 204 }else { 205 return false; 206 } 207 } 208 209 /*public String toString() { 210 return getArea()+" "; 211 }*/ 212 213 } 214 215 class Triangle extends Shape { 216 private double side1; 217 private double side2; 218 private double side3; 219 220 public Triangle(){ 221 222 } 223 224 public Triangle(double side1,double side2,double side3) { 225 super("Triangle"); 226 this.side1 = side1; 227 this.side2 = side2; 228 this.side3 = side3; 229 } 230 231 public double getSide1() { 232 return side1; 233 } 234 public void setSide1(double side1) { 235 this.side1 = side1; 236 } 237 public double getSide2() { 238 return side2; 239 } 240 public void setSide2(double side2) { 241 this.side2 = side2; 242 } 243 public double getSide3() { 244 return side3; 245 } 246 public void setSide3(double side3) { 247 this.side3 = side3; 248 } 249 250 public double getArea() { 251 double s = (side1+side2+side3)*1.0/2; 252 double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)); 253 return area; 254 } 255 public boolean validate() { 256 if((side1>=0)&&(side2>=0)&&(side3>=0)&&(side1+side2>side3)&&(side1+side3>side2)&&(side2+side3>side1)){ 257 return true; 258 }else 259 return false; 260 } 261 262 /*public String toString() { 263 return getArea()+" "; 264 }*/ 265 266 } 267 268 class Trapezoid extends Shape{ 269 private double topSide; 270 private double bottomSide; 271 private double height; 272 public Trapezoid() { 273 274 } 275 public Trapezoid(double topSide,double bottomSide,double height) { 276 super("Trapezoid"); 277 this.topSide=topSide; 278 this.bottomSide=bottomSide; 279 this.height=height; 280 } 281 public double getArea() { 282 double area=(topSide+bottomSide)*height/2; 283 return area; 284 } 285 public boolean validate() { 286 if(topSide>0&&bottomSide>0&&height>0) { 287 return true; 288 }else { 289 return false; 290 } 291 } 292 }

题目集7(7-2)代码类图如下:
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 import java.util.Collections; 4 5 public class Main { 6 //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接 7 //使用Main.input.next…即可(避免采坑) 8 public static Scanner input = new Scanner(System.in); 9 10 public static void main(String[] args){ 11 ArrayList<Integer> list = new ArrayList<Integer>(); 12 int num = input.nextInt(); 13 while(num != 0){ 14 if(num < 0 || num > 4){ 15 System.out.println("Wrong Format"); 16 System.exit(0); 17 } 18 list.add(num); 19 num = input.nextInt(); 20 } 21 DealCardList dealCardList = new DealCardList(list); 22 if(!dealCardList.validate()){ 23 System.out.println("Wrong Format"); 24 System.exit(0); 25 } 26 dealCardList.showResult(); 27 input.close(); 28 } 29 } 30 31 class Card implements Comparable<Card>{ 32 33 private Shape shape; 34 35 public Card() { 36 37 } 38 public Card(Shape shape) { 39 setShape(shape); 40 } 41 public Shape getShape() { 42 return shape; 43 } 44 public void setShape(Shape shape) { 45 this.shape= shape; 46 } 47 48 @Override 49 public int compareTo(Card card) { 50 // TODO 自动生成的方法存根 51 if(shape.getArea()<card.getShape().getArea()) 52 return 1; 53 else if(shape.getArea()>card.getShape().getArea()) 54 return -1; 55 else return 0; 56 } 57 } 58 59 abstract class Shape { 60 61 private String shapeName; 62 63 public Shape() { 64 } 65 public Shape(String shapeName) { 66 setShapeName(shapeName); 67 } 68 public String getShapeName() { 69 return shapeName; 70 } 71 public void setShapeName(String shapeName) { 72 this.shapeName=shapeName; 73 } 74 public double getArea() { 75 return 0; 76 } 77 public boolean validate() { 78 return true; 79 } 80 public String toString() { 81 return getShapeName()+":"+String.format("%.2f", getArea())+" "; 82 } 83 84 } 85 86 class DealCardList { 87 88 private ArrayList<Card> cardList=new ArrayList<Card>(); 89 private ArrayList<Card> CircleList = new ArrayList<Card>(); 90 private ArrayList<Card> RectanglesList = new ArrayList<Card>(); 91 private ArrayList<Card> TriangleList = new ArrayList<Card>(); 92 private ArrayList<Card> TrapezoidList = new ArrayList<Card>(); 93 94 public DealCardList() { 95 } 96 public DealCardList(ArrayList<Integer> list) { 97 // TODO 自动生成的构造函数存根 98 for(Integer num : list){ 99 switch (num){ 100 case 1: this.cardList.add(new Card(new Circle(Main.input.nextDouble()))); 101 break; 102 case 2: this.cardList.add(new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble()))); 103 break; 104 case 3: this.cardList.add(new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()))); 105 break; 106 case 4: this.cardList.add(new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()))); 107 break; 108 default: break; 109 } 110 } 111 for( Card card : this.cardList){ 112 switch (card.getShape().getShapeName()){ 113 case "Circle": CircleList.add(card); 114 break; 115 case "Rectangle": RectanglesList.add(card); 116 break; 117 case "Triangle": TriangleList.add(card); 118 break; 119 case "Trapezoid": TrapezoidList.add(card); 120 break; 121 default: break; 122 } 123 } 124 } 125 public boolean validate() { 126 for(Card card : cardList){ 127 if(card.getShape().validate()){ 128 return true; 129 } 130 } 131 return false; 132 } 133 public void cardSort() { 134 Collections.sort(cardList); 135 Collections.sort(CircleList); 136 Collections.sort(RectanglesList); 137 Collections.sort(TriangleList); 138 Collections.sort(TrapezoidList); 139 } 140 public double getMaxArea() { 141 double max = 0; 142 double all = 0; 143 for(Card card : CircleList){ 144 all += card.getShape().getArea(); 145 } 146 if(all>max){ 147 max = all; 148 } 149 all = 0; 150 for(Card card : RectanglesList){ 151 all += card.getShape().getArea(); 152 } 153 if(all>max){ 154 max = all; 155 } 156 all = 0; 157 for(Card card : TriangleList){ 158 all += card.getShape().getArea(); 159 } 160 if(all>max){ 161 max = all; 162 } 163 all = 0; 164 for(Card card : TrapezoidList){ 165 all += card.getShape().getArea(); 166 } 167 if(all>max){ 168 max = all; 169 } 170 return max; 171 172 } 173 public void showResult() { 174 // TODO 自动生成的方法存根 175 System.out.print("The original list:\n["); 176 for(Card card : cardList){ 177 System.out.print(card.getShape().toString()); 178 } 179 System.out.printf("]\nThe Separated List:\n["); 180 for(Card card : CircleList){ 181 System.out.print(card.getShape().toString()); 182 } 183 System.out.printf("]["); 184 for(Card card : RectanglesList){ 185 System.out.print(card.getShape().toString()); 186 } 187 System.out.printf("]["); 188 for(Card card : TriangleList){ 189 System.out.print(card.getShape().toString()); 190 } 191 System.out.printf("]["); 192 for(Card card : TrapezoidList){ 193 System.out.print(card.getShape().toString()); 194 } 195 cardSort(); 196 System.out.printf("]\nThe Separated sorted List:\n["); 197 for(Card card : CircleList){ 198 System.out.print(card.getShape().toString()); 199 } 200 System.out.printf("]["); 201 for(Card card : RectanglesList){ 202 System.out.print(card.getShape().toString()); 203 } 204 System.out.printf("]["); 205 for(Card card : TriangleList){ 206 System.out.print(card.getShape().toString()); 207 } 208 System.out.printf("]["); 209 for(Card card : TrapezoidList){ 210 System.out.print(card.getShape().toString()); 211 } 212 System.out.printf("]\nThe max area:%.2f",getMaxArea()); 213 } 214 } 215 216 class Circle extends Shape{ 217 private double radius; 218 public Circle() { 219 220 } 221 222 public Circle(double radius) { 223 super("Circle"); 224 this.radius = radius; 225 } 226 227 public double getRadius() { 228 return radius; 229 } 230 231 public void setRadius(double radius) { 232 this.radius = radius; 233 } 234 235 public double getArea(){ 236 237 238 /*double area= Math.PI*getRadius()*getRadius(); 239 BigDecimal bg = new BigDecimal(area); 240 double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 241 return f1;*/ 242 243 double area= Math.PI*getRadius()*getRadius(); 244 return area; 245 246 } 247 public boolean validate() { 248 if(radius>0) { 249 return true; 250 }else { 251 return false; 252 } 253 254 } 255 /*public String toString() { 256 return getArea()+" "; 257 258 }*/ 259 } 260 261 class Rectangle extends Shape { 262 private double width; 263 private double length; 264 public Rectangle() { 265 266 } 267 public Rectangle(double width, double length) {//有参构造方法初始化 268 super("Rectangle"); 269 this.width = width; 270 this.length = length; 271 } 272 273 public double getWidth() { 274 return width; 275 } 276 public void setWidth(double width) { 277 this.width = width; 278 } 279 public double getLength() { 280 return length; 281 } 282 public void setLength(double length) { 283 this.length = length; 284 } 285 286 public double getArea(){ //计算面积并显示.showArea方法的重写 287 double area = getWidth()*getLength(); 288 return area; 289 } 290 public boolean validate() { 291 if(width>0&&length>0) { 292 return true ; 293 }else { 294 return false; 295 } 296 } 297 298 /*public String toString() { 299 return getArea()+" "; 300 }*/ 301 302 } 303 304 class Triangle extends Shape { 305 private double side1; 306 private double side2; 307 private double side3; 308 309 public Triangle(){ 310 311 } 312 313 public Triangle(double side1,double side2,double side3) { 314 super("Triangle"); 315 this.side1 = side1; 316 this.side2 = side2; 317 this.side3 = side3; 318 } 319 320 public double getSide1() { 321 return side1; 322 } 323 public void setSide1(double side1) { 324 this.side1 = side1; 325 } 326 public double getSide2() { 327 return side2; 328 } 329 public void setSide2(double side2) { 330 this.side2 = side2; 331 } 332 public double getSide3() { 333 return side3; 334 } 335 public void setSide3(double side3) { 336 this.side3 = side3; 337 } 338 339 public double getArea() { 340 double s = (side1+side2+side3)*1.0/2; 341 double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)); 342 return area; 343 } 344 public boolean validate() {//对图形的属性进行合法性校验 345 if((side1>=0)&&(side2>=0)&&(side3>=0)&&(side1+side2>side3)&&(side1+side3>side2)&&(side2+side3>side1)){ 346 return true; 347 }else 348 return false; 349 } 350 351 /*public String toString() { 352 return getArea()+" "; 353 }*/ 354 355 } 356 357 class Trapezoid extends Shape{ 358 private double topSide; 359 private double bottomSide; 360 private double height; 361 public Trapezoid() { 362 363 } 364 public Trapezoid(double topSide,double bottomSide,double height) { 365 super("Trapezoid"); 366 this.topSide=topSide; 367 this.bottomSide=bottomSide; 368 this.height=height; 369 } 370 public double getArea() { 371 double area=(topSide+bottomSide)*height/2; 372 return area; 373 } 374 public boolean validate() { 375 if(topSide>0&&bottomSide>0&&height>0) { 376 return true; 377 }else { 378 return false; 379 } 380 } 381 }

分析:
都在一定程度上实现了面向对象的编程思想,不过7-(2)多了多个相同图形面积排序和总面积排序,通过创建多个ArrayList动态数组分别存储相同图形的数据。
②题目集8和题目集9两道ATM机仿真题目的设计思路分析总结
题目集8代码类图如下:
设计ATM仿真系统,具体要求参见作业说明。 OO作业8-1题目说明.pdf
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
- 存款、取款功能输入数据格式:
卡号 密码 ATM机编号 金额(由一个或多个空格分隔), 其中,当金额大于0时,代表取款,否则代表存款。 - 查询余额功能输入数据格式:
卡号
输出格式:
①输入错误处理
- 如果输入卡号不存在,则输出
Sorry,this card does not exist.。 - 如果输入ATM机编号不存在,则输出
Sorry,the ATM's id is wrong.。 - 如果输入银行卡密码错误,则输出
Sorry,your password is wrong.。 - 如果输入取款金额大于账户余额,则输出
Sorry,your account balance is insufficient.。 - 如果检测为跨行存取款,则输出
Sorry,cross-bank withdrawal is not supported.。
②取款业务输出
输出共两行,格式分别为:
[用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
③存款业务输出
输出共两行,格式分别为:
[用户姓名]在[银行名称]的[ATM编号]上存款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
④查询余额业务输出
¥[金额]
金额保留两位小数。
输入样例1:
在这里给出一组输入。例如:
6222081502001312390 88888888 06 -500.00
#
输出样例1:
在这里给出相应的输出。例如:
张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00
输入样例2:
在这里给出一组输入。例如:
6217000010041315709 88888888 02 3500.00
#
输出样例2:
在这里给出相应的输出。例如:
杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥6500.00
输入样例3:
在这里给出一组输入。例如:
6217000010041315715
#
输出样例3:
在这里给出相应的输出。例如:
¥10000.00
输入样例4:
在这里给出一组输入。例如:
6222081502001312390 88888888 06 -500.00
6222081502051320786 88888888 06 1200.00
6217000010041315715 88888888 02 1500.00
6217000010041315709 88888888 02 3500.00
6217000010041315715
#
输出样例4:
在这里给出相应的输出。例如:
张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00
韦小宝在中国工商银行的06号ATM机上取款¥1200.00
当前余额为¥8800.00
杨过在中国建设银行的02号ATM机上取款¥1500.00
当前余额为¥8500.00
杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥5000.00
¥5000.00
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Main { 5 public static void main(String[] args) { 6 String [][]List1 = new String[][] {{"中国银联","中国建设银行","01"}, 7 {"中国银联","中国建设银行","02"}, 8 {"中国银联","中国建设银行","03"}, 9 {"中国银联","中国建设银行","04"}, 10 {"中国银联","中国工商银行","05"}, 11 {"中国银联","中国工商银行","06"}, 12 }; 13 String [][]List2 = new String[][] {{"杨过","中国建设银行","3217000010041315709","10000.00","6217000010041315709"}, 14 {"杨过","中国建设银行","3217000010041315709","10000.00","6217000010041315715"}, 15 {"杨过","中国建设银行","3217000010041315715","10000.00","6217000010041315718"}, 16 {"郭靖","中国建设银行","3217000010051320007","10000.00","6217000010051320007"}, 17 {"张无忌","中国工商银行","3222081502001312389","10000.00","6222081502001312389"}, 18 {"张无忌","中国工商银行","3222081502001312390","10000.00","6222081502001312390"}, 19 {"张无忌","中国工商银行","3222081502001312399","10000.00","6222081502001312399"}, 20 {"张无忌","中国工商银行","3222081502001312399","10000.00","6222081502001312400"}, 21 {"韦小宝","中国工商银行","3222081502051320785","10000.00","6222081502051320785"}, 22 {"韦小宝","中国工商银行","3222081502051320786","10000.00","6222081502051320786"}, 23 }; 24 Scanner input = new Scanner(System.in); 25 String a = input.nextLine(); 26 String []str = new String[10]; 27 String []out = new String[10]; 28 int n=0; 29 while(!a.equals("#")) { 30 str[n] = a;n++; 31 a = input.nextLine(); 32 } 33 34 for(int i=0;i<n;i++) { 35 String[] str1 = str[i].split("\\s+"); 36 int f=0; 37 for(int j=0;j<10;j++) { 38 if(str1[0].equals(List2[j][4])) { 39 f=1; 40 } 41 } 42 if(f==0) { 43 out[i]="Sorry,this card does not exist.\n"; 44 } 45 else { 46 if(str1.length==1) { 47 for(int j=0;j<10;j++) { 48 if(str1[0].equals(List2[j][4])) { 49 double x = Double.parseDouble(List2[j][3]); 50 out[i]="¥"+String.format("%.2f",x)+"\n"; 51 52 } 53 } 54 55 } 56 else { 57 if(str1[1].equals("88888888")) { 58 if(str1[2].matches("0[1-6]")) { 59 String bank =""; 60 for(int k=0;k<6;k++) { 61 if(str1[2].equals(List1[k][2])) { 62 bank = List1[k][1]; 63 } 64 } 65 double money = 0; 66 for(int j=0;j<10;j++) { 67 if(str1[0].equals(List2[j][4])) { 68 if(List2[j][1].equals(bank)) { 69 if(Double.parseDouble(List2[j][3])>=Double.parseDouble(str1[3])) { 70 money=Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3]); 71 if(j==0||j==1) { 72 List2[0][3]=money+""; 73 List2[1][3]=money+""; 74 } 75 if(j==6||j==7) { 76 List2[6][3]=money+""; 77 List2[7][3]=money+""; 78 } 79 List2[j][3]=money+""; 80 if(Double.parseDouble(str1[3])>=0) { 81 double money1 = Double.parseDouble(str1[3]); 82 out[i]=List2[j][0]+"在"+List2[j][1]+"的"+str1[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", money)+"\n"; 83 } 84 else { 85 double money1 = -Double.parseDouble(str1[3]); 86 out[i]=List2[j][0]+"在"+List2[j][1]+"的"+str1[2]+"号ATM机上存款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", money)+"\n"; 87 } 88 } 89 else { 90 out[i]="Sorry,your account balance is insufficient.\n"; 91 } 92 } 93 else { 94 out[i]="Sorry,cross-bank withdrawal is not supported.\n"; 95 } 96 } 97 } 98 } 99 else { 100 out[i]="Sorry,the ATM's id is wrong.\n"; 101 } 102 103 } 104 else { 105 out[i]="Sorry,your password is wrong.\n"; 106 } 107 } 108 109 110 111 112 113 } 114 115 } 116 for(int i=0;i<n;i++) { 117 System.out.print(out[i]); 118 } 119 } 120 121 }

题目集9代码类图如下:
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Main { 5 public static void main(String[] args) { 6 String [][]List1 = new String[][] {{"中国银联","中国建设银行","01"}, 7 {"中国银联","中国建设银行","02"}, 8 {"中国银联","中国建设银行","03"}, 9 {"中国银联","中国建设银行","04"}, 10 {"中国银联","中国工商银行","05"}, 11 {"中国银联","中国工商银行","06"}, 12 {"中国银联","中国农业银行","07"}, 13 {"中国银联","中国农业银行","08"}, 14 {"中国银联","中国农业银行","09"}, 15 {"中国银联","中国农业银行","10"}, 16 {"中国银联","中国农业银行","11"}, 17 }; 18 String [][]List2 = new String[][] {{"杨过","中国建设银行","3217000010041315709","10000.00","6217000010041315709","借记账号"}, 19 {"杨过","中国建设银行","3217000010041315709","10000.00","6217000010041315715","借记账号"}, 20 {"杨过","中国建设银行","3217000010041315715","10000.00","6217000010041315718","借记账号"}, 21 {"郭靖","中国建设银行","3217000010051320007","10000.00","6217000010051320007","借记账号"}, 22 {"张无忌","中国工商银行","3222081502001312389","10000.00","6222081502001312389","借记账号"}, 23 {"张无忌","中国工商银行","3222081502001312390","10000.00","6222081502001312390","借记账号"}, 24 {"张无忌","中国工商银行","3222081502001312399","10000.00","6222081502001312399","借记账号"}, 25 {"张无忌","中国工商银行","3222081502001312399","10000.00","6222081502001312400","借记账号"}, 26 {"韦小宝","中国工商银行","3222081502051320785","10000.00","6222081502051320785","借记账号"}, 27 {"韦小宝","中国工商银行","3222081502051320786","10000.00","6222081502051320786","借记账号"}, 28 {"张三丰","中国建设银行","3640000010045442002","10000.00","6640000010045442002","贷记账号"}, 29 {"张三丰","中国建设银行","3640000010045442002","10000.00","6640000010045442003","贷记账号"}, 30 {"令狐冲","中国工商银行","3640000010045441009","10000.00","6640000010045441009","贷记账号"}, 31 {"乔峰","中国农业银行","3630000010033431001","10000.00","6630000010033431001","贷记账号"}, 32 {"洪七公","中国农业银行","3630000010033431008","10000.00","6630000010033431008","贷记账号"}, 33 34 35 }; 36 Scanner input = new Scanner(System.in); 37 String a = input.nextLine(); 38 String []str = new String[10]; 39 String []out = new String[10]; 40 int n=0; 41 while(!a.equals("#")) { 42 str[n] = a;n++; 43 a = input.nextLine(); 44 } 45 46 for(int i=0;i<n;i++) { 47 String[] str1 = str[i].split("\\s+"); 48 int f=0; 49 for(int j=0;j<List2.length;j++) { 50 if(str1[0].equals(List2[j][4])) { 51 f=1; 52 } 53 } 54 if(f==0) { 55 out[i]="Sorry,this card does not exist.\n"; 56 } 57 else { 58 if(str1.length==1) { 59 for(int j=0;j<List2.length;j++) { 60 if(str1[0].equals(List2[j][4])) { 61 double x = Double.parseDouble(List2[j][3]); 62 out[i]="业务:查询余额 ¥"+String.format("%.2f",x)+"\n"; 63 64 } 65 } 66 67 } 68 else { 69 if(str1[1].equals("88888888")) { 70 if(str1[2].matches("0[1-9]")) { 71 String bank =""; 72 for(int k=0;k<11;k++) { 73 if(str1[2].equals(List1[k][2])) { 74 bank = List1[k][1]; 75 } 76 } 77 double money = 0; 78 for(int j=0;j<List2.length;j++) { 79 if(str1[0].equals(List2[j][4])) { 80 if(List2[j][1].equals(bank)) { 81 if(Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])>=0){ 82 money=Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3]); 83 if(j==0||j==1||j==2) { 84 List2[0][3]=money+""; 85 List2[1][3]=money+""; 86 List2[2][3]=money+""; 87 } 88 if(j==4||j==5||j==6||j==7) { 89 List2[4][3]=money+""; 90 List2[5][3]=money+""; 91 List2[6][3]=money+""; 92 List2[7][3]=money+""; 93 } 94 if(j==8||j==9) { 95 List2[8][3]=money+""; 96 List2[9][3]=money+""; 97 } 98 if(j==10||j==11) { 99 List2[10][3]=money+""; 100 List2[11][3]=money+""; 101 } 102 List2[j][3]=money+""; 103 if(Double.parseDouble(str1[3])>=0&&money>=-50000) { 104 double money1 = Double.parseDouble(str1[3]); 105 out[i]="业务:取款 "+List2[j][0]+"在"+bank+"的"+str1[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", money)+"\n"; 106 107 }else if(money<=-50000){ 108 out[i]="Sorry,your account balance is insufficient."; 109 } 110 111 } 112 else if(Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])<=0&&List2[j][5].equals("贷记账号")) { 113 114 double s; 115 if(Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])<=0&&Double.parseDouble(List2[j][3])>=0) { 116 s = -(Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])); 117 money = Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])-0.05*s; 118 } 119 if(Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])<=0&&Double.parseDouble(List2[j][3])<=0) { 120 s = Double.parseDouble(str1[3]); 121 money = Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])-0.05*s; 122 } 123 if(j==0||j==1||j==2) { 124 List2[0][3]=money+""; 125 List2[1][3]=money+""; 126 List2[2][3]=money+""; 127 } 128 if(j==4||j==5||j==6||j==7) { 129 List2[4][3]=money+""; 130 List2[5][3]=money+""; 131 List2[6][3]=money+""; 132 List2[7][3]=money+""; 133 } 134 if(j==8||j==9) { 135 List2[8][3]=money+""; 136 List2[9][3]=money+""; 137 } 138 if(j==10||j==11) { 139 List2[10][3]=money+""; 140 List2[11][3]=money+""; 141 } 142 List2[j][3]=money+""; 143 if(Double.parseDouble(str1[3])>=0&&money>=-50000) { 144 double money1 = Double.parseDouble(str1[3]); 145 out[i]="业务:取款 "+List2[j][0]+"在"+bank+"的"+str1[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", money)+"\n"; 146 147 }else if(money<=-50000){ 148 out[i]="Sorry,your account balance is insufficient."; 149 } 150 } 151 else { 152 out[i]="Sorry,your account balance is insufficient."; 153 } 154 } 155 else { 156 double s1; 157 if(bank.equals("中国建设银行")) { 158 s1 = Double.parseDouble(str1[3])*0.02; 159 }else if(bank.equals("中国工商银行")) { 160 s1 = Double.parseDouble(str1[3])*0.03; 161 }else{ 162 s1 = Double.parseDouble(str1[3])*0.04; 163 } 164 // out[i]="Sorry,cross-bank withdrawal is not supported.\n"; 165 if(Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])-s1>=0){ 166 167 money=Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])-s1; 168 if(j==0||j==1||j==2) { 169 List2[0][3]=money+""; 170 List2[1][3]=money+""; 171 List2[2][3]=money+""; 172 } 173 if(j==4||j==5||j==6||j==7) { 174 List2[4][3]=money+""; 175 List2[5][3]=money+""; 176 List2[6][3]=money+""; 177 List2[7][3]=money+""; 178 } 179 if(j==8||j==9) { 180 List2[8][3]=money+""; 181 List2[9][3]=money+""; 182 } 183 if(j==10||j==11) { 184 List2[10][3]=money+""; 185 List2[11][3]=money+""; 186 } 187 List2[j][3]=money+""; 188 if(Double.parseDouble(str1[3])>=0&&money>=-50000) { 189 double money1 = Double.parseDouble(str1[3]); 190 out[i]="业务:取款 "+List2[j][0]+"在"+bank+"的"+str1[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", money)+"\n"; 191 192 } 193 else if(money<=-50000){ 194 out[i]="Sorry,your account balance is insufficient."; 195 } 196 } 197 else if(Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])-s1<=0&&List2[j][5].equals("贷记账号")) { 198 199 double s; 200 if(Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])<=0&&Double.parseDouble(List2[j][3])>=0) { 201 s = -(Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])); 202 money = Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])-0.05*s-s1; 203 } 204 if(Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])<=0&&Double.parseDouble(List2[j][3])<=0) { 205 s =Double.parseDouble(str1[3]); 206 money = Double.parseDouble(List2[j][3])-Double.parseDouble(str1[3])-0.05*s; 207 } 208 if(j==0||j==1||j==2) { 209 List2[0][3]=money+""; 210 List2[1][3]=money+""; 211 List2[2][3]=money+""; 212 } 213 if(j==4||j==5||j==6||j==7) { 214 List2[4][3]=money+""; 215 List2[5][3]=money+""; 216 List2[6][3]=money+""; 217 List2[7][3]=money+""; 218 } 219 if(j==8||j==9) { 220 List2[8][3]=money+""; 221 List2[9][3]=money+""; 222 } 223 if(j==10||j==11) { 224 List2[10][3]=money+""; 225 List2[11][3]=money+""; 226 } 227 List2[j][3]=money+""; 228 if(Double.parseDouble(str1[3])>=0&&money>=-50000) { 229 double money1 = Double.parseDouble(str1[3]); 230 out[i]="业务:取款 "+List2[j][0]+"在"+bank+"的"+str1[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", money)+"\n"; 231 232 }else if(money<=-50000){ 233 out[i]="Sorry,your account balance is insufficient."; 234 } 235 } 236 else { 237 out[i]="Sorry,your account balance is insufficient."; 238 } 239 240 } 241 } 242 } 243 } 244 else { 245 out[i]="Sorry,the ATM's id is wrong.\n"; 246 } 247 248 } 249 else { 250 out[i]="Sorry,your password is wrong.\n"; 251 } 252 } 253 254 255 256 257 258 } 259 260 } 261 for(int i=0;i<n;i++) { 262 System.out.print(out[i]); 263 } 264 } 265 266 }


分析:
题目集8主要是编写一个银行 ATM 机的模拟程序,能够完成用户的存款、取款以及查询余额功能;而题目集9难度相较于上题又增大了,增加了跨行取款和透支的手续费。题目集8的设计方面有两个难点,一是数据的初始化,二是如何实现从下而上的查找(如给账户找拥有者)。而且在题目说明里明确说了业务类自行添加。也就是说,像实体类只能进行添加、删除、获取的操作,而对数据进行进一步操作就必须要用业务类了。其实题目集8从下而上的查找是解决此题的关键。使用Iterator(迭代器)和多次while循环,可以进行遍历所有数据进行匹配,最终判断数据是否存在。题目集9的设计方面难点在于如何实现多态。题目说明着重写了卡能否贷款取决于账户类型,所以账户是肯定要继承于抽象类的,同时也便于再添加新类型的账户。而两种账户唯一的差别就在于能否贷款,所以可以针对能否贷款这一点来构建抽象类。至于跨行手续费,和贷款手续费就只是在相应类里面加一个属性的事。
三.采坑心得
本章主要是如何运用arraylist,使用了ArrayList,即意味着程序很少会出现for、while等循环语句,能有效的减少程序的复杂度。当我们不知道到底有多少个数据元素的时候,
就可使用ArrayList;如果知道数据集合有多少个元素,就用数组。封装了数组:操作更简便,代码可读性更高。但是也封装了额外操作,比如安全检查,数组是否越界等,这也带来一些性能开销,
所以ArrayList性能会比数组稍稍低那么一点点。但arraylist的运用开始还不熟练,所以一些题目开始做不出来。
四.改进意见
1.题目难度相对来说偏难,这样的话不仅对于知识的掌握相对较困难,也会打击学生的自信。
2.对于有些题目来说,类名、方法题目已经规定好了,所以大家的代码重复率会大大增加,那查重的作用就会大打折扣,我觉得还是可以适当不规定那么多,尽量让学生发挥创作空间。
五.总结
通过这三次题目集,让我对于Java的了解与运用上了很大一个台阶,在这一阶段中,我们主要学习了类的封装性以及类与类之间的关系设计,类的继承、多态性使用方法以及接口的应用。经过这几次练习,我们对类之间的关系有了更深的了解,并且更深的明白了对象的意义。以后我们可以在如何使代码更加简单,更加具有复用性深入。

浙公网安备 33010602011771号