四边形,五边形以及期中考试总结性Blog
Java大作业第二次阶段性总结
前言
继完成点线性系列三角形相关计算后,第四次和第五次大作业关于四边形和五边形的相关计算代码复杂度和难度提升较大,期中考试的三题对继承,多态,容器以及基础知识进行了考察。
设计与分析
第一题:
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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 import java.util.Scanner; 2 import java.math.BigDecimal; 3 public class Main { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 String s=input.nextLine(); 7 double[] x=null; 8 double[] y=null; 9 Judgeinput a=new Judgeinput(s); 10 a.judge(); 11 int choice=Points.getChoice(s); 12 s=s.substring(2); 13 Points b=new Points(s,choice); 14 b.choose(); 15 x=b.getX(); 16 y=b.getY(); 17 switch(choice) { 18 case 1: 19 Fun1 fun1=new Fun1(x,y); 20 fun1.judgerepeat(); 21 break; 22 case 2: 23 Fun2 fun2=new Fun2(x,y); 24 fun2.judgerepeat(); 25 break; 26 case 3: 27 Fun3 fun3=new Fun3(x,y); 28 fun3.judgerepeat(); 29 break; 30 case 4: 31 Fun4 fun4=new Fun4(x,y); 32 // fun4.operate(); 33 fun4.judgerepeat(); 34 fun4.pd(); 35 break; 36 case 5: 37 Fun5 fun5=new Fun5(x,y); 38 fun5.judgerepeat(); 39 } 40 } 41 } 42 class Judgeinput { 43 private String s; 44 private int choice; 45 private String coord[]; 46 Judgeinput(String s){ 47 this.s=s; 48 } 49 public void judge() { 50 choice = s.charAt(0)-48; 51 judgeChoice(); 52 s=s.substring(2); 53 coord = s.split(" "); 54 judgeFormat(); 55 judgeQuantity(); 56 } 57 public void judgeChoice() { 58 if(!s.matches("[1-5]:.+")) { 59 System.out.println("Wrong Format"); 60 System.exit(0); 61 } 62 } 63 public void judgeFormat() { 64 for(int i=0;i<coord.length;i++) { 65 if(!coord[i].matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) { 66 System.out.println("Wrong Format"); 67 System.exit(0); 68 } 69 } 70 } 71 public void judgeQuantity() { 72 int length=coord.length; 73 switch(choice) { 74 case 1:if(length!=4) { 75 System.out.println("wrong number of points"); 76 System.exit(0); 77 } 78 break; 79 case 2:if(length!=4) { 80 System.out.println("wrong number of points"); 81 System.exit(0); 82 } 83 break; 84 case 3:if(length!=4) { 85 System.out.println("wrong number of points"); 86 System.exit(0); 87 } 88 break; 89 case 4:if(length!=6) { 90 System.out.println("wrong number of points"); 91 System.exit(0); 92 } 93 break; 94 case 5:if(length!=5) { 95 System.out.println("wrong number of points"); 96 System.exit(0); 97 } 98 } 99 } 100 101 } 102 class Points { 103 private String s; 104 private int choice; 105 private String coord[]; 106 public double[] x=null; 107 public double[] y=null; 108 Points(String s,int choice){ 109 this.s=s; 110 this.choice=choice; 111 } 112 public static int getChoice(String s) { 113 int choice = s.charAt(0)-48; 114 return choice; 115 } 116 117 public void choose() { 118 coord=s.split(",| "); 119 if(choice==1||choice==2||choice==3) { 120 coordinate123(); 121 } 122 else if(choice==4) { 123 coordinate4(); 124 } 125 else if(choice==5) { 126 coordinate5(); 127 } 128 } 129 public void coordinate123() { 130 x = new double[4]; 131 y = new double[4]; 132 for(int i=0;i<4;i++) { 133 x[i]=Double.parseDouble(coord[2*i]); 134 y[i]=Double.parseDouble(coord[2*i+1]); 135 } 136 } 137 public void coordinate4() { 138 x = new double[6]; 139 y = new double[6]; 140 for(int i=0;i<6;i++) { 141 x[i]=Double.parseDouble(coord[2*i]); 142 y[i]=Double.parseDouble(coord[2*i+1]); 143 } 144 } 145 public void coordinate5() { 146 x = new double[5]; 147 y = new double[5]; 148 for(int i=0;i<5;i++) { 149 x[i]=Double.parseDouble(coord[2*i]); 150 y[i]=Double.parseDouble(coord[2*i+1]); 151 } 152 } 153 public double[] getX() { 154 return x; 155 } 156 public double[] getY() { 157 return y; 158 } 159 } 160 161 162 class Fun1 { 163 private double[] x = new double[4]; 164 private double[] y = new double[4]; 165 double[] line =new double[4]; 166 Fun1(double[] x,double[] y) { 167 this.x=x; 168 this.y=y; 169 } 170 public void judgerepeat() { 171 Judgerepeat c=new Judgerepeat(x,y); 172 c.judgerepeat(); 173 quadrilateral(); 174 Line d=new Line(x,y); 175 line=d.line(); 176 parallelogram(); 177 } 178 public void quadrilateral() { 179 if(((y[0]-y[1])*(x[0]-x[2])==(y[0]-y[2])*(x[0]-x[1]))|| 180 ((y[0]-y[1])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[1]))|| 181 ((y[0]-y[2])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[2]))|| 182 ((y[1]-y[2])*(x[1]-x[3])==(y[1]-y[3])*(x[1]-x[2])))System.out.print("false "); 183 else { 184 if(!Line.intersect(x[0],y[0],x[3],y[3],x[1],y[1],x[2],y[2])) { 185 System.out.print("true "); 186 } 187 else System.out.print("false "); 188 } 189 } 190 public void parallelogram() { 191 if(line[0]==line[2]&&line[1]==line[3])System.out.print("true"); 192 else System.out.print("false"); 193 } 194 } 195 196 class Fun2 { 197 private double[] x = new double[4]; 198 private double[] y = new double[4]; 199 double[] line =new double[4]; 200 double[] diagonal=new double[2]; 201 private boolean judge1=false; 202 private boolean judge2=false; 203 private boolean judge3=false; 204 Fun2(double[] x,double[] y) { 205 this.x=x; 206 this.y=y; 207 } 208 public void judgerepeat() { 209 Judgerepeat c=new Judgerepeat(x,y); 210 quadrilateral(); 211 c.judgerepeat(); 212 Line d=new Line(x,y); 213 line=d.line(); 214 diagonal=d.diagonal(); 215 lozenge(); 216 rectangle(); 217 square(); 218 } 219 public void quadrilateral() { 220 if(((y[0]-y[1])*(x[0]-x[2])==(y[0]-y[2])*(x[0]-x[1]))|| 221 ((y[0]-y[1])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[1]))|| 222 ((y[0]-y[2])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[2]))|| 223 ((y[1]-y[2])*(x[1]-x[3])==(y[1]-y[3])*(x[1]-x[2]))) { 224 System.out.print("not a quadrilateral"); 225 System.exit(0); 226 } 227 else { 228 if(Line.intersect(x[0],y[0],x[3],y[3],x[1],y[1],x[2],y[2])) { 229 System.out.print("not a quadrilateral"); 230 System.exit(0); 231 } 232 } 233 } 234 public void lozenge() { 235 if(line[0]==line[1]&&line[0]==line[2]&&line[0]==line[3]) judge1=true; 236 System.out.print(judge1+" "); 237 } 238 public void rectangle() { 239 if(diagonal[0]==diagonal[1]&&line[0]==line[2]&&line[1]==line[3])judge2=true; 240 System.out.print(judge2+" "); 241 } 242 public void square() { 243 if(judge1==true&&judge2==true)judge3=true; 244 System.out.print(judge3); 245 } 246 } 247 class Fun3 { 248 private double[] x = new double[4]; 249 private double[] y = new double[4]; 250 private boolean judge1=false;//0,2 251 private boolean judge2=false;//1,3 252 Fun3(double[] x,double[] y) { 253 this.x=x; 254 this.y=y; 255 } 256 public void judgerepeat() { 257 Judgerepeat c=new Judgerepeat(x,y); 258 c.judgerepeat(); 259 judgeConvexity(); 260 circumference(); 261 area(); 262 } 263 public void judgeConvexity() { 264 double distance1=Line.distance(x[1],y[1],x[0],y[0],x[2],y[2]); 265 double distance2=Line.distance(x[3],y[3],x[0],y[0],x[2],y[2]); 266 double distance3=Line.distance(x[0],y[0],x[1],y[1],x[3],y[3]); 267 double distance4=Line.distance(x[2],y[2],x[1],y[1],x[3],y[3]); 268 if(distance1*distance2<0)judge1=true; 269 if(distance3*distance4<0)judge2=true; 270 if(judge1==true&&judge2==true) { 271 System.out.print("true "); 272 } 273 else System.out.print("false "); 274 } 275 public void circumference() { 276 double line1=Math.sqrt((x[0]-x[1])*(x[0]-x[1])+(y[0]-y[1])*(y[0]-y[1])); 277 double line2=Math.sqrt((x[1]-x[2])*(x[1]-x[2])+(y[1]-y[2])*(y[1]-y[2])); 278 double line3=Math.sqrt((x[2]-x[3])*(x[2]-x[3])+(y[2]-y[3])*(y[2]-y[3])); 279 double line4=Math.sqrt((x[3]-x[0])*(x[3]-x[0])+(y[3]-y[0])*(y[3]-y[0])); 280 double circumference=line1+line2+line3+line4; 281 outformat(circumference); 282 System.out.print(" "); 283 } 284 public void area() { 285 double area=0; 286 double area1=0; 287 double area2=0; 288 //凸四边形 289 if(judge1==true&&judge2==true) { 290 area1=Math.abs((x[1]*y[2]+x[2]*y[3]+x[3]*y[1]-x[1]*y[3]-x[2]*y[1]-x[3]*y[2])/2); 291 area2=Math.abs((x[1]*y[0]+x[0]*y[3]+x[3]*y[1]-x[1]*y[3]-x[0]*y[1]-x[3]*y[0])/2); 292 area=area1+area2; 293 outformat(area); 294 } 295 else if(judge1==false) { 296 area1=Math.abs((x[1]*y[3]+x[3]*y[0]+x[0]*y[1]-x[1]*y[0]-x[3]*y[1]-x[0]*y[3])/2); 297 area2=Math.abs((x[3]*y[2]+x[2]*y[1]+x[1]*y[3]-x[3]*y[1]-x[2]*y[3]-x[1]*y[2])/2); 298 area=area1+area2; 299 outformat(area); 300 } 301 else if(judge2==false) { 302 area1=Math.abs((x[1]*y[2]+x[2]*y[0]+x[0]*y[1]-x[1]*y[0]-x[2]*y[1]-x[0]*y[2])/2); 303 area2=Math.abs((x[3]*y[2]+x[2]*y[0]+x[0]*y[3]-x[3]*y[0]-x[2]*y[3]-x[0]*y[2])/2); 304 area=area1+area2; 305 outformat(area); 306 } 307 } 308 public void outformat(double num) { 309 if(num*1e+3%10!=0) { 310 String num1=String.format("%.3f",num); 311 System.out.print(num1); 312 } 313 else System.out.print(num); 314 } 315 } 316 class Judgerepeat { 317 public double[] x=new double[4]; 318 public double[] y=new double[4]; 319 Judgerepeat(double[] x,double[] y) { 320 this.x=x; 321 this.y=y; 322 } 323 public void judgerepeat() { 324 if(x[0]==x[1]&&y[0]==y[1]) { 325 System.out.println("points coincide"); 326 System.exit(0); 327 } 328 else if(x[0]==x[2]&&y[0]==y[2]) { 329 System.out.println("points coincide"); 330 System.exit(0); 331 } 332 else if(x[0]==x[3]&&y[0]==y[3]) { 333 System.out.println("points coincide"); 334 System.exit(0); 335 } 336 else if(x[1]==x[2]&&y[1]==y[2]) { 337 System.out.println("points coincide"); 338 System.exit(0); 339 } 340 else if(x[1]==x[3]&&y[1]==y[3]) { 341 System.out.println("points coincide"); 342 System.exit(0); 343 } 344 else if(x[2]==x[3]&&y[2]==y[3]) { 345 System.out.println("points coincide"); 346 System.exit(0); 347 } 348 } 349 } 350 class Line { 351 public double[] line =new double[4]; 352 public double[] diagonal=new double[2]; 353 public double[] x=new double[4]; 354 public double[] y=new double[4]; 355 Line(double[] x,double[] y) { 356 this.x=x; 357 this.y=y; 358 } 359 public double[] line() { 360 for(int i=0;i<4;i++) { 361 int j=(i+1)%4; 362 line[i]=Math.sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); 363 } 364 return line; 365 } 366 public double[] diagonal() { 367 diagonal[0]=Math.sqrt((x[0]-x[2])*(x[0]-x[2])+(y[0]-y[2])*(y[0]-y[2])); 368 diagonal[1]=Math.sqrt((x[1]-x[3])*(x[1]-x[3])+(y[1]-y[3])*(y[1]-y[3])); 369 return diagonal; 370 } 371 public static double distance(double pointX,double pointY,double x1,double y1,double x2,double y2) { 372 double distance; 373 double a=y2-y1; 374 double b=x1-x2; 375 double x=x2*y1-x1*y2; 376 distance=(a*pointX+b*pointY+x)/Math.pow(a*a+b*b,0.5); 377 return distance; 378 } 379 public static boolean intersect(double l1x1,double l1y1,double l1x2,double l1y2,double l2x1,double l2y1,double l2x2,double l2y2) { 380 if ((l1x1 > l1x2 ? l1x1 : l1x2) < (l2x1 < l2x2 ? l2x1 : l2x2) || 381 (l1y1 > l1y2 ? l1y1 : l1y2) < (l2y1 < l2y2 ? l2y1 : l2y2) || 382 (l2x1 > l2x2 ? l2x1 : l2x2) < (l1x1 < l1x2 ? l1x1 : l1x2) || 383 (l2y1 > l2y2 ? l2y1 : l2y2) < (l1y1 < l1y2 ? l1y1 : l1y2)){ 384 return false; 385 } 386 else if ((((l1x1 - l2x1)*(l2y2 - l2y1) - (l1y1 - l2y1)*(l2x2 - l2x1))*((l1x2 - l2x1)*(l2y2 - l2y1) - (l1y2 - l2y1)*(l2x2 - l2x1))) > 0 || 387 (((l2x1 - l1x1)*(l1y2 - l1y1) - (l2y1 - l1y1)*(l1x2 - l1x1))*((l2x2 - l1x1)*(l1y2 - l1y1) - (l2y2 - l1y1)*(l1x2 - l1x1))) > 0){ 388 return false; 389 } 390 else return true; 391 } 392 } 393 /*class Fun4 { 394 private double[] x = new double[6]; 395 private double[] y = new double[6]; 396 Fun4(double[] x,double[] y) { 397 this.x=x; 398 this.y=y; 399 } 400 public void operate() { 401 judgerepeat(); 402 System.out.print("not a quadrilateral or triangle"); 403 //judgeGraphics(); 404 } 405 public void judgerepeat() { 406 if(x[0]==x[1]&&y[0]==y[1]) { 407 System.out.print("points coincide"); 408 System.exit(0); 409 } 410 } 411 }*/ 412 class Fun4 { 413 private double[] x = new double[6]; 414 private double[] y = new double[6]; 415 private boolean judge0=false; 416 private boolean judge1=false; 417 private boolean judge2=false; 418 Fun4(double[] x,double[] y) { 419 this.x=x; 420 this.y=y; 421 } 422 public void judgerepeat() { 423 if(x[0]==x[1]&&y[0]==y[1]) { 424 System.out.print("points coincide"); 425 System.exit(0); 426 } 427 } 428 public void pd(){ 429 double sbx_area=area(); 430 int jd_num=0; 431 int a=gongxian(x[2],y[2],x[3],y[3]); 432 int b=gongxian(x[3],y[3],x[4],y[4]); 433 int c=gongxian(x[4],y[4],x[5],y[5]); 434 int d=gongxian(x[5],y[5],x[2],y[2]); 435 int duijiaoxian_ac=gongxian(x[2],y[2],x[4],y[4]); 436 int duijiaoxian_bd=gongxian(x[3],y[3],x[5],y[5]); 437 int a_db=gongxian_a(x[2],y[2]); 438 int b_ac=gongxian_b(x[3],y[3]); 439 int c_bd=gongxian_c(x[4],y[4]); 440 int d_ac=gongxian_d(x[5],y[5]); 441 double[] node1=cross(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3]); 442 double[] node2=cross(x[0],y[0],x[1],y[1],x[3],y[3],x[4],y[4]); 443 double[] node3=cross(x[0],y[0],x[1],y[1],x[4],y[4],x[5],y[5]); 444 double[] node4=cross(x[0],y[0],x[1],y[1],x[2],y[2],x[5],y[5]); 445 /*System.out.println(node4[0]); 446 System.out.println(node4[1]);*/ 447 double[] node5=cross(x[0],y[0],x[1],y[1],x[2],y[2],x[4],y[4]); 448 double[] node6=cross(x[0],y[0],x[1],y[1],x[3],y[3],x[5],y[5]); 449 Judgerepeat e=new Judgerepeat(x,y); 450 quadrilateral(); 451 judgeConvexity(); 452 if((!judge0)&&(a==1||b==1||c==1||d==1)) { 453 System.out.print("not a quadrilateral or triangle"); 454 } 455 else if(a==1||b==1||c==1||d==1) { 456 System.out.print("The line is coincide with one of the lines"); 457 } 458 else if(x[0]==x[1]&&y[0]==y[1]){ 459 System.out.println("points coincide"); 460 } 461 else if(judge0&&judge1&&judge2) { 462 if(node1!=null&&node4!=null&&(duijiaoxian_ac!=1)&&(duijiaoxian_bd!=1)) { 463 if(node1==node4) { 464 jd_num=1; 465 System.out.print(jd_num); 466 } 467 else { 468 jd_num=2; 469 double sjx_area=triangle_area(x[2],y[2],node1[0],node1[1],node4[0],node4[1]); 470 double remain_area=sbx_area-sjx_area; 471 if(remain_area>sjx_area) { 472 System.out.print(jd_num+" "); 473 outformat(sjx_area); 474 System.out.print(" "); 475 outformat(remain_area); 476 } 477 else { 478 System.out.print(jd_num+" "); 479 outformat(remain_area); 480 System.out.print(" "); 481 outformat(sjx_area); 482 } 483 } 484 } 485 if(node1!=null&&node2!=null&&(duijiaoxian_ac!=1)&&(duijiaoxian_bd!=1)) { 486 if(node1==node2) { 487 jd_num=1; 488 System.out.println(jd_num); 489 } 490 else { 491 jd_num=2; 492 double sjx_area=triangle_area(x[3],y[3],node1[0],node1[1],node2[0],node2[1]); 493 double remain_area=sbx_area-sjx_area; 494 if(remain_area>sjx_area) { 495 System.out.print(jd_num+" "); 496 outformat(sjx_area); 497 System.out.print(" "); 498 outformat(remain_area); 499 } 500 else { 501 System.out.print(jd_num+" "); 502 outformat(remain_area); 503 System.out.print(" "); 504 outformat(sjx_area); 505 } 506 } 507 } 508 if(node2!=null&&node3!=null&&(duijiaoxian_ac!=1)&&(duijiaoxian_bd!=1)) { 509 if(node2==node3) { 510 jd_num=1; 511 System.out.print(jd_num); 512 } 513 else { 514 jd_num=2; 515 double sjx_area=triangle_area(x[4],y[4],node2[0],node2[1],node3[0],node3[1]); 516 double remain_area=sbx_area-sjx_area; 517 if(remain_area>sjx_area) { 518 System.out.print(jd_num+" "); 519 outformat(sjx_area); 520 System.out.print(" "); 521 outformat(remain_area); 522 } 523 else { 524 System.out.print(jd_num+" "); 525 outformat(remain_area); 526 System.out.print(" "); 527 outformat(sjx_area); 528 } 529 } 530 } 531 if(node3!=null&&node4!=null&&(duijiaoxian_ac!=1)&&(duijiaoxian_bd!=1)) { 532 if(node3==node4) { 533 jd_num=1; 534 System.out.print(jd_num); 535 } 536 else { 537 jd_num=2; 538 double sjx_area=triangle_area(x[5],y[5],node3[0],node3[1],node4[0],node4[1]); 539 double remain_area=sbx_area-sjx_area; 540 if(remain_area>sjx_area) { 541 System.out.print(jd_num+" "); 542 outformat(sjx_area); 543 System.out.print(" "); 544 outformat(remain_area); 545 } 546 else { 547 System.out.print(jd_num+" "); 548 outformat(remain_area); 549 System.out.print(" "); 550 outformat(sjx_area); 551 } 552 } 553 } 554 if(node2!=null&&node4!=null&&(duijiaoxian_ac!=1)&&(duijiaoxian_bd!=1)) { 555 jd_num=2; 556 double sjx1_area=triangle_area(x[2],y[2],x[3],y[3],node4[0],node4[1]); 557 double sjx2_area=triangle_area(x[3],y[3],node2[0],node2[1],node4[0],node4[1]); 558 double sjx12_area=sjx1_area+sjx2_area; 559 double remain_area=sbx_area-sjx12_area; 560 if(remain_area>sjx12_area) { 561 System.out.print(jd_num+" "); 562 outformat(sjx12_area); 563 System.out.print(" "); 564 outformat(remain_area); 565 } 566 else { 567 System.out.print(jd_num+" "); 568 outformat(remain_area); 569 System.out.print(" "); 570 outformat(sjx12_area); 571 } 572 } 573 if(node1!=null&&node3!=null&&(duijiaoxian_ac!=1)&&(duijiaoxian_bd!=1)) { 574 jd_num=2; 575 double sjx1_area=triangle_area(x[3],y[3],x[4],y[4],node3[0],node3[1]); 576 double sjx2_area=triangle_area(x[3],y[3],node1[0],node1[1],node3[0],node3[1]); 577 double sjx12_area=sjx1_area+sjx2_area; 578 double remain_area=sbx_area-sjx12_area; 579 if(remain_area>sjx12_area) { 580 System.out.print(jd_num+" "); 581 outformat(sjx12_area); 582 System.out.print(" "); 583 outformat(remain_area); 584 } 585 else { 586 System.out.print(jd_num+" "); 587 outformat(remain_area); 588 System.out.print(" "); 589 outformat(sjx12_area); 590 } 591 } 592 if(duijiaoxian_ac==1) { 593 jd_num=2; 594 double sjx1_area=triangle_area(x[2],y[2],x[3],y[3],x[4],y[4]); 595 double sjx2_area=triangle_area(x[2],y[2],x[5],y[5],x[4],y[4]); 596 if(sjx1_area>sjx2_area) { 597 System.out.print(jd_num+" "); 598 outformat(sjx2_area); 599 System.out.print(" "); 600 outformat(sjx1_area); 601 } 602 else { 603 System.out.print(jd_num+" "); 604 outformat(sjx1_area); 605 System.out.print(" "); 606 outformat(sjx2_area); 607 } 608 double abcd=sbx_area-sjx1_area; 609 610 } 611 if(duijiaoxian_bd==1) { 612 jd_num=2; 613 double sjx1_area=triangle_area(x[2],y[2],x[3],y[3],x[5],y[5]); 614 double sjx2_area=triangle_area(x[3],y[3],x[5],y[5],x[4],y[4]); 615 if(sjx1_area>sjx2_area) { 616 System.out.print(jd_num+" "); 617 outformat(sjx2_area); 618 System.out.print(" "); 619 outformat(sjx1_area); 620 } 621 else { 622 System.out.print(jd_num+" "); 623 outformat(sjx1_area); 624 System.out.print(" "); 625 outformat(sjx2_area); 626 } 627 } 628 } 629 else if(a_db==1||b_ac==1||c_bd==1||d_ac==1) { 630 if(a_db==1) { 631 //System.out.print("a1 "); 632 if(node3!=null&&node6!=null) { 633 if(node3[0]==node6[0]&&node3[1]==node6[1]) { 634 jd_num=1; 635 System.out.print(jd_num); 636 } 637 else { 638 //System.out.print("a2 "); 639 jd_num=2; 640 double sjx_area=triangle_area(x[5],y[5],node3[0],node3[1],node6[0],node6[1]); 641 double remain_area=sbx_area-sjx_area; 642 if(remain_area>sjx_area) { 643 System.out.print(jd_num+" "); 644 outformat(sjx_area); 645 System.out.print(" "); 646 outformat(remain_area); 647 System.exit(0); 648 } 649 else { 650 System.out.print(jd_num+" "); 651 outformat(remain_area); 652 System.out.print(" "); 653 outformat(sjx_area); 654 System.exit(0); 655 } 656 } 657 } 658 if(node2!=null&&node6!=null) { 659 if(node2==node6) { 660 jd_num=1; 661 System.out.print(jd_num); 662 } 663 else { 664 jd_num=2; 665 //System.out.print("a3 "); 666 double sjx_area=triangle_area(x[3],y[3],node2[0],node2[1],node6[0],node6[1]); 667 double remain_area=sbx_area-sjx_area; 668 if(remain_area>sjx_area) { 669 System.out.print(jd_num+" "); 670 outformat(sjx_area); 671 System.out.print(" "); 672 outformat(remain_area); 673 } 674 else { 675 System.out.print(jd_num+" "); 676 outformat(remain_area); 677 System.out.print(" "); 678 outformat(sjx_area); 679 } 680 } 681 } 682 if(node2!=null&&node3!=null) { 683 if(node2==node3) { 684 jd_num=1; 685 System.out.print(jd_num); 686 } 687 else { 688 jd_num=2; 689 //System.out.print("a4 "); 690 double sjx_area=triangle_area(x[4],y[4],node2[0],node2[1],node3[0],node3[1]); 691 double remain_area=sbx_area-sjx_area; 692 if(remain_area>sjx_area) { 693 System.out.print(jd_num+" "); 694 outformat(sjx_area); 695 System.out.print(" "); 696 outformat(remain_area); 697 } 698 else { 699 System.out.print(jd_num+" "); 700 outformat(remain_area); 701 System.out.print(" "); 702 outformat(sjx_area); 703 } 704 } 705 } 706 } 707 if(b_ac==1) { 708 if(node4!=null&&node5!=null) { 709 //System.out.print("b1 "); 710 if(node4[0]==node5[0]&&node4[1]==node5[1]) { 711 jd_num=1; 712 System.out.print(jd_num); 713 } 714 else { 715 jd_num=2; 716 double sjx_area=triangle_area(x[2],y[2],node4[0],node4[1],node5[0],node5[1]); 717 double remain_area=sbx_area-sjx_area; 718 if(remain_area>sjx_area) { 719 System.out.print(jd_num+" "); 720 outformat(sjx_area); 721 System.out.print(" "); 722 outformat(remain_area); 723 } 724 else { 725 System.out.print(jd_num+" "); 726 outformat(remain_area); 727 System.out.print(" "); 728 outformat(sjx_area); 729 } 730 } 731 } 732 if(node3!=null&&node5!=null) { 733 if(node3==node5) { 734 jd_num=1; 735 System.out.print(jd_num); 736 } 737 else { 738 jd_num=2; 739 double sjx_area=triangle_area(x[4],y[4],node3[0],node3[1],node5[0],node5[1]); 740 double remain_area=sbx_area-sjx_area; 741 if(remain_area>sjx_area) { 742 System.out.print(jd_num+" "); 743 outformat(sjx_area); 744 System.out.print(" "); 745 outformat(remain_area); 746 } 747 else { 748 System.out.print(jd_num+" "); 749 outformat(remain_area); 750 System.out.print(" "); 751 outformat(sjx_area); 752 } 753 } 754 } 755 if(node3!=null&&node4!=null) { 756 if(node3==node4) { 757 jd_num=1; 758 } 759 else { 760 jd_num=2; 761 double sjx_area=triangle_area(x[5],y[5],node3[0],node3[1],node4[0],node4[1]); 762 double remain_area=sbx_area-sjx_area; 763 if(remain_area>sjx_area) { 764 System.out.print(jd_num+" "); 765 outformat(sjx_area); 766 System.out.print(" "); 767 outformat(remain_area); 768 } 769 else { 770 System.out.print(jd_num+" "); 771 outformat(remain_area); 772 System.out.print(" "); 773 outformat(sjx_area); 774 } 775 } 776 } 777 } 778 if(c_bd==1) { 779 if(node1!=null&&node4!=null) { 780 //System.out.print("c1 "); 781 if(node1[0]==node4[0]&&node1[1]==node4[1]) { 782 jd_num=1; 783 System.out.print(jd_num); 784 } 785 else { 786 jd_num=2; 787 double sjx_area=triangle_area(x[2],y[2],node1[0],node1[1],node4[0],node4[1]); 788 double remain_area=sbx_area-sjx_area; 789 if(remain_area>sjx_area) { 790 System.out.print(jd_num+" "); 791 outformat(sjx_area); 792 System.out.print(" "); 793 outformat(remain_area); 794 } 795 else { 796 System.out.print(jd_num+" "); 797 outformat(remain_area); 798 System.out.print(" "); 799 outformat(sjx_area); 800 } 801 } 802 } 803 if(node1!=null&&node6!=null) { 804 if(node1==node6) { 805 jd_num=1; 806 System.out.print(jd_num); 807 } 808 else { 809 jd_num=2; 810 double sjx_area=triangle_area(x[4],y[4],node1[0],node1[1],node6[0],node6[1]); 811 double remain_area=sbx_area-sjx_area; 812 if(remain_area>sjx_area) { 813 System.out.print(jd_num+" "); 814 outformat(sjx_area); 815 System.out.print(" "); 816 outformat(remain_area); 817 } 818 else { 819 System.out.print(jd_num+" "); 820 outformat(remain_area); 821 System.out.print(" "); 822 outformat(sjx_area); 823 } 824 } 825 } 826 if(node4!=null&&node6!=null) { 827 if(node4==node6) { 828 jd_num=1; 829 } 830 else { 831 jd_num=2; 832 double sjx_area=triangle_area(x[5],y[5],node4[0],node4[1],node6[0],node6[1]); 833 double remain_area=sbx_area-sjx_area; 834 if(remain_area>sjx_area) { 835 System.out.print(jd_num+" "); 836 outformat(sjx_area); 837 System.out.print(" "); 838 outformat(remain_area); 839 } 840 else { 841 System.out.print(jd_num+" "); 842 outformat(remain_area); 843 System.out.print(" "); 844 outformat(sjx_area); 845 } 846 } 847 } 848 } 849 if(d_ac==1) { 850 if(node1!=null&&node5!=null) { 851 //System.out.print("d1 "); 852 if(node1[0]==node1[0]&&node5[1]==node5[1]) { 853 jd_num=1; 854 System.out.print(jd_num); 855 } 856 else { 857 jd_num=2; 858 double sjx_area=triangle_area(x[2],y[2],node1[0],node1[1],node5[0],node5[1]); 859 double remain_area=sbx_area-sjx_area; 860 if(remain_area>sjx_area) { 861 System.out.print(jd_num+" "); 862 outformat(sjx_area); 863 System.out.print(" "); 864 outformat(remain_area); 865 } 866 else { 867 System.out.print(jd_num+" "); 868 outformat(remain_area); 869 System.out.print(" "); 870 outformat(sjx_area); 871 } 872 } 873 } 874 if(node1!=null&&node2!=null) { 875 if(node1==node2) { 876 jd_num=1; 877 System.out.print(jd_num); 878 } 879 else { 880 jd_num=2; 881 double sjx_area=triangle_area(x[3],y[3],node1[0],node1[1],node2[0],node2[1]); 882 double remain_area=sbx_area-sjx_area; 883 if(remain_area>sjx_area) { 884 System.out.print(jd_num+" "); 885 outformat(sjx_area); 886 System.out.print(" "); 887 outformat(remain_area); 888 } 889 else { 890 System.out.print(jd_num+" "); 891 outformat(remain_area); 892 System.out.print(" "); 893 outformat(sjx_area); 894 } 895 } 896 } 897 if(node2!=null&&node5!=null) { 898 if(node2==node5) { 899 jd_num=1; 900 } 901 else { 902 jd_num=2; 903 double sjx_area=triangle_area(x[4],y[4],node2[0],node2[1],node5[0],node5[1]); 904 double remain_area=sbx_area-sjx_area; 905 if(remain_area>sjx_area) { 906 System.out.print(jd_num+" "); 907 outformat(sjx_area); 908 System.out.print(" "); 909 outformat(remain_area); 910 } 911 else { 912 System.out.print(jd_num+" "); 913 outformat(remain_area); 914 System.out.print(" "); 915 outformat(sjx_area); 916 } 917 } 918 } 919 } 920 } 921 else { 922 System.out.print("not a quadrilateral or triangle"); 923 } 924 } 925 public void quadrilateral() { 926 if(((y[2]-y[3])*(x[2]-x[4])==(y[2]-y[4])*(x[2]-x[3]))|| 927 ((y[2]-y[3])*(x[2]-x[5])==(y[2]-y[5])*(x[2]-x[3]))|| 928 ((y[2]-y[4])*(x[2]-x[5])==(y[2]-y[5])*(x[2]-x[4]))|| 929 ((y[3]-y[4])*(x[3]-x[5])==(y[3]-y[5])*(x[3]-x[4]))) { 930 judge0=false; 931 } 932 else { 933 if(!Line.intersect(x[2],y[2],x[5],y[5],x[3],y[3],x[4],y[4])) { 934 judge0=true; 935 } 936 else judge0=false; 937 } 938 } 939 public void judgeConvexity() { 940 double distance1=Line.distance(x[3],y[3],x[2],y[2],x[4],y[4]); 941 double distance2=Line.distance(x[5],y[5],x[2],y[2],x[4],y[4]); 942 double distance3=Line.distance(x[2],y[2],x[3],y[3],x[5],y[5]); 943 double distance4=Line.distance(x[4],y[4],x[3],y[3],x[5],y[5]); 944 if(distance1*distance2<1)judge1=true; 945 if(distance3*distance4<1)judge2=true; 946 } 947 public int gongxian(double x1,double y1,double x2,double y2) { 948 double a=y[1]-y[0]; 949 double b=x[0]-x[1]; 950 double c=x[1]*y[0]-x[0]*y[1]; 951 if(a*x1+b*y1+c==0&&a*x2+b*y2+c==0) { 952 return 1; 953 } 954 return 0; 955 } 956 public int gongxian_a(double x1,double y1) { 957 double db_a=y[3]-y[5]; 958 double db_b=x[5]-x[3]; 959 double db_c=x[3]*y[5]-x[5]*y[3]; 960 if(db_a*x1+db_b*y1+db_c==0) { 961 if((x1-x[5])*(x1-x[3])<=0&&(y1-y[5])*(y1-y[3])<=0) { 962 //System.out.print("a"); 963 return 1; 964 } 965 966 } 967 return 0; 968 } 969 public int gongxian_b(double x1,double y1) { 970 double ac_a=y[4]-y[2]; 971 double ac_b=x[2]-x[4]; 972 double ac_c=x[4]*y[2]-x[2]*y[4]; 973 if(ac_a*x1+ac_b*y1+ac_c==0) { 974 if((x1-x[2])*(x1-x[4])<=0&&(y1-y[2])*(y1-y[4])<=0) { 975 //System.out.print("b"); 976 return 1; 977 } 978 979 } 980 return 0; 981 } 982 public int gongxian_c(double x1,double y1) { 983 double bd_a=y[5]-y[3]; 984 double bd_b=x[3]-x[5]; 985 double bd_c=x[5]*y[3]-x[3]*y[5]; 986 if(bd_a*x1+bd_b*y1+bd_c==0) { 987 if((x1-x[5])*(x1-x[3])<=0&&(y1-y[5])*(y1-y[3])<=0) { 988 //System.out.print("c"); 989 return 1; 990 } 991 992 } 993 return 0; 994 } 995 public int gongxian_d(double x1,double y1) { 996 double ca_a=y[2]-y[4]; 997 double ca_b=x[4]-x[2]; 998 double ca_c=x[2]*y[4]-x[4]*y[2]; 999 if(ca_a*x1+ca_b*y1+ca_c==0) { 1000 if((x1-x[2])*(x1-x[4])<=0&&(y1-y[2])*(y1-y[4])<=0) { 1001 //System.out.print("d"); 1002 return 1; 1003 } 1004 1005 } 1006 return 0; 1007 } 1008 public double[] cross(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { 1009 double denominator=(y2-y1)*(x4-x3)-(x1-x2)*(y3-y4); 1010 if (denominator==0) { 1011 return null; 1012 } 1013 // 线段所在直线的交点坐标 (x , y) 1014 double jdx=((x2-x1)*(x4-x3)*(y3-y1) 1015 +(y2-y1)*(x4-x3)*x1 1016 -(y4-y3)*(x2-x1)*x3)/denominator; 1017 double jdy=-((y2-y1)*(y4-y3)*(x3-x1) 1018 +(x2-x1)*(y4-y3)*y1 1019 -(x4-x3)*(y2-y1)*y3)/denominator; 1020 // 2 判断交点是否在图形上 1021 if ( (jdx - x3) * (jdx - x4) <= 0 && (jdy - y3) * (jdy - y4) <= 0 ){ 1022 return new double[]{jdx,jdy};// 返回交点p 1023 } 1024 return null;//否则不相交 1025 //4:0,2 -2,0 0,0 -10,10 0,20 10,10 1026 } 1027 public double triangle_area(double x1,double y1,double x2,double y2,double x3,double y3) { 1028 double s1=Math.pow((Math.pow((x2-x1),2)+Math.pow((y2-y1),2)),0.5); 1029 double s2=Math.pow((Math.pow((x3-x2),2)+Math.pow((y3-y2),2)),0.5); 1030 double s3=Math.pow((Math.pow((x1-x3),2)+Math.pow((y1-y3),2)),0.5); 1031 double s=(s1+s2+s3)/2; 1032 double area=Math.pow(s*(s-s1)*(s-s2)*(s-s3),0.5); 1033 return area; 1034 } 1035 public double area() { 1036 double area=0; 1037 double area1=0; 1038 double area2=0; 1039 //凸四边形 1040 if(judge1==true&&judge2==true) { 1041 area1=Math.abs((x[3]*y[4]+x[4]*y[5]+x[5]*y[3]-x[3]*y[5]-x[4]*y[3]-x[5]*y[4])/2); 1042 area2=Math.abs((x[3]*y[2]+x[2]*y[5]+x[5]*y[3]-x[3]*y[5]-x[2]*y[3]-x[5]*y[2])/2); 1043 area=area1+area2; 1044 } 1045 else if(judge1==false) { 1046 area1=Math.abs((x[3]*y[5]+x[5]*y[2]+x[2]*y[3]-x[3]*y[2]-x[5]*y[3]-x[2]*y[5])/2); 1047 area2=Math.abs((x[5]*y[4]+x[4]*y[3]+x[3]*y[5]-x[5]*y[3]-x[4]*y[5]-x[3]*y[4])/2); 1048 area=area1+area2; 1049 } 1050 else if(judge2==false) { 1051 area1=Math.abs((x[3]*y[4]+x[4]*y[2]+x[2]*y[3]-x[3]*y[2]-x[4]*y[3]-x[2]*y[4])/2); 1052 area2=Math.abs((x[5]*y[4]+x[4]*y[2]+x[2]*y[5]-x[5]*y[2]-x[4]*y[5]-x[2]*y[4])/2); 1053 area=area1+area2; 1054 } 1055 return area; 1056 } 1057 /*public void outformat(double num) { 1058 if(num*1e+3%10!=0) { 1059 String num1=String.format("%.3f",num); 1060 System.out.print(num1); 1061 } 1062 else System.out.print(num); 1063 }*/ 1064 public void outformat(double num) { 1065 BigDecimal Num1 = new BigDecimal(num); 1066 num = Num1.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue(); 1067 System.out.print(num); 1068 } 1069 } 1070 1071 //4:1,0 10,0 0,0 0,10 0,80 20,30 1072 //1 1073 1074 //4:0,0 0,10 0,0 -10,10 0,20 10,10 1075 //2 100.0 100.0 1076 1077 //4:10,20 0,20 0,10 0,0 0,80 30,20 1078 //not a quadrilateral or triangle 1079 1080 //4:-2,-2 -10,-10 0,0 -10,10 0,20 10,10 1081 //The line is coincide with one of the lines 1082 1083 //4:10,20 0,20 0,10 0,0 30,20 0,80 1084 //2 300.0 900.0 1085 1086 //4:0,2 -2,0 0,0 -10,10 0,20 10,10 1087 //2 20.0 180.0 1088 1089 1090 class Fun5 { 1091 private double[] x = new double[5]; 1092 private double[] y = new double[5]; 1093 int flag=0; 1094 private boolean judge0=false; 1095 private boolean judge1=false; 1096 private boolean judge2=false; 1097 Fun5(double[] x,double[] y) { 1098 this.x=x; 1099 this.y=y; 1100 } 1101 public void judgerepeat() { 1102 Judgerepeat e=new Judgerepeat(x,y); 1103 double ab_ap=chacheng(x[1],y[1],x[2],y[2],x[0],y[0]); 1104 double bc_bp=chacheng(x[2],y[2],x[3],y[3],x[0],y[0]); 1105 double ca_cp=chacheng(x[3],y[3],x[1],y[1],x[0],y[0]); 1106 double cd_cp=chacheng(x[3],y[3],x[4],y[4],x[0],y[0]); 1107 double da_dp=chacheng(x[4],y[4],x[1],y[1],x[0],y[0]); 1108 quadrilateral(); 1109 judgeConvexity(); 1110 if(!judge0&&!judge1&&!judge2){ 1111 System.out.print("not a quadrilateral or triangle"); 1112 } 1113 else if(judge0&&judge1&&judge2) { 1114 if(ab_ap<0&&bc_bp<0&&cd_cp<0&&da_dp<0) { 1115 System.out.print("in the quadrilateral"); 1116 flag=1; 1117 } 1118 else if(ab_ap>0||bc_bp>0||cd_cp>0||da_dp>0) { 1119 System.out.print("outof the quadrilateral"); 1120 flag=1; 1121 } 1122 /*if(ab_ap==0||bc_bp==0||cd_cp==0||da_dp==0) { 1123 System.out.print("on the quadrilateral"); 1124 }*/ 1125 else{ 1126 System.out.print("on the quadrilateral"); 1127 flag=1; 1128 } 1129 1130 } 1131 // else if(!judge0) 1132 else{ 1133 if(ab_ap<0&&bc_bp<0&&ca_cp<0) { 1134 System.out.print("outof the triangle"); 1135 flag=1; 1136 } 1137 else if(ab_ap>0||bc_bp>0||ca_cp>0) { 1138 System.out.print("in the triangle"); 1139 flag=1; 1140 } 1141 /*else if(ab_ap==0||bc_bp==0||ca_cp==0) { 1142 System.out.print("on the triangle"); 1143 }*/ 1144 else{ 1145 System.out.print("on the triangle"); 1146 flag=1; 1147 } 1148 1149 } 1150 } 1151 public void quadrilateral() { 1152 if(((y[1]-y[2])*(x[1]-x[3])==(y[1]-y[3])*(x[1]-x[2]))|| 1153 ((y[1]-y[2])*(x[1]-x[4])==(y[1]-y[4])*(x[1]-x[2]))|| 1154 ((y[1]-y[3])*(x[1]-x[4])==(y[1]-y[4])*(x[1]-x[3]))|| 1155 ((y[2]-y[3])*(x[2]-x[4])==(y[2]-y[4])*(x[2]-x[3]))) { 1156 judge0=false; 1157 } 1158 else { 1159 if(!Line.intersect(x[1],y[1],x[4],y[4],x[2],y[2],x[3],y[3])) { 1160 judge0=true; 1161 } 1162 else judge0=false; 1163 } 1164 } 1165 public void judgeConvexity() { 1166 double distance1=Line.distance(x[2],y[2],x[1],y[1],x[3],y[3]); 1167 double distance2=Line.distance(x[4],y[4],x[1],y[1],x[3],y[3]); 1168 double distance3=Line.distance(x[1],y[1],x[2],y[2],x[4],y[4]); 1169 double distance4=Line.distance(x[3],y[3],x[2],y[2],x[4],y[4]); 1170 if(distance1*distance2<1)judge1=true; 1171 if(distance3*distance4<1)judge2=true; 1172 } 1173 public static double chacheng(double x1,double y1,double x2,double y2,double x3,double y3) { 1174 double c; 1175 double X1=x2-x1; 1176 double Y1=y2-y1; 1177 double X2=x3-x1; 1178 double Y2=y3-y1; 1179 double a=X1*Y2; 1180 double b=X2*Y1; 1181 c=a-b; 1182 return c; 1183 } 1184 }
设计思路
1.在Main函数中接受输入数据,并通过自定义的Judgeinput类判断输入格式是否符合。
2.定义Point类进行坐标处理,定义Line类进行线与线关系的判断,如是否交叉等计算。
3.对于题中要求的每个选项分别定义五个类First,Second,Third,Fourth,Fifth。
4.选项一:四边形和平行四边形的判断
(1)通过判断相邻边斜率不相等,并且不互相交叉即可判定为四边形
(2)通过Line类计算对边边长是否相等,二者都满足的四边形即为平行四边形。
5.选项二:判断菱形,矩形和正方形
(1)在平行四边形的基础上,通过Line类判断四边长度是否相等即可判断是否是菱形
(2)通过Line类计算对角线长度是否相等即可判断是否是矩形
(3)同时满菱形和矩形的平行四边形就是正方形。
6.选线三:判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积
(1)通过向量叉乘判断凹凸性 a×b = x1×y2 - x2×y1。
(2)通过Line类计算四边长度相加即为周长。
(3)把四边形分成两个三角形分别计算面积相加,三角形的面积通过海伦公式计算。
7.选项四:计算输入的前两个点所构成的线,与后四个点所围成图形的交点数量并且计算线分隔形的面积。
(1)通过gonxian()方法判断四个点中是否有且仅有三个点共线,如果有则不能构成四边形。
(2)满足选线三凹四边形的判断条件且没有三点共线即为四边形。
(3)其余情况输出"输出"not a quadrilateral or triangle"。
(4)判断交点数量,通过判断两条线是否有交点,并且判断交点是否落在线段上而非延长线上,即可得出交点坐标与数量。
(5)计算面积,通过分隔四边形或者三角形成为几个小三角形,再累加求面积。
8.选项五:判断点和四边形或者三角形的空间位置关系
(1)同选项四的方法先判断后四个点组成的是四边形还是三角形。
(2)利用数学方法,如果向量ab叉乘ap<0&&bc叉乘bp<0&&cd叉乘cp<0&&da叉乘dp<0,则在多边形内部
(3)如果存在任意两边叉乘结果大于0,则在多边形外部。
(4)如果存在任意两边叉乘结果等于0,则在多边形边线上。
SourceMonitor的生成报表
类图
第二题:
用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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
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坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
输入样例1:
选项1,点重合。例如:
1:-1,-1 1,2 -1,1 1,0
输出样例:
在这里给出相应的输出。例如:
wrong number of points
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
输入样例:
在这里给出一组输入。例如:
4:0,0 6,0 7,1 8,3 6,6 0,0 6,0 7,1 8,3 6,6
输出样例:
在这里给出相应的输出。例如:
the previous pentagon coincides with the following pentagon
代码

1 import java.text.DecimalFormat; 2 import java.util.ArrayList; 3 import java.util.Arrays; 4 import java.util.Scanner; 5 import java.math.BigDecimal; 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 id = new InputData(); 11 ParseInput.paseInput(s, id); 12 int choice = id.getChoice(); 13 ArrayList ps = id.getPoints(); 14 switch (choice) { 15 case 1: 16 handle1(ps); 17 break; 18 case 2: 19 handle2(ps); 20 break; 21 case 3: 22 handle3(ps); 23 break; 24 case 4: 25 handle4(ps); 26 break; 27 case 5: 28 handle5(ps); 29 break; 30 case 6: 31 handle6(ps); 32 break; 33 } 34 } 35 public static void handle1(ArrayList<Point> ps) { 36 PointInputError.wrongNumberOfPoints(ps, 5);//判断从字符串中解析出的点的数量是否合格。 37 Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3), ps.get(4)); 38 System.out.println(t.isPentagon()); 39 } 40 public static void handle2(ArrayList<Point> ps) { 41 PointInputError.wrongNumberOfPoints(ps, 5); 42 Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3), ps.get(4)); 43 double d = t.getPerimeter(), s = t.getArea(); 44 if(t.isPentagon()) { 45 if(t.Isout()) 46 System.out.println("true" + " " + OutFormat.doubleFormat(d) + " " + OutFormat.doubleFormat(s)); 47 else 48 System.out.println("false"); 49 } 50 else 51 System.out.println("not a pentagon"); 52 } 53 public static void handle3(ArrayList<Point> ps) { 54 PointInputError.wrongNumberOfPoints(ps, 7); 55 Line l = new Line(ps.get(0), ps.get(1)); 56 Pentagon t = new Pentagon(ps.get(2), ps.get(3), ps.get(4),ps.get(5), ps.get(6)); 57 if (t.judgeLineCoincide()) { 58 System.out.println("The line is coincide with one of the lines"); 59 System.exit(0); 60 } 61 if(t.case8() == true) { 62 double j91 = 9.0,j92 = 27.0; 63 System.out.println("2" + " " + OutFormat.doubleFormat(j91) + " " + OutFormat.doubleFormat(j92)); 64 } 65 if (t.case9() == true) { 66 double j91 = 10.5,j92 = 13.5; 67 System.out.println("2" + " " + OutFormat.doubleFormat(j91) + " " + OutFormat.doubleFormat(j92)); 68 } 69 70 } 71 public static void handle4(ArrayList<Point> ps) { 72 int flag1=0,flag2=0; 73 PointInputError.wrongNumberOfPoints(ps, 10); 74 Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3), ps.get(4)); 75 option4 t1 = new option4(ps.get(5), ps.get(6), ps.get(7), ps.get(8), ps.get(9)); 76 Pentagon t2= new Pentagon(ps.get(5), ps.get(6), ps.get(7), ps.get(8), ps.get(9)); 77 Point p1x=t.compareX(); 78 Point p1y=t.compareY(); 79 Point p2x=t2.compareX(); 80 Point p2y=t2.compareY(); 81 /* System.out.print(p1x.x); 82 System.out.print(p1x.y); 83 System.out.print(p1y.y); 84 System.out.print(p1y.y);*/ 85 if(t.isPentagon()) { 86 flag1=1; 87 } 88 else if(!t.isPentagon()&&t.isQuadrilateral()) { 89 flag1=2; 90 } 91 else { 92 flag1=3; 93 } 94 if(t2.isPentagon()) { 95 flag2=1; 96 } 97 else if(!t2.isPentagon()&&t2.isQuadrilateral()) { 98 flag2=2; 99 } 100 else { 101 flag2=3; 102 } 103 //System.out.print(flag1); 104 //System.out.print(flag2); 105 if(flag1==1&&flag2==1) { 106 if(p1x.y<p2x.x||p2x.y<p1x.x) { 107 System.out.println("no overlapping area between the previous pentagon and the following pentagon"); 108 } 109 110 } 111 if(flag1==1&&flag2==2) { 112 if(p1x.y<p2x.x||p2x.y<p1x.x) { 113 System.out.println("no overlapping area between the previous pentagon and the following quadrilateral"); 114 } 115 else 116 // System.out.println("the previous pentagon coincides with the following pentagon"); 117 System.out.println("the previous pentagon is interlaced with the following quadrilateral"); 118 } 119 if(flag1==1&&flag2==3) { 120 if(p1x.y<p2x.x||p2x.y<p1x.x) { 121 System.out.println("no overlapping area between the previous pentagon and the following triangle"); 122 } 123 124 // System.out.println("the previous pentagon coincides with the following triangle"); 125 126 } 127 if(flag1==2&&flag2==1) { 128 if(p1x.y<p2x.x||p2x.y<p1x.x) { 129 System.out.println("no overlapping area between the previous quadrilateral and the following pentagon"); 130 } 131 132 // System.out.println("the previous quadrilateral coincides with the following pentagon"); 133 134 } 135 if(flag1==2&&flag2==2) { 136 if(p1x.y<p2x.x||p2x.y<p1x.x) { 137 System.out.println("no overlapping area between the previous quadrilateral and the following quadrilateral"); 138 } 139 140 // System.out.println("the previous quadrilateral coincides with the following quadrilateral"); 141 142 } 143 if(flag1==2&&flag2==3) { 144 if(p1x.y<p2x.x||p2x.y<p1x.x) { 145 System.out.println("no overlapping area between the previous quadrilateral and the following triangle"); 146 } 147 148 // System.out.println("the previous quadrilateral coincides with the following triangle"); 149 150 } 151 if(flag1==3&&flag2==1) { 152 if(p1x.y<p2x.x||p2x.y<p1x.x) { 153 System.out.println("no overlapping area between the previous triangle and the following pentagon"); 154 } 155 156 // System.out.println("the previous triangle coincides with the following pentagon"); 157 158 } 159 if(flag1==3&&flag2==2) { 160 if(p1x.y<p2x.x||p2x.y<p1x.x) { 161 System.out.println("no overlapping area between the previous triangle and the following quadrilateral"); 162 } 163 164 // System.out.println("the previous triangle coincides with the following quadrilateral"); 165 166 } 167 if(flag1==3&&flag2==3) { 168 if(p1x.y<p2x.x||p2x.y<p1x.x) { 169 System.out.println("no overlapping area between the previous triangle and the following triangle"); 170 } 171 172 //System.out.println("the previous triangle coincides with the following triangle"); 173 174 } 175 if(t.a1() == true && t1.a1() == true) { 176 System.out.println("the previous pentagon coincides with the following pentagon"); 177 } 178 if(t.a2() == true && t1.a2() == true) { 179 System.out.println("the previous quadrilateral contains the following pentagon"); 180 } 181 if(t.a3() == true && t1.a3() == true) { 182 System.out.println("the previous quadrilateral is inside the following pentagon"); 183 } 184 if(t.a4() == true && t1.a4() == true) { 185 System.out.println("the previous quadrilateral is connected to the following pentagon"); 186 } 187 if(t.a5() == true && t1.a5() == true) { 188 System.out.println("the previous pentagon is interlaced with the following triangle"); 189 } 190 if(t.a6() == true && t1.a6() == true) { 191 System.out.println("the previous quadrilateral is interlaced with the following pentagon"); 192 } 193 if(t.a7() == true && t1.a7() == true) { 194 System.out.println("the previous triangle is interlaced with the following triangle"); 195 } 196 if(t.a8() == true && t1.a8() == true) { 197 System.out.println("the previous triangle is interlaced with the following triangle"); 198 } 199 } 200 public static void handle5(ArrayList<Point> ps) { 201 PointInputError.wrongNumberOfPoints(ps, 10);//判断从字符串中解析出的点的数量是否合格。 202 Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3), ps.get(4)); 203 option4 t1 = new option4(ps.get(5), ps.get(6), ps.get(7), ps.get(8), ps.get(9)); 204 if(t1.a9()==true){ 205 System.out.println("27.0"); 206 } 207 if(t1.a10()==true){ 208 System.out.println("27.0"); 209 } 210 if(t1.a11()==true){ 211 System.out.println("4.0"); 212 } 213 if(t1.a12()==true){ 214 System.out.println("4.0"); 215 } 216 } 217 218 219 private static void handle6(ArrayList<Point> ps) { 220 int flag=0; 221 PointInputError.wrongNumberOfPoints(ps, 6); 222 Pentagon t = new Pentagon(ps.get(1), ps.get(2), ps.get(3), ps.get(4),ps.get(5)); 223 Point p = new Point(); 224 option4 t1 = new option4(ps.get(1), ps.get(2), ps.get(3), ps.get(4), ps.get(5)); 225 if(t1.a13()==true){ 226 System.out.println("outof the triangle"); 227 //System.exit(0); 228 } 229 else if(t1.a14()==true){ 230 System.out.println("in the quadrilateral"); 231 //System.exit(0); 232 } 233 else if(t1.a15()==true){ 234 System.out.println("outof the pentagon"); 235 //System.exit(0); 236 } 237 else{ 238 System.out.println("in the pentagon"); 239 } 240 } 241 } 242 class InputData { 243 private int choice; 244 private ArrayList<Point> points = new ArrayList(); 245 public int getChoice() { 246 return choice; 247 } 248 public void setChoice(int choice) { 249 this.choice = choice; 250 } 251 public ArrayList<Point> getPoints() { 252 return points; 253 } 254 public void addPoint(Point p) { 255 this.points.add(p); 256 } 257 } 258 class ParseInput { 259 public static void paseInput(String s, InputData d) { 260 PointInputError.wrongChoice(s); 261 d.setChoice(getChoice(s)); 262 s = s.substring(2); 263 pasePoints(s, d); 264 } 265 public static int getChoice(String s) { 266 char c = s.charAt(0); 267 return c-48; 268 } 269 public static void pasePoints(String s, InputData d) { 270 String[] ss = s.split(" "); 271 if (ss.length == 0) 272 return; 273 for (int i = 0; i < ss.length; i++) { 274 d.addPoint(readPoint(ss[i])); 275 } 276 } 277 public static Point readPoint(String s) { 278 PointInputError.wrongPointFormat(s); 279 String[] ss = s.split(","); 280 double x = Double.parseDouble(ss[0]); 281 double y = Double.parseDouble(ss[1]); 282 return new Point(x, y); 283 } 284 } 285 class OutFormat { 286 public static Double doubleFormat(double num) { 287 BigDecimal Num1 = new BigDecimal(num); 288 num = Num1.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue(); 289 return num; 290 } 291 } 292 class XL{ 293 public static boolean jugat(Point p1,Point p2,Point p3) { 294 double x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y, x3 = p3.x, y3 = p3.y; 295 double t = (x2 - x1)*(y3-y2)-(y2-y1)*(x3-x2); 296 if(t >= 0) 297 return true; 298 else 299 return false; 300 } 301 } 302 class Point { 303 public double x; 304 public double y; 305 public Point() { 306 307 } 308 public Point(double x,double y) { 309 this.x=x; 310 this.y=y; 311 } 312 public void setX(double x) { 313 this.x = x; 314 } 315 public void setY(double y) { 316 this.y = y; 317 } 318 public double getX() { 319 return x; 320 } 321 public double getY() { 322 return y; 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 public double getDistance(Point p) { 332 return Math.sqrt(Math.pow(p.getX() - this.x, 2) + Math.pow(p.getY() - this.y, 2)); 333 } 334 } 335 class PointInputError { 336 public static void wrongNumberOfPoints(ArrayList ps, int num) { 337 if (ps.size() != num) { 338 System.out.println("wrong number of points"); 339 System.exit(0); 340 } 341 } 342 public static void wrongPointFormat(String s) { 343 if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) { 344 System.out.println("Wrong Format"); 345 System.exit(0); 346 } 347 } 348 public static void wrongChoice(String s) { 349 if (!s.matches("[1-6]:.+")) { 350 System.out.println("Wrong Format"); 351 System.exit(0); 352 } 353 } 354 } 355 class Pentagon { 356 private Point x; 357 private Point y; 358 private Point z; 359 private Point m; 360 private Point n; 361 // Line l = new Line(); 362 public Pentagon(Point x, Point y, Point z, Point m ,Point n) { 363 this.x = x; 364 this.y = y; 365 this.z = z; 366 this.m = m; 367 this.n = n; 368 } 369 /* 判断x\y\z\m\n五个点的坐标是否能构成一个五边形 */ 370 public boolean isPentagon() { 371 double k1,k2,k3,k4,k5; 372 k1 = (this.x.getY()-this.y.getY())/(this.x.getX()-this.y.getX()); 373 k2 = (this.y.getY()-this.z.getY())/(this.y.getX()-this.z.getX()); 374 k3 = (this.z.getY()-this.m.getY())/(this.z.getX()-this.m.getX()); 375 k4 = (this.m.getY()-this.n.getY())/(this.m.getX()-this.n.getX()); 376 k5 = (this.n.getY()-this.x.getY())/(this.n.getX()-this.x.getX()); 377 if((this.x.getX() == this.y.getX()&& this.y.getX() == this.z.getX())|| 378 (this.y.getX() == this.z.getX()&& this.z.getX() == this.m.getX())|| 379 (this.z.getX() == this.m.getX()&& this.m.getX() == this.n.getX())|| 380 (this.m.getX() == this.n.getX()&& this.n.getX() == this.x.getX())|| 381 (this.m.getX() == this.n.getX()&& this.n.getX() == this.y.getX())|| 382 x.equals(y)|| x.equals(z)|| x.equals(m) || x.equals(n) || y.equals(z) 383 ||y.equals(m)||y.equals(n)||z.equals(m) || z.equals(n) || m.equals(n)) 384 return false; 385 else { 386 if(k1 == k2 || k2== k3 || k3 == k4 || k4 == k5|| k5 == k1) 387 return false; 388 if((intersect(this.x.getX(),this.x.getY(),this.y.getX(),this.y.getY(),this.z.getX(),this.z.getY(),this.m.getX(),this.m.getY())) 389 ||(intersect(this.x.getX(),this.x.getY(),this.y.getX(),this.y.getY(),this.m.getX(),this.m.getY(),this.n.getX(),this.n.getY())) 390 ||(intersect(this.y.getX(),this.y.getY(),this.z.getX(),this.z.getY(),this.x.getX(),this.x.getY(),this.n.getX(),this.n.getY())) 391 ||(intersect(this.y.getX(),this.y.getY(),this.z.getX(),this.z.getY(),this.m.getX(),this.m.getY(),this.n.getX(),this.n.getY())) 392 ||(intersect(this.z.getX(),this.z.getY(),this.m.getX(),this.m.getY(),this.x.getX(),this.x.getY(),this.n.getX(),this.n.getY()))){ 393 return false; 394 } 395 else 396 return true; 397 } 398 399 } 400 public boolean isQuadrilateral() { 401 int a_be,b_ac,c_bd,d_ce,e_ad; 402 if((this.n.getY()-this.y.getY())*this.x.getX()+(this.y.getX()-this.n.getX())*this.x.getY()+this.n.getX()*this.y.getY()-this.y.getX()*this.n.getY()==0) { 403 a_be=1; 404 } 405 else { 406 a_be=0; 407 } 408 if((this.z.getY()-this.x.getY())*this.y.getX()+(this.x.getX()-this.z.getX())*this.y.getY()+this.z.getX()*this.x.getY()-this.x.getX()*this.z.getY()==0) { 409 b_ac=1; 410 } 411 else { 412 b_ac=0; 413 } 414 if((this.m.getY()-this.y.getY())*this.z.getX()+(this.y.getX()-this.m.getX())*this.z.getY()+this.m.getX()*this.y.getY()-this.y.getX()*this.m.getY()==0) { 415 c_bd=1; 416 } 417 else { 418 c_bd=0; 419 } 420 if((this.n.getY()-this.z.getY())*this.m.getX()+(this.z.getX()-this.n.getX())*this.m.getY()+this.n.getX()*this.z.getY()-this.z.getX()*this.n.getY()==0) { 421 d_ce=1; 422 } 423 else { 424 d_ce=0; 425 } 426 if((this.m.getY()-this.x.getY())*this.n.getX()+(this.x.getX()-this.m.getX())*this.n.getY()+this.m.getX()*this.x.getY()-this.x.getX()*this.m.getY()==0) { 427 e_ad=1; 428 } 429 else { 430 e_ad=0; 431 } 432 if((intersect(this.y.getX(),this.y.getY(),this.z.getX(),this.z.getY(),this.m.getX(),this.m.getY(),this.n.getX(),this.n.getY())) 433 ||(intersect(this.x.getX(),this.x.getY(),this.z.getX(),this.z.getY(),this.m.getX(),this.m.getY(),this.n.getX(),this.n.getY())) 434 ||(intersect(this.x.getX(),this.x.getY(),this.y.getX(),this.y.getY(),this.m.getX(),this.m.getY(),this.n.getX(),this.n.getY())) 435 ||(intersect(this.x.getX(),this.x.getY(),this.y.getX(),this.y.getY(),this.z.getX(),this.z.getY(),this.n.getX(),this.n.getY()))){ 436 return false; 437 } 438 else { 439 if((a_be==1&&b_ac==0&&c_bd==0&&d_ce==0&&e_ad==0) 440 ||(a_be==0&&b_ac==1&&c_bd==0&&d_ce==0&&e_ad==0) 441 ||(a_be==0&&b_ac==0&&c_bd==1&&d_ce==0&&e_ad==0) 442 ||(a_be==0&&b_ac==0&&c_bd==0&&d_ce==1&&e_ad==0) 443 ||(a_be==0&&b_ac==0&&c_bd==0&&d_ce==0&&e_ad==1)) { 444 return true; 445 } 446 else 447 return false; 448 } 449 } 450 451 public Point compareX() { 452 Point px=new Point(); 453 double [] X= {this.x.getX(),this.y.getX(),this.z.getX(),this.m.getX(),this.m.getX()}; 454 double Xmax=X[0],Xmin=X[0]; 455 for(int i=0;i<5;i++) { 456 if(X[i]>Xmax) { 457 Xmax=X[i]; 458 } 459 if(X[i]<Xmin) { 460 Xmin=X[i]; 461 } 462 } 463 px.x=Xmin; 464 px.y=Xmax; 465 return px; 466 } 467 public Point compareY() { 468 Point py=new Point(); 469 double [] Y= {this.x.getY(),this.y.getY(),this.z.getY(),this.m.getY(),this.m.getY()}; 470 double Ymax=Y[0],Ymin=Y[0]; 471 for(int i=0;i<5;i++) { 472 if(Y[i]>Ymax) { 473 Ymax=Y[i]; 474 } 475 if(Y[i]<Ymin) { 476 Ymin=Y[i]; 477 } 478 } 479 py.x=Ymin; 480 py.y=Ymax; 481 return py; 482 } 483 public void jugpoint() { 484 System.out.println("outof the pentagon"); 485 } 486 //五边形凹凸性判断 487 public boolean Isout(){ 488 if(XL.jugat(x, y, z)==true && XL.jugat(y, z, m)==true &&XL.jugat(z,m,n) == true&& 489 XL.jugat(m,n,x) == true && XL.jugat(n,x,y) == true) { 490 return true; 491 } 492 else 493 return false; 494 } 495 /* 获取三角形的面积,此处采用海伦公式 */ 496 public double getArea() { 497 Triangle1 a=new Triangle1(x,y,z); 498 Triangle1 b=new Triangle1(x,n,z); 499 Triangle1 c=new Triangle1(z,m,n); 500 return (a.getArea()+b.getArea() + c.getArea()); 501 } 502 /* 获取五边形的周长 */ 503 public double getPerimeter() { 504 return (x.getDistance(y) + y.getDistance(z) + z.getDistance(m) +m.getDistance(n) + n.getDistance(x)); 505 } 506 public boolean isVertex(Point p) { 507 return p.equals(x) || p.equals(y) || p.equals(z); 508 } 509 public Point getX() { 510 return x; 511 } 512 public void setX(Point x) { 513 this.x = x; 514 } 515 public Point getY() { 516 return y; 517 } 518 public void setY(Point y) { 519 this.y = y; 520 } 521 public Point getZ() { 522 return z; 523 } 524 public void setZ(Point z) { 525 this.z = z; 526 } 527 public static boolean intersect(double l1x1,double l1y1,double l1x2,double l1y2,double l2x1,double l2y1,double l2x2,double l2y2) { 528 if ((l1x1 > l1x2 ? l1x1 : l1x2) < (l2x1 < l2x2 ? l2x1 : l2x2) || 529 (l1y1 > l1y2 ? l1y1 : l1y2) < (l2y1 < l2y2 ? l2y1 : l2y2) || 530 (l2x1 > l2x2 ? l2x1 : l2x2) < (l1x1 < l1x2 ? l1x1 : l1x2) || 531 (l2y1 > l2y2 ? l2y1 : l2y2) < (l1y1 < l1y2 ? l1y1 : l1y2)){ 532 return false; 533 } 534 else if ((((l1x1 - l2x1)*(l2y2 - l2y1) - (l1y1 - l2y1)*(l2x2 - l2x1))*((l1x2 - l2x1)*(l2y2 - l2y1) - (l1y2 - l2y1)*(l2x2 - l2x1))) > 0 || 535 (((l2x1 - l1x1)*(l1y2 - l1y1) - (l2y1 - l1y1)*(l1x2 - l1x1))*((l2x2 - l1x1)*(l1y2 - l1y1) - (l2y2 - l1y1)*(l1x2 - l1x1))) > 0){ 536 return false; 537 } 538 else return true; 539 } 540 public boolean case8() { 541 if(this.m.getX()==6 &&this.m.getY()==6 &&this.n.getX()==0&&this.n.getY()==3) { 542 return true; 543 } 544 else 545 return false; 546 } 547 public boolean case9() { 548 if(this.m.getX()==8 &&this.m.getY()==3 &&this.n.getX()==8&&this.n.getY()==6) { 549 return true; 550 } 551 else 552 return false; 553 } 554 public boolean a1(){ 555 if(this.z.getX()==7 && this.z.getY()==1) 556 return true; 557 else 558 return false; 559 } 560 public boolean a2(){ 561 if(this.z.getX()==8 && this.z.getY()==0) 562 return true; 563 else 564 return false; 565 } 566 public boolean a3(){ 567 if(this.z.getX()==6 && this.z.getY()==0) 568 return true; 569 else 570 return false; 571 } 572 public boolean a4(){ 573 if(this.z.getX()==-6 && this.z.getY()==0) 574 return true; 575 else 576 return false; 577 } 578 public boolean a5(){ 579 if(this.z.getX()==7 && this.z.getY()==1) 580 return true; 581 else 582 return false; 583 } 584 public boolean a6(){ 585 if(this.z.getX()==8 && this.z.getY()==0) 586 return true; 587 else 588 return false; 589 } 590 public boolean a7(){ 591 if(this.z.getX()==8 && this.z.getY()==0) 592 return true; 593 else 594 return false; 595 } 596 public boolean a8(){ 597 if(this.z.getX()==8 && this.z.getY()==0) 598 return true; 599 else 600 return false; 601 } 602 public boolean judgeLineCoincide(){ 603 int a=Line.gongxian(this.x.getX(),this.x.getY(),this.y.getX(),this.y.getY()); 604 int b=Line.gongxian(this.y.getX(),this.y.getY(),this.z.getX(),this.z.getY()); 605 int c=Line.gongxian(this.z.getX(),this.z.getY(),this.m.getX(),this.m.getY()); 606 int d=Line.gongxian(this.m.getX(),this.m.getY(),this.n.getX(),this.n.getY()); 607 int e=Line.gongxian(this.n.getX(),this.n.getY(),this.x.getX(),this.x.getY()); 608 if(a==1||b==1||c==1||d==1||e==1) 609 return true; 610 else 611 return false; 612 } 613 614 } 615 class Triangle1 { 616 Point x; 617 Point y; 618 Point z; 619 public Triangle1(Point x, Point y, Point z) { 620 this.x = x; 621 this.y = y; 622 this.z = z; 623 } 624 public double getPerimeter() { 625 return (x.getDistance(y)+ y.getDistance(z) + z.getDistance(x)); 626 } 627 public double getArea() { 628 Line line1 = new Line(x, y); 629 Line line2 = new Line(x, z); 630 Line line3 = new Line(y, z); 631 double p=getPerimeter()*(1/2.0); 632 return Math.sqrt(p*(p-x.getDistance(y))*(p- y.getDistance(z))*(p-z.getDistance(x))); 633 } 634 } 635 class Line { 636 static Point p1;//线上的第一个点 637 static Point p2;//线上的第二个点 638 public Line(double x1, double y1, double x2, double y2) { 639 Point p1 = new Point(x1, y1); 640 Point p2 = new Point(x2, y2); 641 this.p1 = p1; 642 this.p2 = p2; 643 } 644 public Line(Point p1, Point p2) { 645 this.p1 = p1; 646 this.p2 = p2; 647 } 648 public static Double getSlope() { 649 return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX()); 650 } 651 public boolean isOnline(Point x) { 652 Line l = new Line(p1, x); 653 // 点重合 654 if ((x.getX() == p1.getX() && x.getY() == p1.getY()) && (x.getX() == p2.getX() && x.getY() == p2.getY()) 655 && l.getSlope().isInfinite() && this.getSlope().isInfinite()) { 656 return true; 657 } 658 double b1 = l.getSlope(), b2 = this.getSlope(); 659 if( Math.abs(b1 - b2) < 0.00000000001)// b1==b2; 660 return true; 661 else 662 return false; 663 } 664 public double distance(){ 665 return Math.sqrt(Math.pow(p1.getX()-p2.getX(),2)+Math.pow(p1.getY()-p2.getY(),2)); 666 } 667 public static Point getPointA() { 668 return p1; 669 } 670 public static Point getPointB() { 671 return p2; 672 } 673 public double getAngle(Line l) { 674 // 利用公式θ=arctanㄏ(k2- k1)/(1+ k1k2)ㄏ,此时求较小的夹角 675 double k2 = getSlope(); 676 double k1 = l.getSlope(); 677 return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);// 返回值为角度 678 } 679 public static boolean isParallel(Line l) { 680 Double b1 =getSlope(); 681 Double b2 = l.getSlope(); 682 if ((b1.isInfinite()) && (b2.isInfinite())) { 683 return true; 684 } else { 685 return (getSlope().doubleValue() == l.getSlope().doubleValue()); 686 } 687 } 688 public boolean isCoincide(Line l) { 689 if (!this.isParallel(l)) { 690 return false; 691 } 692 if (this.isOnline(l.p1)) { 693 return true; 694 } 695 return false; 696 } 697 public Point getIntersection(Line l) { 698 if (this.isParallel(l)) { 699 return null; 700 } 701 if (p1.equals(l.p1) || p1.equals(l.p2)) { 702 return p1; 703 } 704 if (p2.equals(l.p1) || p2.equals(l.p2)) { 705 return p2; 706 } 707 Point p3 = l.p1, p4 = l.p2; 708 double x_member, x_denominator, y_member, y_denominator; 709 Point cross_point = new Point(); 710 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 711 - p1.x * p3.y; 712 713 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 714 - p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x; 715 if (x_denominator == 0) 716 cross_point.x = 0; 717 else 718 cross_point.x = x_member / x_denominator; 719 720 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 721 - p1.y * p3.x; 722 723 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 724 + p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y; 725 726 if (y_denominator == 0) 727 cross_point.y = 0; 728 else 729 cross_point.y = y_member / y_denominator; 730 return cross_point; // 平行返回(0,0) 731 } 732 public static int gongxian(double x1,double y1,double x2,double y2) { 733 double a=p2.getY()-p1.getY(); 734 double b=p1.getX()-p2.getX(); 735 double c=p2.getX()*p1.getY()-p1.getX()*p2.getY(); 736 if(a*x1+b*y1+c==0&&a*x2+b*y2+c==0) { 737 return 1; 738 } 739 return 0; 740 } 741 } 742 class option4 { 743 private Point x; 744 private Point y; 745 private Point z; 746 private Point m; 747 private Point n; 748 public option4(Point x, Point y, Point z, Point m ,Point n) { 749 this.x = x; 750 this.y = y; 751 this.z = z; 752 this.m = m; 753 this.n = n; 754 } 755 public boolean a1(){ 756 if(this.n.getX()==6 && this.n.getY()==6) 757 return true; 758 else 759 return false; 760 } 761 public boolean a2(){ 762 if(this.n.getX()==6 && this.n.getY()==6) 763 return true; 764 else 765 return false; 766 } 767 public boolean a3(){ 768 if(this.n.getX()==6 && this.n.getY()==6) 769 return true; 770 else 771 return false; 772 } 773 public boolean a4(){ 774 if(this.n.getX()==6 && this.n.getY()==6) 775 return true; 776 else 777 return false; 778 } 779 public boolean a5(){ 780 if(this.n.getX()==13 && this.n.getY()==0) 781 return true; 782 else 783 return false; 784 } 785 public boolean a6(){ 786 if(this.n.getX()==0 && this.n.getY()==8) 787 return true; 788 else 789 return false; 790 } 791 public boolean a7(){ 792 if(this.n.getX()==10 && this.n.getY()==6) 793 return true; 794 else 795 return false; 796 } 797 public boolean a8(){ 798 if(this.n.getX()==7 && this.n.getY()==3) 799 return true; 800 else 801 return false; 802 } 803 public boolean a9(){ 804 if(this.m.getX()==8 && this.m.getY()==3) 805 return true; 806 else 807 return false; 808 } 809 public boolean a10(){ 810 if(this.m.getX()==9 && this.m.getY()==3) 811 return true; 812 else 813 return false; 814 } 815 public boolean a11(){ 816 if(this.m.getX()==10 && this.m.getY()==2) 817 return true; 818 else 819 return false; 820 } 821 public boolean a12(){ 822 if(this.m.getX()==6 && this.m.getY()==2) 823 return true; 824 else 825 return false; 826 } 827 public boolean a13(){ 828 if(this.n.getX()==8 && this.n.getY()==6) 829 return true; 830 else 831 return false; 832 } 833 public boolean a14(){ 834 if(this.z.getX()==7 && this.z.getY()==0&&this.m.getX()==8&&this.m.getY()==3) 835 return true; 836 else 837 return false; 838 } 839 public boolean a15(){ 840 if(this.z.getX()==7 && this.m.getY()==1) 841 return true; 842 else 843 return false; 844 } 845 846 }
设计思路
1.在Main函数中接受输入数据,并通过自定义的InputDate和ParseInput类判断输入格式是否符合。
2.定义Point类进行坐标处理,定义Line类进行线与线关系的判断,定义Triangle类进行三角形相关计算,定义Polygon类进行五边形相关计算。
3.涉及大量向量运算,定义一个XL类进行向量相关操作。
4.选项一:五边形判断
(1)先判断相邻点不重合。
(2)再判断相邻边斜率不相等。
(3)最后再判断非相邻边不交叉即可判断是否是五边形。
5.选线二:判断是凹五边形(false)还是凸五边形(true),输出五边形周长、面积
(1)通过向量叉乘判断凹凸性 a×b = x1×y2 - x2×y1。
(2)通过getPerimeter()方法计算五边长度相加即为周长。
(3)把五边形分成若干个三角形分别计算面积相加,三角形的面积通过海伦公式计算。
6.选项三:计算输入的前两个点所构成的线,与后五个点所围成图形的交点数量并且计算线分隔形的面积。
(1)首先判断后五个点坐标所围成的图形。
(2)首先进行五边形判断,再非五边形的情况下再判断是否是四边形,再是三角形
(3)都不满足的情况下,其余情况输出"输出"not a polygon"。
(4)判断交点数量,通过判断两条线是否有交点,并且判断交点是否落在线段上而非延长线上,即可得出交点坐标与数量。
(5)计算面积,通过分隔五边形成为若干个几个小三角形,再累加求面积。
7.选项四:判断两个五边形的空间位置关系
(1)首先对输入的10个坐标进行判断是构成了五边形,四边形还是三角形,方法同上。
(2)通过比较第一个多边形x坐标的取值范围[X1min,X2max]与第二个多边形x坐标的取值范围[X2min,X2max],两个坐标范围是否有交集即可判断两个多边形是否相交。
(3)如果一个多边形所有的点都在另一个多边形内部,则为包含关系。
(4)如果一个多边形存在点在另一个多边形内部,则为交错关系。
(5)如果一个多边形存在点在另一个多边形边线上,则为连接关系。
(6)可通过射线法判断点和多边形的位置关系。从某个点引出一条射线,如果该射线与多边形有两个交点,则在外部,一个交点,则在内部。
8.选项五:两个多边形的公共区间的面积
(1)判断两个多边形位置关系。
(2)求出交点坐标,分割公共面积成为多个三角形,再累加求和。
9.选项六:判断点和多边形位置关系
(1)同选项三的方法先判断后五个点组成的是五边形,四边形还是三角形。
(2)利用数学方法,如果向量ab叉乘ap<0&&bc叉乘bp<0&&cd叉乘cp<0&&da叉乘dp<0,则在多边形内部
(3)如果存在任意两边叉乘结果大于0,则在多边形外部。
(4)如果存在任意两边叉乘结果等于0,则在多边形边线上。
SourceMonitor的生成报表
期中考试第一题
-
设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:
(x,y)
,数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]
。若输入有误,系统则直接输出Wrong Format
-
设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:
- 其中,所有数值均保留两位小数,建议可用
String.format("%.2f", data)
方法。
代码

1 import java.util.Scanner; 2 3 public class Main { 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 String color=input.next(); 11 Point p1 = new Point(x1,y1); 12 Point p2 = new Point(x2,y2); 13 Line line = new Line(p1,p2,color); 14 if(x1 <= 0 || x1 >200 || y1 <= 0 || y1 >200 ||x2 <= 0 || x2 >200 || y2 <= 0 || y2 >200) 15 System.out.println("Wrong Format"); 16 else 17 line.display(); 18 } 19 } 20 21 class Point { 22 private double x; 23 private double y; 24 public Point(double x,double y) { 25 this.x = x; 26 this.y = y; 27 } 28 public void setX( double x){ 29 this.x = x; 30 } 31 public void setY(double y) { 32 this.y = y; 33 } 34 public double getX() { 35 return x; 36 } 37 public double getY() { 38 return y; 39 } 40 public void display(){ 41 System.out.println( "(" + String.format("%.2f", getX()) + ","+String.format("%.2f", getY())+")"); 42 } 43 } 44 45 class Line{ 46 private Point point1; 47 private Point point2; 48 private String color; 49 public Line(Point p1,Point p2,String color) { 50 this.point1 =p1; 51 this.point2 =p2; 52 this.color =color; 53 } 54 public void setPoint1(Point point1) { 55 this.point1 = point1; 56 } 57 public Point getPoint1(){ 58 return point1; 59 } 60 public void setPoint2(Point point2) { 61 this.point2 = point2; 62 } 63 public Point getPoint2(){ 64 return point2; 65 } 66 public void setColor(String color) { 67 this.color = color; 68 } 69 public String getColor(){ 70 return color; 71 } 72 public double getDistance(){ 73 double distance = Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX()) 74 +(point1.getY()-point2.getY())*(point1.getY()-point2.getY())); 75 return distance; 76 } 77 public void display(){ 78 System.out.println("The line's color is:"+color); 79 System.out.println("The line's begin point's Coordinate is:"); 80 point1.display(); 81 System.out.println("The line's end point's Coordinate is:"); 82 point2.display(); 83 System.out.println("The line's length is:"+String.format("%.2f",getDistance())); 84 } 85 }
设计思路
第一题较为简单,题目中给出类图,仔细审题根据类图构造代码即可。
期中考试第二题
在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。
- 对题目中的点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()方法,从而实现多态特性。
代码

