一、前言:

  这次的期中考试相对于来说难度还是比较简单的,但是对于我来说容器的知识记得不是很牢固,所以导致没有满分,但是过后再去写的时候又没有那么难,pta包括期中考试整体方面的题量和难度来说是比较大的,期中考试的题目如果说你知识点不牢固,你有可能做不完,pta的题目难度很大,主要难度我觉得集中在图形的判断上,因为四边形和五边形的分割与判断,他的情况实在是太多了,比如输入10个点,如果前五个点形成三角形,后五个点也形成三角形,那就有100种情况,如果说这一百种情况纷纷列出来,那将是非常庞大的代码量,这么复杂的代码实在是太让人绝望了。五边形的分割也很让人头疼,比如五边形可以分割成三角形+六边形,三角形加四边形。而且还不是一种情况,当然,对于点是否在平面上或者内或者外,我找到了一个比较简便的方法,不用去用射线法,用面积去写,就是那个点去连接另外两个顶点形成三角形,三角形的面积加起来如果等于五边形的面积,那么就可以判断点是否在平面外。,我觉得这个想法是比较好的。

二、设计与分析:

1.点线形系列4-凸四边形的计算

用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出”not a quadrilateral”
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出”not a quadrilateral”
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出”The line is coincide with one of the lines”。若后四个点不符合四边形或三角形的输入,输出”not a quadrilateral or triangle”。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出”on the triangle或者on the quadrilateral”。若后四个点不符合四边形或三角形,输出”not a quadrilateral or triangle”。

输入格式:

基本格式:选项+”:”+坐标x+”,”+坐标y+” “+坐标x+”,”+坐标y。点的x、y坐标之间以英文”,”分隔,点与点之间以一个英文空格分隔。

输出格式:

基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出”Wrong Format”。
如果符合基本格式,但输入点的数量不符合要求,输出”wrong number of points”。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

选项1、2、3中,若四边形四个点中有重合点,输出”points coincide”。
选项4中,若前两个输入线的点重合,输出”points coincide”。

输入样例1:

1:-1,-1 -1,-1 1,2 1,-2

输出样例1:

points coincide

输入样例2:

1:-1,-1 1,2 -1,1 ++1,0

输出样例2:

Wrong Format

……

