OOP第二次作业分析

前言:  

一、知识点总结:

  本次分析的题目涉及到的知识点是Java类与对象的设计、继承与多态、接口、链表的实现以及Java基础语法的应用。

二、题量及难度分析:

  此次分析的题目集为PTA题目集04-7-3、题目集06-7-2、链表、期中考试题目集。题量和难度适中,但是需要设计的类比较多,需要设计的方法也比较多,写程序需要花费的时间比较长


源码设计与分析:

题目集04—7-3:点线形系列3-三角形的计算

源码设计及解释:

本次源码设计我没有设计类,归根结底还是那时没有养成这种编程的风格,但是应有的方法还是很全面的,根据题目所讲,我们所设计的算法应当还要涉及到数学知识,比如关于三角形类型的判断,射线法的应用,值得一提的是,编程时关于三角形面积的求法我个人认为应该用海伦公式,简单清楚又明了,下面附上源码。

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4     static double x1 = 0;
  5     static double y1 = 0;
  6     static double x2 = 0;
  7     static double y2 = 0;
  8     static double x3 = 0;
  9     static double y3 = 0;
 10     static double x4 = 0;
 11     static double y4 = 0;
 12     static double x5 = 0;
 13     static double y5 = 0;
 14     static boolean flag = false;
 15     static double center1 = 0;
 16     static double center2 = 0;
 17     static double same1 = 0;
 18     static double same2= 0;
 19     static double same3 = 0;
 20     static double same4= 0;
 21     
 22     public static boolean LocalCheckInputIegitimate(String alp) {//庞大的判断合法性的方法
 23         int length = alp.length();
 24         int comma = 0;
 25         int space = 0;
 26         for(int i = 0;i < length;i++)
 27         {
 28             if(alp.charAt(i)==','||alp.charAt(i)==' ')
 29             {
 30                 if((flag==true&&alp.charAt(i)==',')||(flag==false&&alp.charAt(i)==' '))
 31                     return false;
 32                 if(alp.charAt(i)==',')
 33                 {
 34                     comma++;
 35                     flag=true;
 36                     if(i<length-2)
 37                     if((alp.charAt(i+1)==','||alp.charAt(i+1)==' '||alp.charAt(i+1)=='0')&&(alp.charAt(i+2)>='0'&&alp.charAt(i+2)<='9'))
 38                         return false;
 39                 }
 40                 if(alp.charAt(i)==' ')
 41                 {
 42                     space++;    
 43                     flag=false;
 44                     if(i<length-2)
 45                     if((alp.charAt(i+1)==','||alp.charAt(i+1)==' '||alp.charAt(i+1)=='0')&&(alp.charAt(i+2)>='0'&&alp.charAt(i+2)<='9'))
 46                         return false;
 47                 }
 48             }
 49         }
 50         if(comma!=(space+1))
 51             return false;
 52         for(int i = 0;i<length;i++)
 53         {
 54             if((alp.charAt(i+1)==','||alp.charAt(i+1)==' ')&&i!=length-2)
 55             {
 56                 if(i+3<=length-1)
 57                 {
 58                     if(alp.charAt(i+2)=='0'&&alp.charAt(i+3)=='0')
 59                     {
 60                         return false;
 61                     }
 62                 }
 63                 if(alp.charAt(i)<'0'||alp.charAt(i)>'9')
 64                 {
 65                     return false;
 66                 }
 67                 i++;
 68             }
 69             else if((alp.charAt(i)<'0'||alp.charAt(i)>'9')&&(alp.charAt(i+1)<'0'||alp.charAt(i+1)>'9'))
 70             {
 71                     return false;
 72             }
 73             else if(i<length-2)
 74             {
 75                 if((alp.charAt(i+2)!=','&&alp.charAt(i+2)!=' ')&&(alp.charAt(i+1)!=','||alp.charAt(i+1)!=' '))
 76                 {
 77                     if(alp.charAt(i)=='.'&&alp.charAt(i+2)=='.')
 78                     {
 79                         return false;
 80                     }
 81                     if(alp.charAt(i)=='.'&&alp.charAt(i+1)=='.')
 82                     {
 83                         return false;
 84                     }
 85                     if(alp.charAt(i)!='.'&&alp.charAt(i+2)!='.')
 86                     {
 87                         if((alp.charAt(i)>'0'||alp.charAt(i)<'9')&&((alp.charAt(i+1)<'0'||alp.charAt(i+1)>'9')&&alp.charAt(i+1)!='.')&&(alp.charAt(i+2)>'0'&&alp.charAt(i+2)<'9'))
 88                         {
 89                             return false;
 90                         }
 91                         if(((alp.charAt(i)<'0'||alp.charAt(i)>'9')&&alp.charAt(i)!='.')&&((alp.charAt(i+1)>'0'&&alp.charAt(i+1)<'9'))&&((alp.charAt(i+2)<'0'||alp.charAt(i+2)>'9')&&alp.charAt(i+1)!='.'))
 92                         {
 93                             return false;
 94                         }
 95                     }
 96                     else if(alp.charAt(i)=='.'&&alp.charAt(i+2)!='.')
 97                     {
 98                         if((alp.charAt(i+2)<'0'||alp.charAt(i+2)>'9'))
 99                         {
100                             return false;
101                         }
102                     }
103                 }
104             }
105             if(i==length-2)
106             {
107                 break;
108             }
109         }
110         return true;
111     }
112     
113     public static boolean JudgmentRepetition(int choose) {
114         if(choose==1)
115         {
116             if(x1==x2&&y1==y2)
117             {
118                 return false;
119             }
120         }
121         return true;
122     }
123         
124     public static boolean WholeCheckInputIegitimate(String alp,int nums) {
125         int length = alp.length();
126         int count = 0;
127         for(int i = 0;i < length;i++)
128         {
129             if(alp.charAt(i)==',')
130                 count++;
131         }
132         if(count!=nums)
133         {
134             return false;
135         }
136         return true;
137     }
138     
139     public static double CharacterTransformation(String transfer) {
140         double nums=(double) Double.parseDouble(transfer);
141         return nums;
142     }
143     
144     public static void DigitalConversion(double x,boolean flag1) {
145         String str = String.valueOf(x);
146         String[] arr = str.split("\\.");
147         if(arr[1].length()>6)
148         {
149             if(str.charAt(7)=='0'&&str.charAt(8)<'5')
150             {
151                 System.out.print((float)x);
152             }
153             else
154             {
155                 System.out.printf("%.6f",x);
156             }
157         }
158         else
159         {
160             System.out.print(x);
161         }
162         if(flag1==true)
163             System.out.print(" ");
164     }
165     
166     public static void TriangleAngle(double a,double b,double c) {
167         if (Math.abs(c*c-(a*a+b*b))<0.000001||Math.abs(b*b-(a*a+c*c))<0.000001||Math.abs(a*a-(b*b+c*c))<0.000001)
168         {
169             System.out.println("false true false");
170         }
171         else if(a * a > b * b + c * c || b * b > a * a + c * c || c * c > a * a + b * b)
172         {
173             System.out.println("true false false");
174         }
175         else
176         {
177             System.out.println("false false true");
178         }
179     }
180     
181     private static void Calculation1(String alp,String falp) {
182         int length = alp.length();
183         if(!WholeCheckInputIegitimate(alp,3))
184         {
185             System.out.println("wrong number of points");
186             System.exit(0);
187         }
188         String X1 = alp.substring(0,alp.indexOf(','));
189         alp = alp.substring(alp.indexOf(',')+1,length);
190         length = alp.length();
191         String Y1 = alp.substring(0,alp.indexOf(' '));
192         alp = alp.substring(alp.indexOf(' ')+1,length);
193         length = alp.length();
194         String X2 = alp.substring(0,alp.indexOf(','));
195         alp = alp.substring(alp.indexOf(',')+1,length);
196         length = alp.length();
197         String Y2 = alp.substring(0,alp.indexOf(' '));
198         alp = alp.substring(alp.indexOf(' ')+1,length);
199         length = alp.length();
200         String X3 = alp.substring(0,alp.indexOf(','));
201         alp = alp.substring(alp.indexOf(',')+1,length);
202         length = alp.length();
203         String Y3 = alp.substring(0,length);
204         x1 = CharacterTransformation(X1);
205         y1 = CharacterTransformation(Y1);
206         x2 = CharacterTransformation(X2);
207         y2 = CharacterTransformation(Y2);
208         x3 = CharacterTransformation(X3);
209         y3 = CharacterTransformation(Y3);
210         if(!JudgmentRepetition(2))
211         {
212             System.out.println("points coincide");
213             System.exit(0);
214         }
215         double a = Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
216         double b = Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2));
217         double c = Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2));
218         if(!TriangleJudgment(a,b,c))
219         {
220             System.out.println("data error");
221             System.exit(0);
222         }
223         if(falp.charAt(0)=='1')
224         {
225             if((a==b)||(a==c)||(b==c))
226             {
227                 System.out.print("true ");
228             }
229             else
230             {
231                 System.out.print("false ");
232             }
233             if(a==b&&b==c)
234             {
235                 System.out.print("true");
236             }
237             else
238             {
239                 System.out.print("false");
240             }
241         }
242         if(falp.charAt(0)=='2')
243         {
244             double C =(a+b+c);
245             double S =area(a,b,c);
246             double x =((x1+x2+x3)/3.0);
247             DigitalConversion(C,true);
248             DigitalConversion(S,true);
249             DigitalConversion(x,false);
250             System.out.print(",");
251             double y = ((y1+y2+y3)/3.0);
252             DigitalConversion(y,false);
253         }
254         if(falp.charAt(0)=='3')
255         {
256             TriangleAngle(a,b,c);
257         }
258     }
259     
260     public static double max(double a,double b) {
261         if(a>b)
262             return a;
263         return b;
264     }
265     
266     public static double min(double a,double b) {
267         if(a<b)
268             return a;
269         return b;
270     }
271     
272     public static boolean TriangleJudgment(double a,double b,double c) {
273         if((a+b>c && a-b<c) && (a+c>b && a-c<b) && (b+c>a && b-c<a))
274         {
275             return true;
276         }
277         return false;
278     }
279     
280     public static double area(double a,double b,double c) {
281         double C = a+b+c;
282         return Math.sqrt((C/2)*(C/2-a)*(C/2-b)*(C/2-c));
283     }
284     
285     public static int IntersectionJudgment()//交点个数
286     {
287         boolean flag1 = false;
288         boolean flag2 = false;
289         boolean flag3 = false;
290         if((x1-x2)!=0)
291         {
292             if((y3==(y2+(x3-x2)*(y2-y1)/(x2-x1))&&y4==(y2+(x4-x2)*(y2-y1)/(x2-x1)))||(y3==(y2+(x3-x2)*(y2-y1)/(x2-x1))&&y5==(y2+(x5-x2)*(y2-y1)/(x2-x1)))||(y4==(y2+(x4-x2)*(y2-y1)/(x2-x1))&&y5==(y2+(x5-x2)*(y2-y1)/(x2-x1))))
293             {
294                 return -1;
295             }
296             if(y3>(y2+(x3-x2)*(y2-y1)/(x2-x1)))
297                 flag1=true;
298             else
299                 flag1=false;
300             if(y4>(y2+(x4-x2)*(y2-y1)/(x2-x1)))
301                 flag2=true;
302             else
303                 flag2=false;
304             if(y5>(y2+(x5-x2)*(y2-y1)/(x2-x1)))
305                 flag3=true;
306             else
307                 flag3=false;
308         }
309         else
310         {
311             if((x3==x1&&x4==x1)||(x3==x1&&x5==x1)||(x4==x1&&x5==x1))
312             {
313                 return -1;
314             }
315             if(x3==x1||x4==x1||x5==x1)
316             {
317                 return 1;
318             }
319             if(x3>x1)
320                 flag1=true;
321             else
322                 flag1=false;
323             if(x4>x1)
324                 flag2=true;
325             else
326                 flag2=false;
327             if(x5>x1)
328                 flag3=true;
329             else
330                 flag3=false;
331         }
332         if((flag1==true&&flag2==true&&flag3==false)||(flag1==false&&flag2==false&&flag3==true))
333         {
334             center1=x5;center2=y5;same1=x3;same2=y3;same3=x4;same4=y4;
335             return 2;
336         }
337         else if((flag1==false&&flag2==true&&flag3==false)||(flag1==true&&flag2==false&&flag3==true))
338         {
339             center1=x4;center2=y4;same1=x3;same2=y3;same3=x5;same4=y5;
340             return 2;
341         }
342         else if((flag1==false&&flag2==true&&flag3==true)||(flag1==true&&flag2==false&&flag3==false))
343         {
344             center1=x3;center2=y3;same1=x4;same2=y4;same3=x5;same4=y5;
345             return 2;
346         }
347         else if(y3==(y2+(x3-x2)*(y2-y1)/(x2-x1))||y4==(y2+(x4-x2)*(y2-y1)/(x2-x1))||y5==(y2+(x5-x2)*(y2-y1)/(x2-x1)))
348         {
349             return 1;
350         }
351             return 0;
352     }
353     
354     public static void Calculation2(String alp) {
355         int length = alp.length();
356         if(!WholeCheckInputIegitimate(alp,5))
357         {
358             System.out.println("wrong number of points");
359             System.exit(0);
360         }
361         String X1 = alp.substring(0,alp.indexOf(','));
362         alp = alp.substring(alp.indexOf(',')+1,length);
363         length = alp.length();
364         String Y1 = alp.substring(0,alp.indexOf(' '));
365         alp = alp.substring(alp.indexOf(' ')+1,length);
366         length = alp.length();
367         String X2 = alp.substring(0,alp.indexOf(','));
368         alp = alp.substring(alp.indexOf(',')+1,length);
369         length = alp.length();
370         String Y2 = alp.substring(0,alp.indexOf(' '));
371         alp = alp.substring(alp.indexOf(' ')+1,length);
372         length = alp.length();
373         String X3 = alp.substring(0,alp.indexOf(','));
374         alp = alp.substring(alp.indexOf(',')+1,length);
375         length = alp.length();
376         String Y3 = alp.substring(0,alp.indexOf(' '));
377         alp = alp.substring(alp.indexOf(' ')+1,length);
378         length = alp.length();
379         String X4 = alp.substring(0,alp.indexOf(','));
380         alp = alp.substring(alp.indexOf(',')+1,length);
381         length = alp.length();
382         String Y4 = alp.substring(0,alp.indexOf(' '));
383         alp = alp.substring(alp.indexOf(' ')+1,length);
384         length = alp.length();
385         String X5 = alp.substring(0,alp.indexOf(','));
386         alp = alp.substring(alp.indexOf(',')+1,length);
387         length = alp.length();
388         String Y5 = alp.substring(0,length);
389         x1 = CharacterTransformation(X1);
390         y1 = CharacterTransformation(Y1);
391         x2 = CharacterTransformation(X2);
392         y2 = CharacterTransformation(Y2);
393         x3 = CharacterTransformation(X3);
394         y3 = CharacterTransformation(Y3);
395         x4 = CharacterTransformation(X4);
396         y4 = CharacterTransformation(Y4);
397         x5 = CharacterTransformation(X5);
398         y5 = CharacterTransformation(Y5);
399         double a = Math.sqrt(Math.pow(x3-x4,2)+Math.pow(y3-y4,2));
400         double b = Math.sqrt(Math.pow(x3-x5,2)+Math.pow(y3-y5,2));
401         double c = Math.sqrt(Math.pow(x4-x5,2)+Math.pow(y4-y5,2));
402         if(!JudgmentRepetition(1))
403         {
404             System.out.println("points coincide");
405             System.exit(0);
406         }
407         if(!TriangleJudgment(a,b,c))
408         {
409             System.out.println("data error");
410             System.exit(0);
411         }
412         if(IntersectionJudgment()==2)
413         {
414             double n1=0,n2=0,n3=0;
415             double m1=0,m2=0,m3=0,m4=0;
416             double S1=0,S2=0;
417             if((x2-x1)!=0&&(same1-center1)!=0&&(same3-center1)!=0)
418             {
419                 double a1=(y2-y1)/(x2-x1);
420                 double b1=-1;
421                 double c1=y1-((y2-y1)*x1/(x2-x1));
422                 double a2=(same2-center2)/(same1-center1);
423                 double b2=-1;
424                 double c2=center2-((same2-center2)*center1/(same1-center1));
425                 double a3=(same4-center2)/(same3-center1);
426                 double b3=-1;
427                 double c3=same4-((same4-center2)*same3/(same3-center1));
428                  m2 = (c1 * a2 - c2 * a1) / (a1 * b2 - a2 * b1);
429                  m1 = (c2 * b1 - c1 * b2) / (a1 * b2 - a2 * b1);
430                  m4 = (c1 * a3 - c3 * a1) / (a1 * b3 - a3 * b1);
431                  m3 = (c3 * b1 - c1 * b3) / (a1 * b3 - a3 * b1);
432                 n1=Math.sqrt(Math.pow(center1-m3, 2)+Math.pow(center2-m4, 2));
433                 n2=Math.sqrt(Math.pow(center1-m1, 2)+Math.pow(center2-m2, 2));
434                 n3=Math.sqrt(Math.pow(m3-m1, 2)+Math.pow(m4-m2, 2));
435             }
436             else if((x2-x1)==0&&(same1-center1)!=0&&(same3-center1)!=0)
437             {
438                 double yy1=center2+((center2-same2)*(x1-center1)/(center1-same1));
439                 double yy2=center2+((center2-same4)*(x1-center1)/(center1-same3));
440                 n1=Math.sqrt(Math.pow(center1-x1, 2)+Math.pow(center2-yy1, 2));
441                 n2=Math.sqrt(Math.pow(center1-x1, 2)+Math.pow(center2-yy2, 2));
442                 n3=yy1-yy2;
443             }
444             else if((x2-x1)!=0&&(same1-center1)==0&&(same3-center1)!=0)
445             {
446                 double a1=(y2-y1)/(x2-x1);
447                 double b1=-1;
448                 double c1=y1-((y2-y1)*x1/(x2-x1));
449                 double a3=(same4-center2)/(same3-center1);
450                 double b3=-1;
451                 double c3=same4-((same4-center2)*same3/(same3-center1));
452                 m4 = (c1 * a3 - c3 * a1) / (a1 * b3 - a3 * b1);
453                 m3 = (c3 * b1 - c1 * b3) / (a1 * b3 - a3 * b1);
454                 n1=Math.sqrt(Math.pow(center1-m3, 2)+Math.pow(center2-m4, 2));
455                 n3=y1+(y2-y1)*(same1-x1)/(x2-x1)-center2;
456                 n2=Math.sqrt(Math.pow(m3-same1, 2)+Math.pow(m4-(n3+center2), 2));
457             }
458             else if((x2-x1)!=0&&(same1-center1)!=0&&(same3-center1)==0)
459             {
460                 double a1=(y2-y1)/(x2-x1);
461                 double b1=-1;
462                 double c1=y1-((y2-y1)*x1/(x2-x1));
463                 double a2=(same2-center2)/(same1-center1);
464                 double b2=-1;
465                 double c2=center2-((same2-center2)*center1/(same1-center1));
466                 m2 = (c1 * a2 - c2 * a1) / (a1 * b2 - a2 * b1);
467                 m1 = (c2 * b1 - c1 * b2) / (a1 * b2 - a2 * b1);
468                 n2=Math.sqrt(Math.pow(center1-m1, 2)+Math.pow(center2-m2, 2));
469                 n3=y1+(y2-y1)*(same3-x1)/(x2-x1)-center2;
470                 n1=Math.sqrt(Math.pow(m1-same3, 2)+Math.pow(m2-(n3+center2), 2));
471             }
472             S1=area(n1,n2,n3);
473             S2=area(a,b,c)-S1;
474             System.out.print("2 ");
475             if(S1>S2)
476             {
477                 DigitalConversion(S2,true);
478                 DigitalConversion(S1,false);
479             }
480             else
481             {
482                 DigitalConversion(S1,true);
483                 DigitalConversion(S2,false);
484             }
485         }
486         if(IntersectionJudgment()==1)
487         {
488             System.out.println("1");
489         }
490         if(IntersectionJudgment()==0)
491         {
492             System.out.println("0");
493         }
494         if(IntersectionJudgment()==-1)
495         {
496             System.out.println("The point is on the edge of the triangle");
497         }
498     }
499     
500     public static void Calculation3(String alp) {
501         int length = alp.length();
502         if(!WholeCheckInputIegitimate(alp,4))
503         {
504             System.out.println("wrong number of points");
505             System.exit(0);
506         }
507         String X1 = alp.substring(0,alp.indexOf(','));
508         alp = alp.substring(alp.indexOf(',')+1,length);
509         length = alp.length();
510         String Y1 = alp.substring(0,alp.indexOf(' '));
511         alp = alp.substring(alp.indexOf(' ')+1,length);
512         length = alp.length();
513         String X2 = alp.substring(0,alp.indexOf(','));
514         alp = alp.substring(alp.indexOf(',')+1,length);
515         length = alp.length();
516         String Y2 = alp.substring(0,alp.indexOf(' '));
517         alp = alp.substring(alp.indexOf(' ')+1,length);
518         length = alp.length();
519         String X3 = alp.substring(0,alp.indexOf(','));
520         alp = alp.substring(alp.indexOf(',')+1,length);
521         length = alp.length();
522         String Y3 = alp.substring(0,alp.indexOf(' '));
523         alp = alp.substring(alp.indexOf(' ')+1,length);
524         length = alp.length();
525         String X4 = alp.substring(0,alp.indexOf(','));
526         alp = alp.substring(alp.indexOf(',')+1,length);
527         length = alp.length();
528         String Y4 = alp.substring(0,length);
529         x1 = CharacterTransformation(X1);
530         y1 = CharacterTransformation(Y1);
531         x2 = CharacterTransformation(X2);
532         y2 = CharacterTransformation(Y2);
533         x3 = CharacterTransformation(X3);
534         y3 = CharacterTransformation(Y3);
535         x4 = CharacterTransformation(X4);
536         y4 = CharacterTransformation(Y4);
537         double a = Math.sqrt(Math.pow(x3-x4,2)+Math.pow(y3-y4,2));
538         double b = Math.sqrt(Math.pow(x3-x5,2)+Math.pow(y3-y5,2));
539         double c = Math.sqrt(Math.pow(x4-x5,2)+Math.pow(y4-y5,2));
540         if(!TriangleJudgment(a,b,c))
541         {
542             System.out.println("data error");
543             System.exit(0);
544         }
545         double aa = (x1-x2)*(y1-y3)-(y1-y2)*(x1-x3);
546         double bb = (x1-x3)*(y1-y4)-(y1-y3)*(x1-x4);
547         double cc = (x1-x4)*(y1-y2)-(y1-y4)*(x1-x2);
548         if((aa < 0 && bb < 0 && cc < 0)||(aa > 0 && bb > 0 && cc > 0))  
549         {
550             System.out.println("in the triangle");
551         }
552         else if(aa==0||bb==0||cc==0)
553         {
554             if(aa==0)
555             {
556                 if(x1>=min(x2,x3)&&x1<=max(x2,x3)&&y1>=min(x2,x3)&&y1<=max(x2,x3))
557                 {
558                     System.out.println("on the triangle");
559                 }
560                 else
561                 {
562                     System.out.println("outof the triangle");
563                 }
564             }
565             else if(bb==0)
566             {
567                 if(x1>=min(x4,x3)&&x1<=max(x4,x3)&&y1>=min(x4,x3)&&y1<=max(x4,x3))
568                 {
569                     System.out.println("on the triangle");
570                 }
571                 else
572                 {
573                     System.out.println("outof the triangle");
574                 }
575             }
576             else if(cc==0)
577             {
578                 if(x1>=min(x4,x2)&&x1<=max(x4,x2)&&y1>=min(x4,x2)&&y1<=max(x4,x2))
579                 {
580                     System.out.println("on the triangle");
581                 }
582                 else
583                 {
584                     System.out.println("outof the triangle");
585                 }
586             }
587         }
588         else
589         {
590             System.out.println("outof the triangle");
591         }
592     }
593     
594     public static void main(String[] args) {
595         Scanner input = new Scanner(System.in);
596         String falp = input.nextLine();
597         if((falp.charAt(0)>'5'||falp.charAt(0)<'1')||(falp.charAt(1)!=':'))
598         {
599             System.out.println("Wrong Format");
600             System.exit(0);
601         }
602         int length = falp.length();
603         String alp = falp.substring(2,length);
604         if(!LocalCheckInputIegitimate(alp))
605         {
606             System.out.println("Wrong Format");
607             System.exit(0);
608         }
609         if(falp.charAt(0)=='1'||falp.charAt(0)=='2'||falp.charAt(0)=='3') 
610         {
611             Calculation1(alp,falp);
612         }
613         if(falp.charAt(0)=='4')
614         {
615             Calculation2(alp);
616         }
617         if(falp.charAt(0)=='5')
618         {
619             Calculation3(alp);
620         }
621     }
622 }

