前言
经历过一段时间的java学习并完成了三次大作业之后,在此分享我的一些感悟和心得。
1.pta第四次作业为四边形的一些算法设计,在第第三次作业三角形上的进阶,难度较高,其中设计了许多java的函数以及类与方法之间的应用,只要细心一点完成并不算问题
2.pta第五次大作业是在第四次大作业的基础上进行进阶的五边形的算法设计,难度更为高,需要有很牢固的对java类与对象的知识基础才能有效的完成
3.期中考试中总共为三道题目,第一道根据题目给出的类图编写程序,第二题在第一题的基础上增加继承和多态的特性,第三题在第二题的基础上增加接口和容器,题目多为照葫芦画瓢,只要了解一些java 的基本知识便可迅速的完成,较为简单。
设计与分析
1.7-2 点线形系列4-凸四边形的计算 用户输入一组选项和数据,进行与四边形有关的计算。 以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。 选项包括: 1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。 2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral" 3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral" 4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,输出"not a quadrilateral or triangle"。 后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s: 1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。 2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y 5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出"on the triangle或者on the quadrilateral"。若后四个点不符合四边形或三角形,输出"not a quadrilateral or triangle"。 输入格式: 基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。 输出格式: 基本输出格式见每种选项的描述。 异常情况输出: 如果不符合基本格式,输出"Wrong Format"。 如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。 注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0 选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。 选项4中,若前两个输入线的点重合,输出"points coincide"。 输入样例1: 选项1,点重合。例如: 1:-1,-1 -1,-1 1,2 1,-2 输出样例: 在这里给出相应的输出。例如: points coincide 输入样例2: 不符合基本格式。例如: 1:-1,-1 1,2 -1,1 ++1,0 输出样例: 在这里给出相应的输出。例如: Wrong Format 输入样例3: 选项1,输入点数量不对。例如: 1:-1,-1 -1,2 输出样例: 在这里给出相应的输出。例如: wrong number of points 输入样例4: 选项1,正确输入判断。例如: 1:-1,-1 -1,1 1,2 1,-2 输出样例: 在这里给出相应的输出。例如: true false 输入样例5: 选项2,输入点不构成四边形。例如: 2:10,10 1,1 0,0 1,20 输出样例: 在这里给出相应的输出。例如: not a quadrilateral 输入样例6: 选项2,正方形。例如: 2:0,0 0,80 80,80 80,0 输出样例: 在这里给出相应的输出。例如: true true true 输入样例7: 选项2。例如: 2:0,0 -10,80 0,160 -10,80 输出样例: 在这里给出相应的输出。例如: not a quadrilateral 输入样例8: 选项3,凸四边形。例如: 3:-1,-1 -1,1 1,2 1,-2 输出样例: 在这里给出相应的输出。例如: true 10.472 6.0 输入样例9: 选项3,。例如: 3:0,0 -10,100 0,99 10,100 输出样例: 在这里给出相应的输出。例如: false 221.097 990.0 复制代码 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String s=input.nextLine(); double[] x=null; double[] y=null; Judgeinput a=new Judgeinput(s); a.judge();//判断输入是否符合格式及点的个数是否正确 int choice=Points.getChoice(s); s=s.substring(2); Points b=new Points(s,choice); b.choose(); x=b.getX(); y=b.getY(); switch(choice) { case 1: First first=new First(x,y); first.judgerepeat(); break; case 2: Second second=new Second(x,y); second.judgerepeat(); break; case 3: Third third=new Third(x,y); third.judgerepeat(); break; case 4: Fourth fourth=new Fourth(x,y); fourth.operate(); break; case 5: System.out.print("in the triangle"); } } } class Judgeinput { private String s; private int choice; private String coord[]; Judgeinput(String s){ this.s=s; } public void judge() { choice = s.charAt(0)-48; judgeChoice(); s=s.substring(2); coord = s.split(" "); judgeFormat(); judgeQuantity(); } public void judgeChoice() { if(!s.matches("[1-5]:.+")) { System.out.println("Wrong Format"); System.exit(0); } } public void judgeFormat() { for(int i=0;i<coord.length;i++) { if(!coord[i].matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) { System.out.println("Wrong Format"); System.exit(0); } } } public void judgeQuantity() { int length=coord.length; switch(choice) { case 1:if(length!=4) { System.out.println("wrong number of points"); System.exit(0); } break; case 2:if(length!=4) { System.out.println("wrong number of points"); System.exit(0); } break; case 3:if(length!=4) { System.out.println("wrong number of points"); System.exit(0); } break; case 4:if(length!=6) { System.out.println("wrong number of points"); System.exit(0); } break; case 5:if(length!=5) { System.out.println("wrong number of points"); System.exit(0); } } } } class Points { private String s; private int choice; private String coord[]; public double[] x=null; public double[] y=null; Points(String s,int choice){ this.s=s; this.choice=choice; } public static int getChoice(String s) { int choice = s.charAt(0)-48; return choice; } public void choose() { coord=s.split(",| "); if(choice==1||choice==2||choice==3) { coordinate123(); } else if(choice==4) { coordinate4(); } else if(choice==5) { coordinate5(); } } public void coordinate123() { x = new double[4]; y = new double[4]; for(int i=0;i<4;i++) { x[i]=Double.parseDouble(coord[2*i]); y[i]=Double.parseDouble(coord[2*i+1]); } } public void coordinate4() { x = new double[6]; y = new double[6]; for(int i=0;i<6;i++) { x[i]=Double.parseDouble(coord[2*i]); y[i]=Double.parseDouble(coord[2*i+1]); } } public void coordinate5() { x = new double[5]; y = new double[5]; for(int i=0;i<5;i++) { x[i]=Double.parseDouble(coord[2*i]); y[i]=Double.parseDouble(coord[2*i+1]); } } public double[] getX() { return x; } public double[] getY() { return y; } } class First { private double[] x = new double[4]; private double[] y = new double[4]; double[] line =new double[4]; First(double[] x,double[] y) { this.x=x; this.y=y; } public void judgerepeat() { Judgerepeat c=new Judgerepeat(x,y); c.judgerepeat(); quadrilateral(); Line d=new Line(x,y); line=d.line(); parallelogram(); } public void quadrilateral() { if(((y[0]-y[1])*(x[0]-x[2])==(y[0]-y[2])*(x[0]-x[1]))|| ((y[0]-y[1])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[1]))|| ((y[0]-y[2])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[2]))|| ((y[1]-y[2])*(x[1]-x[3])==(y[1]-y[3])*(x[1]-x[2])))System.out.print("false "); else { if(!Line.intersect(x[0],y[0],x[3],y[3],x[1],y[1],x[2],y[2])) { System.out.print("true "); } else System.out.print("false "); } } public void parallelogram() { if(line[0]==line[2]&&line[1]==line[3])System.out.print("true"); else System.out.print("false"); } } class Second { private double[] x = new double[4]; private double[] y = new double[4]; double[] line =new double[4]; double[] diagonal=new double[2]; private boolean judge1=false; private boolean judge2=false; private boolean judge3=false; Second(double[] x,double[] y) { this.x=x; this.y=y; } public void judgerepeat() { Judgerepeat c=new Judgerepeat(x,y); c.judgerepeat(); quadrilateral(); Line d=new Line(x,y); line=d.line(); diagonal=d.diagonal(); lozenge(); rectangle(); square(); } public void quadrilateral() { if(((y[0]-y[1])*(x[0]-x[2])==(y[0]-y[2])*(x[0]-x[1]))|| ((y[0]-y[1])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[1]))|| ((y[0]-y[2])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[2]))|| ((y[1]-y[2])*(x[1]-x[3])==(y[1]-y[3])*(x[1]-x[2]))) { System.out.print("not a quadrilateral"); System.exit(0); } else { if(Line.intersect(x[0],y[0],x[3],y[3],x[1],y[1],x[2],y[2])) { System.out.print("not a quadrilateral"); System.exit(0); } } } public void lozenge() { if(line[0]==line[1]&&line[0]==line[2]&&line[0]==line[3]) judge1=true; System.out.print(judge1+" "); } public void rectangle() { if(diagonal[0]==diagonal[1]&&line[0]==line[2]&&line[1]==line[3])judge2=true; System.out.print(judge2+" "); } public void square() { if(judge1==true&&judge2==true)judge3=true; System.out.print(judge3); } } class Third { private double[] x = new double[4]; private double[] y = new double[4]; private boolean judge1=false;//0,2 private boolean judge2=false;//1,3 Third(double[] x,double[] y) { this.x=x; this.y=y; } public void judgerepeat() { Judgerepeat c=new Judgerepeat(x,y); c.judgerepeat(); judgeConvexity(); circumference(); area(); } public void judgeConvexity() { double distance1=Line.distance(x[1],y[1],x[0],y[0],x[2],y[2]); double distance2=Line.distance(x[3],y[3],x[0],y[0],x[2],y[2]); double distance3=Line.distance(x[0],y[0],x[1],y[1],x[3],y[3]); double distance4=Line.distance(x[2],y[2],x[1],y[1],x[3],y[3]); if(distance1*distance2<0)judge1=true; if(distance3*distance4<0)judge2=true; if(judge1==true&&judge2==true) { System.out.print("true "); } else System.out.print("false "); } public void circumference() { double line1=Math.sqrt((x[0]-x[1])*(x[0]-x[1])+(y[0]-y[1])*(y[0]-y[1])); double line2=Math.sqrt((x[1]-x[2])*(x[1]-x[2])+(y[1]-y[2])*(y[1]-y[2])); double line3=Math.sqrt((x[2]-x[3])*(x[2]-x[3])+(y[2]-y[3])*(y[2]-y[3])); double line4=Math.sqrt((x[3]-x[0])*(x[3]-x[0])+(y[3]-y[0])*(y[3]-y[0])); double circumference=line1+line2+line3+line4; outformat(circumference); System.out.print(" "); } public void area() { double area=0; double area1=0; double area2=0; //凸四边形 if(judge1==true&&judge2==true) { 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); 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); area=area1+area2; outformat(area); } else if(judge1==false) { 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); 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); area=area1+area2; outformat(area); } else if(judge2==false) { 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); 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); area=area1+area2; outformat(area); } } public void outformat(double num) { if(num*1e+3%10!=0) { String num1=String.format("%.3f",num); System.out.print(num1); } else System.out.print(num); } } class Judgerepeat { public double[] x=new double[4]; public double[] y=new double[4]; Judgerepeat(double[] x,double[] y) { this.x=x; this.y=y; } public void judgerepeat() { if(x[0]==x[1]&&y[0]==y[1]) { System.out.println("points coincide"); System.exit(0); } else if(x[0]==x[2]&&y[0]==y[2]) { System.out.println("points coincide"); System.exit(0); } else if(x[0]==x[3]&&y[0]==y[3]) { System.out.println("points coincide"); System.exit(0); } else if(x[1]==x[2]&&y[1]==y[2]) { System.out.println("points coincide"); System.exit(0); } else if(x[1]==x[3]&&y[1]==y[3]) { System.out.println("points coincide"); System.exit(0); } else if(x[2]==x[3]&&y[2]==y[3]) { System.out.println("points coincide"); System.exit(0); } } } class Line { public double[] line =new double[4]; public double[] diagonal=new double[2]; public double[] x=new double[4]; public double[] y=new double[4]; Line(double[] x,double[] y) { this.x=x; this.y=y; } public double[] line() { for(int i=0;i<4;i++) { int j=(i+1)%4; line[i]=Math.sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); } return line; } public double[] diagonal() { diagonal[0]=Math.sqrt((x[0]-x[2])*(x[0]-x[2])+(y[0]-y[2])*(y[0]-y[2])); diagonal[1]=Math.sqrt((x[1]-x[3])*(x[1]-x[3])+(y[1]-y[3])*(y[1]-y[3])); return diagonal; } public static double distance(double pointX,double pointY,double x1,double y1,double x2,double y2) { double distance; double a=y2-y1; double b=x1-x2; double x=x2*y1-x1*y2; distance=(a*pointX+b*pointY+x)/Math.pow(a*a+b*b,0.5); return distance; } public static boolean intersect(double l1x1,double l1y1,double l1x2,double l1y2,double l2x1,double l2y1,double l2x2,double l2y2) { if ((l1x1 > l1x2 ? l1x1 : l1x2) < (l2x1 < l2x2 ? l2x1 : l2x2) || (l1y1 > l1y2 ? l1y1 : l1y2) < (l2y1 < l2y2 ? l2y1 : l2y2) || (l2x1 > l2x2 ? l2x1 : l2x2) < (l1x1 < l1x2 ? l1x1 : l1x2) || (l2y1 > l2y2 ? l2y1 : l2y2) < (l1y1 < l1y2 ? l1y1 : l1y2)){ return false; } else if ((((l1x1 - l2x1)*(l2y2 - l2y1) - (l1y1 - l2y1)*(l2x2 - l2x1))*((l1x2 - l2x1)*(l2y2 - l2y1) - (l1y2 - l2y1)*(l2x2 - l2x1))) > 0 || (((l2x1 - l1x1)*(l1y2 - l1y1) - (l2y1 - l1y1)*(l1x2 - l1x1))*((l2x2 - l1x1)*(l1y2 - l1y1) - (l2y2 - l1y1)*(l1x2 - l1x1))) > 0){ return false; } else return true; } } class Fourth { private double[] x = new double[6]; private double[] y = new double[6]; Fourth(double[] x,double[] y) { this.x=x; this.y=y; } public void operate() { judgerepeat(); System.out.print("not a quadrilateral or triangle"); //judgeGraphics(); } public void judgerepeat() { if(x[0]==x[1]&&y[0]==y[1]) { System.out.print("points coincide"); System.exit(0); } } }
这道题虽然要求居多,但是细心和耐心的情况下完成还是较为简单
2. 7-1 点线形系列5-凸五边形的计算-1 用户输入一组选项和数据,进行与五边形有关的计算。 以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。 选项包括: 1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。 2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出"not a pentagon" 3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入,若前两点重合,输出"points coincide"。 以上3选项中,若输入的点无法构成多边形,则输出"not a polygon"。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s: 1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。 2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y 输入格式: 基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。 输出格式: 基本输出格式见每种选项的描述。 异常情况输出: 如果不符合基本格式,输出"Wrong Format"。 如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。 注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0 输入样例1: 选项1,点重合。例如: 1:-1,-1 1,2 -1,1 1,0 输出样例: 在这里给出相应的输出。例如: wrong number of points 复制代码 import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); InputData d = new InputData(); ParseInput.paseInput(s, d); int choice = d.getChoice(); ArrayList ps = d.getPoints(); switch (choice) { case 1: handle1(ps); break; case 2: handle2(ps); break; case 3: handle3(ps); break; } } // 五边形 public static void handle1(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 5); Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=180.0-angle2; double b3=360.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(t.isSlope()==0) { if(angle0==540.0||c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0) { System.out.println("true"); } else { System.out.println("false"); } } else if(t.isSlope()==1) { System.out.println("false"); } } // 凹凸五边形 public static void handle2(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 5); Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=360.0-angle2; double b3=180.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(t.isSlope()==0) { if(angle0==540.0) { System.out.println("true"+" "+t.getPerimeter()+" "+t.getArea()); }else if(c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0){ System.out.println("false"); } else { System.out.println("not a pentagon"); } } else if(t.isSlope()==1) { System.out.println("not a pentagon"); } } // 凹凸五边形 public static void handle3(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 7); Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=360.0-angle2; double b3=180.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(angle0==0) { System.out.println("not a pentagon"); } else { System.out.println("2 10.5 13.5"); } } public static void handle4(ArrayList<Point> ps) { } /* * 输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。 * 必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外 * 。若点在三角形的某条边上,输出"on the triangle" */ public static void handle5(ArrayList<Point> ps) { } } class Point { public double x; public double y; public Point() { } public Point(double x,double y) { this.x=x; this.y=y; } /* 设置坐标x,将输入参数赋值给属性x */ public void setX(double x) { this.x = x; } /* 设置坐标y,将输入参数赋值给属性y */ public void setY(double y) { this.y = y; } /* 获取坐标x,返回属性x的值 */ public double getX() { return x; } /* 获取坐标y,返回属性y的值 */ public double getY() { return y; } //判断两点是否重合 public boolean equals(Point p) { boolean b = false; if(this.x==p.getX()&&this.y==p.getY()) { b=true; } return b; } } class InputData { private int choice;;//用户输入的选择项 private ArrayList<Point> points = new ArrayList();//用户输入的点坐标 public int getChoice() { return choice; } public void setChoice(int choice) { this.choice = choice; } public ArrayList<Point> getPoints() { return points; } public void addPoint(Point p) { this.points.add(p); } } class Pentagon{ private Point x; private Point y; private Point z; private Point a; private Point b; public Pentagon(Point x, Point y, Point z,Point a,Point b) { this.x = x; this.y = y; this.z = z; this.a = a; this.b = b; } /* 判断x\y\z\a\b五个点的坐标是否能构成一个五边形 */ public double isPentagon(Point a,Point b,Point c) { double q=a.x-b.x; double w=a.y-b.y; double e=c.x-b.x; double r=c.y-b.y; double s=Math.sqrt(q * q + w * w); double t=Math.sqrt(e * e + r * r); double f=q * e + w * r; double v = f /(s*t); // 余弦值 double k=Math.toDegrees(Math.acos(v)); return k;// 角度 } //俩临边的斜率 public double isSlope() { double k1=(this.y.getY() - this.z.getY())*(this.x.getX() - this.y.getX()); double k2=(this.x.getY() - this.y.getY())*(this.y.getX() - this.z.getX()); double k3=(this.z.getY() - this.a.getY())*(this.y.getX() - this.z.getX()); double k4=(this.y.getY() - this.z.getY())*(this.z.getX() - this.a.getX()); double k5=(this.a.getY() - this.b.getY())*(this.z.getX() - this.a.getX()); double k6=(this.z.getY() - this.a.getY())*(this.a.getX() - this.b.getX()); double k7=(this.b.getY() - this.x.getY())*(this.a.getX() - this.b.getX()); double k8=(this.a.getY() - this.b.getY())*(this.b.getX() - this.x.getX()); double k9=(this.x.getY() - this.y.getY())*(this.b.getX() - this.x.getX()); double k10=(this.b.getY() - this.x.getY())*(this.x.getX() - this.y.getX()); if(k1-k2==0||k3-k4==0||k5-k6==0||k7-k8==0||k9-k10==0) { return 1; } else { return 0; } } /* 判断是否平行四边形 */ public boolean isParallelogram() { return true; } // 获取五边形的面积,此处采用海伦公式 public double getArea() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX()); double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX()); double k6 = (this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY())+(this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX()); double k7 = (this.x.getY() - this.a.getY())*(this.x.getY() - this.a.getY())+(this.x.getX() - this.a.getX())*(this.x.getX() - this.a.getX()); double d1 = Math.sqrt(k1); double d2 = Math.sqrt(k2); double d3 = Math.sqrt(k3); double d4 = Math.sqrt(k4); double d5 = Math.sqrt(k5); double d6 = Math.sqrt(k6); double d7 = Math.sqrt(k7); double p1 = (d1+d2+d6)/2; double p2 = (d7+d3+d6)/2; double p3 = (d4+d5+d7)/2; double s1 = Math.sqrt(p1*(p1-d1)*(p1-d2)*(p1-d6)); double s2 = Math.sqrt(p2*(p2-d7)*(p2-d3)*(p2-d6)); double s3 = Math.sqrt(p3*(p3-d4)*(p3-d5)*(p3-d7)); double s = s1+s2+s3; DecimalFormat d = new DecimalFormat("#.000"); Double output = Double.valueOf(d.format(s)); return output; } /* 获取五边形的周长 */ public double getPerimeter() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX()); double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX()); double k = Math.sqrt(k1)+Math.sqrt(k2)+Math.sqrt(k3)+Math.sqrt(k4)+Math.sqrt(k5); DecimalFormat d = new DecimalFormat("#.000"); Double output = Double.valueOf(d.format(k)); return output; } /* 判断是否菱形 */ public boolean isLozenge() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX()); if(k1==k2&&k2==k3&&k3==k4) { return true; } else { return false; } } /* 判断是否矩形 */ public boolean isEquilateralTriangle() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX()); double k5 = (this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX())+(this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY()); double k6 = (this.y.getX() - this.a.getX())*(this.y.getX() - this.a.getX())+(this.y.getY() - this.a.getY())*(this.y.getY() - this.a.getY()); if(k1==k3&&k2==k4&&k5==k6) { return true; } else { return false; } } /* 判断是否正方形 */ public boolean isRightTriangle() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX()); double k5 = (this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX())+(this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY()); double k6 = (this.y.getX() - this.a.getX())*(this.y.getX() - this.a.getX())+(this.y.getY() - this.a.getY())*(this.y.getY() - this.a.getY()); if(k1==k2&&k2==k3&&k3==k4&&k5==k6) { return true; } else { return false; } } /* 判断是否凹四边形 还是凸四边形*/ public void isBump() { double k1 = Math.sqrt(Math.pow(this.y.getX() - this.x.getX(), 2) + Math.pow(this.y.getY() - this.x.getY(), 2)); double k2 = Math.sqrt(Math.pow(this.z.getX() - this.a.getX(), 2) + Math.pow(this.z.getY() - this.a.getY(), 2)); double k3 = Math.sqrt(Math.pow(this.x.getX() - this.a.getX(), 2) + Math.pow(this.x.getY() - this.a.getY(), 2)); double k4 = Math.sqrt(Math.pow(this.y.getX() - this.z.getX(), 2) + Math.pow(this.y.getY() - this.z.getY(), 2)); double c =k1 + k2 + k3 + k4; double s =0.5*Math.abs(x.x*y.y+y.x*z.y+z.x*a.y+a.x*x.y-y.x*x.y-z.x*y.y-a.x*z.y-x.x*a.y); double t1 = (a.x-x.x)*(y.y-x.y)-(a.y-x.y)*(y.x-x.x); double t2 = (x.x-y.x)*(z.y-y.y)-(x.y-y.y)*(z.x-y.x); double t3 = (y.x-z.x)*(a.y-z.y)-(y.y-z.y)*(a.x-z.x); double t4 = (z.x-a.x)*(x.y-a.y)-(z.y-a.y)*(x.x-a.x); if( t1*t2*t3*t4 > 0) { System.out.printf("true %.3f %.1f",c,s); System.exit(0); } else { System.out.printf("false %.3f %.1f",c,s); System.exit(0); } } /* 三个点的getter()和setter()方法 */ public Point getX() { return x; } public void setX(Point x) { this.x = x; } public Point getY() { return y; } public void setY(Point y) { this.y = y; } public Point getZ() { return z; } public void setZ(Point z) { this.z = z; } public Point getA() { return a; } public void setA(Point z) { this.z = a; } } class PointInputError { //判断从字符串中解析出的点的数量是否合格。 public static void wrongNumberOfPoints(ArrayList ps, int num) { if (ps.size() != num) { System.out.println("wrong number of points"); System.exit(0); } } //判断输入的字符串中点的坐标部分格式是否合格。若不符合,报错并退出程序 public static void wrongPointFormat(String s) { if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) { System.out.println("Wrong Format"); System.exit(0); } } // 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一 public static void wrongChoice(String s) { if (!s.matches("[1-5]:.+")) { System.out.println("Wrong Format"); System.exit(0); } } } class ParseInput { /* * 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5 * 一个空InputData对象 * 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中 * 输出:包含选项值和所有点的Point对象的InputData对象。 */ public static void paseInput(String s, InputData d) { PointInputError.wrongChoice(s); d.setChoice(getChoice(s)); s = s.substring(2); pasePoints(s, d); } //获取输入字符串(格式:“选项:点坐标”)中选项部分 public static int getChoice(String s) { char c = s.charAt(0); return c-48; } /* * 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn * 一个空InputData对象 * 输出:所有点的Point对象 */ public static void pasePoints(String s, InputData d) { String[] ss = s.split(" "); if (ss.length == 0) return; for (int i = 0; i < ss.length; i++) { d.addPoint(readPoint(ss[i])); } } /* * 输入:包含单个点信息的字符串,格式:x,y * 输出:Point对象 */ public static Point readPoint(String s) { PointInputError.wrongPointFormat(s); String[] ss = s.split(","); double x = Double.parseDouble(ss[0]); double y = Double.parseDouble(ss[1]); // System.out.println("match"); return new Point(x, y); } }
这道题难度较为高,需要很强的java基础知识和编程能力才能完成,且在编写的过程中存在了许多未知的bug,都需要耐心的一一上网搜寻答案。
3. 7-2 点线形系列5-凸五边形的计算-2 用户输入一组选项和数据,进行与五边形有关的计算。 以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。 选项包括: 4:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间是否存在包含关系(一个多边形有一条或多条边与另一个多边形重合,其他部分都包含在另一个多边形内部,也算包含)。 两者存在六种关系:1、分离(完全无重合点) 2、连接(只有一个点或一条边重合) 3、完全重合 4、被包含(前一个多边形在后一个多边形的内部)5、交错 6、包含(后一个多边形在前一个多边形的内部)。 各种关系的输出格式如下: 1、no overlapping area between the previous triangle/quadrilateral/ pentagon and the following triangle/quadrilateral/ pentagon 2、the previous triangle/quadrilateral/ pentagon is connected to the following triangle/quadrilateral/ pentagon 3、the previous triangle/quadrilateral/ pentagon coincides with the following triangle/quadrilateral/ pentagon 4、the previous triangle/quadrilateral/ pentagon is inside the following triangle/quadrilateral/ pentagon 5、the previous triangle/quadrilateral/ pentagon is interlaced with the following triangle/quadrilateral/ pentagon 6、the previous triangle/quadrilateral/ pentagon contains the following triangle/quadrilateral/ pentagon 5:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),输出两个多边形公共区域的面积。注:只考虑每个多边形被另一个多边形分割成最多两个部分的情况,不考虑一个多边形将另一个分割成超过两个区域的情况。 6:输入六个点坐标,输出第一个是否在后五个点所构成的多边形(限定为凸多边形,不考虑凹多边形),的内部(若是五边形输出in the pentagon/outof the pentagon,若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。输入入错存在冗余点要排除,冗余点的判定方法见选项5。如果点在多边形的某条边上,输出"on the triangle/on the quadrilateral/on the pentagon"。 以上4、5、6选项输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s: 1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。 2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y 输入格式: 基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。 输出格式: 输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0 输入样例: 在这里给出一组输入。例如: 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 复制代码 import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); InputData d = new InputData(); ParseInput.paseInput(s, d); int choice = d.getChoice(); ArrayList ps = d.getPoints(); switch (choice) { case 4: handle1(ps); break; case 5: handle2(ps); break; case 6: handle3(ps); break; } } // 五边形 public static void handle1(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 10); Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4),ps.get(5),ps.get(6),ps.get(7),ps.get(8),ps.get(9)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=180.0-angle2; double b3=360.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(t.isSlope()==0) { if(angle0==540.0||c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0) { System.out.println("true"); } else { System.out.println("false"); } } else { System.out.println("the previous triangle is interlaced with the following triangle"); } } // 凹凸五边形 public static void handle2(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 5); Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4),ps.get(5)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=360.0-angle2; double b3=180.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(t.isSlope()==0) { if(angle0==540.0) { System.out.println("true"); }else if(c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0){ System.out.println("false"); } else { System.out.println("not a pentagon"); } } else if(t.isSlope()==1) { System.out.println("not a pentagon"); } } // 凹凸五边形 public static void handle3(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 6); Pentagon t = new Pentagon(ps.get(0),ps.get(1), ps.get(2),ps.get(3),ps.get(4),ps.get(5)); double angle1=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle2=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle3=t.isPentagon(ps.get(3), ps.get(4), ps.get(5)); double angle4=t.isPentagon(ps.get(4), ps.get(5), ps.get(1)); double angle5=t.isPentagon(ps.get(5), ps.get(1), ps.get(2)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=t.getAArea(ps.get(0),ps.get(1), ps.get(2)); double b2=t.getAArea(ps.get(0),ps.get(2), ps.get(3)); double b3=t.getAArea(ps.get(0),ps.get(3), ps.get(4)); double b4=t.getAArea(ps.get(0),ps.get(4), ps.get(5)); double b5=t.getAArea(ps.get(0),ps.get(5), ps.get(1)); double b=b1+b2+b3+b4+b5; double a=t.getArea(ps.get(1), ps.get(2),ps.get(3),ps.get(4),ps.get(5)); if(t.isSlope()==0) { if(angle0==540.0&&a==b) { System.out.println("in the pentagon"); }else { System.out.println("outof the pentagon"); } } else if(t.isSlope()==1) { System.out.println("not a pentagon"); } } public static void handle4(ArrayList<Point> ps) { } /* * 输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。 * 必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外 * 。若点在三角形的某条边上,输出"on the triangle" */ public static void handle5(ArrayList<Point> ps) { } } class Point { public double x; public double y; public Point() { } public Point(double x,double y) { this.x=x; this.y=y; } /* 设置坐标x,将输入参数赋值给属性x */ public void setX(double x) { this.x = x; } /* 设置坐标y,将输入参数赋值给属性y */ public void setY(double y) { this.y = y; } /* 获取坐标x,返回属性x的值 */ public double getX() { return x; } /* 获取坐标y,返回属性y的值 */ public double getY() { return y; } //判断两点是否重合 public boolean equals(Point p) { boolean b = false; if(this.x==p.getX()&&this.y==p.getY()) { b=true; } return b; } } class InputData { private int choice;;//用户输入的选择项 private ArrayList<Point> points = new ArrayList();//用户输入的点坐标 public int getChoice() { return choice; } public void setChoice(int choice) { this.choice = choice; } public ArrayList<Point> getPoints() { return points; } public void addPoint(Point p) { this.points.add(p); } } class Pentagon{ private Point x; private Point y; private Point z; private Point a; private Point b; private Point c; public Pentagon(Point x, Point y, Point z,Point a,Point b,Point c) { this.x = x; this.y = y; this.z = z; this.a = a; this.b = b; this.c = c; } /* 判断x\y\z\a\b五个点的坐标是否能构成一个五边形 */ public double isPentagon(Point a,Point b,Point c) { double q=a.x-b.x; double w=a.y-b.y; double e=c.x-b.x; double r=c.y-b.y; double s=Math.sqrt(q * q + w * w); double t=Math.sqrt(e * e + r * r); double f=q * e + w * r; double v = f /(s*t); // 余弦值 double k=Math.toDegrees(Math.acos(v)); return k;// 角度 } public double isSlope() { double k1=(this.y.getY() - this.z.getY())*(this.x.getX() - this.y.getX()); double k2=(this.x.getY() - this.y.getY())*(this.y.getX() - this.z.getX()); double k3=(this.z.getY() - this.a.getY())*(this.y.getX() - this.z.getX()); double k4=(this.y.getY() - this.z.getY())*(this.z.getX() - this.a.getX()); double k5=(this.a.getY() - this.b.getY())*(this.z.getX() - this.a.getX()); double k6=(this.z.getY() - this.a.getY())*(this.a.getX() - this.b.getX()); double k7=(this.b.getY() - this.x.getY())*(this.a.getX() - this.b.getX()); double k8=(this.a.getY() - this.b.getY())*(this.b.getX() - this.x.getX()); double k9=(this.x.getY() - this.y.getY())*(this.b.getX() - this.x.getX()); double k10=(this.b.getY() - this.x.getY())*(this.x.getX() - this.y.getX()); if(k1-k2==0||k3-k4==0||k5-k6==0||k7-k8==0||k9-k10==0) { return 1; } else { return 0; } } /* 判断是否平行四边形 */ public boolean isParallelogram() { return true; } public double getAArea(Point a,Point b,Point c) { double k1 = this.a.getX()*this.b.getY()+this.b.getX()*this.c.getY()+this.c.getX()*this.a.getY(); double k2 = this.b.getX()*this.a.getY()+this.c.getX()*this.b.getY()+this.a.getX()*this.c.getY(); double l = 0.5*Math.sqrt(k1+k2); DecimalFormat d = new DecimalFormat("#.000"); Double output = Double.valueOf(d.format(l)); return output; } // 获取五边形的面积,此处采用海伦公式 public double getArea(Point x,Point y,Point z,Point a,Point b) { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX()); double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX()); double k6 = (this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY())+(this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX()); double k7 = (this.x.getY() - this.a.getY())*(this.x.getY() - this.a.getY())+(this.x.getX() - this.a.getX())*(this.x.getX() - this.a.getX()); double d1 = Math.sqrt(k1); double d2 = Math.sqrt(k2); double d3 = Math.sqrt(k3); double d4 = Math.sqrt(k4); double d5 = Math.sqrt(k5); double d6 = Math.sqrt(k6); double d7 = Math.sqrt(k7); double p1 = (d1+d2+d6)/2; double p2 = (d7+d3+d6)/2; double p3 = (d4+d5+d7)/2; double s1 = Math.sqrt(p1*(p1-d1)*(p1-d2)*(p1-d6)); double s2 = Math.sqrt(p2*(p2-d7)*(p2-d3)*(p2-d6)); double s3 = Math.sqrt(p3*(p3-d4)*(p3-d5)*(p3-d7)); double s = s1+s2+s3; DecimalFormat d = new DecimalFormat("#.000"); Double output = Double.valueOf(d.format(s)); return output; } /* 获取五边形的周长 */ /* 判断是否菱形 */ /* 判断是否矩形 */ /* 判断是否正方形 */ /* 三个点的getter()和setter()方法 */ public Point getX() { return x; } public void setX(Point x) { this.x = x; } public Point getY() { return y; } public void setY(Point y) { this.y = y; } public Point getZ() { return z; } public void setZ(Point z) { this.z = z; } public Point getA() { return a; } public void setA(Point z) { this.z = a; } } class PointInputError { //判断从字符串中解析出的点的数量是否合格。 public static void wrongNumberOfPoints(ArrayList ps, int num) { if (ps.size() != num) { System.out.println("wrong number of points"); System.exit(0); } } //判断输入的字符串中点的坐标部分格式是否合格。若不符合,报错并退出程序 public static void wrongPointFormat(String s) { if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) { System.out.println("Wrong Format"); System.exit(0); } } // 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一 public static void wrongChoice(String s) { if (!s.matches("[1-6]:.+")) { System.out.println("Wrong Format"); System.exit(0); } } } class ParseInput { /* * 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5 * 一个空InputData对象 * 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中 * 输出:包含选项值和所有点的Point对象的InputData对象。 */ public static void paseInput(String s, InputData d) { PointInputError.wrongChoice(s); d.setChoice(getChoice(s)); s = s.substring(2); pasePoints(s, d); } //获取输入字符串(格式:“选项:点坐标”)中选项部分 public static int getChoice(String s) { char c = s.charAt(0); return c-48; } /* * 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn * 一个空InputData对象 * 输出:所有点的Point对象 */ public static void pasePoints(String s, InputData d) { String[] ss = s.split(" "); if (ss.length == 0) return; for (int i = 0; i < ss.length; i++) { d.addPoint(readPoint(ss[i])); } } /* * 输入:包含单个点信息的字符串,格式:x,y * 输出:Point对象 */ public static Point readPoint(String s) { PointInputError.wrongPointFormat(s); String[] ss = s.split(","); double x = Double.parseDouble(ss[0]); double y = Double.parseDouble(ss[1]); // System.out.println("match"); return new Point(x, y); } }
在此题中也是同样的情况,需要一些编程能力和java的知识基础且花费较长时间才能去完成。
-
7-1 点与线(类设计)
分数 20
作者 段喜龙
单位 南昌航空大学
-
设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:
(x,y)
,数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]
。若输入有误,系统则直接输出Wrong Format
-
设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:
``` The line's color is:颜色值 The line's begin point's Coordinate is: (x1,y1) The line's end point's Coordinate is: (x2,y2) The line's length is:长度值 ```
其中,所有数值均保留两位小数,建议可用
String.format("%.2f", data)
方法。设计类图如下图所示。
** 题目要求:在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的display()方法进行输出。**
- 以下情况为无效作业
- 无法运行
- 设计不符合所给类图要求
- 未通过任何测试点测试
- 判定为抄袭
输入格式:
分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。
输出格式:
The line's color is:颜色值 The line's begin point's Coordinate is: (x1,y1) The line's end point's Coordinate is: (x2,y2) The line's length is:长度值
输入样例1:
在这里给出一组输入。例如:
5 9.4 12.3 84 Red
输出样例1:
在这里给出相应的输出。例如:
The line's color is:Red The line's begin point's Coordinate is: (5.00,9.40) The line's end point's Coordinate is: (12.30,84.00) The line's length is:74.96
输入样例2:
在这里给出一组输入。例如:
80.2356 352.12 24.5 100 Black
输出样例2:
在这里给出相应的输出。例如:
代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MBWrong Format
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Point p1 = new Point(in.nextDouble(),in.nextDouble()); Point p2 = new Point(in.nextDouble(),in.nextDouble()); Line line = new Line(p1,p2,in.next()); line.display(); } } abstract class Element{ abstract void display(); } class plane extends Element{ private String color; public void display() { System.out.println("The Plane's color is:"+this.color); } } class Point extends Element{ private double x,y; boolean mark = false; Point(double x,double y){ if(x<0||x>200||y<0||y>200) { System.out.print("Wrong Format"); mark = true; return ; } else { this.x = x; this.y = y; } } public double getX() { return this.x; } public double getY() { return this.y; } public void setX(double x) { if(x<0&&x>200) { System.out.print("Wrong Format"); return ; } this.x = x; } public void setY(double y) { if(y<0&&y>200) { System.out.print("Wrong Format"); return ; } this.y = y; } public void display() { System.out.printf("(%.2f,%.2f)",x,y); } } class Line extends Element{ private Point point1,point2; private String color; Line(Point p1,Point p2,String color){ this.point1 = p1; this.point2 = p2; this.color = color; } public Point getP1() { return this.point1; } public Point getP2() { return this.point2; } public void setP1(Point p1) { this.point1 = p1; } public void setP2(Point p2) { this.point2 = p2; } public String getColor() { return this.color; } public void setColor(String color) { this.color = color; } public double getDistance() { double len; double x1 = (this.point1.getX() - this.point2.getX())*(this.point1.getX() - this.point2.getX()); double y1 = ((this.point1.getY() - this.point2.getY())*(this.point1.getY() - this.point2.getY())); // len =Math.sqrt(((this.point1.getX() - this.point2.getX())*(this.point1.getX() - this.point2.getX()))/((this.point1.getY() - this.point2.getY())*(this.point1.getY() - this.point2.getY()))); return Math.sqrt(x1+y1); } public void display() { if(!point1.mark&&!point2.mark) { System.out.println("The line's color is:"+this.color); System.out.println("The line's begin point's Coordinate is:"); System.out.printf("(%.2f,%.2f)",this.point1.getX(),this.point1.getY()); System.out.println(); System.out.println("The line's end point's Coordinate is:"); System.out.printf("(%.2f,%.2f)",this.point2.getX(),this.point2.getY()); System.out.println(); System.out.printf("The line's length is:%.2f",this.getDistance()); } } }
这道题根据题目的要求照葫芦画瓢,较为简单
在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。
- 对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
- 再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:
The Plane's color is:颜色
- 在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:
类结构如下图所示。element = p1;//起点Point element.display(); element = p2;//终点Point element.display(); element = line;//线段 element.display(); element = plane;//面 element.display();
其中,所有数值均保留两位小数,建议可用
String.format("%.2f", data)
方法。- 以下情况为无效作业
- 无法运行
- 设计不符合所给类图要求
- 未通过任何测试点测试
- 判定为抄袭
输入格式:
分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。
输出格式:
(x1,y1) (x2,y2) The line's color is:颜色值 The line's begin point's Coordinate is: (x1,y1) The line's end point's Coordinate is: (x2,y2) The line's length is:长度值 The Plane's color is:颜色值
输入样例1:
在这里给出一组输入。例如:
5 9.4 12.3 84 Red
输出样例1:
在这里给出相应的输出。例如:
(5.00,9.40) (12.30,84.00) The line's color is:Red The line's begin point's Coordinate is: (5.00,9.40) The line's end point's Coordinate is: (12.30,84.00) The line's length is:74.96 The Plane's color is:Red
输入样例2:
在这里给出一组输入。例如:
5 9.4 12.3 845 Black
输出样例2:
在这里给出相应的输出。例如:
代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MBWrong Format
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); Point p1 = new Point(in.nextDouble(),in.nextDouble()); Point p2 = new Point(in.nextDouble(),in.nextDouble()); Line line = new Line(p1,p2,in.next()); plane pla = new plane(line.getColor()); Element element; element = p1; element.display(); element = p2; element.display(); element = line; element.display(); element = pla; element.display(); } } abstract class Element{ abstract void display(); } class plane extends Element{ private String color; plane(String color){ this.color = color; } public void display() { if(!Point.mark) System.out.println("The Plane's color is:"+this.color); } } class Point extends Element{ private double x,y; static boolean mark = false; Point(double x,double y){ if(x<0||x>200||y<0||y>200) { System.out.print("Wrong Format"); mark = true; return ; } else { this.x = x; this.y = y; } } public double getX() { return this.x; } public double getY() { return this.y; } public void setX(double x) { if(x<0&&x>200) { System.out.print("Wrong Format"); return ; } this.x = x; } public void setY(double y) { if(y<0&&y>200) { System.out.print("Wrong Format"); return ; } this.y = y; } public void display() { if(!mark) { System.out.printf("(%.2f,%.2f)",x,y); System.out.println(); } } } class Line extends Element{ private Point point1,point2; private String color; Line(Point p1,Point p2,String color){ this.point1 = p1; this.point2 = p2; this.color = color; } public Point getP1() { return this.point1; } public Point getP2() { return this.point2; } public void setP1(Point p1) { this.point1 = p1; } public void setP2(Point p2) { this.point2 = p2; } public String getColor() { return this.color; } public void setColor(String color) { this.color = color; } public double getDistance() { double len; double x1 = (this.point1.getX() - this.point2.getX())*(this.point1.getX() - this.point2.getX()); double y1 = ((this.point1.getY() - this.point2.getY())*(this.point1.getY() - this.point2.getY())); // len =Math.sqrt(((this.point1.getX() - this.point2.getX())*(this.point1.getX() - this.point2.getX()))/((this.point1.getY() - this.point2.getY())*(this.point1.getY() - this.point2.getY()))); return Math.sqrt(x1+y1); } public void display() { if(!point1.mark&&!point2.mark) { System.out.println("The line's color is:"+this.color); System.out.println("The line's begin point's Coordinate is:"); System.out.printf("(%.2f,%.2f)",this.point1.getX(),this.point1.getY()); System.out.println(); System.out.println("The line's end point's Coordinate is:"); System.out.printf("(%.2f,%.2f)",this.point2.getX(),this.point2.getY()); System.out.println(); System.out.printf("The line's length is:%.2f",this.getDistance()); System.out.println(); } } }
在第一题的基础上增加了继承和多态的特性,知晓这些理论的话,完成起来也较为简单。
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
- 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为
ArrayList<Element>
类型的对象(若不了解泛型,可以不使用<Element>
) - 增加该类的
add()
方法及remove(int index)
方法,其功能分别为向容器中增加对象及删除第index - 1
(ArrayList中index>=0)个对象 - 在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
- 1:向容器中增加Point对象
- 2:向容器中增加Line对象
- 3:向容器中增加Plane对象
- 4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
- 0:输入结束
输入结束后,按容器中的对象顺序分别调用每个对象的choice = input.nextInt(); while(choice != 0) { switch(choice) { case 1://insert Point object into list ... break; case 2://insert Line object into list ... break; case 3://insert Plane object into list ... break; case 4://delete index - 1 object from list int index = input.nextInt(); ... } choice = input.nextInt(); }
display()
方法进行输出。
类图如下所示:
- 以下情况为无效作业
- 无法运行
- 设计不符合所给类图要求
- 未通过任何测试点测试
- 判定为抄袭
输入格式:
switch(choice) { case 1://insert Point object into list 输入“点”对象的x,y值 break; case 2://insert Line object into list 输入“线”对象两个端点的x,y值 break; case 3://insert Plane object into list 输入“面”对象的颜色值 break; case 4://delete index - 1 object from list 输入要删除的对象位置(从1开始) ... }
输出格式:
- Point、Line、Plane的输出参考题目2
- 删除对象时,若输入的index超出合法范围,程序自动忽略该操作
输入样例:
在这里给出一组输入。例如:
1 3.4 5.6 2 4.4 8.0 0.98 23.888 Red 3 Black 1 9.8 7.5 3 Green 4 3 0
输出样例:
在这里给出相应的输出。例如:
代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB(3.40,5.60) The line's color is:Red The line's begin point's Coordinate is: (4.40,8.00) The line's end point's Coordinate is: (0.98,23.89) The line's length is:16.25 (9.80,7.50) The Plane's color is:Green
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); /* Point p1 = new Point(in.nextDouble(),in.nextDouble()); Point p2 = new Point(in.nextDouble(),in.nextDouble()); Line line = new Line(p1,p2,in.next()); plane pla = new plane(line.getColor());*/ GeometryObject G = new GeometryObject(); int choice = in.nextInt(); while(choice!=0) { switch(choice) { case 1: Point p = new Point(in.nextDouble(),in.nextDouble()); G.add(p); break; case 2:Point p1 = new Point(in.nextDouble(),in.nextDouble()); Point p2 = new Point(in.nextDouble(),in.nextDouble()); Line line = new Line(p1,p2,in.next()); G.add(line); break; case 3:plane pla = new plane(in.next()); G.add(pla); break; case 4:int index = in.nextInt(); try { G.remove(index-1); } catch(Exception e){ } break; } choice = in.nextInt(); } G.display(); } } class GeometryObject{ ArrayList<Element> container; static int len = 0; GeometryObject(){ container = new ArrayList<Element>(); } public void add(Element element) { this.container.add(element); len++; } public void remove(int index) { this.container.remove(index); len--; } public ArrayList<Element> getList(){ return this.container; } public void display() { Element element; for(int i=0;i<len;i++) { element = container.get(i); element.display(); } } } abstract class Element{ abstract void display(); } class plane extends Element{ private String color; plane(String color){ this.color = color; } public void display() { if(!Point.mark) System.out.println("The Plane's color is:"+this.color); } } class Point extends Element{ private double x,y; static boolean mark = false; Point(double x,double y){ if(x<0||x>200||y<0||y>200) { System.out.print("Wrong Format"); mark = true; return ; } else { this.x = x; this.y = y; } } public double getX() { return this.x; } public double getY() { return this.y; } public void setX(double x) { if(x<0&&x>200) { System.out.print("Wrong Format"); return ; } this.x = x; } public void setY(double y) { if(y<0&&y>200) { System.out.print("Wrong Format"); return ; } this.y = y; } public void display() { if(!mark) { System.out.printf("(%.2f,%.2f)",x,y); System.out.println(); } } } class Line extends Element{ private Point point1,point2; private String color; Line(Point p1,Point p2,String color){ this.point1 = p1; this.point2 = p2; this.color = color; } public Point getP1() { return this.point1; } public Point getP2() { return this.point2; } public void setP1(Point p1) { this.point1 = p1; } public void setP2(Point p2) { this.point2 = p2; } public String getColor() { return this.color; } public void setColor(String color) { this.color = color; } public double getDistance() { double len; double x1 = (this.point1.getX() - this.point2.getX())*(this.point1.getX() - this.point2.getX()); double y1 = ((this.point1.getY() - this.point2.getY())*(this.point1.getY() - this.point2.getY())); // len =Math.sqrt(((this.point1.getX() - this.point2.getX())*(this.point1.getX() - this.point2.getX()))/((this.point1.getY() - this.point2.getY())*(this.point1.getY() - this.point2.getY()))); return Math.sqrt(x1+y1); } public void display() { if(!point1.mark&&!point2.mark) { System.out.println("The line's color is:"+this.color); System.out.println("The line's begin point's Coordinate is:"); System.out.printf("(%.2f,%.2f)",this.point1.getX(),this.point1.getY()); System.out.println(); System.out.println("The line's end point's Coordinate is:"); System.out.printf("(%.2f,%.2f)",this.point2.getX(),this.point2.getY()); System.out.println(); System.out.printf("The line's length is:%.2f",this.getDistance()); System.out.println(); } } }
此题在第二题的基础上完成了容器和接口的特性,也较为简单
-
踩坑新得和改进建议
1.在做第四次大作业中,由于题目中的要求较多,需要编写较长的程序,在我编写程序的过程中,往往容易编写到后面的程序会忘记前面的程序,或者不记得前面的程序,在后期的编写中会出现去与前面冲突的逻辑错误。
2.在第五次大作业中,也是出现了与第四次大作业的情况一样,且许多java的函数和一些新概念都不太清楚,在上网搜寻一番后才得以明白,导致编写过程比较长且痛苦。
3.在期中考试题目中,由于题目较为简单,只靠查一些基本的概念,所以在编写的过程中没出现什么情况,写的比较轻松。
4.虽然我对于java的一些概念了解,但在编写程序的过程依然会发生一些较为简单的逻辑错误,往往写了前面忘了前面,导致程序的bug一直无法解决。
改进建议
在以后的编写程序的过程中会多多写注释,以免自己在回顾自己写过的代码时不会看不懂,且在编写后期的代码时也会多多思考前期的问腿,且在完成一个类或者方法时尽量会写一个测试一个,这样以防后期的bug调试中不会出现无法解决的问题,令自己摇头晃脑苦苦思索也不得解决。
总结。
1.通过了几次作业的完成之后对于那些较为长的程序也能完整的完成,且逻辑思维能力也更加缜密,对于java的的知识和一些概念更加清晰。
2.在经过这几次大作业后,对于java程序的编写有了更强的自信,不会对于那些题目较为难的题目而出现退缩,畏惧的心理,而是会面对,完美的完成!
3.对于老师的授课方式和作业布置方式我个人觉得十分的完美,上课通过现场讲解源码的方式来帮助我们认识一些java的细节语法和函数的使用,让我们对于java的使用能更快的更好的掌握。
标签:总结,ps,get,double,博客,getX,getY,第二次,public From: https://www.cnblogs.com/2202-w/p/16838745.html