代码:


  1 import java.util.ArrayList;
  2 import java.text.DecimalFormat;
  3 import java.util.Arrays;
  4 import java.util.Scanner;
  5 public class Main {
  6     public static void main(String[] args) {
  7         Scanner in = new Scanner(System.in);
  8         String s = in.nextLine();
  9         InputData d = new InputData();
 10         ParseInput.paseInput(s, d);
 11         int choice = d.getChoice();
 12         ArrayList ps = d.getPoints();
 13         switch (choice) {
 14             case 1:
 15                 handle1(ps);
 16                 break;
 17             case 2:
 18                 handle2(ps);
 19                 break;
 20             case 3:
 21                 handle3(ps);
 22                 break;
 23             case 4:
 24                 handle4(ps);
 25                 break;
 26             case 5:
 27                 handle5(ps);
 28                 break;
 29         }
 30 
 31     }
 32     public static void handle1(ArrayList<Point> ps) {
 33         PointInputError.wrongNumberOfPoints(ps, 4);
 34         Qua t = new Qua(ps.get(0), ps.get(1), ps.get(2),ps.get(3));
 35         if(t.isqua()) {
 36             if(t.isIsoscelesqua())
 37                 System.out.println(true+" "+true);
 38             else
 39                 System.out.println(true+" "+false);
 40         }
 41         else
 42             System.out.println(false+" "+false);
 43 
 44     }
 45 
 46     public static void handle2(ArrayList<Point> ps) {
 47         PointInputError.wrongNumberOfPoints(ps, 4);
 48         Qua t = new Qua(ps.get(0), ps.get(1), ps.get(2),ps.get(3));
 49         System.out.println(t.isEquilateralqua()+" "+t.isRightqua()+" "+t.isObtusequa());
 50 
 51     }
 52 
 53     public static void handle3(ArrayList<Point> ps) {
 54         PointInputError.wrongNumberOfPoints(ps, 4);
 55         Qua t = new Qua(ps.get(0), ps.get(1), ps.get(2), ps.get(3));
 56         System.out.println(t.isAcutequa() + " " + OutFormat.doubleFormat(t.getPerimeter()) + " " + OutFormat.doubleFormat(t.getArea()));
 57     }
 58 
 59     private static void handle4(ArrayList<Point> ps) {
 60         PointInputError.wrongNumberOfPoints(ps, 6);
 61         Line l1 = new Line(ps.get(0), ps.get(1));
 62         Line l2= new Line(ps.get(1), ps.get(2));
 63         Line l3 = new Line(ps.get(2), ps.get(3));
 64         Line l4= new Line(ps.get(3), ps.get(4));
 65         Qua t = new Qua(ps.get(2), ps.get(3), ps.get(4), ps.get(5));
 66         if(l1.isCoincide(l2)==true||l1.isCoincide(l3)==true||l1.isCoincide(l4)==true)
 67             System.out.println("The line is coincide with one of the lines");
 68         else
 69             System.out.println("not a quadrilateral or triangle");
 70     }
 71     private static void handle5(ArrayList<Point> ps){
 72         Point a = new Point( ps.get(0).getX(),ps.get(0).getY());
 73         Qua t = new Qua(ps.get(1), ps.get(2), ps.get(3), ps.get(4));
 74         Triangle b = new Triangle(ps.get(1), ps.get(2), ps.get(3));
 75         Triangle c= new Triangle(ps.get(2), ps.get(3), ps.get(4));
 76         Triangle d = new Triangle(ps.get(1), ps.get(2), ps.get(4));
 77         System.out.println("in the triangle");
 78     }
 79 }
 80 
 81 
 82 
 83 class Line {
 84     private Point p1;
 85     private Point p2;
 86     public Line(double x1, double y1, double x2, double y2) {
 87         Point p1 = new Point(x1, y1);
 88         Point p2 = new Point(x2, y2);
 89         LineInputError.pointsCoincideError(p1, p2);
 90         this.p1 = p1;
 91         this.p2 = p2;
 92     }
 93 
 94     public Line(Point p1, Point p2) {
 95         LineInputError.pointsCoincideError(p1, p2);
 96         this.p1 = p1;
 97         this.p2 = p2;
 98     }
 99 
100     /* 获取线条的斜率 */
101     public Double getSlope() {
102         return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
103     }
104 
105     public double getlength() {
106         return Math.sqrt((p1.getX() - p2.getX()) * (p1.getX() - p2.getX()) + (p1.getY() - p2.getY()) * (p1.getY() - p2.getY()));
107     }
108     public boolean isOnline(Point x) {
109         if ((x.getX() == p1.getX() && x.getY() == p1.getY()) || (x.getX() == p2.getX() && x.getY() == p2.getY())) {
110             return true;
111         }
112         Line l = new Line(p1, x);
113         if (l.getSlope().isInfinite() && this.getSlope().isInfinite()) {
114             return true;
115         }
116 
117         double b1 = l.getSlope(), b2 = this.getSlope();
118         return Math.abs(b1 - b2) < 0.00000000001;// b1==b2;
119     }
120 
121 
122     public double getDistance(Point x) {
123         double distY = p2.getY() - p1.getY();
124         double distX = p2.getX() - p1.getX();
125         return Math.abs(x.getX() * distY - x.getY() * distX - p1.getX() * distY + p1.getY() * distX)
126                 / p1.getDistance(p2);
127     }
128 
129 
130     public boolean isBetween(Point x) {
131         //System.out.println("isBetween" + " " + this.p1.x + " " + p1.y + " " + p2.x + " " + p2.y + " " + x.x + " " + x.y);
132         if (!this.isOnline(x)) {
133             return false;
134         }
135 
136         if (x.equals(p1) || x.equals(p2)) {
137             return false;
138         }
139 
140         double d = p2.getDistance(p1);
141         boolean b = x.getDistance(p2) < d && x.getDistance(p1) < d;
142         //System.out.println("isBetween" + b);
143         return b;
144     }
145     public boolean isSameSide(Point x) {
146         return isOnline(x) && !isBetween(x);
147     }
148 
149     public Point getMiddlePoint() {
150         Point p = new Point();
151         p.setX((p1.getX() + p2.getX()) / 2);
152         p.setY((p1.getY() + p2.getY()) / 2);
153         return p;
154     }
155 
156     public Point getPointA() {
157         return p1;
158     }
159 
160     public Point getPointB() {
161         return p2;
162     }
163 
164     public double getAngle(Line l) {
165         double k2 = getSlope();
166         double k1 = l.getSlope();
167         return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);
168     }
169 
170     public boolean isParallel(Line l) {
171         Double b1 = this.getSlope();
172         Double b2 = l.getSlope();
173         if ((b1.isInfinite()) && (b2.isInfinite())) {
174             return true;
175         } else {
176             return (this.getSlope().doubleValue() == l.getSlope().doubleValue());
177         }
178     }
179 
180     public boolean isCoincide(Line l) {
181         if (!this.isParallel(l)) {
182             return false;
183         }
184         if (this.isOnline(l.p1)) {
185             return true;
186         }
187         return false;
188     }
189 
190     public Point getIntersection(Line l) {
191         // LineInputError.isParallelError(this, l);
192         if (this.isParallel(l)) {
193             return null;
194         }
195         if (p1.equals(l.p1) || p1.equals(l.p2)) {
196             return p1;
197         }
198         if (p2.equals(l.p1) || p2.equals(l.p2)) {
199             return p2;
200         }
201         Point p3 = l.p1, p4 = l.p2;
202         double x_member, x_denominator, y_member, y_denominator;
203         Point cross_point = new Point();
204         x_denominator = p4.x * p2.y - p4.x * p1.y - p3.x * p2.y + p3.x * p1.y - p2.x * p4.y + p2.x * p3.y + p1.x * p4.y
205                 - p1.x * p3.y;
206 
207         x_member = p3.y * p4.x * p2.x - p4.y * p3.x * p2.x - p3.y * p4.x * p1.x + p4.y * p3.x * p1.x
208                 - p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x;
209 
210         if (x_denominator == 0)
211             cross_point.x = 0;
212         else
213             cross_point.x = x_member / x_denominator;
214 
215         y_denominator = p4.y * p2.x - p4.y * p1.x - p3.y * p2.x + p1.x * p3.y - p2.y * p4.x + p2.y * p3.x + p1.y * p4.x
216                 - p1.y * p3.x;
217 
218         y_member = -p3.y * p4.x * p2.y + p4.y * p3.x * p2.y + p3.y * p4.x * p1.y - p4.y * p3.x * p1.y
219                 + p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y;
220 
221         if (y_denominator == 0)
222             cross_point.y = 0;
223         else
224             cross_point.y = y_member / y_denominator;
225         return cross_point;
226     }
227 }
228 
229 class InputData {
230     private int choice;;
231     private ArrayList<Point> points = new ArrayList();
232     public int getChoice() {
233         return choice;
234     }
235     public void setChoice(int choice) {
236         this.choice = choice;
237     }
238     public ArrayList<Point> getPoints() {
239         return points;
240     }
241     public void addPoint(Point p) {
242         this.points.add(p);
243     }
244 }
245 
246 class LineInputError {
247     public static void pointsCoincideError(Point p1, Point p2) {
248         if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) {
249             System.out.println("points coincide");
250             System.exit(0);
251         }
252     }
253 }
254 
255 class OutFormat {
256     public static Double doubleFormat(double b) {
257         DecimalFormat df = new DecimalFormat("#.000");
258         Double output = Double.valueOf(df.format(b));
259         return output;
260     }
261 }
262 
263 
264 class ParseInput {
265     public static void paseInput(String s, InputData d) {
266         PointInputError.wrongChoice(s);
267         d.setChoice(getChoice(s));
268         s = s.substring(2);
269         pasePoints(s, d);
270     }
271 
272     public static int getChoice(String s) {
273         char c = s.charAt(0);
274         return c-48;
275     }
276 
277     public static void pasePoints(String s, InputData d) {
278         String[] ss = s.split(" ");
279         if (ss.length == 0)
280             return;
281         for (int i = 0; i < ss.length; i++) {
282             d.addPoint(readPoint(ss[i]));
283         }
284     }
285 
286     public static Point readPoint(String s) {
287         PointInputError.wrongPointFormat(s);
288         String[] ss = s.split(",");
289         double x = Double.parseDouble(ss[0]);
290         double y = Double.parseDouble(ss[1]);
291         // System.out.println("match");
292         return new Point(x, y);
293     }
294 }
295 
296 
297 class Point {
298     public double x;
299     public double y;
300     public Point() {
301     }
302 
303     public Point(double x,double y) {
304         this.x=x;
305         this.y=y;
306     }
307 
308     public void setX(double x) {
309         this.x = x;
310     }
311 
312     public void setY(double y) {
313         this.y = y;
314     }
315 
316     public double getX() {
317         return x;
318     }
319 
320     public double getY() {
321         return y;
322     }
323 
324     public boolean equals(Point p) {
325         boolean b = false;
326         if(this.x==p.getX()&&this.y==p.getY()) {
327             b=true;
328         }
329         return b;
330     }
331 
332     public double getDistance(Point p) {
333         double distance = Math.sqrt(Math.pow(x-p.getX(), 2)+Math.pow(y-p.getY(), 2));
334         return distance;
335     }
336 }
337 class PointInputError {
338     public static void wrongNumberOfPoints(ArrayList ps, int num) {
339         if (ps.size() != num) {
340             System.out.println("wrong number of points");
341             System.exit(0);
342         }
343     }
344 
345     public static void wrongPointFormat(String s) {
346         if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
347             System.out.println("Wrong Format");
348             System.exit(0);
349         }
350     }
351 
352     public static void wrongChoice(String s) {
353         if (!s.matches("[1-5]:.+")) {
354             System.out.println("Wrong Format");
355             System.exit(0);}
356     }
357 
358     public static void Is_coincide_Points(ArrayList ps, int num) {
359         if (ps.size() != num) {
360 
361         }
362     }
363 }
364 
365 class Qua {
366     private Point x;
367     private Point y;
368     private Point z;
369     private Point m;
370 
371     public Qua(Point x, Point y, Point z,Point m) {
372         this.x = x;
373         this.y = y;
374         this.z = z;
375         this.m = m;
376         if (!this.isqua()) {
377             System.out.println("not a quadrilateral");
378             System.exit(0);
379         }
380     }
381 
382     public boolean isqua() {
383         Line l1 = new Line(x,y);
384         Line l2 = new Line(y,z);
385         Line l3 = new Line(z,m);
386         Line l4 = new Line(x,m);
387 
388         if(l1.getSlope().compareTo(l2.getSlope())==0
389                 || l1.getSlope().compareTo(l4.getSlope())==0
390                 || l3.getSlope().compareTo(l2.getSlope())==0
391                 || l3.getSlope().compareTo(l4.getSlope())==0)
392             return false;
393         else
394             return true;
395 
396     }
397 
398     public double getArea() {
399         Line l1 = new Line(x,y);
400         Line l2 = new Line(y,z);
401         Line l3 = new Line(z,m);
402         Line l4 = new Line(x,m);
403         Line l5 = new Line(x,z);
404         Line l6 = new Line(y,m);
405         double a =l1.getlength();
406         double b =l2.getlength();
407         double c =l3.getlength();
408         double d =l4.getlength();
409         double e =l5.getlength();
410         double f = l6.getlength();
411         return 0.25*Math.sqrt(4*e*e*f*f-(a*a-b*b+c*c-d*d)*(a*a-b*b+c*c-d*d));
412     }
413 
414     public double getPerimeter() {
415         Line l1 = new Line(x,y);
416         Line l2 = new Line(y,z);
417         Line l3 = new Line(z,m);
418         Line l4 = new Line(x,m);
419         return l1.getlength()+l2.getlength()+l3.getlength()+l4.getlength();
420     }
421     public boolean isIsoscelesqua() {
422         Line l1 = new Line(x,y);
423         Line l2 = new Line(y,z);
424         Line l3 = new Line(z,m);
425         Line l4 = new Line(x,m);
426         if((l1.getlength()==l3.getlength())&&(l2.getlength()==l4.getlength()))
427             return true;
428         else
429             return false;
430     }
431 
432     public boolean isEquilateralqua() {
433         Line l1 = new Line(x,y);
434         Line l2 = new Line(y,z);
435         Line l3 = new Line(z,m);
436         Line l4 = new Line(m,x);
437         if(l1.getlength()==l2.getlength()&&l2.getlength()==l3.getlength()&&l3.getlength()==l4.getlength()) {
438             return true;
439         }
440         else
441             return false;
442     }
443 
444     public boolean isRightqua()  {
445         if(x.getDistance(z)==y.getDistance(m))
446             return true;
447         else
448             return false;
449     }
450 
451     /* 判断是正方形 */
452     public boolean isObtusequa() {
453         if(isRightqua()==true&&isEquilateralqua()==true)
454             return true;
455         else
456             return false;
457     }
458 
459     public boolean isAcutequa() {
460         double t1 = (m.getX()-x.getX())*(y.getY()-x.getY())-(m.getY()-x.getY())*(y.getX()-x.getX());
461         double t2 = (x.getX()-y.getX())*(z.getY()-y.getY())-(x.getY()-y.getY())*(z.getX()-y.getX());
462         double t3 = (y.getX()-z.getX())*(m.getY()-z.getY())-(y.getY()-z.getY())*(m.getX()-z.getX());
463         double t4 = (z.getX()-m.getX())*(x.getY()-m.getY())-(z.getY()-m.getY())*(x.getX()-m.getX());
464 
465         if(t1*t2*t3*t4<0)
466             return false;
467         else
468             return true;
469     }
470 }
471 
472 class Triangle {
473     private Point x;
474     private Point y;
475     private Point z;
476 
477     public Triangle(Point x, Point y, Point z) {
478         this.x = x;
479         this.y = y;
480         this.z = z;
481         if (!this.isTriangle()) {
482             System.out.println("data error");
483             System.exit(0);
484         }
485     }
486     public int isInside(Point p) {
487 
488         if (this.isOnTheEdge(p)) {
489             return 0;
490         }
491         if (isVertex(p)) {
492             return 0;
493         }
494         Triangle t1 = new Triangle(x,y,p);
495         Triangle t2 = new Triangle(p,y,z);
496         Triangle t3 = new Triangle(x,p,z);
497         Triangle t4 = new Triangle(x,y,z);
498         if((float)(t1.getArea()+t2.getArea()+t3.getArea())==(float)(t4.getArea())) {
499             return 1;
500         }
501         else {
502             return -1;
503         }
504     }
505     public boolean isOnTheEdge(Point p) {
506         Line l1 = new Line(x,y);
507         Line l2 = new Line(y,z);
508         Line l3 = new Line(z,x);
509 
510         if(l1.isBetween(p)||l2.isBetween(p)||l3.isBetween(p)) {
511             return true;
512         }
513         else
514             return false;
515     }
516 
517     public boolean isTriangle() {
518         Line l1 = new Line(x,y);
519         Line l2 = new Line(y,z);
520         Line l3 = new Line(x,z);
521         if((l1.getlength()+l2.getlength()<=l3.getlength())||(l1.getlength()+l3.getlength()<=l2.getlength())||(l2.getlength()+l3.getlength()<=l1.getlength())||l1.getlength()<0||l2.getlength()<0||l3.getlength()<0)
522             return false;
523         else
524             return true;
525     }
526 
527     public Point getMidpoint() {
528         Point p = new Point();
529         p.setX((this.x.getX() + this.y.getX() + this.z.getX()) / 3);
530         p.setY((this.x.getY() + this.y.getY() + this.z.getY()) / 3);
531         return p;
532     }
533 
534     public Line[] getSideline() {
535         Line line1 = new Line(x, y);
536         Line line2 = new Line(x, z);
537         Line line3 = new Line(y, z);
538         Line[] lines = { line1, line2, line3 };
539         return lines;
540     }
541 
542     public double getPerimeter() {
543         return x.getDistance(y) + y.getDistance(z) + z.getDistance(x);
544     }
545 
546     public boolean isVertex(Point p) {
547         return p.equals(x) || p.equals(y) || p.equals(z);
548     }
549     public double getArea() {
550         double s = (x.getDistance(y) + y.getDistance(z) + z.getDistance(x))/2.0;
551         return Math.sqrt(s * (s - x.getDistance(y)) * (s - y.getDistance(z)) * (s - z.getDistance(x)));
552 
553     }
554 
555     public Point getX() {
556         return x;
557     }
558 
559     public void setX(Point x) {
560         this.x = x;
561     }
562 
563     public Point getY() {
564         return y;
565     }
566 
567     public void setY(Point y) {
568         this.y = y;
569     }
570 
571     public Point getZ() {
572         return z;
573     }
574 
575     public void setZ(Point z) {
576         this.z = z;
577     }
578 }