采坑心得:

(1)关于直角三角形的判断不能直接用a²+b²=c²的这么一种形式,应为a,b,c均为double型,Java在计算时存在精度的丢失,即为所说的误差,正确的表示方法应当为

这么一种形式

 

(2)判断点的数量可以采取数逗号的方法,逗号有几个那就是几个点,前提是要保证输入的数据是合法的

(3)判断数据的合法性应当考虑各种情况。

改进建议:

(1)从整体看,程序的整个过程都是在main里进行的,这样就显得程序很繁琐且没有条理,main中的耦合性太强,应该设计点类和线类,相关的方法放在点类和线类中。

(2)对数据合法性的判断过于繁琐,这里采用的是最基本的for循环逐一判断,这样的效率非常的低,而且需要写的代码很多,使得代码繁琐,应当使用正则表达式来判断数据合法性的问题

(3)数据的提取过于繁琐,应当采用数组的形式,结合正则表达式的提取将相应的数据存放置数组中。


 

题目集04—7-2:7-2 点线形系列4-凸四边形的计算

源码设计及解释:

  这次同样是图形类设计的题目,但采取的是不同的设计思路和方法,首先设计两个类,即点类和线类

点类:                                                                               线类:        

          

 

 

对于数据合法性的判断采用的是正则表达式:

 

 

 

 

 数据的提取使用数组和正则提取的结合:

 

 