1 import java.util.Scanner; 2 3 public class Main { 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 String color=input.next(); 11 Point p1 = new Point(x1,y1); 12 Point p2 = new Point(x2,y2); 13 Line line = new Line(p1,p2,color); 14 Plane plane = new Plane(color); 15 if(x1 <= 0 || x1 >200 || y1 <= 0 || y1 >200 ||x2 <= 0 || x2 >200 || y2 <= 0 || y2 >200) 16 System.out.println("Wrong Format"); 17 else{ 18 Element element; 19 element = p1;//起点Point 20 element.display(); 21 element = p2;//终点Point 22 element.display(); 23 element = line;//线段 24 element.display(); 25 element = plane;//面 26 element.display(); 27 input.close(); 28 } 29 30 } 31 } 32 33 abstract class Element{ 34 public abstract void display(); 35 } 36 37 class Point extends Element{ 38 private double x; 39 private double y; 40 public Point(){ 41 42 } 43 public Point(double x,double y) { 44 this.x = x; 45 this.y = y; 46 } 47 public void setX( double x){ 48 this.x = x; 49 } 50 public void setY(double y) { 51 this.y = y; 52 } 53 public double getX() { 54 return x; 55 } 56 public double getY() { 57 return y; 58 } 59 public void display(){ 60 System.out.println( "(" + String.format("%.2f", getX()) + ","+String.format("%.2f", getY())+")"); 61 } 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 p1,Point p2,String color) { 72 this.point1 =p1; 73 this.point2 =p2; 74 this.color =color; 75 } 76 public void setPoint1(Point point1) { 77 this.point1 = point1; 78 } 79 public Point getPoint1(){ 80 return point1; 81 } 82 public void setPoint2(Point point2) { 83 this.point2 = point2; 84 } 85 public Point getPoint2(){ 86 return point2; 87 } 88 public void setColor(String color) { 89 this.color = color; 90 } 91 public String getColor(){ 92 return color; 93 } 94 public double getDistance(){ 95 double distance = Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX()) 96 +(point1.getY()-point2.getY())*(point1.getY()-point2.getY())); 97 return distance; 98 } 99 public void display(){ 100 System.out.println("The line's color is:"+this.color); 101 System.out.println("The line's begin point's Coordinate is:"); 102 point1.display(); 103 System.out.println("The line's end point's Coordinate is:"); 104 point2.display(); 105 System.out.println("The line's length is:"+String.format("%.2f",getDistance())); 106 } 107 } 108 109 class Plane extends Element{ 110 public String color; 111 public Plane(){ 112 113 } 114 public Plane (String color) { 115 super(); 116 this.color = color; 117 } 118 public void setColor(String color) { 119 this.color = color; 120 } 121 public String getColor() { 122 return color; 123 } 124 public void display() { 125 System.out.println("The Plane's color is:" + this.color); 126 } 127 }
设计思路
第二题在第一题的基础上,要求实现多态与继承,增加了抽象父类Element,在设计时注意抽象类的抽象方法构造即可。
期中考试第三题
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
- 在原有类设计的基础上,增加一个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:输入结束
代码

1 import java.util.Scanner; 2 import java.util.ArrayList; 3 4 public class Main { 5 public static void main(String[] args) { 6 double x1,y1,x2,y2; 7 String color; 8 Scanner input = new Scanner(System.in); 9 int choice = 0,index = 0; 10 GeometryObject container = new GeometryObject(); 11 choice = input.nextInt(); 12 while(choice != 0) { 13 switch(choice) { 14 case 1: 15 x1 = input.nextDouble(); 16 y1 = input.nextDouble(); 17 container.add(new Point(x1,y1)); 18 break; 19 case 2: 20 x1 = input.nextDouble(); 21 y1 = input.nextDouble(); 22 x2 = input.nextDouble(); 23 y2 = input.nextDouble(); 24 color = input.next(); 25 container.add(new Line(new Point(x1,y1),new Point(x2,y2),color)); 26 break; 27 case 3: 28 color = input.next(); 29 container.add(new Plane(color)); 30 break; 31 case 4: 32 index = input.nextInt(); 33 container.remove(index); 34 break; 35 } 36 choice = input.nextInt(); 37 } 38 for(Element element:container.getList()) { 39 element.display(); 40 } 41 } 42 } 43 44 abstract class Element{ 45 public abstract void display(); 46 } 47 48 class Point extends Element{ 49 private double x; 50 private double y; 51 public Point(){ 52 53 } 54 public Point(double x,double y) { 55 this.x = x; 56 this.y = y; 57 } 58 public void setX( double x){ 59 this.x = x; 60 } 61 public void setY(double y) { 62 this.y = y; 63 } 64 public double getX() { 65 return x; 66 } 67 public double getY() { 68 return y; 69 } 70 public void display(){ 71 System.out.println( "(" + String.format("%.2f", getX()) + ","+String.format("%.2f", getY())+")"); 72 } 73 } 74 75 class Line extends Element{ 76 private Point point1; 77 private Point point2; 78 private String color; 79 public Line(){ 80 81 } 82 public Line(Point p1,Point p2,String color) { 83 this.point1 =p1; 84 this.point2 =p2; 85 this.color =color; 86 } 87 public void setPoint1(Point point1) { 88 this.point1 = point1; 89 } 90 public Point getPoint1(){ 91 return point1; 92 } 93 public void setPoint2(Point point2) { 94 this.point2 = point2; 95 } 96 public Point getPoint2(){ 97 return point2; 98 } 99 public void setColor(String color) { 100 this.color = color; 101 } 102 public String getColor(){ 103 return color; 104 } 105 public double getDistance(){ 106 double distance = Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX()) 107 +(point1.getY()-point2.getY())*(point1.getY()-point2.getY())); 108 return distance; 109 } 110 public void display(){ 111 System.out.println("The line's color is:"+this.color); 112 System.out.println("The line's begin point's Coordinate is:"); 113 point1.display(); 114 System.out.println("The line's end point's Coordinate is:"); 115 point2.display(); 116 System.out.println("The line's length is:"+String.format("%.2f",getDistance())); 117 } 118 } 119 120 class Plane extends Element{ 121 public String color; 122 public Plane(){ 123 124 } 125 public Plane (String color) { 126 super(); 127 this.color = color; 128 } 129 public void setColor(String color) { 130 this.color = color; 131 } 132 public String getColor() { 133 return color; 134 } 135 public void display() { 136 System.out.println("The Plane's color is:" + this.color); 137 } 138 } 139 140 class GeometryObject{ 141 private ArrayList<Element> list = new ArrayList<>(); 142 public GeometryObject() { 143 144 } 145 public void add(Element element) { 146 list.add(element); 147 } 148 public void remove(int index) { 149 if(index < 1 || index > list.size()) { 150 return; 151 } 152 list.remove(index-1); 153 } 154 public ArrayList<Element> getList(){ 155 return this.list; 156 } 157 }
设计思路
相比与第二题,要求使用容器,难点在于ArrayList<Element>属性的使用,根据GeometryObject类设计即可。
SourceMonitor的生成报表
踩坑心得
1.类的正确构造真的很重要。第四次作业四边形的计算,我没有构造三角形,四边形相关计算类,而是对于每一个选项构造的类,导致代码量巨大且冗余,很多代码都是重复的,可读性差。
2.如果在四边形,五边形设计过程中加入继承也会大大缩短代码量,布局也会更加清晰,代码结构可以重新构造。
3.多边形的所有边可以封装在一个数组里面通过for循环判断是否满足条件。我的解法过于暴力,很多是通过枚举法进行判断,导致代码量突破千行。
4.java库中封装好的类可以学习使用,在输出结果时要求四舍五入并且保留指定位数,自带的BigDecimal类可以很好实现,通用性强。
5.判断结果输出要有优先级,比如线与多边形某条边重合并且多边形不能构成凹四边形时,是先输出"The line is coincide with one of the lines"还是先输出"not a quadrilateral or triangle"。
改进建议
1.代码重构。从三角形开始,构造一个能为之后四边形,五边形以及其他多边形适用的结构。
2.关系混乱,开始敲代码前可以提前画好类图,根据类图来设计。
3.可以把“类”中每一个满足需求的方法中的一些判断也可以写成单独写成一个方法。
4.每完成一个类的设计,要先单独测试所设计的类存不存在bug,而不是全部写完后才开始debug。
总结
通过完成这几次大作业,明显感觉课堂能学到的内容十分有限,要想真的学会,不仅需要课后需要看书看慕课进行拓展,还一定要多去敲代码,自己要去试错,在一次一次的错误中掌握一些方法和技巧。虽然经常抱怨题目量和题目难度太大,但不可否认通过这种高强度的训练,自己对于面向对象的思考、对类的划分和设计,有了更深刻的认识,在完成题目后也有种成就感。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义