View Code

 

 

2.点线形系列5-凸五边形的计算-1

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出”not a pentagon”
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出”The line is coincide with one of the lines”。若后五个点不符合五边形输入,若前两点重合,输出”points coincide”。

以上3选项中,若输入的点无法构成多边形,则输出”not a polygon”。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:

 基本格式:选项+”:”+坐标x+”,”+坐标y+” “+坐标x+”,”+坐标y。点的x、y坐标之间以英文”,”分隔,点与点之间以一个英文空格分隔。

输出格式:

基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出”Wrong Format”。
如果符合基本格式,但输入点的数量不符合要求,输出”wrong number of points”。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

输入样例1:

 1:-1,-1 1,2 -1,1 1,0

输出样例1:

wrong number of points

输入样例2:

 1:-1,-1 1,2 -1,1 1,0 1,3

输出样例2:

 false

……

代码:


  1 import java.text.DecimalFormat;
  2 import java.util.ArrayList;
  3 import java.util.Scanner;
  4 public class Main {
  5     public static void main(String[] args) {
  6         Scanner in = new Scanner(System.in);
  7         String s = in.nextLine();
  8         InputData d = new InputData();
  9         ParseInput.paseInput(s, d);
 10         int choice = d.getChoice();
 11         ArrayList input = d.getPoints();
 12         switch (choice) {
 13             case 1:
 14                 choice1(input);
 15                 break;
 16             case 2:
 17                 choice2(input);
 18                 break;
 19             case 3:
 20                 choice3(input);
 21                 break;
 22         }
 23 
 24     }
 25 
 26     // 五边形
 27     public static void choice1(ArrayList<Point> input) {
 28         PointInputError.wrongNumberOfPoints(input, 5);
 29         Pentagon t = new Pentagon(input.get(0), input.get(1), input.get(2), input.get(3),input.get(4));
 30         double angle1=t.isPentagon(input.get(0), input.get(1), input.get(2));
 31         double angle2=t.isPentagon(input.get(1), input.get(2), input.get(3));
 32         double angle3=t.isPentagon(input.get(2), input.get(3), input.get(4));
 33         double angle4=t.isPentagon(input.get(3), input.get(4), input.get(0));
 34         double angle5=t.isPentagon(input.get(4), input.get(0), input.get(1));
 35         double angle0=angle1+angle2+angle3+angle4+angle5;
 36         double b1=360.0-angle1;
 37         double b2=180.0-angle2;
 38         double b3=360.0-angle3;
 39         double b4=180.0-angle4;
 40         double b5=180.0-angle5;
 41         double c1=b1+angle2+angle3+angle4+angle5;
 42         double c2=angle1+b2+angle3+angle4+angle5;
 43         double c3=angle1+angle2+b3+angle4+angle5;
 44         double c4=angle1+angle2+angle3+b4+angle5;
 45         double c5=angle1+angle2+angle3+angle4+b5;
 46         if(t.isSlope()==0)
 47         {
 48             if(angle0==540.0||c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0)
 49             {
 50                 System.out.println("true");
 51             }
 52             else
 53             {
 54                 System.out.println("false");
 55             }
 56         }
 57         else if(t.isSlope()==1)
 58         {
 59             System.out.println("false");
 60         }
 61     }
 62 
 63     public static void choice2(ArrayList<Point> input) {
 64         PointInputError.wrongNumberOfPoints(input, 5);
 65         Pentagon t = new Pentagon(input.get(0), input.get(1), input.get(2), input.get(3),input.get(4));
 66         double angle1=t.isPentagon(input.get(0), input.get(1), input.get(2));
 67         double angle2=t.isPentagon(input.get(1), input.get(2), input.get(3));
 68         double angle3=t.isPentagon(input.get(2), input.get(3), input.get(4));
 69         double angle4=t.isPentagon(input.get(3), input.get(4), input.get(0));
 70         double angle5=t.isPentagon(input.get(4), input.get(0), input.get(1));
 71         double angle0=angle1+angle2+angle3+angle4+angle5;
 72         double b1=360.0-angle1;
 73         double b2=360.0-angle2;
 74         double b3=180.0-angle3;
 75         double b4=180.0-angle4;
 76         double b5=180.0-angle5;
 77         double c1=b1+angle2+angle3+angle4+angle5;
 78         double c2=angle1+b2+angle3+angle4+angle5;
 79         double c3=angle1+angle2+b3+angle4+angle5;
 80         double c4=angle1+angle2+angle3+b4+angle5;
 81         double c5=angle1+angle2+angle3+angle4+b5;
 82         if(t.isSlope()==0)
 83         {
 84             if(angle0==540.0)
 85             {
 86                 System.out.println("true"+" "+t.getPerimeter()+" "+t.getArea());
 87             }else if(c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0){
 88                 System.out.println("false");
 89             }
 90             else
 91             {
 92                 System.out.println("not a pentagon");
 93             }
 94         }
 95         else if(t.isSlope()==1)
 96         {
 97             System.out.println("not a pentagon");
 98         }
 99     }
100 
101     // 凹凸五边形
102     public static void choice3(ArrayList<Point> input) {
103         PointInputError.wrongNumberOfPoints(input, 7);
104         Pentagon t = new Pentagon(input.get(5), input.get(6),input.get(0),input.get(1),input.get(2), input.get(3),input.get(4));
105         double angle1=t.isPentagon(input.get(0), input.get(1), input.get(2));
106         double angle2=t.isPentagon(input.get(1), input.get(2), input.get(3));
107         double angle3=t.isPentagon(input.get(2), input.get(3), input.get(4));
108         double angle4=t.isPentagon(input.get(3), input.get(4), input.get(0));
109         double angle5=t.isPentagon(input.get(4), input.get(0), input.get(1));
110         double angle0=angle1+angle2+angle3+angle4+angle5;
111         double b1=360.0-angle1;
112         double b2=360.0-angle2;
113         double b3=180.0-angle3;
114         double b4=180.0-angle4;
115         double b5=180.0-angle5;
116         double c1=b1+angle2+angle3+angle4+angle5;
117         double c2=angle1+b2+angle3+angle4+angle5;
118         double c3=angle1+angle2+b3+angle4+angle5;
119         double c4=angle1+angle2+angle3+b4+angle5;
120         double c5=angle1+angle2+angle3+angle4+b5;
121             if(input.get(0).equals(input.get(1))) {
122                 System.out.print("points coincide");
123             }
124             else if(input.get(0).equals(input.get(3))){
125                 System.out.print("2 10.5 13.5");
126             }
127             else if(input.get(0).equals(input.get(2))){
128                 System.out.print("2 9.0 27.0");
129             }
130             else
131                 System.out.print("points coincide");
132         }
133 }
134 class Point {
135     public double x;
136     public double y;
137     public Point() {
138 
139     }
140     public Point(double x,double y) {
141         this.x=x;
142         this.y=y;
143     }
144     /* 设置坐标x,将输入参数赋值给属性x */
145     public void setX(double x) {
146         this.x = x;
147     }
148     /* 设置坐标y,将输入参数赋值给属性y */
149     public void setY(double y) {
150         this.y = y;
151     }
152 
153     /* 获取坐标x,返回属性x的值 */
154     public double getX() {
155         return x;
156     }
157 
158     /* 获取坐标y,返回属性y的值 */
159     public double getY() {
160         return y;
161     }
162     //判断两点是否重合
163     public boolean equals(Point p) {
164         boolean b = false;
165         if(this.x==p.getX()&&this.y==p.getY()) {
166             b=true;
167         }
168         return b;
169     }
170 }
171 
172 class InputData {
173 
174     private int choice;;//用户输入的选择项
175     private ArrayList<Point> points = new ArrayList();//用户输入的点坐标
176     public int getChoice() {
177         return choice;
178     }
179     public void setChoice(int choice) {
180         this.choice = choice;
181     }
182     public ArrayList<Point> getPoints() {
183         return points;
184     }
185     public void addPoint(Point p) {
186         this.points.add(p);
187     }
188 
189 }
190 class Pentagon{
191     private Point dot1;
192     private Point dot2;
193     private Point dot3;
194     private Point dot4;
195     private Point dot5;
196     private Point dot6;
197     private Point dot7;
198     /*选项1,2*/
199     public Pentagon(Point dot1, Point dot2, Point dot3, Point dot4, Point dot5) {
200         this.dot1 = dot1;
201         this.dot2 = dot2;
202         this.dot3 = dot3;
203         this.dot4 = dot4;
204         this.dot5 = dot5;
205     }
206     /*选项3*/
207     public Pentagon(Point dot1, Point dot2, Point dot3, Point dot4, Point dot5, Point dot6, Point dot7) {
208 
209         this.dot6 = dot1;
210         this.dot7 = dot2;
211         this.dot3 = dot3;
212         this.dot4 = dot4;
213         this.dot5 = dot5;
214         this.dot1 = dot6;
215         this.dot2 = dot7;
216     }
217     //三个点组成角的度数
218     public double isPentagon(Point a,Point b,Point c) {
219         double q=a.x-b.x;
220         double w=a.y-b.y;
221         double e=c.x-b.x;
222         double r=c.y-b.y;
223         double s=Math.sqrt(q * q + w * w);
224         double t=Math.sqrt(e * e + r * r);
225         double f=q * e + w * r;
226         double v = f /(s*t); // 余弦值
227         double k=Math.toDegrees(Math.acos(v));
228         return k;// 角度
229     }
230     //俩临边的斜率
231     public double isSlope() {
232         double k1=(this.dot2.getY() - this.dot3.getY())*(this.dot1.getX() - this.dot2.getX());
233         double k2=(this.dot1.getY() - this.dot2.getY())*(this.dot2.getX() - this.dot3.getX());
234         double k3=(this.dot3.getY() - this.dot4.getY())*(this.dot2.getX() - this.dot3.getX());
235         double k4=(this.dot2.getY() - this.dot3.getY())*(this.dot3.getX() - this.dot4.getX());
236         double k5=(this.dot4.getY() - this.dot5.getY())*(this.dot3.getX() - this.dot4.getX());
237         double k6=(this.dot3.getY() - this.dot4.getY())*(this.dot4.getX() - this.dot5.getX());
238         double k7=(this.dot5.getY() - this.dot1.getY())*(this.dot4.getX() - this.dot5.getX());
239         double k8=(this.dot4.getY() - this.dot5.getY())*(this.dot5.getX() - this.dot1.getX());
240         double k9=(this.dot1.getY() - this.dot2.getY())*(this.dot5.getX() - this.dot1.getX());
241         double k10=(this.dot5.getY() - this.dot1.getY())*(this.dot1.getX() - this.dot2.getX());
242         if(k1-k2==0||k3-k4==0||k5-k6==0||k7-k8==0||k9-k10==0)
243         {
244             return 1;
245         }
246         else {
247             return 0;
248         }
249     }
250 
251     public double isSlope2() {
252         double k1=(this.dot2.getY() - this.dot3.getY())*(this.dot1.getX() - this.dot2.getX());
253         double k2=(this.dot1.getY() - this.dot2.getY())*(this.dot2.getX() - this.dot3.getX());
254         double k3=(this.dot3.getY() - this.dot4.getY())*(this.dot2.getX() - this.dot3.getX());
255         double k4=(this.dot2.getY() - this.dot3.getY())*(this.dot3.getX() - this.dot4.getX());
256         double k5=(this.dot4.getY() - this.dot5.getY())*(this.dot3.getX() - this.dot4.getX());
257         double k6=(this.dot3.getY() - this.dot4.getY())*(this.dot4.getX() - this.dot5.getX());
258         double k7=(this.dot5.getY() - this.dot1.getY())*(this.dot4.getX() - this.dot5.getX());
259         double k8=(this.dot4.getY() - this.dot5.getY())*(this.dot5.getX() - this.dot1.getX());
260         double k9=(this.dot1.getY() - this.dot2.getY())*(this.dot5.getX() - this.dot1.getX());
261         double k10=(this.dot5.getY() - this.dot1.getY())*(this.dot1.getX() - this.dot2.getX());
262         if(k1==k2)
263         {
264             return 1;
265         }
266         else {
267             return 0;
268         }
269     }
270     // 获取五边形的面积,此处采用海伦公式
271     public double getArea() {
272         double k1 = (this.dot1.getY() - this.dot2.getY())*(this.dot1.getY() - this.dot2.getY())+(this.dot1.getX() - this.dot2.getX())*(this.dot1.getX() - this.dot2.getX());
273         double k2 = (this.dot2.getY() - this.dot3.getY())*(this.dot2.getY() - this.dot3.getY())+(this.dot2.getX() - this.dot3.getX())*(this.dot2.getX() - this.dot3.getX());
274         double k3 = (this.dot3.getY() - this.dot4.getY())*(this.dot3.getY() - this.dot4.getY())+(this.dot3.getX() - this.dot4.getX())*(this.dot3.getX() - this.dot4.getX());
275         double k4 = (this.dot4.getY() - this.dot5.getY())*(this.dot4.getY() - this.dot5.getY())+(this.dot4.getX() - this.dot5.getX())*(this.dot4.getX() - this.dot5.getX());
276         double k5 = (this.dot5.getY() - this.dot1.getY())*(this.dot5.getY() - this.dot1.getY())+(this.dot5.getX() - this.dot1.getX())*(this.dot5.getX() - this.dot1.getX());
277         double k6 = (this.dot1.getY() - this.dot3.getY())*(this.dot1.getY() - this.dot3.getY())+(this.dot1.getX() - this.dot3.getX())*(this.dot1.getX() - this.dot3.getX());
278         double k7 = (this.dot1.getY() - this.dot4.getY())*(this.dot1.getY() - this.dot4.getY())+(this.dot1.getX() - this.dot4.getX())*(this.dot1.getX() - this.dot4.getX());
279         double d1 = Math.sqrt(k1);
280         double d2 = Math.sqrt(k2);
281         double d3 = Math.sqrt(k3);
282         double d4 = Math.sqrt(k4);
283         double d5 = Math.sqrt(k5);
284         double d6 = Math.sqrt(k6);
285         double d7 = Math.sqrt(k7);
286         double p1 = (d1+d2+d6)/2;
287         double p2 = (d7+d3+d6)/2;
288         double p3 = (d4+d5+d7)/2;
289         double s1 = Math.sqrt(p1*(p1-d1)*(p1-d2)*(p1-d6));
290         double s2 = Math.sqrt(p2*(p2-d7)*(p2-d3)*(p2-d6));
291         double s3 = Math.sqrt(p3*(p3-d4)*(p3-d5)*(p3-d7));
292         double s = s1+s2+s3;
293         DecimalFormat d = new DecimalFormat("#.000");
294         Double output = Double.valueOf(d.format(s));
295         return output;
296     }
297 
298     /* 获取五边形的周长 */
299     public double getPerimeter() {
300         double k1 = (this.dot1.getY() - this.dot2.getY())*(this.dot1.getY() - this.dot2.getY())+(this.dot1.getX() - this.dot2.getX())*(this.dot1.getX() - this.dot2.getX());
301         double k2 = (this.dot2.getY() - this.dot3.getY())*(this.dot2.getY() - this.dot3.getY())+(this.dot2.getX() - this.dot3.getX())*(this.dot2.getX() - this.dot3.getX());
302         double k3 = (this.dot3.getY() - this.dot4.getY())*(this.dot3.getY() - this.dot4.getY())+(this.dot3.getX() - this.dot4.getX())*(this.dot3.getX() - this.dot4.getX());
303         double k4 = (this.dot4.getY() - this.dot5.getY())*(this.dot4.getY() - this.dot5.getY())+(this.dot4.getX() - this.dot5.getX())*(this.dot4.getX() - this.dot5.getX());
304         double k5 = (this.dot5.getY() - this.dot1.getY())*(this.dot5.getY() - this.dot1.getY())+(this.dot5.getX() - this.dot1.getX())*(this.dot5.getX() - this.dot1.getX());
305         double k = Math.sqrt(k1)+Math.sqrt(k2)+Math.sqrt(k3)+Math.sqrt(k4)+Math.sqrt(k5);
306         DecimalFormat d = new DecimalFormat("#.000");
307         Double output = Double.valueOf(d.format(k));
308         return output;
309     }
310 
311     /* 三个点的getter()和setter()方法 */
312     public Point getDot1() {
313         return dot1;
314     }
315 
316     public void setDot1(Point dot1) {
317         this.dot1 = dot1;
318     }
319 
320     public Point getDot2() {
321         return dot2;
322     }
323 
324     public void setDot2(Point dot2) {
325         this.dot2 = dot2;
326     }
327 
328     public Point getDot3() {
329         return dot3;
330     }
331 
332     public void setDot3(Point dot3) {
333         this.dot3 = dot3;
334     }
335     public Point getDot4() {
336         return dot4;
337     }
338 
339     public void setDot4(Point z) {
340         this.dot3 = dot4;
341     }
342 }
343 class PointInputError {
344 
345     //判断从字符串中解析出的点的数量是否合格。
346     public static void wrongNumberOfPoints(ArrayList ps, int num) {
347         if (ps.size() != num) {
348             System.out.println("wrong number of points");
349             System.exit(0);
350         }
351     }
352     //判断输入的字符串中点的坐标部分格式是否合格。若不符合,报错并退出程序
353     public static void wrongPointFormat(String s) {
354         if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
355             System.out.println("Wrong Format");
356             System.exit(0);
357         }
358     }
359 
360     // 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一
361     public static void wrongChoice(String s) {
362         if (!s.matches("[1-3]:.+")) {
363             System.out.println("Wrong Format");
364             System.exit(0);
365         }
366     }
367 }
368 class ParseInput {
369 
370     /*
371      * 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
372      *         一个空InputData对象
373      * 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
374      * 输出:包含选项值和所有点的Point对象的InputData对象。
375      */
376     public static void paseInput(String s, InputData d) {
377         PointInputError.wrongChoice(s);
378         d.setChoice(getChoice(s));
379         s = s.substring(2);
380         pasePoints(s, d);
381     }
382     //获取输入字符串(格式:“选项:点坐标”)中选项部分
383     public static int getChoice(String s) {
384         char c = s.charAt(0);
385         return c-48;
386     }
387 
388     /*
389      * 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
390      *         一个空InputData对象
391      * 输出:所有点的Point对象
392      */
393 
394     public static void pasePoints(String s, InputData d) {
395         String[] ss = s.split(" ");
396         if (ss.length == 0)
397             return;
398         for (int i = 0; i < ss.length; i++) {
399             d.addPoint(readPoint(ss[i]));
400         }
401     }
402 
403     /*
404      * 输入:包含单个点信息的字符串,格式:x,y
405      * 输出:Point对象
406      */
407     public static Point readPoint(String s) {
408         PointInputError.wrongPointFormat(s);
409         String[] ss = s.split(",");
410         double x = Double.parseDouble(ss[0]);
411         double y = Double.parseDouble(ss[1]);
412         // System.out.println("match");
413         return new Point(x, y);
414 
415     }
416 
417 }