源码:

 

 

  1 import java.util.HashSet;
  2 import java.util.Scanner;
  3 import java.util.regex.Matcher;
  4 import java.util.regex.Pattern;
  5 
  6 public class Main {
  7     static Point A;
  8     static Point B;
  9     static Point C;
 10     static Point D;
 11     static Point u1;
 12     static Point u2;
 13     static HashSet<Point> sites = new HashSet<Point>();
 14     
 15     public static void main(String[] args) {
 16         Scanner in = new Scanner(System.in);
 17         String alp = in.nextLine();
 18         if(!CheckInputIegitimate(alp)) {
 19             System.out.println("Wrong Format");
 20             System.exit(0);
 21         }
 22         double[] array = new double[20];
 23         Pattern pattern = Pattern.compile("(-?\\d*)\\.?\\d+");
 24         Matcher matcher = pattern.matcher(alp.substring(2, alp.length()));
 25         int count = 0;
 26         while(matcher.find()) {
 27             array[count] = Double.parseDouble(matcher.group(0));
 28             count++;
 29         }
 30         if(alp.charAt(0)=='1'||alp.charAt(0)=='2'||alp.charAt(0)=='3') {
 31             A = new Point(array[0],array[1]);
 32             B = new Point(array[2],array[3]);
 33             C = new Point(array[4],array[5]);
 34             D = new Point(array[6],array[7]);
 35             if(alp.charAt(0)=='1') {
 36                 operate1(alp.substring(2, alp.length()));
 37             }
 38             if(alp.charAt(0)=='2') {
 39                 operate2(alp.substring(2, alp.length()));
 40             }
 41             if(alp.charAt(0)=='3') {
 42                 operate3(alp.substring(2, alp.length()));
 43             }
 44         }
 45         if(alp.charAt(0)=='4') {
 46             A = new Point(array[4],array[5]);
 47             B = new Point(array[6],array[7]);
 48             C = new Point(array[8],array[9]);
 49             D = new Point(array[10],array[11]);
 50             u1 = new Point(array[0],array[1]);
 51             u2 = new Point(array[2],array[3]);
 52             operate4(alp.substring(2, alp.length()));
 53         }
 54         if(alp.charAt(0)=='5') {
 55             u1 = new Point(array[0],array[1]);
 56             A = new Point(array[2],array[3]);
 57             B = new Point(array[4],array[5]);
 58             C = new Point(array[6],array[7]);
 59             D = new Point(array[8],array[9]);
 60             operate5(alp.substring(2, alp.length()));
 61         }
 62     }
 63 
 64     public static void operate1(String alp) {//选项1操作
 65         dataDetection(alp,4);
 66         if(JudgeQuadrilateral()) {
 67             System.out.println("false false");
 68             System.exit(0);
 69         }
 70         Line l1 = new Line(A,B);Line l2 = new Line(A,C);Line l3 = new Line(A,D);Line l4 = new Line(B,C);Line l5 = new Line(B,D);Line l6 = new Line(C,D);
 71         if(l1.newLineSegment(C)||l1.newLineSegment(D)||l2.newLineSegment(B)||l2.newLineSegment(D)||l3.newLineSegment(B)||l3.newLineSegment(C)||l4.newLineSegment(A)||l4.newLineSegment(D)||l5.newLineSegment(A)||l5.newLineSegment(C)||l6.newLineSegment(A)||l6.newLineSegment(B)) {            
 72             System.out.println("false false");
 73         }else {//这里一定能构成四边形
 74             System.out.print("true ");
 75             if(A.getSlope(B)==C.getSlope(D)&&A.CalculateDistance(B)==C.CalculateDistance(D)) {
 76                 System.out.print("true");
 77             }else {
 78                 System.out.print("false");
 79             }
 80         }
 81     }
 82     
 83     public static void operate2(String alp) {//选项2操作
 84         boolean flag1 = false;
 85         boolean flag2 = false;
 86         if(!NumberCheckInputIegitimate(alp,4)) {
 87             System.out.println("wrong number of points");
 88             System.exit(0);
 89         }
 90         if(JudgeQuadrilateral()) {
 91             System.out.println("not a quadrilateral");
 92             System.exit(0);
 93         }
 94         Line l1 = new Line(A,B);Line l2 = new Line(A,C);Line l3 = new Line(A,D);Line l4 = new Line(B,C);Line l5 = new Line(B,D);Line l6 = new Line(C,D);
 95         if(l1.newLineSegment(C)||l1.newLineSegment(D)||l2.newLineSegment(B)||l2.newLineSegment(D)||l3.newLineSegment(B)||l3.newLineSegment(C)||l4.newLineSegment(A)||l4.newLineSegment(D)||l5.newLineSegment(A)||l5.newLineSegment(C)||l6.newLineSegment(A)||l6.newLineSegment(B)) {
 96             System.out.println("not a quadrilateral");
 97             System.exit(0);
 98         }    
 99         if(A.getSlope(B)==C.getSlope(D)&&A.CalculateDistance(B)==C.CalculateDistance(D)) {//平行四边形
100             if(A.CalculateDistance(B)==A.CalculateDistance(D)) {//邻边相等为菱形
101                 flag1 = true;
102                 System.out.print("true ");
103             }else {
104                 System.out.print("false ");
105             }
106             if(Math.abs(Math.pow(B.CalculateDistance(D),2)-Math.pow(A.CalculateDistance(B),2)-Math.pow(A.CalculateDistance(D),2))<1e-6) {//有一直角为矩形
107                 flag2 = true;
108                 System.out.print("true ");
109             }else {
110                 System.out.print("false ");
111             }
112             if(flag1==true&&flag2==true) {
113                 System.out.print("true");
114             }else {
115                 System.out.print("false");
116             }
117         }else {
118             System.out.print("false false false");
119         }
120     }
121     
122     public static void operate3(String alp) {//选项3操作
123         dataDetection(alp,4);
124         if((A.getSlope(B)==A.getSlope(C))||(B.getSlope(C)==B.getSlope(D))||(A.getSlope(C)==A.getSlope(D))||(A.getSlope(B)==B.getSlope(D))) {
125             System.out.println("not a quadrilateral");
126             System.exit(0);
127         }    //前提条件,一定能构成四边形
128         if(pointInsideTriangle(A,B,C,D)||pointInsideTriangle(B,A,C,D)||pointInsideTriangle(C,A,B,D)||pointInsideTriangle(D,A,B,C)) {
129             System.out.print("false ");
130         }else {
131             System.out.print("true ");
132         }
133         DigitalConversion(A.CalculateDistance(B)+B.CalculateDistance(C)+C.CalculateDistance(D)+D.CalculateDistance(A),true);
134         Line l1 = new Line(A,C);
135         Line l2 = new Line(B,D);
136         DigitalConversion(0.5*A.CalculateDistance(C)*B.CalculateDistance(D)*(l1.GetIncludedAngle(l2)),false);
137     }
138     
139     public static void operate4(String alp) {
140         if(!NumberCheckInputIegitimate(alp,6)) {
141             System.out.println("wrong number of points");
142             System.exit(0);
143         }
144         if(u1.CoincidenceJudgment(u2)) {
145             System.out.println("points coincide");
146             System.exit(0);
147         }
148         Line l = new Line(u1,u2);
149         Line l1 = new Line(A,B);Line l2 = new Line(B,C);Line l3 = new Line(C,D); Line l4 = new Line(D,A);Line l5 = new Line(A,C); Line l6 = new Line(B,D);
150         if(l1.newLineSegment(C)||l1.newLineSegment(D)||l2.newLineSegment(A)||l2.newLineSegment(D)||l3.newLineSegment(B)||l3.newLineSegment(A)||l4.newLineSegment(B)||l4.newLineSegment(C)||l5.newLineSegment(B)||l5.newLineSegment(D)||l6.newLineSegment(A)||l6.newLineSegment(C)) {
151             if(l5.newLineSegment(D)) {//D在CA上,D是重复点
152                 Caculate(l,A,B,C);
153             }
154             else if(l5.newLineSegment(B)) {
155                 Caculate(l,A,D,C);
156             }
157             else if(l6.newLineSegment(C)) {
158                 Caculate(l,A,B,D);
159             }
160             else if(l6.newLineSegment(A)) {
161                 if(l.getSlope()==0)
162                 Caculate(l,C,B,D);
163             }else if(A.CoincidenceJudgment(C)) {
164                 Caculate(l,A,B,D);
165             }else if(A.CoincidenceJudgment(B)) {
166                 Caculate(l,A,C,D);
167             }else if(A.CoincidenceJudgment(D)) {
168                 Caculate(l,C,B,A);
169             }else if(B.CoincidenceJudgment(C)) {
170                 Caculate(l,A,B,D);
171             }else if(B.CoincidenceJudgment(D)) {
172                 Caculate(l,C,B,A);
173             }else if(C.CoincidenceJudgment(D)) {
174                 Caculate(l,A,B,D);
175             }else {
176                 System.out.println("not a quadrilateral or triangle");
177             }
178         }else {//多边形
179             int intersection = 0;
180            if(JudgeQuadrilateral()) {
181             System.out.println("not a quadrilateral or triangle");
182             System.exit(0);
183             }
184             if(l.StraightLine(A)&&l.StraightLine(B)||l.StraightLine(A)&&l.StraightLine(D)||l.StraightLine(B)&&l.StraightLine(C)||l.StraightLine(C)&&l.StraightLine(D)) {
185                 System.out.println("The line is coincide with one of the lines");
186                 System.exit(0);
187             }
188             intersection = GetIntersection(l,l1,l2,l3,l4);
189             if(intersection==2) {
190                 if(pointInsideTriangle(A,B,C,D)||pointInsideTriangle(B,A,C,D)||pointInsideTriangle(C,A,B,D)||pointInsideTriangle(D,A,B,C)){
191                     ConcaveQuadrilateral(l,l1,l2,l3,l4);
192                 }else {
193                     ConvexQuadrilateral(l,l1,l2,l3,l4);
194                 }
195             }else {
196                 System.out.println(intersection);
197             }
198         }    
199     }
200     
201     public static void operate5(String alp) {
202         int choose = 0;
203         if(JudgeQuadrilateral()) {
204             System.out.println("not a quadrilateral or triangle");
205             System.exit(0);
206         }
207         Line l1 = new Line(A,B);Line l2 = new Line(B,C);Line l3 = new Line(C,D); Line l4 = new Line(D,A);Line l5 = new Line(A,C); Line l6 = new Line(B,D);
208         if(l1.newLineSegment(C)||l1.newLineSegment(D)||l2.newLineSegment(A)||l2.newLineSegment(D)||l3.newLineSegment(B)||l3.newLineSegment(A)||l4.newLineSegment(B)||l4.newLineSegment(C)||l5.newLineSegment(B)||l5.newLineSegment(D)||l6.newLineSegment(A)||l6.newLineSegment(C)) {
209             if(l5.newLineSegment(D)) {//D在CA上,D是重复点
210                 choose = PointInTriangle(u1,A,B,C);
211             }
212             else if(l5.newLineSegment(B)) {
213                 choose = PointInTriangle(u1,A,D,C);
214             }
215             else if(l6.newLineSegment(C)) {
216                 choose = PointInTriangle(u1,A,B,D);
217             }
218             else if(l6.newLineSegment(A)) {
219                 choose = PointInTriangle(u1,C,B,D);
220             }else {
221                 System.out.println("not a quadrilateral or triangle");
222             }
223             if(choose==0) {
224                 System.out.println("outof the triangle");
225             }else if(choose==1) {
226                 System.out.println("in the triangle");
227             }else {
228                 System.out.println("on the triangle");
229             }
230         }else {//多边形
231             int flag1 = 0;int flag2=0;
232             flag1=PointInTriangle(u1,A,C,D);
233             flag2=PointInTriangle(u1,A,B,C);
234             if(flag1==1||flag2==1||(l5.newLineSegment(u1)&&!u1.CoincidenceJudgment(A)&&!u1.CoincidenceJudgment(C))) {
235                 choose = 1;
236             }else if(flag1==2||flag2==2) {
237                 choose = 2;
238             }else {
239                 choose = 0;
240             }
241             if(choose==0) {
242                 System.out.println("outof the quadrilateral");
243             }else if(choose==1) {
244                 System.out.println("in the quadrilateral");
245             }else {
246                 System.out.println("on the quadrilateral");
247             }
248         }
249     }
250     
251     public static int PointInTriangle(Point p1,Point p2,Point p3,Point p4) {
252         double flag1 = (p1.getX()-p2.getX())*(p1.getY()-p3.getY())-(p1.getY()-p2.getY())*(p1.getX()-p3.getX());
253         double flag2 = (p1.getX()-p3.getX())*(p1.getY()-p4.getY())-(p1.getY()-p3.getY())*(p1.getX()-p4.getX());
254         double flag3 = (p1.getX()-p4.getX())*(p1.getY()-p2.getY())-(p1.getY()-p4.getY())*(p1.getX()-p2.getX());
255         if((flag1<0&&flag2<0&&flag3<0)||(flag1>0&&flag2>0&&flag3>0)) {
256             return 1;
257         }else if(flag1==0||flag2==0||flag3==0) {
258             if(flag1==0) {
259                 if(new Line(p2,p3).newLineSegment(p1)){
260                     return 2;
261                 }else {
262                     return 0;
263                 }
264             }else if(flag2==0) {
265                 if(new Line(p3,p4).newLineSegment(p1)) {
266                     return 2;
267                 }else {
268                     return 0;
269                 }
270             }else if(flag3==0) {
271                 if(new Line(p2,p4).newLineSegment(p1)) {
272                     return 2;
273                 }else {
274                     return 0;
275                 }
276             }
277         }
278         return 0;
279     }
280     
281     public static void ConcaveQuadrilateral(Line l,Line l1,Line l2,Line l3,Line l4) {
282         double S = 0.5*A.CalculateDistance(C)*B.CalculateDistance(D)*new Line(A,C).GetIncludedAngle(new Line(B,D));
283         double s1 = 0;
284         double s2 = 0;
285         Point n1 = l.GetPoint(l1);Point n2 = l.GetPoint(l2);Point n3 = l.GetPoint(l3);Point n4 = l.GetPoint(l4);
286         if(l.StraightLine(A)&&l.StraightLine(C)) {
287             s1 = area(A.CalculateDistance(B),A.CalculateDistance(C),B.CalculateDistance(C));
288             s2 = S-s1;
289             input(s1,s2);
290             System.exit(0);
291         }
292         if(l.StraightLine(B)&&l.StraightLine(D)) {
293             s1 = area(A.CalculateDistance(B),A.CalculateDistance(D),B.CalculateDistance(D));
294             s2 = S-s1;
295             input(s1,s2);
296             System.exit(0);
297         }
298         if(pointInsideTriangle(D,A,B,C)&&l.StraightLine(C)&&l1.LineSegment(n1)) {
299             s1 = area(B.CalculateDistance(C),C.CalculateDistance(n1),B.CalculateDistance(n1));
300             s2 = S-s1;
301             input(s1,s2);
302             System.exit(0);
303         }
304         if(n1!=null&&n2!=null) {
305             if(l1.LineSegment(n1)&&l2.LineSegment(n2)) {
306                 s1 = area(n1.CalculateDistance(n2),n1.CalculateDistance(B),n2.CalculateDistance(B));
307                 s2 = S - s1;
308             }
309         }
310         if(n1!=null&&n3!=null) {
311             if(l1.LineSegment(n1)&&l3.LineSegment(n3)) {
312                 if(pointInsideTriangle(A,B,C,D)||pointInsideTriangle(C,A,B,D)) {
313                     s1 = area(C.CalculateDistance(n1),B.CalculateDistance(n1),C.CalculateDistance(B))+area(C.CalculateDistance(n1),C.CalculateDistance(n3),n1.CalculateDistance(n3));
314                     s2 = S-s1;
315                 }else {
316                     s1 = area(C.CalculateDistance(B),C.CalculateDistance(n3),B.CalculateDistance(n3))+area(B.CalculateDistance(n1),B.CalculateDistance(n3),n1.CalculateDistance(n3));
317                     s2 = S- s1;
318                 }
319             }
320         }
321         if(n1!=null&&n4!=null) {
322             if(l1.LineSegment(n1)&&l4.LineSegment(n4)) {
323                 s1 = area(A.CalculateDistance(n1),A.CalculateDistance(n4),n1.CalculateDistance(n4));
324                 s2 = S - s1;
325             }
326         }
327         if(n2!=null&&n3!=null) {
328             if(l2.LineSegment(n2)&&l3.LineSegment(n3)) {
329                 s1 = area(n2.CalculateDistance(n3),n2.CalculateDistance(C),n3.CalculateDistance(C));
330                 s2 = S - s1;
331             }
332         }
333         if(n2!=null&&n4!=null) {
334             if(l2.LineSegment(n2)&&l4.LineSegment(n4)) {
335                 if(pointInsideTriangle(A,B,C,D)||pointInsideTriangle(C,A,B,D)) {
336                     s1 = area(A.CalculateDistance(n2),A.CalculateDistance(n4),n2.CalculateDistance(n4))+area(A.CalculateDistance(n2),n2.CalculateDistance(B),A.CalculateDistance(B));
337                     s2 = S-s1;
338                 }else {
339                     s1 = area(B.CalculateDistance(n2),B.CalculateDistance(n4),n2.CalculateDistance(n4))+area(B.CalculateDistance(n4),A.CalculateDistance(n4),A.CalculateDistance(B));
340                     s2 = S - s1;
341                 }
342             }
343         }
344         if(n3!=null&&n4!=null) {
345             if(l3.LineSegment(n3)&&l4.LineSegment(n4)) {
346                 s1 = area(D.CalculateDistance(n4),D.CalculateDistance(n3),n4.CalculateDistance(n3));
347                 s2 = S - s1;
348             }
349         }
350         input(s1,s2);
351     }
352     
353     public static void ConvexQuadrilateral(Line l,Line l1,Line l2,Line l3,Line l4) {//凸四边形
354         double S = 0.5*A.CalculateDistance(C)*B.CalculateDistance(D)*new Line(A,C).GetIncludedAngle(new Line(B,D));
355         double s1 = 0;
356         double s2 = 0;
357         Point n1 = l.GetPoint(l1);Point n2 = l.GetPoint(l2);Point n3 = l.GetPoint(l3);Point n4 = l.GetPoint(l4);
358         if(l.StraightLine(B)&&l.StraightLine(D)) {
359             s1 = area(A.CalculateDistance(B),A.CalculateDistance(D),B.CalculateDistance(D));
360             s2 = S-s1;
361             input(s1,s2);
362             System.exit(0);
363         }
364         if(l.StraightLine(A)&&l.StraightLine(C)) {
365             s1 = area(A.CalculateDistance(B),A.CalculateDistance(C),B.CalculateDistance(C));
366             s2 = S-s1;
367             input(s1,s2);
368             System.exit(0);
369         }
370         if(n1!=null&&n2!=null) {
371             if(l1.LineSegment(n1)&&l2.LineSegment(n2)) {
372                 s1 = area(n1.CalculateDistance(n2),n1.CalculateDistance(B),n2.CalculateDistance(B));
373                 s2 = S - s1;
374             }
375         }
376         if(n1!=null&&n3!=null) {
377             if(l1.LineSegment(n1)&&l3.LineSegment(n3)) {
378                 s1 = area(A.CalculateDistance(n1),A.CalculateDistance(n3),n1.CalculateDistance(n3))+area(A.CalculateDistance(n3),D.CalculateDistance(n3),A.CalculateDistance(D));
379                 s2 = S-s1;
380             }
381         }
382         if(n1!=null&&n4!=null) {
383             if(l1.LineSegment(n1)&&l4.LineSegment(n4)) {
384                 s1 = area(A.CalculateDistance(n1),A.CalculateDistance(n4),n1.CalculateDistance(n4));
385                 s2 = S - s1;
386             }
387         }
388         if(n2!=null&&n3!=null) {
389             if(l2.LineSegment(n2)&&l3.LineSegment(n3)) {
390                 s1 = area(n2.CalculateDistance(n3),n2.CalculateDistance(C),n3.CalculateDistance(C));
391                 s2 = S - s1;
392             }
393         }
394         if(n2!=null&&n4!=null) {
395             if(l2.LineSegment(n2)&&l4.LineSegment(n4)) {
396                 s1 = area(A.CalculateDistance(n4),A.CalculateDistance(n2),n4.CalculateDistance(n2))+area(A.CalculateDistance(n2),n2.CalculateDistance(B),A.CalculateDistance(B));
397                 s2 = S - s1;
398             }
399         }
400         if(n3!=null&&n4!=null) {
401             if(l3.LineSegment(n3)&&l4.LineSegment(n4)) {
402                 s1 = area(D.CalculateDistance(n4),D.CalculateDistance(n3),n4.CalculateDistance(n3));
403                 s2 = S - s1;
404             }
405         }
406         input(s1,s2);
407     }
408     public static void input(double s1,double s2) {
409         System.out.print("2 ");
410         if(s2>s1) {
411             DigitalConversion(s1,true);
412             DigitalConversion(s2,false);
413         }else {
414             DigitalConversion(s2,true);
415             DigitalConversion(s1,false);
416         }
417     }
418     public static int GetIntersection(Line l,Line l1,Line l2,Line l3,Line l4) {
419         boolean flag = false;
420         Point m1;Point m2;Point m3;Point m4;
421         m1 = l.GetPoint(l1);m2 = l.GetPoint(l2);m3 = l.GetPoint(l3);m4 = l.GetPoint(l4);
422         if(m1!=null) {
423             if(l1.LineSegment(m1)) {
424                 sites.add(m1);
425             }
426         }
427         flag = false;
428         if(m2!=null) {
429             if(l2.LineSegment(m2)) {
430                 for(Point element : sites) {
431                     if(element.getX()==m2.getX()&&element.getY()==m2.getY()) {
432                         flag = true;
433                     }
434                 }
435                 if(flag==false) {
436                     sites.add(m2);
437                 }
438             }
439         }
440         flag = false;
441         if(m3!=null) {
442             if(l3.LineSegment(m3)) {
443                 for(Point element : sites) {
444                     if(element.getX()==m3.getX()&&element.getY()==m3.getY()) {
445                         flag = true;
446                     }
447                 }
448                 if(flag==false) {
449                     sites.add(m3);
450                 }
451             }
452         }
453         flag = false;
454         if(m4!=null) {
455             if(l4.LineSegment(m4)) {
456                 for(Point element : sites) {
457                     if(element.getX()==m4.getX()&&element.getY()==m4.getY()) {
458                         flag = true;
459                     }
460                 }
461                 if(flag==false) {
462                     sites.add(m4);
463                 }
464             }
465         }
466         return sites.size();
467     }
468     
469     
470     public static void Caculate(Line l,Point a,Point b,Point c) {
471         if(l.StraightLine(a)&&l.StraightLine(b)||l.StraightLine(a)&&l.StraightLine(c)||l.StraightLine(b)&&l.StraightLine(c)) {
472             System.out.println("The line is coincide with one of the lines");
473             System.exit(0);
474         }
475         int intersection = 0;
476         double S = area(a.CalculateDistance(b),b.CalculateDistance(c),c.CalculateDistance(a));
477         double s1 = 0;
478         double s2 = 0;
479         Point pointcenter;Point same1;Point same2;
480         if((l.StraightLocation(a)==1&&l.StraightLocation(b)==1&&l.StraightLocation(c)==1)||(l.StraightLocation(a)==-1&&l.StraightLocation(b)==-1&&l.StraightLocation(c)==-1)) {
481             intersection = 0;
482         }
483         if((l.StraightLocation(a)==0&&l.StraightLocation(b)==1&&l.StraightLocation(c)==1)||(l.StraightLocation(a)==0&&l.StraightLocation(b)==-1&&l.StraightLocation(c)==-1)||
484           (l.StraightLocation(a)==1&&l.StraightLocation(b)==0&&l.StraightLocation(c)==1)||(l.StraightLocation(a)==-1&&l.StraightLocation(b)==0&&l.StraightLocation(c)==-1)||
485           (l.StraightLocation(a)==1&&l.StraightLocation(b)==1&&l.StraightLocation(c)==0)||(l.StraightLocation(a)==-1&&l.StraightLocation(b)==-1&&l.StraightLocation(c)==0)) {
486             intersection = 1;
487         }
488         if((l.StraightLocation(a)==1&&l.StraightLocation(b)==-1&&l.StraightLocation(c)==-1)||(l.StraightLocation(a)==-1&&l.StraightLocation(b)==1&&l.StraightLocation(c)==1)||
489           (l.StraightLocation(a)==1&&l.StraightLocation(b)==0&&l.StraightLocation(c)==-1)||(l.StraightLocation(a)==-1&&l.StraightLocation(b)==0&&l.StraightLocation(c)==0)){//a基准点
490             intersection = 2;
491             pointcenter = a;
492             Line n1 = new Line(a,b);Line n2 = new Line(a,c);
493             same1 = l.GetPoint(n1);
494             same2 = l.GetPoint(n2);
495             s1 = area(pointcenter.CalculateDistance(same1),pointcenter.CalculateDistance(same2),same1.CalculateDistance(same2));
496             s2 = S-s1;
497         }
498         if((l.StraightLocation(a)==-1&&l.StraightLocation(b)==1&&l.StraightLocation(c)==-1)||(l.StraightLocation(a)==1&&l.StraightLocation(b)==-1&&l.StraightLocation(c)==1)||
499           (l.StraightLocation(a)==1&&l.StraightLocation(b)==-1&&l.StraightLocation(c)==0)||(l.StraightLocation(a)==-1&&l.StraightLocation(b)==1&&l.StraightLocation(c)==0)){//b基准点
500             intersection = 2;
501             pointcenter = b;
502             Line n1 = new Line(b,a);Line n2 = new Line(b,c);
503             same1 = l.GetPoint(n1);
504             same2 = l.GetPoint(n2);
505             s1 = area(pointcenter.CalculateDistance(same1),pointcenter.CalculateDistance(same2),same1.CalculateDistance(same2));
506             s2 = S-s1;
507         }
508         if((l.StraightLocation(a)==1&&l.StraightLocation(b)==1&&l.StraightLocation(c)==-1)||(l.StraightLocation(a)==-1&&l.StraightLocation(b)==-1&&l.StraightLocation(c)==1)||
509           (l.StraightLocation(a)==0&&l.StraightLocation(b)==-1&&l.StraightLocation(c)==1)||(l.StraightLocation(a)==0&&l.StraightLocation(b)==1&&l.StraightLocation(c)==-1)){//c基准点
510             intersection = 2;
511             pointcenter = c;
512             Line n1 = new Line(c,a);Line n2 = new Line(b,c);
513             same1 = l.GetPoint(n1);
514             same2 = l.GetPoint(n2);
515             s1 = area(pointcenter.CalculateDistance(same1),pointcenter.CalculateDistance(same2),same1.CalculateDistance(same2));
516             s2 = S-s1;
517         }
518         if(intersection==2) {
519             input(s1,s2);
520         }else {
521             System.out.print(intersection);
522         }
523     }
524     
525     public static double area(double a,double b,double c) {
526         double C = a+b+c;
527         return Math.sqrt((C/2)*(C/2-a)*(C/2-b)*(C/2-c));
528     }
529     
530     public static boolean JudgeQuadrilateral() {//合法四边形
531         Line l1 = new Line(A,C);
532         if(l1.StraightLocation(B)==l1.StraightLocation(D)) {
533             if(!(pointInsideTriangle(A,B,C,D)||pointInsideTriangle(C,A,B,D)||pointInsideTriangle(D,A,B,C))) {
534                 return true;
535             }
536         }
537         return false;
538     }
539     
540     public static boolean pointInsideTriangle(Point p1,Point p2,Point p3,Point p4) {//判断一个点是否在其他三个点围成的三角形内
541         double flag1 = (p1.getX()-p2.getX())*(p1.getY()-p3.getY())-(p1.getY()-p2.getY())*(p1.getX()-p3.getX());
542         double flag2 = (p1.getX()-p3.getX())*(p1.getY()-p4.getY())-(p1.getY()-p3.getY())*(p1.getX()-p4.getX());
543         double flag3 = (p1.getX()-p4.getX())*(p1.getY()-p2.getY())-(p1.getY()-p4.getY())*(p1.getX()-p2.getX());
544         if((flag1<0&&flag2<0&&flag3<0)||(flag1>0&&flag2>0&&flag3>0)) {
545             return true;//在里面
546         }
547         return false;//不在里面
548     }
549     
550     public static void DigitalConversion(double x,boolean flag1) {//输出
551         String str = String.format("%.3f",x);
552         double nums = Double.parseDouble(str);
553         System.out.print(nums);
554         if(flag1==true)
555             System.out.print(" ");
556     }
557     
558     public static void dataDetection(String alp,int nums) {
559         if(!NumberCheckInputIegitimate(alp,nums)) {
560             System.out.println("wrong number of points");
561             System.exit(0);
562         }
563         if(A.CoincidenceJudgment(B)||A.CoincidenceJudgment(C)||A.CoincidenceJudgment(D)||B.CoincidenceJudgment(C)||B.CoincidenceJudgment(D)||C.CoincidenceJudgment(D)) {
564             System.out.println("points coincide");
565             System.exit(0);
566         }
567     }
568     
569     public static boolean NumberCheckInputIegitimate(String alp,int nums) {//数量是否正确
570         int length = alp.length();
571         int count = 0;
572         for(int i = 0;i < length;i++)
573         {
574             if(alp.charAt(i)==',')
575                 count++;
576         }
577         if(count!=nums)
578         {
579             return false;
580         }
581         return true;
582     }
583     
584     public static boolean CheckInputIegitimate(String alp) {//判断合法性
585         String regStr = "([1-5]:((\\+?|-?)(0(\\.0+)?|[1-9]\\d*|[1-9]\\d*\\.\\d+|0\\.\\d*[1-9]\\d*),(\\+?|-?)(0(\\.0+)?|[1-9]\\d*|[1-9]\\d*\\.\\d+|0\\.\\d*[1-9]\\d*) )+(\\+?|-?)(0(\\.0+)?|[1-9]\\d*|[1-9]\\d*\\.\\d+|0.\\d*[1-9]\\d*),(\\+?|-?)(0|[1-9]\\d*|[1-9]\\d*\\.\\d+|0\\.\\d*[1-9]\\d*))( ?)";
586         if(Pattern.matches(regStr, alp)) {
587             return true;
588         }
589         return false;
590     }
591 }
592 
593 class Point{
594     private double x = 0;
595     private double y = 0;
596     
597     public Point(double x,double y) {
598         this.x = x;
599         this.y = y;
600     }
601     
602     public double CalculateDistance(Point p) {//两点距离
603         double distance = Math.sqrt(Math.pow(this.x-p.x, 2)+(Math.pow(this.y-p.y, 2)));
604         return distance;
605     }
606     
607     public boolean CoincidenceJudgment(Point p) {//判断点重合
608         if(this.x==p.getX()&&this.y==p.getY()) {
609             return true;
610         }
611         return false;
612     }
613     
614     public double getX() {
615         return this.x;
616     }
617     
618     public double getY() {
619         return this.y;
620     }
621     
622     public double getSlope(Point p) {
623         if(this.getX()-p.getX()!=0) {
624             return (this.getY()-p.getY())/(this.getX()-p.getX());
625         }
626         return -99999999;
627     }
628 }
629 
630 class Line{
631     private Point p1;
632     private Point p2;
633     
634     public Line(Point p1,Point p2) {
635         this.p1 = p1;
636         this.p2 = p2;
637     }
638     
639     public double getSlope() {
640         if(p1.getX()-p2.getX()!=0) {
641             return (p1.getY()-p2.getY())/(p1.getX()-p2.getX());
642         }
643         return -9999999;//斜率无穷大
644     }
645     
646     public boolean JudgeParallel(Line line) {//判断是否平行
647         if(this.getSlope()==line.getSlope()) {
648             return true;
649         }
650         return false;
651     }
652     
653     public double GetIncludedAngle(Line l) {//获得两直线夹角度数(正弦值)
654         double vectorx1 = this.p2.getX()-this.p1.getX();
655         double vectory1 = this.p2.getY()-this.p1.getY();
656         double vectorx2 = l.p2.getX()-l.p1.getX();
657         double vectory2 = l.p2.getY()-l.p1.getY();
658         return Math.sqrt(1-Math.pow(Math.abs(vectorx1*vectorx2+vectory1*vectory2)/(Math.sqrt(Math.pow(vectorx1, 2)+Math.pow(vectory1, 2))*Math.sqrt(Math.pow(vectorx2, 2)+Math.pow(vectory2, 2))),2));
659     }
660     
661     public Point GetPoint(Line l) {//返回两直线交点
662         if(this.JudgeParallel(l)) {
663             return null;
664         }
665         double A1 = p1.getY()-p2.getY();
666         double B1 = p2.getX()-p1.getX();
667         double C1 = (p1.getX()-p2.getX())*p1.getY()-(p1.getY()-p2.getY())*p1.getX();
668         double A2 = l.p1.getY()-l.p2.getY();
669         double B2 = l.p2.getX()-l.p1.getX();
670         double C2 = (l.p1.getX()-l.p2.getX())*l.p1.getY()-(l.p1.getY()-l.p2.getY())*l.p1.getX();
671         double x = (-C1*B2+B1*C2)/(A1*B2-A2*B1);
672         double y = (-A1*C2+A2*C1)/(A1*B2-A2*B1);
673         return new Point(x,y);
674     }
675     
676     public boolean LineSegment(Point p) {
677         if(p.getX()>=Math.min(this.p1.getX(), this.p2.getX())&&p.getX()<=Math.max(this.p1.getX(), this.p2.getX())&&
678            p.getY()>=Math.min(this.p1.getY(),this.p2.getY())&&p.getY()<=Math.max(this.p1.getY(),this.p2.getY())){
679             return true;
680         }
681         return false;
682     }
683     
684     public boolean StraightLine(Point p) {//判断点是否在直线上
685         double A1 = p1.getY()-p2.getY();
686         double B1 = p2.getX()-p1.getX();
687         double C1 = (p1.getX()-p2.getX())*p1.getY()-(p1.getY()-p2.getY())*p1.getX();
688         if(A1*p.getX()+B1*p.getY()+C1==0) {
689             return true;
690         }
691         return false;
692     }
693     
694     public int StraightLocation(Point p) {//判断直线位置,上面返回1,中间0,下面-1
695         double A1 = p1.getY()-p2.getY();
696         double B1 = p2.getX()-p1.getX();
697         double C1 = (p1.getX()-p2.getX())*p1.getY()-(p1.getY()-p2.getY())*p1.getX();
698         if(A1*p.getX()+B1*p.getY()+C1>0) {
699             return 1;
700         }else if(A1*p.getX()+B1*p.getY()+C1==0){
701             return 0;
702         }
703         return -1;
704     }
705     
706     public boolean newLineSegment(Point p) {//判断点是否在该线段上
707         if(LineSegment(p)&&StraightLine(p)) {
708             return true;
709         }
710         return false;
711     }
712 }

 

 

 

