Blog作业2

第二阶段的学习结束了,接下来做个在总结吧。

前言:

  1. PTA:本阶段完成了三次PTA的题目集,其中,最难的莫过于几何多边形的实现了。然后就是对正则表达式的考察,还有一个ATM机系统。最后完成了一次期中测验,主要是对多态的考察,难度不算大。

  2. 学习通:完成了链表和双向链表的代码编写,难度适中,但是个人感觉完成度不是很完美。

  3. 实验:主要是对农夫过河这个小游戏的完善迭代,也是对继承和多态的考察。

  (注:为了排版方便和美观,接下来的代码都折叠了,可以点开查看!!!)

作业内容:

  PTA:

    1、正则表达式:这部分内容比较简单,靠的是自学。显示了解了正则表达从式匹配字符的机制。主要就是一组一组,一个圆括号()为一组,然后就是一些表达式。

      例如:[ ],+,-,\s等等的作用,还有类Pattern,Match的运用。不过一般可以不用调用,直接用字符串String有的方法 s.match(regex);就能够判断了。

 1 import java.util.Scanner;
 2 import java.util.regex.Matcher;
 3 import java.util.regex.Pattern;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8         Scanner in = new Scanner(System.in);
 9         
10         String qqNums = in.next();
11         String regex = "^([1-9])([\\d]{4,14})$";
12         Pattern p = Pattern.compile(regex);
13         Matcher m = p.matcher(qqNums);
14         
15         if(m.find()) 
16             System.out.println("你输入的QQ号验证成功");
17         else 
18             System.out.println("你输入的QQ号验证失败");
19         
20         in.close();
21     }
22 
23 }
View Code

     这里是其中一段检测qq号的。

    2、多边形:

      到这里我可太多要说得了!由于一开始看题目没有整题分析,导致后期的代码非常难写!最开始我是现有一些些简单的设计的,比如MCV模式如何运用到这题,但是弄巧成拙,对象的思想并没有太                  

    深刻的理解,仅仅是简单的分为输入,处理,输出,中间的类,然后导致每一题都跟新题目一样!现在都没眼看那时候写的代码,不过还是放上去吧  

  1 import java.util.Scanner;
  2 import java.util.ArrayList;
  3 public class Main {
  4 
  5     public static void main(String[] args) {
  6         Intermediary inter = new Intermediary();
  7         inter.work();
  8     }
  9 }
 10 
 11 class Intermediary {
 12     ProcessData process = new ProcessData();
 13     Calculate calculate = new Calculate();
 14     Display display = new Display();
 15     
 16     public void work() {
 17 
 18         Scanner in = new Scanner(System.in);
 19         process.setcoordinates(in.nextLine());
 20         in.close();
 21         
 22         process.segMentation();
 23         if(process.dateValidity() && process.getNumbersOfComma() <= 5)
 24             process.transform();
 25 
 26         if(process.worngFormat() == 1) {
 27             calculate.setChoose(process.getChoose());
 28             calculate.setArr(process.getCoord());
 29             calculate.doing();
 30             process.setTriangle(calculate.isTriangle());
 31 
 32             display.setWrongFormat(process.worngFormat());
 33             display.setChoose(process.getChoose());
 34             display.setStr(calculate.getCoord());
 35         }else {
 36             display.setWrongFormat(process.worngFormat());
 37         }
 38         display.show();
 39     }
 40 }
 41 
 42 class ProcessData {
 43     
 44     private ArrayList<String> coordinate = new ArrayList<String>(); 
 45     private String coordinates;
 46     private int numbersOfBlank =0 , numbersOfComma = 0;
 47     private double [] coord = new double[11];
 48     private int choose;
 49     private boolean isTriangle = true;
 50     
 51     public void setTriangle(boolean isTriangle) {
 52         this.isTriangle = isTriangle;
 53     }
 54 
 55     public int getChoose() {
 56         return choose;
 57     }
 58 
 59     public int getNumbersOfComma() {
 60         return numbersOfComma;
 61     }
 62 
 63     public double[] getCoord() {
 64         return coord;
 65     }
 66     
 67     public void setcoordinates(String coordinates) {
 68         this.coordinates = coordinates;
 69     }
 70     
 71     public void transform() {
 72         int i = 0;
 73         for(String a : coordinate) {
 74             this.coord[i] = Double.parseDouble(a);
 75             i++;
 76         }
 77     }
 78 
 79     public boolean blankValidity() {
 80         if(this.numbersOfComma - 1 == this.numbersOfBlank)
 81             return true;
 82         return false;
 83     }
 84     
 85     public int worngFormat() {
 86         if(!this.dateValidity() && !this.blankValidity())
 87             return 0;
 88         else if(((choose == 1 || choose == 2 || choose == 3) && this.numbersOfComma != 3)||
 89                 (choose == 4 && this.numbersOfComma != 5)||
 90                 ( choose == 5 && this.numbersOfComma != 4)||
 91                 this.numbersOfComma > 5) {
 92             return 2;
 93         } else if(choose == 4) {
 94             if(coord[1] == coord [3] && coord[2] == coord[4])
 95                 return 4;
 96             if(!isTriangle)
 97                 return 5;
 98         } else if(choose == 1 || choose == 2 || choose == 3) {
 99             if(!isTriangle)
100                 return 5;
101         } return 1; 
102     }
103     
104     public void segMentation() {
105         int j = 0;
106         for(int i = 0; i < coordinates.length(); i++) {
107             if(coordinates.charAt(i) == ',') {
108                 coordinate.add(coordinates.substring(j, i));
109                 j = i + 1;
110                 numbersOfComma++;
111             }
112             
113             if(coordinates.charAt(i) == ' ') {
114                 coordinate.add(coordinates.substring(j,i));
115                 j = i + 1;
116                 numbersOfBlank++;
117             }
118             if(coordinates.charAt(i) == ':') {
119                 coordinate.add(coordinates.substring(j,i));
120                 j = i + 1;
121             }
122         }
123         coordinate.add(coordinates.substring(j,coordinates.length()));
124         choose = Integer.parseInt(coordinates.substring(0, 1));
125     }
126 
127     
128     public boolean dateValidity() {
129         if(this.choose >=1 && this.choose <=5) {
130             String pattern = "[12345]\\:[\\+|-]?([0]|([1-9]((\\d)?)+))(\\.\\d+)?\\,([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?(\\s([+\\-" + "]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?\\,([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?)+";
131             if(this.coordinates.matches(pattern)) {
132                 return true;
133             }
134         }
135         return false;
136     }
137 
138 }
139 
140 class Display {
141     private int wrongFormat = 0,choose = 0;
142     private String str = "";
143     
144     public void setChoose(int choose) {
145         this.choose = choose;
146     }
147 
148     public void setWrongFormat(int wrongFormat) {
149         this.wrongFormat = wrongFormat;
150     }
151     
152     public void setStr(String str) {
153         this.str = str;
154     }
155 
156     public void show() {
157         if(this.wrongFormat == 0)
158             System.out.println("Wrong Format");
159         else if(this.wrongFormat == 2)
160             System.out.println("wrong number of points");
161         else if(this.wrongFormat == 4)
162             System.out.println("points coincide");
163         else if(this.wrongFormat == 5)
164             System.out.println("data error");
165         else if(this.wrongFormat == 1){
166             if(choose == 1 || choose == 2 || choose == 3) 
167                 System.out.println(this.str);
168             else if(choose == 4) {
169                 
170             }else if(choose == 5) {
171                 System.out.println(str);
172             }
173         }
174     }
175 }
176 
177 class Calculate {
178     private double[] arr;
179     private int choose;
180     String coord = "";
181     
182     public String getCoord() {
183         return coord;
184     }
185 
186     public void setChoose(int choose) {
187         this.choose = choose;
188     }
189     
190     public void setArr(double[] arr) {
191         this.arr = arr;
192     }
193     
194     public void doing() {
195         if(this.choose == 1) {
196             equilateralTriangle();
197         } else if(this.choose == 2) {
198             this.OptionTwo();
199         } else if(this.choose == 3) {
200             this.optionThree();
201         } else if(this.choose == 4) {
202         } else {
203         }
204     }
205 
206     private void optionThree() {
207         double x,y,z;
208         x = (arr[3]-arr[1])*(arr[5]-arr[1])+(arr[4]-arr[2])*(arr[6]-arr[2]);
209         y = (arr[1]-arr[3])*(arr[5]-arr[3])+(arr[2]-arr[4])*(arr[6]-arr[4]);
210         z = (arr[1]-arr[5])*(arr[3]-arr[5])+(arr[4]-arr[6])*(arr[2]-arr[6]);
211         if(x==0 || y==0 || z==0) {
212             coord = "false true false";
213         }else if(x < 0 || y < 0 || z < 0) {
214             coord = "true false false";
215         }else if(x > 0 && y > 0 && z > 0) {
216             coord = "false false true";
217         }
218     }
219     
220     public void equilateralTriangle() {
221         double x,y,z;
222         x = Math.sqrt(Math.pow((arr[1]-arr[3]),2) + Math.pow((arr[2]-arr[4]),2));
223         y = Math.sqrt(Math.pow((arr[1]-arr[5]),2) + Math.pow((arr[2]-arr[6]),2));
224         z = Math.sqrt(Math.pow((arr[3]-arr[5]),2) + Math.pow((arr[4]-arr[6]),2));
225         if(x==y && x==z && y==z)
226             coord = "true true";
227         else if(x==y || x==z || y==z)
228             coord = "true false";
229         else coord = "false false";
230     }
231     
232     public String transfer(double c) {
233         String str_d = String.valueOf(c);
234               str_d = str_d.substring(str_d.indexOf(".") + 1);
235                 int len = str_d.length();
236                 len = len > 6 ? 6 : len;
237                  String area1 = String.format("%."+len+"f", c);
238                  return area1;
239     }
240     
241     public void OptionTwo() {
242         double x,y,z,area,x1,y1;
243         x = Math.sqrt(Math.pow((arr[1]-arr[3]),2) + Math.pow((arr[2]-arr[4]),2));
244         y = Math.sqrt(Math.pow((arr[1]-arr[5]),2) + Math.pow((arr[2]-arr[6]),2));
245         z = Math.sqrt(Math.pow((arr[3]-arr[5]),2) + Math.pow((arr[4]-arr[6]),2));
246         area = Math.abs((arr[1] - arr[3])*(arr[2]-arr[6])-(arr[1]-arr[5])*(arr[2]-arr[4]))/2;
247         x1 = (arr[1]+arr[3]+arr[5])/3;
248         y1 = (arr[2]+arr[4]+arr[6])/3;
249         if(this.isTriangle()){
250             coord += transfer(x+y+z)+" "+transfer(area)+" "
251                     +transfer(x1) +","+transfer(y1);
252         }
253     }
254 
255     public boolean isTriangle() {
256         double len1,len2,len3;
257         if(choose == 1 || choose == 2 || choose == 3) {
258             len1=Math.sqrt((arr[1]-arr[3])*(arr[1]-arr[3])+((arr[2]-arr[4])*(arr[2]-arr[4])));
259             len2=Math.sqrt((arr[1]-arr[5])*(arr[1]-arr[5])+((arr[2]-arr[6])*(arr[2]-arr[6])));
260             len3=Math.sqrt((arr[3]-arr[5])*(arr[3]-arr[5])+((arr[6]-arr[4])*(arr[6]-arr[4])));
261         } else {
262             len1=Math.sqrt((arr[7]-arr[9])*(arr[7]-arr[9])+((arr[8]-arr[10])*(arr[8]-arr[10])));
263             len2=Math.sqrt((arr[7]-arr[5])*(arr[7]-arr[5])+((arr[8]-arr[6])*(arr[8]-arr[6])));
264             len3=Math.sqrt((arr[9]-arr[5])*(arr[9]-arr[5])+((arr[6]-arr[10])*(arr[6]-arr[10])));
265         }
266         if(Math.abs(len1+len2-len3) > 0.01 && 
267                 Math.abs(len3+len2-len1) > 0.01 && 
268                 Math.abs(len1+len3-len2) > 0.01) {
269             return true;
270         }else return false;
271     }
272 }
View Code

    PTA04-3:这是三角形的。写的真的丑,而且到最后实在不会写了,还没写完,浅看一下吧。。。

  类图奉上:

        然后老师发了他的类图,对比一下才发现原来应该这样写!

     然后我花了三个下午的时间,完成了PTA06-2:

     PTA06-2

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 ////
  5 public class Main {
  6 
  7     public static void main(String[] args) {
  8         InputData d = new InputData();
  9         Scanner input = new Scanner(System.in);
 10 
 11         String s = input.nextLine().trim();
 12 
 13         BasicType.isBasicType(s);
 14 
 15         ParseInput.parseInput(s, d);
 16         PointInputError.wrongNumberOfPoints(d.getPoints(), d.getChoice());
 17         if (d.getChoice() == 1 || d.getChoice() == 2 || d.getChoice() == 3)
 18             LineInputError.pointsCoincideError(d.getPoints().get(0), d.getPoints().get(1), d.getPoints().get(2),
 19                     d.getPoints().get(3));
 20         else if (d.getChoice() == 4)
 21             LineInputError.pointsCoincideError(d.getPoints().get(0), d.getPoints().get(1));
 22 
 23         if (d.getChoice() == 1)
 24             handle1(d.getPoints());
 25         else if (d.getChoice() == 2)
 26             handle2(d.getPoints());
 27         else if (d.getChoice() == 3)
 28             handle3(d.getPoints());
 29         else if (d.getChoice() == 4)
 30             handle4(d.getPoints());
 31         else if (d.getChoice() == 5)
 32             handle5(d.getPoints());
 33 
 34         input.close();
 35 
 36     }
 37 
 38     public static void handle1(ArrayList<Point> ps) {
 39         Quadrilateral q = new Quadrilateral(ps.get(0), ps.get(1), ps.get(2), ps.get(3));
 40         boolean bool = q.isQuadrangle() && q.isSuitableQuadrangle();
 41         System.out.println(bool + " " + q.isRhomboid());
 42     }
 43 
 44     public static void handle2(ArrayList<Point> ps) {
 45         Quadrilateral q = new Quadrilateral(ps.get(0), ps.get(1), ps.get(2), ps.get(3));
 46         if (q.isSuitableQuadrangle() && q.isQuadrangle()) {
 47             System.out.println(q.isRhombus() + " " + q.isRectangle() + " " + q.isSquare());
 48         } else {
 49             System.out.println("not a quadrilateral");
 50         }
 51     }
 52 
 53     public static void handle3(ArrayList<Point> ps) {
 54         Quadrilateral q = new Quadrilateral(ps.get(0), ps.get(1), ps.get(2), ps.get(3));
 55         if (q.isSuitableQuadrangle()&&q.isQuadrangle()) {
 56             System.out.println(q.isConvexQua() + " " + BasicType.transfer(q.getLengthOfSides()) + " " + BasicType.transfer(q.getArea()));
 57         } else {
 58             System.out.println("not a quadrilateral");
 59         }
 60     }
 61 
 62     public static void handle4(ArrayList<Point> ps) {
 63         Line l = new Line(ps.get(0), ps.get(1));
 64         Quadrilateral q = new Quadrilateral(ps.get(2), ps.get(3), ps.get(4), ps.get(5));
 65         if (q.isSuitableQuadrangle() && q.isQuadrangle()) {
 66             if (q.isCoincideLine(l)) {
 67                 System.out.println("The line is coincide with one of the lines");
 68             } else {
 69                 System.out.print(q.getIntersections(l).size());
 70                 if(q.getIntersections(l).size() == 2) {
 71                     double [] a = q.calArea(q.getIntersections(l).get(0), q.getIntersections(l).get(1));
 72                     System.out.print(" " + BasicType.transfer(Math.min(a[0], a[1])) + " " + BasicType.transfer(Math.max(a[0], a[1])));
 73                 }
 74             }
 75         } else if (q.isSuitableTriangle()) {
 76             Triangle t = q.removePoint();
 77             if (t.isLineCoincide(l)) {
 78                 System.out.println("The line is coincide with one of the lines");
 79             } else {
 80                 System.out.print(t.getIntersections(l).size());
 81                 if (t.getIntersections(l).size() == 2) {
 82                     double [] a = t.calArea(t.getIntersections(l).get(0), t.getIntersections(l).get(1));
 83                     System.out.print(" " + BasicType.transfer(Math.min(a[0], a[1])) + " " + BasicType.transfer(Math.max(a[0], a[1])));
 84                 }
 85             }
 86         } else {
 87             System.out.println("not a quadrilateral or triangle");
 88         }
 89     }
 90     
 91     public static void handle5(ArrayList<Point> ps) {
 92         Quadrilateral q = new Quadrilateral(ps.get(1), ps.get(2), ps.get(3), ps.get(4));
 93         if (q.isSuitableQuadrangle() && q.isConvexQua()) {
 94             if (q.isOnTheEdge(ps.get(0)) || q.isVertex(ps.get(0))) {
 95                 System.out.println("on the quadrilateral");
 96             } else if (q.isInside(ps.get(0))) {
 97                 System.out.println("in the quadrilateral");
 98             } else {
 99                 System.out.println("outof the quadrilateral");
100             }
101 
102         } else if (q.isSuitableTriangle()) {
103             Triangle t = q.removePoint();
104             if (t.isOnTheEdge(ps.get(0)) || t.isVertex(ps.get(0))) {
105                 System.out.println("on the triangle");
106             } else if (t.isInside(ps.get(0))) {
107                 System.out.println("in the triangle");
108             } else {
109                 System.out.println("outof the triangle");
110             }
111         } else {
112             System.out.println("not a quadrilateral or triangle");
113         }
114     }
115 }
116 
117 ////////
118 class Point {
119     private double x;
120     private double y;
121 
122     public Point(double x, double y) {
123         super();
124         this.x = x;
125         this.y = y;
126     }
127 
128     public double getX() {
129         return x;
130     }
131 
132     public void setX(double x) {
133         this.x = x;
134     }
135 
136     public double getY() {
137         return y;
138     }
139 
140     public void setY(double y) {
141         this.y = y;
142     }
143 
144     public boolean equal(Point p) {
145         if (this.x == p.getX() && this.y == p.getY())
146             return true;
147         return false;
148     }
149 
150     public double getDistance(Point p) {
151         return Math.sqrt(Math.pow(x - p.getX(), 2) + Math.pow(y - p.getY(), 2));
152     }
153 }
154 
155 ////////
156 class Line {
157     private Point a;
158     private Point b;
159 
160     public Line(double x1, double y1, double x2, double y2) {
161         super();
162         a.setX(x1);
163         a.setY(y1);
164         b.setX(x2);
165         b.setY(y2);
166     }
167 
168     public Line(Point a, Point b) {
169         super();
170         this.a = a;
171         this.b = b;
172     }
173 
174     public Point getA() {
175         return a;
176     }
177 
178     public Point getB() {
179         return b;
180     }
181 
182     public boolean isHasSlope() {
183         if (a.getX() == b.getX())
184             return false;
185         return true;
186     }
187 
188     public double getSlope() {
189         return (a.getY() - b.getY()) / (a.getX() - b.getX());
190     }
191 
192     public static double getSlope(Point a, Point b) {
193         return (a.getY() - b.getY()) / (a.getX() - b.getX());
194     }
195 
196     public boolean isOnline(Point p) {
197         if (isHasSlope()) {
198             if (this.getSlope() == Line.getSlope(a, p) )
199                 return true;
200             return false;
201         } else {
202             if (p.getX() == a.getX())
203                 return true;
204             return false;
205         }
206     }
207 
208 
209     public boolean isBetween(Point p) {
210         if(this.isOnline(p)) {
211             if ((p.getX() > Math.min(a.getX(), b.getX()) && p.getX() < Math.max(a.getX(), b.getX())) ||
212                     (p.getY() > Math.min(a.getY(), b.getY()) && p.getY() < Math.max(a.getY(), b.getY())))
213                 return true;
214             return false;
215         }
216         return false;
217     }
218 
219 
220     public Point getMiddlePoint() {
221         return new Point((a.getX() + b.getX()) / 2, (a.getY() + b.getY()) / 2);
222     }
223 
224     public double getAngle(Line line) {
225         double cos;
226         cos = ((a.getX()-b.getX())*(line.getA().getX()-line.getB().getX())+
227                 (a.getY()-b.getY())*(line.getA().getY()-line.getB().getY()))/
228                 a.getDistance(b)*line.getA().getDistance(line.getB());
229         return Math.acos(cos);
230         //方向为BA方向
231     }
232     
233     public double getLengthOfSegment() {
234         return Math.sqrt(Math.pow(a.getX()-b.getX(),2) + Math.pow(a.getY()-b.getY(),2));
235     }
236     
237     public boolean isParallel(Line line) {
238         if((!line.isHasSlope() && !this.isHasSlope())||
239                 (line.getSlope() == this.getSlope()))
240             return true;
241         return false;    
242     }
243     
244     public boolean isCoincide(Line line) {
245         if(line.isOnline(a) && line.isOnline(b))
246             return true;
247         return false;
248     }
249     
250     public Point getIntersection(Line line) {
251         double A1,B1,C1;
252         A1 = b.getY()-a.getY();
253         B1 = a.getX()-b.getX();
254         C1 = b.getX()*a.getY()-a.getX()*b.getY();
255         double A2,B2,C2;
256         A2 = line.getB().getY()-line.getA().getY();
257         B2 = line.getA().getX()-line.getB().getX();
258         C2 = line.getB().getX()*line.getA().getY()-line.getA().getX()*line.getB().getY();
259         double D,D1,D2;
260         D = A1*B2-A2*B1;
261         D1 = B1*C2-B2*C1;
262         D2 = C1*A2-C2*A1;
263         return new Point(D1/D,D2/D);
264     }
265     
266     public double getDistancesOfPointToLine(Point p) {
267         double A,B,C;
268         A = b.getY()-a.getY();
269         B = a.getX()-b.getX();
270         C = b.getX()*a.getY()-a.getX()*b.getY();
271         return Math.abs(A*p.getX() + B*p.getY() + C)/Math.sqrt(A*A + B*B);
272     }
273     public int getSideOfLine(Point p) {
274         double A,B,C;
275         A = b.getY()-a.getY();
276         B = a.getX()-b.getX();
277         C = b.getX()*a.getY()-a.getX()*b.getY();
278         if(A*p.getX() + B*p.getY() + C > 0)
279             return 1;
280         else if(A*p.getX() + B*p.getY() + C == 0)
281             return 0;
282         else return -1;
283     }
284 }
285 
286 ////////
287 class Triangle {
288     private Point x, y, z;
289     private Line xy,xz,yz;
290     
291     public Triangle(Point x, Point y, Point z) {
292         super();
293         this.x = x;
294         this.y = y;
295         this.z = z;
296         this.xy = new Line(x,y);
297         this.xz = new Line(x,z);
298         this.yz = new Line(y,z);
299     }
300 
301     public Point getX() {
302         return x;
303     }
304 
305     public void setX(Point x) {
306         this.x = x;
307     }
308 
309     public Point getY() {
310         return y;
311     }
312 
313     public void setY(Point y) {
314         this.y = y;
315     }
316 
317     public Point getZ() {
318         return z;
319     }
320 
321     public void setZ(Point z) {
322         this.z = z;
323     }
324 
325     public Line getXy() {
326         return xy;
327     }
328 
329     public Line getXz() {
330         return xz;
331     }
332 
333     public Line getYz() {
334         return yz;
335     }
336     public boolean isTriangle() {
337         
338         if (!xy.isOnline(z))
339             return true;
340         return false;
341     }
342 
343     public Point getMidpoint() {
344         return null;
345     }
346 
347     public Line[] getMidline() {
348         Line[] lineList = new Line[3];
349         lineList[0] = new Line(x,yz.getMiddlePoint());
350         lineList[1] = new Line(y,xz.getMiddlePoint());
351         lineList[2] = new Line(z,xy.getMiddlePoint());
352         
353         return lineList;
354     }
355 
356     public Line[] getSideline() {
357         Line[] lineList = new Line[3];
358         lineList[0] = xy;
359         lineList[1] = xz;
360         lineList[2] = yz;
361         return lineList;
362     }
363 
364     public double getArea() {
365         return Math.abs((x.getX()-y.getX())*(x.getY()-z.getY()) - 
366                 (x.getY()-y.getY())*(x.getX()-z.getX()))/2;
367                 
368     }
369 
370     public double getPerimeter() {
371         return 0;
372     }
373 
374     public double[] getAngleS() {
375         return null;
376     }
377 
378     public boolean isIsoscelesTriangle() {//等腰
379         return false;
380     }
381 
382     public boolean isEquilateralTriangle() {//等边
383         return false;
384     }
385 
386     public boolean isRightTriangle() {//直角
387         return false;
388     }
389 
390     public boolean isObtuseTriangle() {//钝角
391         return false;
392     }
393 
394     public boolean isAcuteTriangle() {//锐角
395         return false;
396     }
397 
398     public boolean isVertex(Point p) { 
399         if(p.equal(x) || p.equal(y) ||p.equal(z))
400             return true;
401         return false;
402     }
403 
404     public boolean isInside(Point p) {
405         Triangle t1,t2,t3;
406         t1 = new Triangle(x,y,p);
407         t2 = new Triangle(x,z,p);
408         t3 = new Triangle(y,z,p);
409         if(t1.getArea()+t2.getArea()+t3.getArea() > this.getArea())
410             return false;
411         return true;
412     }
413 
414     public ArrayList<Point> getIntersections(Line line) {
415         ArrayList<Point> list = new ArrayList<Point>();
416         Point p1 = line.getIntersection(xy);
417         Point p2 = line.getIntersection(xz);
418         Point p3 = line.getIntersection(yz);
419         if (this.isOnTheEdge(p1) || this.isVertex(p1)) {
420             list.add(p1);
421         }
422 
423         if (this.isOnTheEdge(p2) || this.isVertex(p2)) {
424             if (!p2.equal(p1))
425                 list.add(p2);
426         }
427 
428         if (this.isOnTheEdge(p3) || this.isVertex(p3)) {
429             if (!p3.equal(p2) && !p3.equal(p1))
430                 list.add(p3);
431         }
432 
433         return list;
434 
435     }
436 
437     public double[] calArea(Point p1, Point p2) {
438 
439         Line l = new Line(p1, p2);
440         double[] a = new double[2];
441         
442         if (l.getSideOfLine(x) == l.getSideOfLine(y) || l.getSideOfLine(x) == 0 || l.getSideOfLine(y) == 0)
443             a[0] = new Triangle(p1, p2, z).getArea();
444         else if (l.getSideOfLine(x) == l.getSideOfLine(z) || l.getSideOfLine(x) == 0 || l.getSideOfLine(z) == 0)
445             a[0] = new Triangle(p1, p2, y).getArea();
446         else if (l.getSideOfLine(y) == l.getSideOfLine(z) || l.getSideOfLine(y) == 0 || l.getSideOfLine(z) == 0)
447             a[0] = new Triangle(p1, p2, x).getArea();
448 
449         a[1] = this.getArea() - a[0];
450 
451         return a;
452     }
453 
454     public double calAreaDiffrence(Triangle t1) {
455         return 0;
456     }
457 
458     public boolean isOnTheEdge(Point p) {
459         if(xy.isBetween(p) || xz.isBetween(p) || yz.isBetween(p))
460             return true;
461         return false;
462     }
463 
464     public boolean isLineCoincide(Line l) {
465         if(l.isCoincide(xy) || l.isCoincide(xz) ||l.isCoincide(yz))
466             return true;
467         return false;
468     }
469 
470 }
471 
472 ////////
473 class Quadrilateral {
474     private Point a, b, c, d;
475     private Line ab, bc, cd, da;
476 
477     public Quadrilateral(Point a, Point b, Point c, Point d) {
478         super();
479         this.a = a;
480         this.b = b;
481         this.c = c;
482         this.d = d;
483         this.ab = new Line(a, b);
484         this.bc = new Line(b, c);
485         this.cd = new Line(c, d);
486         this.da = new Line(d, a);
487     }
488 
489     public double getLengthOfSides() {
490         return ab.getLengthOfSegment() + bc.getLengthOfSegment() + 
491                 cd.getLengthOfSegment() + da.getLengthOfSegment();
492     }
493     
494     public double getArea() {
495         if(this.isConvexQua()) {
496             Triangle t1 = new Triangle(a,b,c);
497             Triangle t2 = new Triangle(a,d,c);
498             return t1.getArea()+t2.getArea();
499         }else {
500             if(new Triangle(b,c,d).isInside(a) || new Triangle(b,a,d).isInside(c)) {
501                 Triangle t1 = new Triangle(a,b,c);
502                 Triangle t2 = new Triangle(a,d,c);
503                 return t1.getArea()+t2.getArea();
504             }else if(new Triangle(a,c,d).isInside(b) || new Triangle(a,c,b).isInside(d)) {
505                 Triangle t1 = new Triangle(d,b,c);
506                 Triangle t2 = new Triangle(a,d,b);
507                 return t1.getArea()+t2.getArea();
508             }return 0;
509         }
510     }
511     
512     public boolean isQuadrangle() {
513         Triangle t = new Triangle(a, b, c);
514         if(t.isTriangle()) {
515             if (t.getXy().isOnline(d) || t.getXz().isOnline(d)|| t.getYz().isOnline(d))
516                 return false;
517             return true;
518         }return false;
519     }
520     
521     public boolean isSuitableQuadrangle() {
522         Point p1,p2;
523         if(ab.isParallel(cd) && da.isParallel(bc))
524             return true;
525         else if(ab.isParallel(cd) && !da.isParallel(bc)) {
526             p1 = da.getIntersection(bc);
527             if(da.isBetween(p1) && bc.isBetween(p1))
528                 return false;
529             return true;
530         }
531         else if(!ab.isParallel(cd) && da.isParallel(bc)) {
532             p1 = ab.getIntersection(cd);
533             if(ab.isBetween(p1) && cd.isBetween(p1))
534                 return false;
535             return true;
536         }
537         else {
538             p1 = ab.getIntersection(cd); 
539             p2 = da.getIntersection(bc);
540             if((ab.isBetween(p1) && cd.isBetween(p1))||(da.isBetween(p2) && bc.isBetween(p2)))
541                 return false;
542             return true;
543         }
544     }
545     
546     public boolean isConvexQua() {
547         Triangle t1 = new Triangle(b,c,d);
548         Triangle t2 = new Triangle(a,c,d);
549         Triangle t3 = new Triangle(a,b,d);
550         Triangle t4 = new Triangle(a,b,c);
551         if(t1.isInside(a) || t2.isInside(b) || t3.isInside(c) || t4.isInside(d))
552             return false;
553         return true;
554             
555     }
556 
557     public boolean isRhomboid() {
558         if (this.isConvexQua()) {
559             if (ab.isParallel(cd) && bc.isParallel(da))
560                 return true;
561             return false;
562         }
563         return false;
564     }
565 
566     public boolean isRhombus() {// 菱形
567         if (this.isRhomboid()) {
568             if (ab.getLengthOfSegment() == bc.getLengthOfSegment())
569                 return true;
570             return false;
571         }
572         return false;
573     }
574 
575     public boolean isRectangle() {
576         if(this.isRhomboid()) {
577             if(ab.getAngle(da) == Math.acos(0))
578                 return true;
579             return false;
580         }
581         return false;
582     }
583     
584     public boolean isSquare() {
585         if(this.isRectangle() && this.isRhombus())
586             return true;
587         return false;
588     }
589     
590     public boolean isCoincideLine(Line line) {
591         if(line.isCoincide(ab) || line.isCoincide(bc) || line.isCoincide(cd) || line.isCoincide(da))
592             return true;
593         return false;
594     }
595     
596     public boolean isSuitableTriangle() { 
597         if(this.isTriangle()) {
598             if(new Line(a,c).isBetween(d) || new Line(a,c).isBetween(b) ||
599                     new Line(b,d).isBetween(a) || new Line(b,d).isBetween(c) ||
600                     d.equal(a) || d.equal(c) ||
601                     a.equal(b) || a.equal(d) ||
602                     b.equal(a) || d.equal(c) ||
603                     c.equal(b) || c.equal(d))
604                 return true;
605         }
606         return false;
607     }
608     
609     public boolean isTriangle() {
610         if(new Triangle(a,b,c).isOnTheEdge(d) || new Triangle(a,b,c).isVertex(d) ||
611                 new Triangle(a,b,d).isOnTheEdge(c) || new Triangle(a,b,d).isVertex(c)||
612                 new Triangle(a,d,c).isOnTheEdge(b) || new Triangle(a,d,c).isVertex(b)||
613                 new Triangle(b,d,c).isOnTheEdge(a) || new Triangle(b,d,c).isVertex(a))
614             return true;
615         return false;
616     }
617     
618     public Triangle removePoint() {
619         if(new Line(a,c).isBetween(d) || d.equal(a) || d.equal(c)) {
620             return new Triangle(a,b,c);
621         }
622         if(new Line(a,c).isBetween(b) || b.equal(a) || b.equal(c)) {
623             return new Triangle(a,d,c);
624         }
625         if(new Line(b,d).isBetween(a) || a.equal(b) || a.equal(d)) {
626             return new Triangle(d,b,c);
627         }
628         if(new Line(b,d).isBetween(c) || c.equal(b) || c.equal(d)) {
629             return new Triangle(a,b,d);
630         }
631         return null;
632     }
633     
634     public boolean isOnTheEdge(Point p) {
635         if(ab.isBetween(p) || bc.isBetween(p) || cd.isBetween(p) || da.isBetween(p))
636             return true;
637         return false;
638     }
639     
640     public boolean isVertex(Point p) { 
641         if(p.equal(a) || p.equal(b) || p.equal(c) || p.equal(d))
642             return true;
643         return false;
644     }
645     
646     public boolean isInside(Point p) {
647         if(new Triangle(a,b,c).isInside(p) || new Triangle(a,d,c).isInside(p) || new Line(a,c).isBetween(p))
648             return true;
649         return false;
650     }
651     public ArrayList<Point> getIntersections(Line line) {
652         ArrayList<Point> list = new ArrayList<Point>();
653         Point p1 = line.getIntersection(ab);
654         Point p2 = line.getIntersection(bc);
655         Point p3 = line.getIntersection(cd);
656         Point p4 = line.getIntersection(da);
657         if (this.isOnTheEdge(p1) || this.isVertex(p1)) {
658             list.add(p1);
659         }
660 
661         if (this.isOnTheEdge(p2) || this.isVertex(p2)) {
662             if (!p2.equal(p1))
663                 list.add(p2);
664         }
665 
666         if (this.isOnTheEdge(p3) || this.isVertex(p3)) {
667             if (!p3.equal(p2) && !p3.equal(p1))
668                 list.add(p3);
669         }
670         
671         if (this.isOnTheEdge(p4) || this.isVertex(p4)) {
672             if ( !p4.equal(p3) && !p4.equal(p2) && !p4.equal(p1))
673                 list.add(p4);
674         }
675 
676         return list;
677 
678     }
679     public double[] calArea(Point point1, Point point2) {
680         int p1, p2, p3, p4;
681         double[] area = new double[2];
682         Line l = new Line(point1, point2);
683         p1 = l.getSideOfLine(a);
684         p2 = l.getSideOfLine(b);
685         p3 = l.getSideOfLine(c);
686         p4 = l.getSideOfLine(d);
687 
688         if ((p1 == p2 && p2 == p3) || (p3 == 0 && (p1 == p2)) || (p1 == 0 && (p3 == p2)) || (p1 == p3))
689             area[0] = new Triangle(d, point1, point2).getArea();
690         else if ((p1 == p2 && p2 == p4) || (p2 == 0 && (p1 == p4)) || (p4 == 0 && (p1 == p2)) || (p2 == p4))
691             area[0] = new Triangle(c, point1, point2).getArea();
692         else if ((p1 == p3 && p4 == p3) || (p3 == 0 && (p1 == p4)) || (p1 == 0 && (p3 == p4)) || (p1 == p3))
693             area[0] = new Triangle(b, point1, point2).getArea();
694         else if ((p4 == p2 && p2 == p3) || (p2 == 0 && (p3 == p4)) || (p4 == 0 && (p3 == p2)) || (p2 == p4))
695             area[0] = new Triangle(a, point1, point2).getArea();
696         else if (p1 == p2 && p3 == p4) {
697             if (bc.isBetween(point1))
698                 area[0] = new Quadrilateral(a, b, point1, point2).getArea();
699             if (bc.isBetween(point2))
700                 area[0] = new Quadrilateral(a, b, point2, point1).getArea();
701         } else if (p1 == p4 && p2 == p3) {
702             if (ab.isBetween(point1))
703                 area[0] = new Quadrilateral(a, d, point2, point1).getArea();
704             if (ab.isBetween(point2))
705                 area[0] = new Quadrilateral(a, d, point1, point2).getArea();
706         }
707 
708         area[1] = this.getArea() - area[0];
709         return area;
710     }
711 }
712 ////////
713 class ParseInput {
714     
715     public static void parseInput(String s, InputData d) {
716         d.setChoice(getChoice(s.substring(0,1)));
717         String[] str;
718         str = s.substring(2).split("\\s+");
719         for(String a : str) {
720             parsePoints(a, d);
721         }
722         
723     }
724 
725     public static int getChoice(String s) {
726         return Integer.parseInt(s);
727     }
728 
729     public static void parsePoints(String s, InputData d) {
730         Point x = readPoint(s);
731         d.addPoint(x);
732     }
733 
734     public static Point readPoint(String s) {
735         double x = 0,y = 0;
736         int i = 0;
737         while( s.charAt(i) != ',') {
738             i++;
739         }
740         x = Double.parseDouble(s.substring(0, i));
741         y = Double.parseDouble(s.substring(i+1));
742         return new Point(x,y);
743     }
744 }
745 ////////
746 class LineInputError {
747 
748     public static void pointsCoincideError(Point p1, Point p2) {
749         if (p1.equal(p2)) {
750             System.out.println("points coincide");
751             System.exit(0);
752         }
753     }
754     
755     public static void pointsCoincideError(Point p1, Point p2, Point p3, Point p4) {
756         if (p1.equal(p2) || p1.equal(p3)|| p1.equal(p4)|| p2.equal(p3)|| p2.equal(p4)|| p3.equal(p4)) {
757             System.out.println("points coincide");
758             System.exit(0);
759         }
760     }
761 }
762 ////////
763 class InputData {
764     private int choice;
765     private ArrayList<Point> points = new ArrayList<Point>();
766 
767     public int getChoice() {
768         return choice;
769     }
770 
771     public void setChoice(int choice) {
772         this.choice = choice;
773     }
774 
775     public ArrayList<Point> getPoints() {
776         return points;
777     }
778 
779     public void addPoint(Point p) {
780         points.add(p);
781     }
782 }
783 ////////
784 class BasicType {
785     public static void isBasicType(String s) {
786         String regex = "[12345]\\:[\\+|-]?([0]|([1-9]((\\d)?)+))(\\.\\d+)?\\,([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?(\\s([+\\-+]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?\\,([+\\-]?([0]|([1-9]((\\d)?)+)))(\\.\\d+)?){0,}";
787         if(!s.matches(regex)) {
788             System.out.println("Wrong Format");
789             System.exit(0);
790         }
791     }
792     public static String transfer(double c) {
793         String str_d = String.valueOf(c);
794               str_d = str_d.substring(str_d.indexOf(".") + 1);
795                 int len = str_d.length();
796                 len = len > 3 ? 3 : len;
797                  String area1 = String.format("%."+len+"f", c);
798                  return area1;
799     }
800 }
801 ////////
802 class PointInputError {
803     public static void wrongNumberOfPoints(ArrayList<Point> ps, int num) {
804         if (((num == 1 || num == 2 || num == 3) && ps.size() != 4) ||
805                 (num == 4 && ps.size() != 6) || 
806                 (num == 5 && ps.size() != 5)){
807             System.out.println("wrong number of points");
808             System.exit(0);
809         }
810     }
811 }
View Code

    这个代码写的比之前的更加科学一点,不过老师好像说这样写其实也不是很好,不过确实比之前我那种写法要方便不少,最后这题只扣了5分,已经有了很大的进步!

    这是类图:

    这么写的好处很多,比如在判断三角形是否存在时,只需判断其中一点是否在另外两点上,如果不在就可以判断,而四边形也可以判断点是否在三角形三边或者顶点上,不在就是四边形,在内部就是凹

  凹四边形,也就是说,如果最初就这么写的话,到最后这些代码都可以扒过来直接用的。甚至判断基本格式的时候正则表达式都不需要改变。

    通过这次练习,我学到了了一些真正关于对象的的思考。不过有一些踩过的坑还是要说一说的。

    其实还是老错误,就是对PTA题目的一些边界值判断,比如题目说的,按A,B,C,D顺序排列的点,并不是表示测试点没有乱序的点,而是需要我们自己去判断这些点顺序的正确性。有些方法写成静

  静态static看似很简单,但是有时候是需要继承扩展的,写成static就会出问题。虽然静态方法可以继承,但是不能重写(也就是override),这样就做不到多态。不过这题其实并不需要考虑以后继承的问题    

  了,所以为了方便我还写成了静态的。

    然后就是一些方法的实现的时候出了一系列问题,比如判断点是否在线段内的时候是否需要先判断点是否在直线上,在计算两直线交点的时候使用线代克拉默法则是否会更方便等。

  3、ATM机系统:

    这题的话还是题目没有看清楚就写完了,我甚至花了挺长时间来设计的,最后发现小丑竟是我自己。。。

    PTA05-5:

    这次作业依然没有获得满分,代码如下:

  1 import java.util.ArrayList;
  2 import java.util.LinkedList;
  3 import java.util.Scanner;
  4 
  5 
  6 public class Main {
  7 
  8     public static void main(String[] args) {
  9         // TODO Auto-generated method stub
 10         Atm atm = new Atm();
 11         atm.work();
 12     }
 13 
 14 }
 15 class Atm {
 16 
 17     private DataAnalyze analyze = new DataAnalyze();
 18     private Execute execute = new Execute();
 19     private Display display = new Display();
 20     private LinkedList<User> dealtList = new LinkedList<User>();
 21     private LinkedList<User> originList = new LinkedList<User>();
 22     
 23     public void work() {
 24         originList = this.banckInitial(); 
 25         execute.setList(originList);
 26         analyze.setOriginList(originList);
 27         dealtList = analyze.cutList(initialization());
 28         for(User user : dealtList) {
 29             User u = analyze.find(user);
 30             if(analyze.getWrongFormat() == 0) {
 31                 execute.execute(analyze.getOpreat(), u, analyze.getMoney());
 32                 originList = execute.getList();
 33                 display.show(analyze.getOpreat(), originList.get(execute.getIndex()),analyze.getMoney());
 34             } else {
 35                 display.worngFormat(analyze.getWrongFormat());
 36             }
 37         }
 38         
 39     }
 40     
 41     public ArrayList<String> initialization() {
 42         Scanner input = new Scanner(System.in);
 43         ArrayList<String> str = new ArrayList<String>();
 44         String a = input.nextLine();
 45         while(!a.equals("#")) {
 46             str.add(a);
 47             a = input.nextLine();
 48         }
 49         input.close();
 50         return str;
 51     }
 52     
 53     public LinkedList<User> banckInitial() {
 54         LinkedList<User> list = new LinkedList<User>();
 55         User user1 = new User("杨过", "6217000010041315709", "10000.00", "", "88888888");
 56         User user2 = new User("杨过", "6217000010041315715", "10000.00", "", "88888888");
 57         User user3 = new User("杨过", "6217000010041315718", "10000.00", "", "88888888");
 58         User user4 = new User("郭靖", "6217000010051320007", "10000.00", "", "88888888");
 59         User user5 = new User("张无忌", "6222081502001312389", "10000.00", "", "88888888");
 60         User user6 = new User("张无忌", "6222081502001312390", "10000.00", "", "88888888");
 61         User user7 = new User("张无忌", "6222081502001312399", "10000.00", "", "88888888");
 62         User user8 = new User("张无忌", "6222081502001312400", "10000.00", "", "88888888");
 63         User user9 = new User("韦小宝", "6222081502051320785", "10000.00", "", "88888888");
 64         User user10 = new User("韦小宝", "6222081502051320786", "10000.00", "", "88888888");
 65         list.add(user1);
 66         list.add(user2);
 67         list.add(user3);
 68         list.add(user4);
 69         list.add(user5);
 70         list.add(user6);
 71         list.add(user7);
 72         list.add(user8);
 73         list.add(user9);
 74         list.add(user10);
 75         return list;
 76     }
 77 }
 78 
 79 class DataAnalyze {
 80 
 81     private LinkedList<User> originList = new LinkedList<User>();
 82     private int opreat,wrongFormat = 0;
 83     private double money = 0;
 84     private String num;
 85     
 86     public int getOpreat() {
 87         return opreat;
 88     }
 89 
 90     public int getWrongFormat() {
 91         return wrongFormat;
 92     }
 93 
 94     public void setOriginList(LinkedList<User> originList) {
 95         this.originList = originList;
 96     }
 97 
 98     public double getMoney() {
 99         return money;
100     }
101 
102     public User find(User user) {
103         if(user.getPassword() != null)
104             return compare(user);
105         else return query(user);
106     }
107     
108     public User query(User user) {
109         for(User a : this.originList) {
110             if(a.getCardId().equals(user.getCardId())) {
111                 this.opreat = 0;
112                 this.wrongFormat = 0;
113                 return a;
114             }else {
115                 this.wrongFormat = 1;
116             }
117         }
118         return null;
119     }
120     
121     public User compare(User user) {
122         for(User a : this.originList) {
123             if(a.getCardId().equals(user.getCardId())) {
124                 if(this.bankCardIDValidity(user.getAtmNumber())) {
125                     if(user.getPassword().equals(a.getPassword())) {
126                         if(this.bankCardIDValidity(user.getCardId(), user.getAtmNumber())) {
127                             if(Double.parseDouble(user.getBalance()) <= Double.parseDouble(a.getBalance())) {
128                                 this.money = Double.parseDouble(user.getBalance());
129                                 this.wrongFormat = 0;
130                                 if(money <= 0)
131                                     this.opreat = -1;
132                                 else if(money >= 0)
133                                     this.opreat = 1;
134                                 a.setAtmNumber(this.num);
135                                 return a;
136                             }else {
137                                 this.wrongFormat = 4;
138                                 return null;
139                             }
140                         }else {
141                             this.wrongFormat = 5;
142                             return null;
143                         }
144                     }else {
145                         this.wrongFormat = 3;
146                         return null;
147                     }
148                 }else {
149                     this.wrongFormat = 2;
150                     return null;
151                 }
152             }else {
153                 this.wrongFormat = 1;
154             }
155         }
156         return null;
157     }
158     
159     public boolean bankCardIDValidity(String cardID, String atm) {
160         if(cardID.substring(0,10).equals("6217000010")) {
161             if(atm.equals("01") || atm.equals("02") || atm.equals("03") ||atm.equals("04"))
162                 return true;
163             else return false;
164         }else if(cardID.substring(0,10).equals("6222081502")){
165             if(atm.equals("05") || atm.equals("06"))
166                 return true;
167             else return false;
168         }else return false;
169     }
170     
171     public boolean bankCardIDValidity(String atm) {
172         if(atm.equals("01") || atm.equals("02") || atm.equals("03") || atm.equals("04") 
173                 || atm.equals("05") || atm.equals("06"))
174             return true;
175         else return false;
176     }
177     
178     public LinkedList<User> cutList(ArrayList<String> originalData) {
179         String[] str;
180         LinkedList<User> list = new LinkedList<User>();
181         User user;
182         for(String data : originalData ) {
183             str = data.split("\\s+");
184             user = new User();
185             if(str.length > 1) {
186                 user.setAtmNumber(str[2]);
187                 this.num = str[2];
188                 user.setBalance(str[3]);
189                 user.setCardId(str[0]);
190                 user.setPassword(str[1]);
191             }else {
192                 user.setCardId(str[0]);
193             }
194             list.add(user);
195         }
196         return list;
197     }
198 }
199 
200 
201 class Display {
202 
203     
204     public void worngFormat(int wrongFormat) {
205         if(wrongFormat == 1) {
206             System.out.println("Sorry,this card does not exist.");
207         }
208         if(wrongFormat == 2) {
209             System.out.println("Sorry,the ATM's id is wrong.");    
210         }
211         if(wrongFormat == 3) {
212             System.out.println("Sorry,your password is wrong.");    
213         }
214         if(wrongFormat == 4) {
215             System.out.println("Sorry,your account balance is insufficient.");
216         }
217         if(wrongFormat == 5) {
218             System.out.println("Sorry,cross-bank withdrawal is not supported.");
219         }
220     }
221     
222     public void show(int oparate,User user,double money) {
223         if(oparate == 1) {
224             System.out.printf("%s在%s的%s号ATM机上取款¥%.2f\n",user.getName(),user.getBank(),user.getAtmNumber(),Math.abs(money));
225             System.out.printf("当前余额为¥%.2f\n",Double.parseDouble(user.getBalance()));
226         }
227         if(oparate == -1) {
228             System.out.printf("%s在%s的%s号ATM机上存款¥%.2f\n",user.getName(),user.getBank(),user.getAtmNumber(),Math.abs(money));
229             System.out.printf("当前余额为¥%.2f\n",Double.parseDouble(user.getBalance()));
230         }
231         if(oparate == 0)
232             System.out.printf("¥%.2f\n",Double.parseDouble(user.getBalance()));
233     }
234     
235 }
236 
237 class Execute {
238 
239     private LinkedList<User> list = new LinkedList<User>();
240     private int index;
241     
242     public int getIndex() {
243         return index;
244     }
245 
246     public LinkedList<User> getList() {
247         return list;
248     }
249     
250     public void setList(LinkedList<User> list) {
251         this.list = list;
252     }
253 
254     public void getIndex(User user) {
255         index = list.indexOf(user);
256     }
257     
258     public void execute(int oparate,User user,double money) {
259         getIndex(user);
260         whichBank(user.getCardId());
261         doing(oparate,user,money);
262     }
263     
264     public void doing(int oparate,User user,double money) {
265         if(oparate == 0) {
266         }else {
267             double balance = Double.parseDouble(user.getBalance());
268             balance -= money;
269             list.get(index).setBalance(String.valueOf(balance));
270         }
271     }
272     
273     public void whichBank(String Id) {
274         if(Id.substring(0,10).equals("6217000010")) {
275             list.get(index).setBank("中国建设银行");
276         }
277         if(Id.substring(0,10).equals("6222081502")) {
278             list.get(index).setBank("中国工商银行");
279         }
280     }
281 }
282 class User {
283     /*
284      * 用户信息类
285      */
286 
287     private String name = null;
288     private String cardId = null;
289     private String balance = null;
290     private String atmNumber = null;
291     private String password = null;
292     private String bank = null;
293     
294     public String getBank() {
295         return bank;
296     }
297 
298     public void setBank(String bank) {
299         this.bank = bank;
300     }
301 
302     public User(String name, String cardId, String balance, String atmNumber, String password) {
303         super();
304         this.name = name;
305         this.cardId = cardId;
306         this.balance = balance;
307         this.atmNumber = atmNumber;
308         this.password = password;
309     }
310 
311     public User() {
312         super();
313         // TODO Auto-generated constructor stub
314     }
315     
316     public String getName() {
317         return name;
318     }
319 
320     public void setName(String name) {
321         this.name = name;
322     }
323 
324     public String getCardId() {
325         return cardId;
326     }
327 
328     public void setCardId(String cardId) {
329         this.cardId = cardId;
330     }
331 
332     public String getBalance() {
333         return balance;
334     }
335 
336     public void setBalance(String balance) {
337         this.balance = balance;
338     }
339 
340     public String getAtmNumber() {
341         return atmNumber;
342     }
343 
344     public void setAtmNumber(String atmNumber) {
345         this.atmNumber = atmNumber;
346     }
347 
348     public String getPassword() {
349         return password;
350     }
351 
352     public void setPassword(String password) {
353         this.password = password;
354     }
355     
356     
357 }
View Code

    类图:

 

 

 

    这次作业出现的问题主要是以下几个方面:

    1、没有认真审题,导致写道最后发现账户关系不对。

    2、设计思想太低级,没有搞清楚银行,用户,账户,银行卡,ATM机等之间的关系。

    个人认为其实这题设计思想应该是银行下有用户和ATM机,用户下有银行账户,账户下有银行卡和余额,银行卡有账号密码,Atm负责处理输入输出和数据处理。

    

    4、链表:

    链表其实没太大问题,因为c语言基础不错,但是有一点不太懂,就是get(int index);这个方法的index到底是索引值还是第几个元素。或者说索引值是从0开始还是从1开始。老师上课写get和remove方法

  的时候都是写第几个,也就是换到索引值的话是下标为 index-1 的元素进行操作。再写双链表的时候,其实也没太大问题,增加一个“指针”,指向前一个元素罢了。在一定程度上方便的增加删除的方便性。

  DoublelinkedList:

  1 public interface DoubleLinkedListImpl<E> {
  2 
  3     /**
  4      * 
  5      * 判断链表是否为空
  6      * 
  7      * @return
  8      * 
  9      */
 10 
 11     public boolean isEmpty();
 12 
 13     /**
 14      * 
 15      * 获取当前链表节点数量
 16      * 
 17      * @return 节点数
 18      * 
 19      */
 20 
 21     public int getSize();
 22 
 23     /**
 24      * 
 25      * 获取链表中第index个位置的节点的data值
 26      * 
 27      * @param index:节点在链表中的位置
 28      * 
 29      * @return:返回该节点的data值
 30      * 
 31      */
 32 
 33     public E getData(int index);
 34 
 35     /**
 36      * 
 37      * 删除链表最后一个节点
 38      * 
 39      */
 40 
 41     public void remove();
 42 
 43     /**
 44      * 
 45      * 删除链表中第index位置的节点
 46      * 
 47      * @param index:节点在链表中的位置
 48      * 
 49      */
 50 
 51     public void remove(int index);
 52 
 53     /**
 54      * 
 55      * 在链表的第index个位置之前插入一个节点,值为theElement,index∈[1,size]
 56      * 
 57      * @param index:插入节点在链表中的位置
 58      * 
 59      * @param theElement:新插入节点的data值
 60      * 
 61      */
 62 
 63     public void add(int index, E theElement);
 64 
 65     /**
 66      * 
 67      * 在链表尾插入节点,插入节点data值为element
 68      * 
 69      * @param element
 70      * 
 71      */
 72 
 73     public void add(E element);
 74 
 75     /**
 76      * 
 77      * 输出链表
 78      * 
 79      */
 80 
 81     public void printList();
 82 
 83     /**
 84      * 
 85      * 获取第一个节点的data值
 86      * 
 87      * @return
 88      * 
 89      */
 90 
 91     public E getFirst();
 92 
 93     /**
 94      * 
 95      * 获取链表最后一个节点的data值
 96      * 
 97      * @return
 98      * 
 99      */
100 
101     public E getLast();
102 
103 }
DoubleLinkedListImpl
 1 public class Node<E> {
 2     private E data;// 数据域,类型为泛型E
 3 
 4     private Node<E> next;// 后继引用(指针)
 5 
 6     private Node<E> previous;// 前驱引用(指针)
 7 
 8     public Node(E data, Node<E> next, Node<E> previous) {
 9         super();
10         this.data = data;
11         this.next = next;
12         this.previous = previous;
13     }
14 
15     public Node() {
16         // TODO Auto-generated constructor stub
17     }
18 
19     public E getData() {
20         return data;
21     }
22 
23     public void setData(E data) {
24         this.data = data;
25     }
26 
27     public Node<E> getNext() {
28         return next;
29     }
30 
31     public void setNext(Node<E> next) {
32         this.next = next;
33     }
34 
35     public Node<E> getPrevious() {
36         return previous;
37     }
38 
39     public void setPrevious(Node<E> previous) {
40         this.previous = previous;
41     }
42 }
Node
  1 public class DoubleLinkedList<E> implements 
  2 DoubleLinkedListImpl<E> {
  3     private Node<E> head;// 头结点,非第一个节点
  4 
  5     private Node<E> curr;// 当前节点
  6 
  7     private Node<E> tail;// 最后一个节点
  8 
  9     private int size;// 当前链表节点数
 10 
 11     public DoubleLinkedList() {
 12         super();
 13         this.head = new Node<E>();
 14         head.setNext(null);
 15         curr = tail = null;
 16         size = 0;
 17     }
 18 
 19     @Override
 20     public boolean isEmpty() {
 21         return this.size == 0;
 22     }
 23 
 24     @Override
 25     public int getSize() {
 26         return this.size;
 27     }
 28 
 29     @Override
 30     public E getData(int index) {
 31         curr = head;
 32         int num = 0;
 33         if (index < 0 || index >= size) {
 34             System.out.print("error!");
 35             return null;
 36         } else {
 37             while (true) {
 38                 if (index-1 == num) {
 39                     return curr.getData();
 40                 }
 41                 curr = curr.getNext();
 42                 num++;
 43             }
 44         }
 45     }
 46 
 47     @Override
 48     public void remove() {
 49         if (size <= 0)
 50             System.out.print("error!");
 51         else if (size == 1) {
 52             head = tail = null;
 53             this.size--;
 54         } else {
 55             tail.getPrevious().setNext(null);
 56             tail = tail.getPrevious();
 57             this.size--;
 58         }
 59     }
 60 
 61     @Override
 62     public void remove(int index) {
 63         if (index <= 0)
 64             System.out.print("error!");
 65         else {
 66             if (size == 1) {
 67                 head = tail = null;
 68             } else if (index == 1) {
 69                 head.getNext().setPrevious(null);
 70                 head = head.getNext();
 71             } else if (index == size) {
 72                 tail.getPrevious().setNext(null);
 73                 tail = tail.getPrevious();
 74             } else {
 75                 curr = head;
 76                 for (int i = 1; i < index; i++) {
 77                     curr = curr.getNext();
 78                 }
 79                 curr.getPrevious().setNext(curr.getNext());
 80                 curr.getNext().setPrevious(curr.getPrevious());
 81             }
 82             this.size--;
 83         }
 84     }
 85 
 86     @Override
 87     public void add(int index, E theElement) {
 88         curr = new Node<E>();
 89         curr.setData(theElement);
 90         curr.setNext(null);
 91         curr.setPrevious(null);
 92 
 93         if (index < 0 || index >= this.size) {
 94             System.out.print("error!");
 95         } else {
 96             if (size == 0) {
 97                 head = curr;
 98                 tail = curr;
 99             } else if (index == 0) {
100                 head.setPrevious(curr);
101                 curr.setNext(head);
102                 head = curr;
103             } else if (index == size - 1) {
104                 tail.getPrevious().setNext(curr);
105                 curr.setPrevious(tail);
106                 tail = curr;
107             } else {
108                 Node<E> p = head;
109                 for (int i = 0; i < index; i++)
110                     p = p.getNext();
111                 p.getPrevious().setNext(curr);
112                 curr.setPrevious(p.getPrevious());
113                 p.setPrevious(curr);
114                 curr.setNext(p);
115             }
116             this.size++;
117         }
118 
119     }
120 
121     @Override
122     public void add(E element) {
123         curr = new Node<E>();
124         curr.setData(element);
125         curr.setNext(null);
126 
127         if (size == 0) {
128             head = tail = curr;
129             curr.setPrevious(null);
130         } else {
131             tail.setNext(curr);
132             curr.setPrevious(tail);
133         }
134         this.size += 1;
135         tail = curr;
136     }
137 
138     @Override
139     public void printList() {
140         curr = head;
141         if (size > 0) {
142             for (int i = 0; i < size; i++) {
143                 System.out.print(curr.getData() + " ");
144                 curr = curr.getNext();
145             }
146             System.out.println();
147         }
148     }
149 
150     @Override
151     public E getFirst() {
152         return head.getData();
153     }
154 
155     @Override
156     public E getLast() {
157         return tail.getData();
158     }
159 
160 }
DoubleLinkedList

    这是三个类,主要就是注意一下边界的问题,还有一些特殊情况比如头为空的判断,还有一些“指针”指向逻辑,还有确保node的封装性等等。而且我发现Java的linkedlist是双向指针,索引是从0开始的。

  链表的难度不是很高,但是对于我们了解一些结构运行上有很大帮助!

 

    5:农夫过河:

    这题从最开始的三类,不过比较老师已经写好了大部分,所以其实就一些类的设计上是比较简单的,只需要填充一些方法就行了。这部分代码也就不一一展示了,在迭代了一轮之后,我添加了几个父            类,不过依然感觉有些欠缺。先给代码吧