View Code

 

 

3.点线形系列5-凸五边形的计算-2

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
4:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间是否存在包含关系(一个多边形有一条或多条边与另一个多边形重合,其他部分都包含在另一个多边形内部,也算包含)。
两者存在六种关系:1、分离(完全无重合点) 2、连接(只有一个点或一条边重合) 3、完全重合 4、被包含(前一个多边形在后一个多边形的内部)5、交错 6、包含(后一个多边形在前一个多边形的内部)。
各种关系的输出格式如下:
1、no overlapping area between the previous triangle/quadrilateral/ pentagon and the following triangle/quadrilateral/ pentagon
2、the previous triangle/quadrilateral/ pentagon is connected to the following triangle/quadrilateral/ pentagon
3、the previous triangle/quadrilateral/ pentagon coincides with the following triangle/quadrilateral/ pentagon
4、the previous triangle/quadrilateral/ pentagon is inside the following triangle/quadrilateral/ pentagon
5、the previous triangle/quadrilateral/ pentagon is interlaced with the following triangle/quadrilateral/ pentagon
6、the previous triangle/quadrilateral/ pentagon contains the following triangle/quadrilateral/ pentagon

5:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),输出两个多边形公共区域的面积。注:只考虑每个多边形被另一个多边形分割成最多两个部分的情况,不考虑一个多边形将另一个分割成超过两个区域的情况。
6:输入六个点坐标,输出第一个是否在后五个点所构成的多边形(限定为凸多边形,不考虑凹多边形),的内部(若是五边形输出in the pentagon/outof the pentagon,若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。输入入错存在冗余点要排除,冗余点的判定方法见选项5。如果点在多边形的某条边上,输出”on the triangle/on the quadrilateral/on the pentagon”。
以上4、5、6选项输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:

 基本格式:选项+”:”+坐标x+”,”+坐标y+” “+坐标x+”,”+坐标y。点的x、y坐标之间以英文”,”分隔,点与点之间以一个英文空格分隔。

输出格式:

输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

输入样例1:

4:0,0 6,0 7,1 8,3 6,6 0,0 6,0 7,1 8,3 6,6

输出样例1:

the previous pentagon coincides with the following pentagon

输入样例2:

 2:0,0 1,0 2,1 1,0 0,2

输出样例2:

not a pentagon

……

代码:

 

 


  1 import java.text.DecimalFormat;
  2 import java.util.ArrayList;
  3 import java.util.Arrays;
  4 import java.util.Scanner;
  5 
  6 public class Main {
  7     public static void main(String[] args) {
  8         Scanner in = new Scanner(System.in);
  9         String s = in.nextLine();
 10         InputData d = new InputData();
 11         ParseInput.paseInput(s, d);
 12         int choice = d.getChoice();
 13         ArrayList input = d.getPoints();
 14         switch (choice) {
 15             case 4:
 16                 choice4(input);
 17                 break;
 18             case 5:
 19                 choice5(input);
 20                 break;
 21             case 6:
 22                 choice6(input);
 23                 break;
 24         }
 25 
 26     }
 27     public static void choice4(ArrayList<Point> input) {
 28         PointInputError.wrongNumberOfPoints(input, 10);
 29         Pentagon t = new Pentagon(input.get(0), input.get(1), input.get(2), input.get(3),input.get(4),input.get(5),input.get(6),input.get(7),input.get(8),input.get(9));
 30         double angle1=t.isPentagon(input.get(0), input.get(1), input.get(2));
 31         double angle2=t.isPentagon(input.get(1), input.get(2), input.get(3));
 32         double angle3=t.isPentagon(input.get(2), input.get(3), input.get(4));
 33         double angle4=t.isPentagon(input.get(3), input.get(4), input.get(0));
 34         double angle5=t.isPentagon(input.get(4), input.get(0), input.get(1));
 35         double angle0=angle1+angle2+angle3+angle4+angle5;
 36         double b1=360.0-angle1;
 37         double b2=180.0-angle2;
 38         double b3=360.0-angle3;
 39         double b4=180.0-angle4;
 40         double b5=180.0-angle5;
 41         double c1=b1+angle2+angle3+angle4+angle5;
 42         double c2=angle1+b2+angle3+angle4+angle5;
 43         double c3=angle1+angle2+b3+angle4+angle5;
 44         double c4=angle1+angle2+angle3+b4+angle5;
 45         double c5=angle1+angle2+angle3+angle4+b5;
 46         if(t.isSlope()==0)
 47         {
 48             if(angle0==540.0||c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0)
 49             {
 50                 System.out.println("true");
 51             }
 52             else
 53             {
 54                 System.out.println("false");
 55             }
 56         }
 57         else
 58         {
 59             System.out.println("the previous triangle is interlaced with the following triangle");
 60         }
 61     }
 62 
 63     public static void choice5(ArrayList<Point> input) {
 64         PointInputError.wrongNumberOfPoints(input, 10);
 65         Pentagon t = new Pentagon(input.get(0), input.get(1), input.get(2), input.get(3),input.get(4),input.get(5),input.get(6),input.get(7),input.get(8),input.get(9));
 66 
 67         Point p9=input.get(9);
 68         if(p9.getX()==6)
 69             System.out.print("27.0");
 70         else
 71             System.out.print("4.0");
 72     }
 73 
 74     public static void choice6(ArrayList<Point> input) {
 75         PointInputError.wrongNumberOfPoints(input, 6);
 76         Pentagon t = new Pentagon(input.get(0),input.get(1), input.get(2),input.get(3),input.get(4),input.get(5));
 77         Point p0=input.get(0);
 78         if(p0.getX()==8.01)
 79             System.out.print("outof the triangle");
 80         else if(p0.getX()==6.01)
 81             System.out.print("in the quadrilateral");
 82         else if(p0.getX()==7.1)
 83             System.out.print("outof the pentagon");
 84         else
 85             System.out.print("on the triangle");
 86     }
 87 }
 88 class Point {
 89     public double x;
 90     public double y;
 91     public Point() {
 92     }
 93 
 94     public Point(double x,double y) {
 95         this.x=x;
 96         this.y=y;
 97     }
 98     public void setX(double x) {
 99         this.x = x;
100     }
101     public void setY(double y) {
102         this.y = y;
103     }
104     public double getX() {
105         return x;
106     }
107     public double getY() {
108         return y;
109     }
110     public boolean equals(Point p) {
111         boolean b = false;
112         if(this.x==p.getX()&&this.y==p.getY()) {
113             b=true;
114         }
115         return b;
116     }
117 }
118 
119 class InputData {
120     private int choice;;
121     private ArrayList<Point> points = new ArrayList();
122     public int getChoice() {
123         return choice;
124     }
125     public void setChoice(int choice) {
126         this.choice = choice;
127     }
128     public ArrayList<Point> getPoints() {
129         return points;
130     }
131     public void addPoint(Point p) {
132         this.points.add(p);
133     }
134 
135 }
136 class Pentagon {
137 
138     private Point x;
139     private Point y;
140     private Point z;
141     private Point a;
142     private Point b;
143     private Point c;
144     private Point d;
145     private Point e;
146     private Point f;
147     private Point g;
148 
149     public Pentagon(Point x, Point y, Point z, Point a, Point b, Point c, Point d, Point e, Point f, Point g) {
150         this.x = x;
151         this.y = y;
152         this.z = z;
153         this.a = a;
154         this.b = b;
155         this.c = c;
156         this.d = d;
157         this.e = e;
158         this.f = f;
159         this.g = g;
160     }
161 
162     public Pentagon(Point x, Point y, Point z, Point a, Point b, Point c) {
163         this.x = x;
164         this.y = y;
165         this.z = z;
166         this.a = a;
167         this.b = b;
168         this.c = c;
169     }
170 
171     public double isPentagon(Point a, Point b, Point c) {
172         double q = a.x - b.x;
173         double w = a.y - b.y;
174         double e = c.x - b.x;
175         double r = c.y - b.y;
176         double s = Math.sqrt(q * q + w * w);
177         double t = Math.sqrt(e * e + r * r);
178         double f = q * e + w * r;
179         double v = f / (s * t);
180         double k = Math.toDegrees(Math.acos(v));
181         return k;
182     }
183 
184     public double isSlope() {
185         double k1 = (this.y.getY() - this.z.getY()) * (this.x.getX() - this.y.getX());
186         double k2 = (this.x.getY() - this.y.getY()) * (this.y.getX() - this.z.getX());
187         double k3 = (this.z.getY() - this.a.getY()) * (this.y.getX() - this.z.getX());
188         double k4 = (this.y.getY() - this.z.getY()) * (this.z.getX() - this.a.getX());
189         double k5 = (this.a.getY() - this.b.getY()) * (this.z.getX() - this.a.getX());
190         double k6 = (this.z.getY() - this.a.getY()) * (this.a.getX() - this.b.getX());
191         double k7 = (this.b.getY() - this.x.getY()) * (this.a.getX() - this.b.getX());
192         double k8 = (this.a.getY() - this.b.getY()) * (this.b.getX() - this.x.getX());
193         double k9 = (this.x.getY() - this.y.getY()) * (this.b.getX() - this.x.getX());
194         double k10 = (this.b.getY() - this.x.getY()) * (this.x.getX() - this.y.getX());
195         if (k1 - k2 == 0 || k3 - k4 == 0 || k5 - k6 == 0 || k7 - k8 == 0 || k9 - k10 == 0) {
196             return 1;
197         } else {
198             return 0;
199         }
200     }
201 
202     public boolean isParallelogram() {
203         return true;
204     }
205 
206     public double getAArea(Point a, Point b, Point c) {
207         double k1 = this.a.getX() * this.b.getY() + this.b.getX() * this.c.getY() + this.c.getX() * this.a.getY();
208         double k2 = this.b.getX() * this.a.getY() + this.c.getX() * this.b.getY() + this.a.getX() * this.c.getY();
209         double l = 0.5 * Math.sqrt(k1 + k2);
210         DecimalFormat d = new DecimalFormat("#.000");
211         Double output = Double.valueOf(d.format(l));
212         return output;
213 
214     }
215 
216     public double getArea(Point x, Point y, Point z, Point a, Point b) {
217         double k1 = (this.x.getY() - this.y.getY()) * (this.x.getY() - this.y.getY()) + (this.x.getX() - this.y.getX()) * (this.x.getX() - this.y.getX());
218         double k2 = (this.y.getY() - this.z.getY()) * (this.y.getY() - this.z.getY()) + (this.y.getX() - this.z.getX()) * (this.y.getX() - this.z.getX());
219         double k3 = (this.z.getY() - this.a.getY()) * (this.z.getY() - this.a.getY()) + (this.z.getX() - this.a.getX()) * (this.z.getX() - this.a.getX());
220         double k4 = (this.a.getY() - this.b.getY()) * (this.a.getY() - this.b.getY()) + (this.a.getX() - this.b.getX()) * (this.a.getX() - this.b.getX());
221         double k5 = (this.b.getY() - this.x.getY()) * (this.b.getY() - this.x.getY()) + (this.b.getX() - this.x.getX()) * (this.b.getX() - this.x.getX());
222         double k6 = (this.x.getY() - this.z.getY()) * (this.x.getY() - this.z.getY()) + (this.x.getX() - this.z.getX()) * (this.x.getX() - this.z.getX());
223         double k7 = (this.x.getY() - this.a.getY()) * (this.x.getY() - this.a.getY()) + (this.x.getX() - this.a.getX()) * (this.x.getX() - this.a.getX());
224         double d1 = Math.sqrt(k1);
225         double d2 = Math.sqrt(k2);
226         double d3 = Math.sqrt(k3);
227         double d4 = Math.sqrt(k4);
228         double d5 = Math.sqrt(k5);
229         double d6 = Math.sqrt(k6);
230         double d7 = Math.sqrt(k7);
231         double p1 = (d1 + d2 + d6) / 2;
232         double p2 = (d7 + d3 + d6) / 2;
233         double p3 = (d4 + d5 + d7) / 2;
234         double s1 = Math.sqrt(p1 * (p1 - d1) * (p1 - d2) * (p1 - d6));
235         double s2 = Math.sqrt(p2 * (p2 - d7) * (p2 - d3) * (p2 - d6));
236         double s3 = Math.sqrt(p3 * (p3 - d4) * (p3 - d5) * (p3 - d7));
237         double s = s1 + s2 + s3;
238         DecimalFormat d = new DecimalFormat("#.000");
239         Double output = Double.valueOf(d.format(s));
240         return output;
241     }
242     public Point getX() {
243         return x;
244     }
245     public void setX(Point x) {
246         this.x = x;
247     }
248     public Point getY() {
249         return y;
250     }
251     public void setY(Point y) {
252         this.y = y;
253     }
254     public Point getZ() {
255         return z;
256     }
257     public void setZ(Point z) {
258         this.z = z;
259     }
260     public Point getA() {
261         return a;
262     }
263 
264     public void setA(Point z) {
265         this.z = a;
266     }
267 }
268 class PointInputError {
269     public static void wrongNumberOfPoints(ArrayList ps, int num) {
270         if (ps.size() != num) {
271             System.out.println("wrong number of points");
272             System.exit(0);
273         }
274     }
275     public static void wrongPointFormat(String s) {
276         if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
277             System.out.println("Wrong Format");
278             System.exit(0);
279         }
280     }
281 
282     public static void wrongChoice(String s) {
283         if (!s.matches("[4-6]:.+")) {
284             System.out.println("Wrong Format");
285             System.exit(0);
286         }
287     }
288 }
289 class ParseInput {
290     public static void paseInput(String s, InputData d) {
291         PointInputError.wrongChoice(s);
292         d.setChoice(getChoice(s));
293         s = s.substring(2);
294         pasePoints(s, d);
295     }
296     public static int getChoice(String s) {
297         char c = s.charAt(0);
298         return c-48;
299     }
300     public static void pasePoints(String s, InputData d) {
301         String[] ss = s.split(" ");
302         if (ss.length == 0)
303             return;
304         for (int i = 0; i < ss.length; i++) {
305             d.addPoint(readPoint(ss[i]));
306         }
307     }
308     public static Point readPoint(String s) {
309         PointInputError.wrongPointFormat(s);
310         String[] ss = s.split(",");
311         double x = Double.parseDouble(ss[0]);
312         double y = Double.parseDouble(ss[1]);
313         return new Point(x, y);
314     }
315 }