采坑心得:

(1) 题目所给的四边形的顶点是按照顺序输入的,所以在做任何计算和判断之前,先判断凸四边形是否合法,判断凸四边形合法的方式是连接AC,若BD在AC的异侧即为该四边形合法

(2)关于直线分割多边形的问题,目前我认为最好的方法还是穷举法,四边形有4条边,那么有两个交点的情况就是C(4,2),将每种情况列举出来,拆分成几个三角形求面积

(3)限定保留小数的解决方法:先转换成字符串再转换成double型输出即可,这样做的好处就是不用考虑存在小数点后面全是0的问题

 

 (4)此类问题的正则表达式为:

(5)注意判断顺序

改进建议:

(1)main类的耦合性依然太强,应当可以设计三角形类,四边形类,格式输出类等类来降低main类的耦合性

(2)类中的方法不够完善,main类的很多方法应该是点或线类的方法,在设计上存在问题


链表

源码设计解释:

 接口:

 

 

节点类:

 

节点类包括数据域和指针域

链表类:

 

其中head为头指针,tail为尾指针

源码:

  1 package Linkedlist;
  2 
  3 interface LinearListInterface<E> {//泛型接口
  4     public boolean isEmpty();
  5     public int size();
  6     public E get(int index);
  7     public void remove(int index);
  8     public void add(int index, E theElement);
  9     public void add(E  element);
 10     public void printList();    
 11 }
 12 //LinkedList class
 13 class LList<E> implements LinearListInterface<E>{
 14     private Node<E> head;
 15     private Node<E> tail;
 16     private Node<E> curr;
 17     private int size;
 18     @Override
 19     public boolean isEmpty() {//判断当前节点是否为空节点
 20         // TODO Auto-generated method stub
 21         if(this.curr==null) {
 22             return true;
 23         }
 24         return false;
 25     }
 26     @Override
 27     public int size() {
 28         // TODO Auto-generated method stub
 29         return this.size;
 30     }
 31     @Override
 32     public E get(int index) {//获得第Index节点的值
 33         // TODO Auto-generated method stub
 34         int count = 0;
 35         if(index>=0&&index<=size) {
 36             this.curr = head;
 37             while(count<index) {
 38                 this.curr = this.curr.next;
 39                 count++;
 40             }
 41             return this.curr.o;//返回当前节点
 42         }
 43         return null;
 44     }
 45     @Override
 46     public void remove(int index) {
 47         // TODO Auto-generated method stub
 48         int count = 0;
 49         if(index>=0&&index<=size) {
 50             if(index==0) {//删除头部
 51                 this.head = this.head.next;
 52                 this.size--;
 53             }else {
 54                 this.curr = this.head;
 55                 while(count<index-1) {
 56                     this.curr = this.curr.next;
 57                     count++;
 58                 }
 59                 this.curr.next = this.curr.next.next;
 60             }
 61             this.size--;
 62         }
 63         
 64     }
 65     @Override
 66     public void add(int index, E theElement) {//任意位置添加节点
 67         // TODO Auto-generated method stub
 68         int count = 0;
 69         if(index>=0&&index<=this.size) {
 70             if(index==0) {
 71                 this.head = new Node<E>(theElement,this.head);//如果添加在头部,theElement成为新头部
 72             }else {
 73                 this.curr = head;
 74                 while(count<index-1) {//找到要添加节点的位置
 75                     this.curr = this.curr.next;
 76                     count++;
 77                 }
 78                 this.curr.next = new Node<E>(theElement,this.curr.next);
 79             }
 80             this.size++;
 81         }
 82         
 83     }
 84     @Override
 85     public void add(E element) {//尾部添加节点
 86         // TODO Auto-generated method stub
 87         Node<E> node =new Node<E>(element,null);//新节点    
 88         this.curr = this.tail;//
 89         this.tail = node;
 90         if(isEmpty()) {
 91             head = node;
 92         }else{
 93             curr.next = node;
 94         }
 95         this.size++;
 96     }
 97     @Override
 98     public void printList() {
 99         // TODO Auto-generated method stub
100         for(int i = 0;i<this.size;i++) {
101             System.out.print(get(i).toString()+" ");
102         }
103     }
104 
105 }
106 //Node
107 
108 class Node<E>{//节点
109         public E o;//数据域
110         public Node<E> next;//指针域
111         
112         public Node(E o,Node<E> next) {//构造器
113             this.o = o;
114             this.next = next;
115         }
116 }
117 
118 
119 public class Lianbiao {
120 
121     public static void main(String[] args) {
122         // TODO Auto-generated method stub
123         LList<Integer> list = new LList<Integer>();
124         for(int i = 0;i<10;i++) {
125             list.add(i);
126         }
127         list.printList();//输出链表
128         list.remove(12);//删除第四个节点
129         System.out.println("\n删除第四个节点后的链表:");
130         list.printList();//输出链表
131         list.add(4, 4);//第四个节点添加4
132         System.out.println("\n增加第四个节点后的链表:");
133         list.printList();//输出链表
134     }
135 }

