OO题目集7~9作业总结
OO题目集7~9作业总结
1.前言
针对于PTA题目集7~9的作业,有如下分析:
题目集7的知识点、题量、难度:
7-1:图形卡片排序游戏:继承、多态的应用;ArrayList泛型的应用方法;Comparable接口及泛型的应用;单一职责原则的应用;“开-闭”原则的应用;
7-2:图形卡片分组游戏:沿袭图片卡片排序游戏,对卡片进行分组,考察继承与多态等知识点。
题目集7的难度:相对于我个人而言,对于图形卡片排序以及卡片分组游戏,感觉很难。需要用到继承与多态,提高代码的复用性,以及对Comparable接口及泛型需要了解透彻,明白单一职责原则以及开闭原则的应用。
题目集8的知识点、题量、难度:
7-1:ATM机类结构设计(一):设计ATM仿真系统,要求可实现存款、取款以及查询余额的功能;注意题目中的实体类关系尤其是一对多的组合关系,单一职责原则等。
题目集8的难度:多行输入多行输出,需要对输出进行存储,输入有误时之前正确的输入给予输出。
题目集9的知识点、题量、难度:
7-1:ATM机类结构设计(二):设计ATM仿真系统,要求含有借记卡贷记卡等银行卡,实现本行取款、跨行取款并收取手续费以及查询余额的功能;注意题目中的实体类关系尤其是一对多的组合关系,符合单一职责原则、合成复用原则以及开闭原则等。
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 public static void main(String[] args){
10 ArrayList<Integer> list = new ArrayList<Integer>();
11 int num = input.nextInt();
12 while(num != 0){
13 if(num < 0 || num > 4){
14 System.out.println("Wrong Format");
15 System.exit(0);
16 }
17 list.add(num);
18 num = input.nextInt();
19 }
20 DealCardList dealCardList = new DealCardList(list);
21 if(!dealCardList.validate()){
22 System.out.println("Wrong Format");
23 System.exit(0);
24 }
25 dealCardList.showResult();
26 input.close();
27 }
28 }
29 class DealCardList {
30 private ArrayList<Card> cardList = new ArrayList<Card>();
31
32 public DealCardList() {
33
34 }
35
36 public DealCardList(ArrayList<Integer> list) {
37 Scanner input = new Scanner(System.in);
38 ArrayList<Card> cardlist = new ArrayList<Card>();
39 this.cardList = cardlist;
40 for(int i = 0;i < list.size();i++){
41 if(list.get(i) == 1){
42 Circle circle = new Circle(Main.input.nextDouble());
43 circle.setShapeName("Circle");
44 Card cardradius = new Card(circle);
45 cardlist.add(cardradius);
46 }else if(list.get(i) == 2){
47 Rectangle rectangle = new Rectangle(Main.input.nextDouble(),Main.input.nextDouble());
48 rectangle.setShapeName("Rectangle");
49 Card cardrectangle = new Card(rectangle);
50 cardlist.add(cardrectangle);
51 }else if(list.get(i) == 3){
52 Triangle triangle = new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
53 triangle.setShapeName("Triangle");
54 Card cardtriangle = new Card(triangle);
55 cardlist.add(cardtriangle);
56 }else{
57 Trapezoid trapezoid = new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
58 trapezoid.setShapeName("Trapezoid");
59 Card cardtrapezoid = new Card(trapezoid);
60 cardlist.add(cardtrapezoid);
61 }
62 }
63 }
64
65 public boolean validate() {
66 int flag = 1;
67 for(int i = 0;i < cardList.size();i++) {
68 if(cardList.get(i).getShape().validate() == false) {
69 flag = 0;
70 }
71 }
72 if(flag == 0) {
73 return false;
74 }else {
75 return true;
76 }
77 }
78
79 public void showResult() {
80 System.out.println("The original list:");
81 for(int i = 0;i < cardList.size();i++) {
82 System.out.printf(cardList.get(i).getShape().toString());
83 }
84 System.out.println("\nThe sorted list:");
85 Collections.sort(cardList);
86 for(int i = 0;i < cardList.size();i++) {
87 System.out.printf(cardList.get(i).getShape().toString());
88 }
89 System.out.printf("\nSum of area:");
90 double sum = 0;
91 for(int i = 0;i < cardList.size();i++) {
92 sum += cardList.get(i).getShape().getArea();
93 }
94 System.out.printf("%.2f",sum);
95 }
96
97 }
98 abstract class Shape {
99 private String shapeName;
100 public abstract double getArea();
101 public abstract boolean validate();
102 public abstract String toString();
103 public Shape(){
104
105 }
106 public Shape(String shapeName){
107 this.shapeName = shapeName;
108 }
109 public String getShapeName() {
110 return shapeName;
111 }
112 public void setShapeName(String shapeName) {
113 this.shapeName = shapeName;
114 }
115
116 }
117 class Card implements Comparable<Card>{
118 private Shape shape;
119
120 public Card() {
121
122 }
123
124 public Card(Shape shape) {
125 this.shape = shape;
126 }
127
128 public Shape getShape() {
129 return shape;
130 }
131
132 public void setShape(Shape shape) {
133 this.shape = shape;
134 }
135 @Override
136 public int compareTo(Card card) {
137 if(this.shape.getArea() < card.shape.getArea()) {
138 return 1;
139 }else if(this.shape.getArea() > card.shape.getArea()) {
140 return -1;
141 }else {
142 return 0;
143 }
144 }
145 }
146
147 class Circle extends Shape{
148 private double radius;
149 public Circle() {
150
151 }
152 public Circle(double radius) {
153 this.radius = radius;
154 }
155
156 public double getRadius() {
157 return radius;
158 }
159
160 public void setRadius(double radius) {
161 this.radius = radius;
162 }
163
164 @Override
165 public double getArea() {
166 return Math.PI*this.radius*this.radius;
167 }
168
169 @Override
170 public boolean validate() {
171 if(this.radius < 0) {
172 return false;
173 }else {
174 return true;
175 }
176 }
177 @Override
178 public String toString() {
179 String str = this.getShapeName() + ":" + String.format("%.2f", getArea()) + " ";
180 return str;
181 }
182
183 }
184 class Rectangle extends Shape{
185 private double width;
186 private double length;
187 public Rectangle() {
188
189 }
190 public Rectangle(double width, double length) {
191 this.width = width;
192 this.length = length;
193 }
194 public double getWidth() {
195 return width;
196 }
197 public void setWidth(double width) {
198 this.width = width;
199 }
200 public double getLength() {
201 return length;
202 }
203 public void setLength(double length) {
204 this.length = length;
205 }
206 @Override
207 public double getArea() {
208 return this.length * this.width;
209 }
210 @Override
211 public boolean validate() {
212 if(this.length < 0||this.width < 0) {
213 return false;
214 }else {
215 return true;
216 }
217 }
218 @Override
219 public String toString() {
220 String str = this.getShapeName() + ":" + String.format("%.2f", getArea()) + " ";
221 return str;
222 }
223
224 }
225 class Triangle extends Shape{
226 private double side1;
227 private double side2;
228 private double side3;
229 public Triangle() {
230
231 }
232 public Triangle(double side1, double side2, double side3) {
233 this.side1 = side1;
234 this.side2 = side2;
235 this.side3 = side3;
236 }
237 public double getSide1() {
238 return side1;
239 }
240 public void setSide1(double side1) {
241 this.side1 = side1;
242 }
243 public double getSide2() {
244 return side2;
245 }
246 public void setSide2(double side2) {
247 this.side2 = side2;
248 }
249 public double getSide3() {
250 return side3;
251 }
252 public void setSide3(double side3) {
253 this.side3 = side3;
254 }
255 @Override
256 public double getArea() {
257 double p = (this.getSide1() + this.getSide2() + this.getSide3()) / 2;
258 double area = Math.sqrt(p * (p - this.getSide1()) * (p - this.getSide2()) * (p - this.getSide3()));
259 return area;
260 }
261 @Override
262 public boolean validate() {
263 if(this.side1 + this.side2 > this.side3&&this.side1 + this.side3 > this.side2&&this.side3 + this.side2 > this.side1) {
264 return true;
265 }else {
266 return false;
267 }
268 }
269 @Override
270 public String toString() {
271 String str = this.getShapeName() + ":" + String.format("%.2f", getArea()) + " "