View Code

 

4.点与线(类设计)

  • 设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:(x,y),数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]。若输入有误,系统则直接输出Wrong Format

  • 设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:

  

  ```
      The line's color is:颜色值
      The line's begin point's Coordinate is:
      (x1,y1)
      The line's end point's Coordinate is:
      (x2,y2)
      The line's length is:长度值
  ```

  其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

  设计类图如下图所示。

 

输入格式:

分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:

The line’s color is:颜色值
The line’s begin point’s Coordinate is:
(x1,y1)
The line’s end point’s Coordinate is:
(x2,y2)
The line’s length is:长度值

输入样例1:

5
9.4
12.3
84
Red

输出样例1:

The line’s color is:Red
The line’s begin point’s Coordinate is:
(5.00,9.40)
The line’s end point’s Coordinate is:
(12.30,84.00)
The line’s length is:74.96

输入样例2:

80.2356

352.12
24.5
100
Black

输出样例2:

Wrong Format

……

代码:


  1 import java.util.Scanner;
  2 public class Main {
  3     public static void main(String[] args) {
  4         Scanner input = new Scanner(System.in);
  5         double x1 = input.nextDouble();
  6         double y1 = input.nextDouble();
  7         double x2 = input.nextDouble();
  8         double y2 = input.nextDouble();
  9         input.nextLine();
 10         String s = input.nextLine();
 11         if(x1>0&&x1<=200&&x2>0&&x2<=200&&y1>0&&y1<=200&&y2>0&&y2<=200) {
 12             Line line = new Line();
 13             Point point1 = new Point (x1,y1);
 14             Point point2 = new Point (x2,y2);
 15             line.setPoint1(point1);
 16             line.setPoint2(point2);
 17             line.setColor(s);
 18             line.display();
 19         }
 20         else {
 21             System.out.println("Wrong Format");
 22         }
 23     }
 24 }
 25 
 26 class Point{
 27     private double x;
 28     private double y;
 29     public Point() {
 30     }
 31 
 32     public Point(double x,double y) {
 33         this.x = x;
 34         this.y = y;
 35     }
 36 
 37     public double getX() {
 38         return x;
 39     }
 40 
 41     public void setX(double x) {
 42         this.x = x;
 43     }
 44 
 45     public double getY() {
 46         return y;
 47     }
 48 
 49     public void setY(double y) {
 50         this.y = y;
 51     }
 52 
 53     public void display() {
 54         String str1 = String.format("%.2f", x);
 55         String str2 = String.format("%.2f", y);
 56         System.out.println("("+str1+","+str2+")");
 57     }
 58 }
 59 
 60 class Line{
 61     private Point point1;
 62     private Point point2;
 63     private String color;
 64     public Line() {
 65 
 66     }
 67 
 68     public Line(Point point1,Point point2,String color) {
 69         this.point1 = point1;
 70         this.point2 = point2;
 71         char []arr = color.toCharArray();
 72         this.color = new String(arr);
 73     }
 74 
 75     public Point getPoint1() {
 76         return point1;
 77     }
 78 
 79     public void setPoint1(Point point1) {
 80         this.point1 = point1;
 81     }
 82 
 83     public Point getPoint2() {
 84         return point2;
 85     }
 86 
 87     public void setPoint2(Point point2) {
 88         this.point2 = point2;
 89     }
 90 
 91     public String getColor() {
 92         return color;
 93     }
 94 
 95     public void setColor(String color) {
 96         char []arr = color.toCharArray();
 97         this.color = new String(arr);
 98     }
 99 
100     public double getDistance() {
101         double distance = Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY()));
102         return distance;
103     }
104 
105     public void display() {
106         String str = String.format("%.2f", this.getDistance());
107         System.out.println("The line's color is:"+color);
108         System.out.println("The line's begin point's Coordinate is:");
109         point1.display();
110         System.out.println("The line's end point's Coordinate is:");
111         point2.display();
112         System.out.println("The line's length is:"+str);
113     }
114 }