采坑心得:

    链表的结构很简单,就是一个个节点连接在一起,形成一个完整的链条,每个节点包含2部分,数据域,和一个指向下一个节点引用的指针next,具体的更详细的大家可以参考相关资料解释,再说说删除操作,同样需要找到数据所在的位置,然后进行删除,不同的是,删除的时候,链表只需要改变一下前后节点的引用关系即可,就完成了节点的删除,而没有像数组那样触发一次全部数据的移动,从这个描述来看,链表在进行删除的时候,速度比数组快。

改进建议:

不能够自定义实现链表,应当做适当修改。


期中考试分析

点与线(类设计)

源码设计解释:

 

 源码:

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4 
  5     public static void main(String[] args) {
  6         // TODO Auto-generated method stub
  7         Scanner in = new Scanner(System.in);
  8         double px = in.nextDouble();
  9         double py = in.nextDouble();
 10         double qx = in.nextDouble();
 11         double qy = in.nextDouble();
 12         String color = in.next();
 13         if(px<=0||px>200||py<=0||py>200||qx<=0||qx>200||qy<=0||qy>200) {
 14             System.out.println("Wrong Format");
 15             System.exit(0);
 16         }
 17         Point p = new Point(px,py);
 18         Point q = new Point(qx,qy);
 19         Line l = new Line(p,q,color);
 20         System.out.println("The line's color is:"+l.getColor());
 21         l.display();
 22         System.out.printf("The line's length is:%.2f",l.getDistance());
 23     }
 24 
 25 }
 26 
 27 class Point{
 28     private double x = 0;
 29     private double y = 0;
 30     
 31     public Point(double x,double y) {
 32         this.x = x;
 33         this.y = y;
 34     }
 35     
 36     public void display() {
 37         System.out.println("The line's begin point's Coordinate is:");
 38         System.out.printf("(%.2f,%.2f)",this.x,this.y);
 39     }
 40     
 41     public double getX() {
 42         return this.x;
 43     }
 44     
 45     public double getY() {
 46         return this.y;
 47     }
 48     
 49     public void setX(double x) {
 50         this.x = x;
 51     }
 52     
 53     public void setY(double y) {
 54         this.y = y;
 55     }
 56 }
 57 
 58 class Line{
 59     private Point point1;
 60     private Point point2;
 61     private String color;
 62     
 63     public Line(Point p1,Point p2,String color) {
 64         this.point1 = p1;
 65         this.point2 = p2;
 66         this.color = color;
 67     }
 68     
 69     public Point getPoint1() {
 70         return point1;
 71     }
 72     
 73     public Point getPoint2() {
 74         return point2;
 75     }
 76     
 77     public void setPoint1(Point point1) {
 78         this.point1 = point1;
 79     }
 80     
 81     public void setPoint2(Point point2) {
 82         this.point2 = point2;
 83     }
 84     
 85     public String getColor() {
 86         return color;
 87     }
 88     
 89     public void setColor(String color) {
 90         this.color = color;
 91     }
 92     
 93     public double getDistance() {
 94         return Math.sqrt(Math.pow(this.point1.getX()-this.point2.getX(), 2)+Math.pow(this.point1.getY()-this.point2.getY(), 2));
 95     }
 96     
 97     public void display() {
 98         System.out.println("The line's begin point's Coordinate is:");
 99         System.out.printf("(%.2f,%.2f)\n",this.point1.getX(),this.point1.getY());
100         System.out.println("The line's end point's Coordinate is:");
101         System.out.printf("(%.2f,%.2f)\n",this.point2.getX(),this.point2.getY());
102     }
103 }

