第二次博客
一、前言
本次博客包含三次作业,分别是第四次作业,第五次作业和期中考试。前两次作业考核核的是与对象的构造及其方法的运用,每次的题目数量不超过三题,但包含的考察点涉及几何方法的多种表现形式,故对于知识点的覆盖十分全面。第四次作业考核的内容是四边形的判断,第五次作业考核的内容是五边形的判断。期中考试考核的是类,父类,抽象类以及抽象类容器。第一题考核的就是简单的类的构造,第二题考核的是类的继承和多态以及抽象类,第三题考核的是抽象类容器。这些题目中第四次作业和第五次作业的难度是巨大的,涉及到相当多的数学知识和相当刁钻的测试点。而期中考试的难度相对而言大幅下降。
二、设计与分析
1、第四次作业第二题
7-2 点线形系列4-凸四边形的计算 分数 70 作者 蔡轲 单位 南昌航空大学
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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
源代码展示:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String arr = input.nextLine(); String[] str1Array = arr.split("\\:|\\,|\\ "); double[] num = new double[str1Array.length]; int m = 0; for (int i = 0; i < str1Array.length; i++) { num[i] = Double.parseDouble(str1Array[i]); //System.out.print(num[i]+" "); } for(int k = 0; k < arr.length();k++) { if(arr.charAt(k) == ',') m++; } double j = num[0]; int n = 0; //System.out.println(m); int u =0; if(arr.charAt(1) != ':') { System.out.print("Wrong Format"); } else if(arr.charAt(u) == '+'&&arr.charAt(u+1) == '+') { System.out.print("Wrong Format"); u++; } else { if(douhao(j,m) == 0) { panduan(num); } } } public static int douhao(double j,int m) { int n = 0; if(j == 1 && m != 4) { System.out.println("wrong number of points"); n = 1; } else if(j == 2 && m != 4 ) { System.out.println("wrong number of points"); n = 1; } else if(j == 3 && m != 4 ) { System.out.println("wrong number of points"); n = 1; } else if(j == 4 && m != 6 ) { System.out.println("wrong number of points"); n = 1; } else if(j == 5 && m != 5 ) { System.out.println("wrong number of points"); n = 1; } return n; } public static void panduan(double num[]) { double x1=num[1]; double y1=num[2]; double x2=num[3]; double y2=num[4]; double x3=num[5]; double y3=num[6]; double x4=num[7]; double y4=num[8]; //double x5=num[9]; //double y5=num[10]; //double x6=num[11]; //double y6=num[12]; if(num[0]==1) { op1(x1,y1,x2,y2,x3,y3,x4,y4); } else if(num[0]==2) { op2(x1,y1,x2,y2,x3,y3,x4,y4); } else if(num[0]==3) { op3(x1,y1,x2,y2,x3,y3,x4,y4); } else if(num[0]==4) { op4(); } else if (num[0]==5) { op5(); } } public static void op1(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { double kab= (y1-y2)/(x1-x2); double kdc= (y4-y3)/(x4-x3); double kad= (y1-y4)/(x1-x4); double kbc= (y2-y3)/(x2-x3); if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x2==x3&&y2==y3)||(x3==x4&&y3==y4)||(x2==x4&&y2==y4)) { System.out.println("points coincide"); } else { double k1 = (y1-y2)/(x1-x2); double k2 = (y2-y3)/(x2-x3); double p = 0; double m = 0; p = jiaodian1(x1,y1,x2,y2,x3,y3,x4,y4); m = jiaodian2(x1,y1,x2,y2,x3,y3,x4,y4); if((k1 == k2)||(x1==x2&&x2==x3)||(x1==x2&&x2==x4)||(x2==x3&&x3==x4)||p == 1) { System.out.println("false false"); } else { if(x1==x4&&x2==x3&&kbc==kad) { System.out.println("true true"); } else { if((kab == kdc)&&(kad == kbc)) { System.out.println("true true"); } else { System.out.println("true false"); } } } } } public static int jiaodian1(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int p = 0; double a1=y2-y1; double b1=(x2-x1); double c1=x1*y2-x2*y1; double a2=y4-y3; double b2=(x4-x3); double c2=x3*y4-x4*y3; double d=a1*b2-a2*b1; //System.out.print((b2*c1-b1*c2)/d + "," + (a2*c1-a1*c2)/d); if(((b2*c1-b1*c2)/d) > x1 && ((b2*c1-b1*c2)/d) < x2 && ((a2*c1-a1*c2)/d) < y3 && ((a2*c1-a1*c2)/d) > y4) { p = 1; } //System.out.println(p); return p; } public static int jiaodian2(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int p = 0; double a1=y4-y1; double b1=(x4-x1); double c1=x1*y4-x4*y1; double a2=y2-y3; double b2=(x2-x3); double c2=x3*y2-x2*y3; double d=a1*b2-a2*b1; //System.out.print((b2*c1-b1*c2)/d + "," + (a2*c1-a1*c2)/d); if(((b2*c1-b1*c2)/d) > x1 && ((b2*c1-b1*c2)/d) < x4 && ((a2*c1-a1*c2)/d) < y3 && ((a2*c1-a1*c2)/d) > y2) { p = 1; } return p; } public static void op2(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int p = jiaodian1(x1,y1,x2,y2,x3,y3,x4,y4); double k1 = (y1-y2)/(x1-x2); double k2 = (y2-y3)/(x2-x3); if((k1 == k2)||(x1==x2&&x2==x3)||(x1==x2&&x2==x4)||(x2==x3&&x3==x4)||p == 1) { System.out.println("not a quadrilateral"); } else { int sq1 = linxing(x1,y1,x2,y2,x3,y3,x4,y4); if(sq1 == 1) { System.out.print("true "); } else { System.out.print("false "); } int sq2 = juxing(x1,y1,x2,y2,x3,y3,x4,y4); if(sq2 == 1) { System.out.print("true "); } else { System.out.print("fasle "); } int sq3 = fangxing(x1,y1,x2,y2,x3,y3,x4,y4); if(sq3 == 1) { System.out.print("true"); } else { System.out.print("false"); } } } public static int fangxing(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int m1 = linxing(x1,y1,x2,y2,x3,y3,x4,y4); int m2 = juxing(x1,y1,x2,y2,x3,y3,x4,y4); int sq = 0; if(m1 == 1 && m2 == 1) { sq = 1; } return sq ; } public static int linxing (double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int sq = 0 ; double lab = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2)); double lbc = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2)); double lcd = Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2)); double lda = Math.sqrt(Math.pow(x1-x4,2)+Math.pow(y1-y4,2)); if(lab==lbc&&lab==lcd&&lcd==lda) { sq = 1; } return sq ; } public static int juxing (double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int sq = 0; double kab= (y2-y1)/(x2-x1); double kbc= (y3-y2)/(x3-x2); double kcd= (y4-y3)/(x4-x3); double kda= (y1-y4)/(y1-x4); if(((x1==x4 && x2==x3 )&&(y1==y2 && y3==y4))||((x1==x2 && x4==x3 )&&(y1==y4 && y3==y2))) { sq = 1; } else { if(kab*kbc==-1&&kcd*kda==-1) { sq = 1; } } return sq ; } public static void op3(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int p = jiaodian1(x1,y1,x2,y2,x3,y3,x4,y4); double k1 = (y1-y2)/(x1-x2); double k2 = (y2-y3)/(x2-x3); double lab = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2)); double lbc = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2)); double lcd = Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2)); double lda = Math.sqrt(Math.pow(x1-x4,2)+Math.pow(y1-y4,2)); double lac = Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2)); double t1 = (x4-x1)*(y2-y1)-(y4-y1)*(x2-x1); double t2 = (x1-x2)*(y3-y2)-(y1-y2)*(x3-x2); double t3 = (x2-x3)*(y4-y3)-(y2-y3)*(x4-x3); double t4 = (x3-x4)*(y1-y4)-(y3-y4)*(x1-x4); if((k1 == k2)||(x1==x2&&x2==x3)||(x1==x2&&x2==x4)||(x2==x3&&x3==x4)||p == 1) { System.out.println("not a quadrilateral"); } else { if(t1*t2*t3*t4 < 0) { System.out.print("false "); } else { System.out.print("true "); } double C = lab + lbc +lcd + lda ; double c = C*1000 % 10; //System.out.println(C); //System.out.println(c); if(c==0) { System.out.print(C+" "); } else { System.out.print(String.format("%.3f ", C)); } } //S=%√[p(p-a)(p-b)(p-c)] double S = 0 ; double s1,s2; double p1 = (lab+lbc+lac)/2; double p2 = (lcd+lda+lac)/2; s1 = Math.sqrt(p1*(p1-lab)*(p1-lbc)*(p1-lac)); s2 = Math.sqrt(p2*(p2-lcd)*(p2-lda)*(p2-lac)); S = s1 + s2; // double s = S*1000 % 10; //if(s==0) //{ //System.out.print(S); //} //else //{ System.out.print(String.format("%.1f", S)); //} } public static void op4() { System.out.println("not a quadrilateral or triangle"); } public static void op5() { System.out.println("in the triangle"); } }View Code
2、第五次作业第一题
分数 50
作者 蔡轲 单位 南昌航空大学 用户输入一组选项和数据,进行与五边形有关的计算。以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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
源代码展示:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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); } }View Code
类图如下:
3、第五次作业第二题
7-2 点线形系列5-凸五边形的计算-2
分数 50 作者 蔡轲 单位 南昌航空大学用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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
源代码展示:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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); } }View Code
类图如图所示:
4、期中考试第一题
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:
在这里给出相应的输出。例如:
Wrong Format
源代码展示:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.Scanner; class Point { private double x; private double y; public Point(double x,double y) { this.x = x; this.y = y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } public double getX(double x) { return x; } public double getY(double y) { return y; } public void display() { System.out.println("The line's begin point's Coordinate is:"+"\n"+"("+String.format("%.2f", x)+","+String.format("%.2f", y)+")"); } } class Line { private Point p1; private Point p2; private String color; public Line (Point p1,Point p2,String color) { this.p1 = p1; this.p2 = p2; this.color = color; } } public class Main { public static void main (String[] args) { Scanner in = new Scanner (System.in); double x1 = in.nextDouble(), y1 = in.nextDouble(), x2 = in.nextDouble(), y2 = in.nextDouble(); Point p1 = new Point(x1,y1); Point p2 = new Point(x2,y2); Line l = new Line(p1,p2,in.next()); if(x1<=0||y1>200||x2<=0||y2>200) { System.out.println("Wrong Format"); } else { p1.display(); p2.display(); } } }View Code
说明:由于本人当时期中考试时不在学校,所以第二第三两道题没来的及去编程,所以二三两道提交代码和第一题一样
三、踩坑心得
1、设计类的时候忘记添加构造函数。如果不添加构造函数,那么需要向类中传递参数并调用方法的功能便无法实现。
2、类中和方法漏写和判断情况缺失。这个就在图形类中体现得较为明显。涉及几何学得判断时要尤为注意那些情况该图形不存在,哪些情况点是重合的,哪些情况斜率不存在或者斜率为零,三角形哪些情况不成立,哪些情况三角形成为了一条直线等极端情况。
3.对新知识的掌握尚未完全。在这个阶段的学习中,我们在类与对象、方法基本构建的基础上,新学习了继承、多态的使用以及抽象类、抽象方法的构造,新知较多,在期中考试中,我就遇到了抽象类使用不熟练的问题,比如抽象方法该怎么处理以及各个类怎么确立关系等问题。
四、改进建议
前三次作业最大的问题就是披着java的外衣,但是里面全是C语言的肉,在一个类里面将所有问题解决。使用类使程序发挥java的优势,调用方法使程序更有层次性,可以规避面向过程的编程中出现的需要很麻烦才能解决的问题,在字符串处理上更加改进,避免误差,可以采用正则表达式,来省去很大的冗余。从第四次作业开始,仍存在着使用面向过程的思想编程的情况,从第五次作业开始,这种情况发生了显著的改变,懂得了识别对象并创造相应的类,让他们分开工作,增加代码的可读性。在编程的过程中尽量不要粗心,出现类名前后不一致的情况。输入的过程可以尝试使用正则表达式。从这几次作业的情况来看,类的继承和多态的理解还不是那么的深刻,还是要多加练习。
五、总结
1.我在这三次作业中初步学会了java的基本语法,它与C语言有着很大出入,总结一下就是长着类似的肉,但是二者的根骨不同。
2.在这三次的作业中,我做得并不理想,首先最开始没有采用面向对象的思想,其次是对与数学的结合做的不太好,很多刁钻的测试点根本就没得分。最后就是有关的概念理解的不是那么透彻。在今后的学习中,要深化对类的理解,学会运用网络资源搜寻程序所需相关函数,做得对程序精益求精,争取在现场的作业里做得更好。
3.在题目拿到手时,常常不知道从何处下手,同时还存在着考虑问题不全面的情况。
标签:ps,get,double,博客,getX,getY,第二次,public From: https://www.cnblogs.com/zhangruifeng29/p/16839546.html