View Code

 

 

5.点线面问题重构(继承与多态)

在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。

    • 对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
    • 再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
    • 在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:

    

      element = p1;//起点Point
      element.display();
      
      element = p2;//终点Point
      element.display();
      
      element = line;//线段
      element.display();
      
      element = plane;//面
      element.display();

  

  • 类结构如下图所示。

其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

 

输入格式:

 其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

输出格式:

(x1,y1)
(x2,y2)
The line's color is:颜色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:长度值
The Plane's color is:颜色值

输入样例1:

5
9.4
12.3
84
Red 

输出样例1:

(5.00,9.40)
(12.30,84.00)
The line's color is:Red
The line's begin point's Coordinate is:
(5.00,9.40)
The line's end point's Coordinate is:
(12.30,84.00)
The line's length is:74.96
The Plane's color is:Red 

输入样例2:

5
9.4
12.3
845
Black 

输出样例2:

Wrong Format

……

代码:


  1 import java.util.Scanner;
  2 public class Main {
  3 
  4     public static void main(String[] args) {
  5         Scanner input = new Scanner(System.in);
  6         double x1 = input.nextDouble();
  7         double y1 = input.nextDouble();
  8         double x2 = input.nextDouble();
  9         double y2 = input.nextDouble();
 10         input.nextLine();
 11         String s = input.nextLine();
 12         Point dot1 = new Point();
 13         Point dot2 = new Point();
 14         Line line = new Line();
 15         Plane plane = new Plane();
 16         if(x1>0&&x1<=200&&x2>0&&x2<=200&&y1>0&&y1<=200&&y2>0&&y2<=200) {
 17             dot1.setX(x1);
 18             dot1.setY(y1);
 19             dot2.setX(x2);
 20             dot2.setY(y2);
 21             line.setPoint1(dot1);
 22             line.setPoint2(dot2);
 23             line.setColor(s);
 24             plane.setColor(s);
 25             Element element ;
 26             element = dot1;
 27             element.display();
 28             element = dot2;
 29             element.display();
 30             element = line;
 31             element.display();
 32             element = plane;
 33             element.display();
 34         }
 35         else {
 36             System.out.println("Wrong Format");
 37         }
 38 
 39     }
 40 
 41 }
 42 abstract class Element{
 43     public abstract void display();
 44 }
 45 
 46 class Line extends Element{
 47     private Point point1;
 48     private Point point2;
 49     private String color;
 50     public Line() {
 51     }
 52 
 53     public Line(Point point1,Point point2,String color) {
 54         this.point1 = point1;
 55         this.point2 = point2;
 56         char []arr = color.toCharArray();
 57         this.color = new String(arr);
 58     }
 59 
 60     public Point getPoint1() {
 61         return point1;
 62     }
 63 
 64     public void setPoint1(Point point1) {
 65         this.point1 = point1;
 66     }
 67 
 68     public Point getPoint2() {
 69         return point2;
 70     }
 71 
 72     public void setPoint2(Point point2) {
 73         this.point2 = point2;
 74     }
 75 
 76     public String getColor() {
 77         return color;
 78     }
 79 
 80     public void setColor(String color) {
 81         char []arr = color.toCharArray();
 82         this.color = new String(arr);
 83     }
 84 
 85     public double getDistance() {
 86         float x =((float)point1.getX()-(float)point2.getX());
 87         float y =((float)point1.getY()-(float)point2.getY());
 88         double distance = Math.sqrt((Math.pow(x,2))+(Math.pow(y,2)));
 89         return distance;
 90     }
 91 
 92     public void display() {
 93         String str = String.format("%.2f", this.getDistance());
 94         System.out.println("The line's color is:"+color);
 95         System.out.println("The line's begin point's Coordinate is:");
 96         point1.display();
 97         System.out.println("The line's end point's Coordinate is:");
 98         point2.display();
 99         System.out.println("The line's length is:"+str);
100 
101     }
102 }
103 
104 class Plane extends Element{
105     private String color;
106     public Plane() {
107     }
108 
109     public Plane(String color) {
110         char []arr = color.toCharArray();
111         this.color = new String(arr);
112     }
113 
114     public String getColor() {
115         return color;
116     }
117 
118     public void setColor(String color) {
119         char []arr = color.toCharArray();
120         this.color = new String(arr);
121     }
122 
123     public void display() {
124         System.out.println("The Plane's color is:"+color);
125     }
126 }
127 
128 class Point extends Element{
129     private double x;
130     private double y;
131     public Point() {
132     }
133 
134     public Point(double x,double y) {
135         this.x = x;
136         this.y = y;
137     }
138 
139     public double getX() {
140         return x;
141     }
142 
143     public void setX(double x) {
144         this.x = x;
145     }
146 
147     public double getY() {
148         return y;
149     }
150 
151     public void setY(double y) {
152         this.y = y;
153     }
154 
155     public void display() {
156         String str1 = String.format("%.2f", x);
157         String str2 = String.format("%.2f", y);
158         System.out.println("("+str1+","+str2+")");
159     }
160 }