说明:

最基础的类的设计,通过创建对象然后调用类中的方法实现对点和线信息的输出

下面对点线面问题重构,新增一个抽象类Element,点类和线类成为该类的子类,再创建一个面类,也是Element的子类

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4 
  5     public static void main(String[] args) {
  6         // TODO Auto-generated method stub
  7         Scanner in = new Scanner(System.in);
  8         double px = in.nextDouble();
  9         double py = in.nextDouble();
 10         double qx = in.nextDouble();
 11         double qy = in.nextDouble();
 12         String color = in.next();
 13         if(px<=0||px>200||py<=0||py>200||qx<=0||qx>200||qy<=0||qy>200) {
 14             System.out.println("Wrong Format");
 15             System.exit(0);
 16         }
 17         Point p = new Point(px,py);
 18         Point q = new Point(qx,qy);
 19         Line l = new Line(p,q,color);
 20         Plane plane = new Plane(color);
 21         Element element = p;
 22         p.display();
 23         element = q;
 24         q.display();
 25         element = l;
 26         l.display();
 27         element = plane;
 28         plane.display();
 29     }
 30 
 31 }
 32 
 33 class Point extends Element{
 34     private double x = 0;
 35     private double y = 0;
 36     
 37     public Point(double x,double y) {
 38         this.x = x;
 39         this.y = y;
 40     }
 41     @Override
 42     public void display() {
 43         System.out.printf("(%.2f,%.2f)\n",this.x,this.y);
 44     }
 45     
 46     public double getX() {
 47         return this.x;
 48     }
 49     
 50     public double getY() {
 51         return this.y;
 52     }
 53     
 54     public void setX(double x) {
 55         this.x = x;
 56     }
 57     
 58     public void setY(double y) {
 59         this.y = y;
 60     }
 61 }
 62 
 63 class Line extends Element{
 64     private Point point1;
 65     private Point point2;
 66     private String color;
 67     
 68     public Line(Point p1,Point p2,String color) {
 69         this.point1 = p1;
 70         this.point2 = p2;
 71         this.color = color;
 72     }
 73     
 74     public Point getPoint1() {
 75         return point1;
 76     }
 77     
 78     public Point getPoint2() {
 79         return point2;
 80     }
 81     
 82     public void setPoint1(Point point1) {
 83         this.point1 = point1;
 84     }
 85     
 86     public void setPoint2(Point point2) {
 87         this.point2 = point2;
 88     }
 89     
 90     public String getColor() {
 91         return color;
 92     }
 93     
 94     public void setColor(String color) {
 95         this.color = color;
 96     }
 97     
 98     public double getDistance() {
 99         return Math.sqrt(Math.pow(this.point1.getX()-this.point2.getX(), 2)+Math.pow(this.point1.getY()-this.point2.getY(), 2));
100     }
101     @Override
102     public void display() {
103         System.out.println("The line's color is:"+this.getColor());
104         System.out.println("The line's begin point's Coordinate is:");
105         System.out.printf("(%.2f,%.2f)\n",this.point1.getX(),this.point1.getY());
106         System.out.println("The line's end point's Coordinate is:");
107         System.out.printf("(%.2f,%.2f)\n",this.point2.getX(),this.point2.getY());
108         System.out.printf("The line's length is:%.2f",this.getDistance());
109         System.out.printf("\n");
110     }
111 }
112 
113 abstract class Element{
114     public void display() {
115         
116     }
117 }
118 
119 class Plane extends Element{
120     private String color;
121     
122     public Plane(String color) {
123         this.color = color;
124     }
125     
126     public String getColor() {
127         return color;
128     }
129     
130     public void setColor() {
131         this.color = color;
132     }
133     
134     public void display() {
135         System.out.println("The Plane's color is:"+this.getColor());
136     }
137 }

 说明:

在主方法内,定义两个Point对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,这就实现了多态特性。

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

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

说明:

 ArrayList相当于一个对象容器,里面存入的是Element类型的,这就能够完成增、删、遍历操作。

采坑心得:

 (1)给有相同性质的类设计一个父类是一个很好的编程思维,再进行相关操作的时候使用向上造型实现多态特性,这不仅能提高代码的复用性还能使代码更加的简洁,编程效率更加高效

(2)使用泛型也是一种很好的编程思维,能够实现增、删、遍历操作。对任意的一个对象都能够修改。


总结:

学会了什么:

1.通过对本阶段综合性总结,我对Java的基础语法(选择、函数、字符字符串相关操作、循环、方法、数组)有了更深层次的理解

2.对类与对象的设计原则有了更深层次的理解,了解了继承原则和多态特性,并能在实际编程加以应用

3.了解了什么是接口、并能熟练应用

还需要改进加强的地方:

1.考虑问题不够全面

2.主函数不能做到完全解耦

3.算法不够精确不够简单

 

 

 

 

 

 

 

 

 

 

 

 

 

.

 

 

122

posted @ 2022-05-01 19:17  -浮光-  阅读(43)  评论(0)    收藏  举报