1 package 农夫过河;
2 
3 public class Main {
4         public static void main(String[] args) {
5             // TODO Auto-generated method stub
6             Game game = new Game();
7             game.play();
8     }
9 }
Main
Game
 1 package 农夫过河;
 2 
 3 public class GameGui {
 4     
 5     public void modleChoose() {
 6         System.out.println("==================Choose the game mode:==================");
 7         System.out.println("==========1:Five roles and can take two characters across the river==========");
 8         System.out.println("==========2:Three roles and only take one character across the river==========");
 9     }
10     
11     public void menu1() {
12         System.out.println("==================Please choose operation============");
13         System.out.println("\t==========1:Cross the river alone===========");
14         System.out.println("\t==========2:Cross the river with wolf=========");
15         System.out.println("\t==========3:Cross the river with sheep============");
16         System.out.println("\t==========4:Cross the river with rabbit==========");
17         System.out.println("\t==========5:Cross the river with cabbage=========");
18         System.out.println("\t==========6:Cross the river with carrot============");
19         System.out.println("\t==========7:Cross the river with wolf and sheep=========");
20         System.out.println("\t==========8:Cross the river with wolf and rabbit============");
21         System.out.println("\t==========9:Cross the river with wolf and cabbage============");
22         System.out.println("\t==========10:Cross the river with wolf and carrot============");
23         System.out.println("\t==========11:Cross the river with sheep and rabbit============");
24         System.out.println("\t==========12:Cross the river with sheep and cabbage============");
25         System.out.println("\t==========13:Cross the river with sheep and carrot============");
26         System.out.println("\t==========14:Cross the river with rabbit and cabbage============");
27         System.out.println("\t==========15:Cross the river with rabbit and carrot============");
28         System.out.println("\t==========16:Cross the river with cabbage and carrot==========");
29         System.out.println("\t==========0:Quit===============");   
30         System.out.println("===================================================");
31         System.out.println("Input the number(0~16):");
32     }
33     
34     public void menu2() {
35         /* 显示菜单 */
36         System.out.println("==================Please choose operation============");
37         System.out.println("\t==========1:Cross the river alone===========");
38         System.out.println("\t==========2:Cross the river with wolf=========");
39         System.out.println("\t==========3:Cross the river with sheep============");
40         System.out.println("\t==========4:Cross the river with cabbage==========");
41         System.out.println("\t==========0:Quit===============");   
42         System.out.println("===================================================");
43         System.out.println("Input the number(0~4):");
44     }    
45 
46     public void showStatus(Passenger farmer, Passenger wolf, Passenger sheep, Passenger cabbage) {
47     /* 输出农夫、各种动物、物品的状态(生存、位置) */
48         farmer.showStatus();
49         wolf.showStatus();
50         sheep.showStatus();
51         cabbage.showStatus();
52     }
53     
54     public void showStatus(Passenger farmer, Passenger wolf, Passenger sheep, Passenger cabbage,
55             Passenger rabbit, Passenger carrot) {
56         /* 输出农夫、各种动物、物品的状态(生存、位置) */
57             farmer.showStatus();
58             wolf.showStatus();
59             sheep.showStatus();
60             rabbit.showStatus();
61             cabbage.showStatus();
62             carrot.showStatus();
63         }
64 
65 }
GameGui
 1 package 农夫过河;
 2 
 3 public abstract class Passenger {
 4     private boolean crossRiver = false;
 5     
 6     public boolean isCrossRiver() {
 7         return crossRiver;
 8     }
 9 
10     public void setCrossRiver(boolean crossRiver) {
11         this.crossRiver = crossRiver;
12     }
13     
14     public void showStatus() {
15     }
16 
17     public Passenger(boolean crossRiver) {
18         super();
19         this.crossRiver = crossRiver;
20     }
21 
22     public Passenger() {
23         // TODO Auto-generated constructor stub
24     }
25 }
Passenger  
 1 package 农夫过河;
 2 
 3 public class Farmer extends Passenger{
 4 
 5     public Farmer() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9 
10     @Override
11     public void showStatus() {
12         System.out.println("Farmer has Cross  :" + isCrossRiver());
13     }    
14 
15 }
Farmer
 1 package 农夫过河;
 2 
 3 public abstract class Animal extends Passenger {
 4 
 5     private String name;
 6 
 7     public Animal(String name) {
 8         super();
 9         this.name = name;
10     }
11 
12     public String getName() {
13         return name;
14     }
15 
16     public void setName(String name) {
17         this.name = name;
18     }
19 
20 }
Animal
 1 package 农夫过河;
 2 
 3 public class Wolf extends Animal{
 4         
 5         public Wolf(String name) {
 6             super(name);
 7             System.out.println("啊呜~~~我 "+ name +" 又回来了");
 8         }
 9         
10         @Override
11         public void showStatus() {
12             System.out.println("Wolf has Cross  :" + isCrossRiver());
13         }
14 
15         public void eat(Farmer farmer, VegetarianAnimal animal) {
16             if(farmer.isCrossRiver() != this.isCrossRiver())
17                 if(this.isCrossRiver() == animal.isCrossRiver())
18                     animal.beEaten();
19         }
20 
21 
22 }
Wolf
 1 package 农夫过河;
 2 
 3 public class VegetarianAnimal extends Animal {
 4 
 5     private boolean isAlive = true;
 6 
 7     public VegetarianAnimal(String name) {
 8         super(name);
 9         // TODO Auto-generated constructor stub
10     }
11 
12     public boolean isAlive() {
13         return isAlive;
14     }
15 
16     public void beEaten() {
17         this.isAlive = false;
18     }
19     
20     public void eat(Farmer farmer, Botany botany) {
21         if(farmer.isCrossRiver() != this.isCrossRiver())
22             if(this.isCrossRiver() == botany.isCrossRiver())
23                 botany.beEaten();
24     }
25 
26 }
VegetarianAnimal
 1 package 农夫过河;
 2 
 3 public class Sheep extends VegetarianAnimal{
 4     
 5     public Sheep(String name) {
 6         super(name);
 7         System.out.println("咩咩,我是可爱的小羊 "+ name );
 8     }
 9     
10     @Override
11     public void showStatus() {
12         System.out.println("Sheep "+ this.getName() +" is alive :" + this.isAlive());
13         System.out.println("Sheep has Cross  :" + isCrossRiver());
14     }
15 }
Sheep
 1 package 农夫过河;
 2 
 3 public class Rabbit extends VegetarianAnimal {
 4 
 5     public Rabbit(String name) {
 6         super(name);
 7         // TODO Auto-generated constructor stub
 8         System.out.println("我是" + name);
 9     }
10 
11     @Override
12     public void showStatus() {
13         System.out.println("Rabbit "+ this.getName() +" is alive :" + this.isAlive());
14         System.out.println("Rabbit has Cross  :" + isCrossRiver());
15     }
16 }
Rabbit
 1 package 农夫过河;
 2 
 3 public abstract class Botany extends Passenger {
 4     private boolean isAlive = true;
 5 
 6     public boolean isAlive() {
 7         return isAlive;
 8     }
 9 
10     public void beEaten() {
11         this.isAlive = false;
12     }
13 }
Botany
 1 package 农夫过河;
 2 
 3 public class Cabbage extends Botany {
 4     
 5     @Override
 6     public void showStatus() {
 7         System.out.println("Cabbage has Cross  :" + isCrossRiver());
 8     }
 9 
10     public Cabbage() {
11         super();
12         // TODO Auto-generated constructor stub
13     }
14 
15 }
Cabbage
 1 package 农夫过河;
 2 
 3 public class Carrot extends Botany {
 4 
 5     @Override
 6     public void showStatus() {
 7         System.out.println("Carrot has Cross  :" + isCrossRiver());
 8     }
 9 
10     public Carrot() {
11         super();
12         // TODO Auto-generated constructor stub
13     }
14 }
Carrot
  1 package 农夫过河;
  2 
  3 public class Boat {
  4 
  5     public boolean isRightLocation(int choose, Passenger farmer, Passenger wolf, Passenger sheep, Passenger cabbage) {
  6         if(choose == 2) {
  7             if(wolf.isCrossRiver() == farmer.isCrossRiver())
  8                 return true;
  9             else return false;
 10         }
 11         if(choose == 3) {
 12             if(sheep.isCrossRiver() == farmer.isCrossRiver())
 13                 return true;
 14             else return false;
 15         }
 16         if(choose == 4){
 17             if(cabbage.isCrossRiver() == farmer.isCrossRiver())
 18                 return true;
 19             else return false;
 20         }
 21         else return true;
 22     }
 23     
 24     public boolean isRightLocation(int choose, Passenger farmer, Passenger wolf, Passenger sheep, Passenger cabbage,
 25             Passenger rabbit, Passenger carrot) {
 26         if(choose == 2) {
 27             if(wolf.isCrossRiver() == farmer.isCrossRiver())
 28                 return true;
 29             else return false;
 30         }
 31         if(choose == 3) {
 32             if(sheep.isCrossRiver() == farmer.isCrossRiver())
 33                 return true;
 34             else return false;
 35         }
 36         if(choose == 4){
 37             if(rabbit.isCrossRiver() == farmer.isCrossRiver())
 38                 return true;
 39             else return false;
 40         }
 41         if(choose == 5) {
 42             if(cabbage.isCrossRiver() == farmer.isCrossRiver())
 43                 return true;
 44             else return false;
 45         }
 46         if(choose == 6) {
 47             if(carrot.isCrossRiver() == farmer.isCrossRiver())
 48                 return true;
 49             else return false;
 50         }
 51         if(choose == 7){
 52             if(wolf.isCrossRiver() == farmer.isCrossRiver() && wolf.isCrossRiver() == sheep.isCrossRiver())
 53                 return true;
 54             else return false;
 55         }
 56         if(choose == 8) {
 57             if(wolf.isCrossRiver() == farmer.isCrossRiver()&& wolf.isCrossRiver() == rabbit.isCrossRiver())
 58                 return true;
 59             else return false;
 60         }
 61         if(choose == 9) {
 62             if(wolf.isCrossRiver() == farmer.isCrossRiver()&& wolf.isCrossRiver() == cabbage.isCrossRiver())
 63                 return true;
 64             else return false;
 65         }
 66         if(choose == 10){
 67             if(wolf.isCrossRiver() == farmer.isCrossRiver()&& wolf.isCrossRiver() == carrot.isCrossRiver())
 68                 return true;
 69             else return false;
 70         }
 71         if(choose == 11) {
 72             if(sheep.isCrossRiver() == farmer.isCrossRiver()&& sheep.isCrossRiver() == rabbit.isCrossRiver())
 73                 return true;
 74             else return false;
 75         }
 76         if(choose == 12) {
 77             if(sheep.isCrossRiver() == farmer.isCrossRiver()&& sheep.isCrossRiver() == cabbage.isCrossRiver())
 78                 return true;
 79             else return false;
 80         }
 81         if(choose == 13){
 82             if(sheep.isCrossRiver() == farmer.isCrossRiver()&& sheep.isCrossRiver() == carrot.isCrossRiver())
 83                 return true;
 84             else return false;
 85         }
 86         if(choose == 14) {
 87             if(rabbit.isCrossRiver() == farmer.isCrossRiver()&& rabbit.isCrossRiver() == cabbage.isCrossRiver())
 88                 return true;
 89             else return false;
 90         }
 91         if(choose == 15) {
 92             if(rabbit.isCrossRiver() == farmer.isCrossRiver()&& rabbit.isCrossRiver() == carrot.isCrossRiver())
 93                 return true;
 94             else return false;
 95         }
 96         if(choose == 16){
 97             if(cabbage.isCrossRiver() == farmer.isCrossRiver() && carrot.isCrossRiver() == cabbage.isCrossRiver())
 98                 return true;
 99             else return false;
100         }
101         else return true;
102     }
103 }
Boat

    类图:

 

 

     在做这个实验的时候我妄图使用一个父类,但发现一个父类做不到这些事,因为就比如eat方法,蔬菜是不会吃的,还有isAlive这个属性wolf也没有,于是我就写了这么多的父类来实现。此次实验,如                

  果写好父类的话,可以大大减少写子类的时间。大多时候只是增加了菜单界面的修改。这次试验让我更加了解到代码复用性和可扩展性的重要。不过此次实验仍有不完美的地方,比如eat这个方法任然没把

  他写在父类中做到真正的多态,然后game函数太复杂。还有就是多态其实也做得不太好,但是我确实是没有其他办法了。

             

    6、期中考试:

    期中考试没啥说的,类图都给了,总不能写不出来吧。依我看,这个不就是MOOC里面老师讲的嘛,什么Item类,然后DataBase里加一个Item,意思都一样,主要考察的是多态

    期中:

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 public class Main {
  5 
  6     public static void main(String[] args) {
  7         // TODO Auto-generated method stub
  8 
  9         GeometryObject o = new GeometryObject();
 10          
 11         Scanner input = new Scanner(System.in);
 12         int choice;
 13 
 14         choice = input.nextInt();
 15         while (choice != 0) {
 16             switch (choice) {
 17             case 1:// insert Point object into list
 18                 o.add(new Point(input.nextDouble(),input.nextDouble()));
 19                 break;
 20             case 2:// insert Line object into list
 21                 o.add(new Line(new Point(input.nextDouble(),input.nextDouble()),new Point(input.nextDouble(),input.nextDouble()),input.next()));
 22                 break;
 23             case 3:// insert Plane object into list
 24                 o.add(new Plane(input.next()));
 25                 break;
 26             case 4:// delete index - 1 object from list
 27                 int index = input.nextInt();
 28                 if(index <= o.getList().size() && index > 0)
 29                     o.remove(index-1);
 30             }
 31             choice = input.nextInt();
 32         }
 33         
 34         for(Element e : o.getList()) {
 35             e.display();
 36         }
 37         input.close();
 38     }
 39 
 40 }
 41 abstract class Element {
 42     public void display() {
 43         
 44     }
 45 }
 46 class Point extends Element{
 47     private double x,y;
 48 
 49     public Point() {
 50         super();
 51         // TODO Auto-generated constructor stub
 52     }
 53 
 54     public Point(double x, double y) {
 55         super();
 56         this.x = x;
 57         this.y = y;
 58     }
 59 
 60     public double getX() {
 61         return x;
 62     }
 63 
 64     public void setX(double x) {
 65         this.x = x;
 66     }
 67 
 68     public double getY() {
 69         return y;
 70     }
 71 
 72     public void setY(double y) {
 73         this.y = y;
 74     }
 75     
 76     @Override
 77     public void display() {
 78         if(x > 0 && x <= 200 && y > 0 && y <= 200)
 79             System.out.printf("(%.2f,%.2f)\n",x,y);
 80         else System.out.println("Wrong Format");
 81     }
 82 }
 83 class Line extends Element{
 84     private Point point1;
 85     private Point point2;
 86     private String color = null;
 87 
 88     public Line() {
 89         super();
 90         // TODO Auto-generated constructor stub
 91     }
 92 
 93     public Line(Point point1, Point point2, String color) {
 94         super();
 95         this.point1 = point1;
 96         this.point2 = point2;
 97         this.color = color;
 98     }
 99 
100     public Point getPoint1() {
101         return point1;
102     }
103 
104     public void setPoint1(Point point1) {
105         this.point1 = point1;
106     }
107 
108     public Point getPoint2() {
109         return point2;
110     }
111 
112     public void setPoint2(Point point2) {
113         this.point2 = point2;
114     }
115 
116     public String getColor() {
117         return color;
118     }
119 
120     public void setColor(String color) {
121         this.color = color;
122     }
123     
124     public double getDistance(){
125         return Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + 
126                          Math.pow(point1.getY() - point2.getY(), 2));
127     }
128     
129     @Override
130     public void display() {
131         if(point1.getX() > 0&& point1.getX() < 200 && point1.getY() > 0 && point1.getY() < 200 &&
132           point2.getX() > 0&& point2.getX() < 200 && point2.getY() > 0 && point2.getY() < 200){
133             System.out.println("The line's color is:" + color);
134             System.out.println("The line's begin point's Coordinate is:");
135             point1.display();
136             System.out.println("The line's end point's Coordinate is:");
137             point2.display();
138             System.out.printf("The line's length is:%.2f\n" , getDistance());
139         }else
140             System.out.println("Wrong Format");
141         
142     }
143 }
144 class Plane extends Element {
145     private String color;
146 
147     public Plane() {
148         super();
149         // TODO Auto-generated constructor stub
150     }
151 
152     public Plane(String color) {
153         super();
154         this.color = color;
155     }
156 
157     public String getColor() {
158         return color;
159     }
160 
161     public void setColor(String color) {
162         this.color = color;
163     }
164     
165     @Override
166     public void display() {
167         System.out.println("The Plane's color is:" + color);
168     }
169 }
170 class GeometryObject {
171     private ArrayList<Element> list = new ArrayList<Element>();
172 
173     public ArrayList<Element> getList() {
174         return list;
175     }
176 
177     public void add(Element element) {
178         list.add(element);
179     }
180 
181     public void remove(int index) {
182         list.remove(index);
183     }
184 
185     public GeometryObject() {
186         super();
187         // TODO Auto-generated constructor stub
188     }
189 
190 }
View Code

 

           这里特别的需要注意边界值,index并不是自动会屏蔽,而是需要人为判断,(虽然题目这样我每次都会忽略。。。)。

 

课堂内容:

  Set: 集合,内部元素不可重复。还接触到了hashcode这种东西,哈希算法一般用于排序查找比较方便。不过hashset里面的和输入的元素是乱序的,不过有linkedhashset,这个顺序不打乱

  Map:图,有HashMap,TreeMap,这里又接触到树这个数据结构。大的放右边,小的放左边,查大小非常方便,不过只是初步接触。以后学的时候肯定掉头发。

  Stream:流,可用于一些数据的间接操作和终止操作,不是必要,但是也要了解。

 

以上就是这个阶段下来的Java学习的主要内容了,虽然疫情之下搞得我真的难受,PTA写的也真的秃头,但是以后靠这个吃饭的家伙,还是得好好学啊。。。

    最后:蟹蟹阅读,留下你的小心心吧^-^!

参考文献:菜鸟教程 https://www.runoob.com/

     CSDN https://www.csdn.net/

 

  

posted @ 2022-05-01 19:26  房阳淘  阅读(28)  评论(0编辑  收藏  举报