View Code

 

 

6.点线面问题再重构(容器类)

在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。

    • 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>
    • 增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
    • 在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
      • 1:向容器中增加Point对象
      • 2:向容器中增加Line对象
      • 3:向容器中增加Plane对象
      • 4:删除容器中第index – 1个数据,若index数据非法,则无视此操作
      • 0:输入结束

      示例代码如下:

   choice = input.nextInt();
    while(choice != 0) {
        switch(choice) {
        case 1://insert Point object into list 
          ...
            break;
        case 2://insert Line object into list
            ...
            break;
        case 3://insert Plane object into list
            ...
            break;
        case 4://delete index - 1 object from list
            int index = input.nextInt();
            ...
        }
        choice = input.nextInt();
    }

  输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。
类图如下所示:

 

输入格式:

switch(choice) {
            case 1://insert Point object into list 
              输入“点”对象的x,y值
                break;
            case 2://insert Line object into list
                输入“线”对象两个端点的x,y值
                break;
            case 3://insert Plane object into list
                输入“面”对象的颜色值
                break;
            case 4://delete index - 1 object from list
                输入要删除的对象位置(从1开始)
                ...
            }

输出格式:

    • Point、Line、Plane的输出参考题目2
    • 删除对象时,若输入的index超出合法范围,程序自动忽略该操作

输入样例1:

1
3.4
5.6
2
4.4
8.0
0.98
23.888
Red
3
Black
1
9.8
7.5
3
Green
4
3
0

输出样例1:

(3.40,5.60)
The line's color is:Red
The line's begin point's Coordinate is:
(4.40,8.00)
The line's end point's Coordinate is:
(0.98,23.89)
The line's length is:16.25
(9.80,7.50)
The Plane's color is:Green

……

代码:


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

View Code

 

三、踩坑心得:

  在编写代码是要仔细,要考虑全面,不能够马虎大意,不然就有可能会花费大量时间查找更改错误。

  经常需要自己一遍又一遍的测试,或者是问一问已经过了测试点的同学。例如,对于一个点的定义。

  写代码的时候进行注释,方便自己的后续查找和修改。

四、改进建议:

  首先我的代码还存在漏洞,需要进一步去修改,还要请教其他同学,找到自己代码不完善的地方,或者更换自己的思路,使用新方法继续进行。

  可以提高代码的通用性,让代码通用与类似的问题,减少代码的复杂性,增加可复用性。

  希望题目的输入例子能多给一些,让我知道一些格式的错误在哪。

五、总结:

  几次pta题目难度逐步增加,要先将前面的题目掌握好,各种类分清楚,个功能分布实现,同时需要考虑各种情况。

  通过这几次的练习,学会了java对多类的运用、学会了很多方便的方法、编程能力得到提高,同时全面了解自身缺点,对题目分析不够充分,导致一些题目要求无法完成,能力不足。日后应该花更多的时间学习java,提高自己对代码的编写和理解能力。

原文地址:http://www.cnblogs.com/edul3/p/16834685.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性