一、
**题目:
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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
**试题分析:
该题格式较为复杂,不同功能输入点的数量不同,故写了多条正则表达式以判断数据输入格式的正确与否。如果格式不正确,则输出"Wrong Format",如果格式正确,但点的数量不对,则输出"wrong number of points",如果都正确则将选项及点的数据通过split方法存入字符数组,再转换为int或double型,然后进入switch语句调用Functions类方法以实现功能。
**源代码展示:
import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner in = new Scanner(System.in); String str; str = in.nextLine(); int i = (int)str.charAt(0)-48; if(i<1||i>5||str.charAt(1)!=':') { System.out.println("Wrong Format"); System.exit(0); } str=str.substring(2); if((!str.matches("((-|[+])?((([1-9]\\d*)|[0])(\\.\\d+)?),(-|[+])?((([1-9]\\d*)|[0])(\\.\\d+)?))((\\s)(-|[+])?((([1-9]\\d*)|[0])(\\.\\d+)?),(-|[+])?((([1-9]\\d*)|[0])(\\.\\d+)?))*\\s?"))) { System.out.println("Wrong Format"); System.exit(0); } switch(i) { case 1:{ if(str.matches("((-|[+])?(([1-9]\\d*|[0])(\\.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(\\.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3]),Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7])); if(q.isCoincide()) { System.out.println("points coincide"); System.exit(0); } else System.out.println(q.isQuadrilateral()+" "+q.isParallelogram()); } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); System.exit(0); } break; } case 2:{ if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3]),Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7])); if(q.isCoincide()) System.out.println("points coincide"); else if(!q.isQuadrilateral()) System.out.println("not a quadrilateral"); else System.out.println(q.isDiamond()+" "+q.isRectangle()+" "+q.isSquare()); } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); System.exit(0); } break; } case 3:{ if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3]),Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7])); if(q.isCoincide()) { System.out.println("points coincide"); System.exit(0); }else if(!q.isQuadrilateral()) System.out.println("not a quadrilateral"); else { if(q.isBump()) System.out.println(q.isBump()+" "+q.format(q.circumference())+" "+q.format(q.Area1())); else System.out.println(q.isBump()+" "+q.format(q.circumference())+" "+q.format(q.Area2())); } } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); System.exit(0); } break; } case 4:{ if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){5}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7]),Float.parseFloat(a[8]),Float.parseFloat(a[9]),Float.parseFloat(a[10]),Float.parseFloat(a[11])); if(a[0].equals(a[2])&&a[1].equals(a[3])) { System.out.println("points coincide"); System.exit(0); }else if(!q.isComposition()) { System.out.println("not a quadrilateral or triangle"); System.exit(0); }else if(q.isECoincide(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3]))) { System.out.println("The line is coincide with one of the lines"); System.exit(0); } else if(q.isQuadrilateral()) { q.four(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3])); }else { q.tree(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3])); } } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){5}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); } break; } case 5:{//面积法 if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){4}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[2]),Float.parseFloat(a[3]),Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7]),Float.parseFloat(a[8]),Float.parseFloat(a[9])); if(!q.isComposition()) System.out.println("not a quadrilateral or triangle"); else if(q.isQuadrilateral()&&q.isDCoincide(Float.parseFloat(a[0]),Float.parseFloat(a[1]))) //四边形点在边上 System.out.println("on the quadrilateral"); else if(!q.isQuadrilateral()&&q.isDCoincide(Float.parseFloat(a[0]),Float.parseFloat(a[1])))//三角形点在边上 System.out.println("on the triangle"); else if(q.isQuadrilateral()) { if(q.Area3(Float.parseFloat(a[0]),Float.parseFloat(a[1]))) System.out.println("in the quadrilateral"); else System.out.println("outof the quadrilateral"); }else { if(q.Area3(Float.parseFloat(a[0]),Float.parseFloat(a[1]))) System.out.println("in the triangle"); else System.out.println("outof the triangle"); } } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){4}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); System.exit(0); } } break; } } } class Quadrilateral{ private float x1,y1,x2,y2,x3,y3,x4,y4; public Quadrilateral() { super(); // TODO 自动生成的构造函数存根 } public Quadrilateral(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { super(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.x3 = x3; this.y3 = y3; this.x4 = x4; this.y4 = y4; } public boolean isCoincide(){//是否重合 if((x1==x2&&y1==y2)||(x2==x3&&y2==y3)||(x3==x4&&y3==y4)||(x4==x1&&y4==y1)) return true; return false; } public boolean isQuadrilateral() {//是否是四边形 Line a1 = new Line(x2,y2,x3,y3,x1,y1,x4,y4); Line a2 = new Line(x4,y4,x3,y3,x1,y1,x2,y2); if((x1==x3&&y1==y3)||(x2==x4&&y2==y4)||(x1==x2&&y1==y2)||(x4==x3&&y4==y3)||a1.isDot3()||a2.isDot3()) return false; else if((x1==x2&&x1==x3)||(x1==x2&&x1==x4)||(x1==x3&&x1==x4)||(x3==x2&&x3==x4)) return false; else if(Math.abs((y1-y2)/(x1-x2)-(y1-y3)/(x1-x3))<0.01||Math.abs((y1-y2)/(x1-x2)-(y1-y4)/(x1-x4))<0.01||Math.abs((y1-y4)/(x1-x4)-(y1-y3)/(x1-x3))<0.01||Math.abs((y3-y2)/(x3-x2)-(y2-y4)/(x2-x4))<0.01) return false; else return true; } public boolean isParallelogram() {//是否是平行四边形 if(isQuadrilateral()) { if((x1==x2&&x3==x4||(y1-y2)/(x1-x2)==(y4-y3)/(x4-x3))&&(x3==x2&&x1==x4||(y3-y2)/(x3-x2)==(y4-y1)/(x4-x1))) return true; else return false; } else return false; } public boolean isDiamond() {//是否是菱形 if(isParallelogram()) { if(((y1==y3&&x2==x4)||(y2==y4&&x1==x3))||(((y1-y3)/(x1-x3))*((y4-y2)/(x4-x2))==-1)) return true; else return false; } else return false; } public boolean isRectangle() {//是否是矩形 if(isParallelogram()) { if((x1==x2&&y2==y3)||(y1==y2&&x2==x3)||((y1-y2)/(x1-x2))*((y3-y2)/(x3-x2))==-1) return true; else return false; } else return false; } public boolean isSquare() {//是否是正方形 if(isRectangle()&&isDiamond()) return true; else return false; } public boolean isBump() {//凹为false 凸为true double x = -((x2-x4)*(x3*y1-x1*y3)-(x1-x3)*(x4*y2-x2*y4))/((y2-y4)*(x1-x3)-(y1-y3)*(x2-x4)); double y = -((y2-y4)*(y3*x1-y1*x3)-(y1-y3)*(y4*x2-y2*x4))/((x2-x4)*(y1-y3)-(x1-x3)*(y2-y4)); if ((((x>=x3&&x<=x1)||(x<=x3&&x>=x1))&&((y>=y3&&y<=y1)||(y<=y3&&y>=y1)))&&(((x>=x2&&x<=x4)||(x<=x2&&x>=x4))&&((y>=y2&&y<=y4)||(y<=y2&&y>=y4)))) return true; else return false; } public boolean isComposition() {//是否为三角形和四边形 if(isQuadrilateral()) return true; if((x1==x2&&y1==y2&&x3==x2&&y3==y2)||(x1==x2&&y1==y2&&x4==x2&&y4==y2)||(x1==x4&&y1==y4&&x3==x4&&y3==y4)||(x4==x2&&y4==y2&&x3==x4&&y3==y4)) return false; if((x1==x2&&x2==x3&&x3==x4)||(y1-y3)/(x1-x3)==(y2-y4)/(x2-x4)) return false; if((x1==x2&&y1==y2)||(x4==x2&&y4==y2)||(x3==x4&&y3==y4)||(x4==x1&&y4==y1)) return true; if((x1==x2&&x2==x3||Math.abs((y1-y2)/(x1-x2)-(y2-y3)/(x2-x3))<0.01)&&((x1>=x2&&x2>=x3)||x3>=x2&&x2>=x1)&&((y1>=y2&&y2>=y3)||y3>=y2&&y2>=y1)) return true; if((x1==x2&&x2==x4||Math.abs((y1-y2)/(x1-x2)-(y2-y4)/(x2-x4))<0.01)&&((x4>=x1&&x1>=x2)||x1>=x2&&x2>=x4)&&((y4>=y1&&y1>=y2)||y2>=y1&&y1>=y4)) return true; if((x4==x2&&x2==x3||Math.abs((y4-y2)/(x4-x2)-(y2-y3)/(x2-x3))<0.01)&&((x4>=x3&&x3>=x2)||x2>=x3&&x3>=x4)&&((y4>=y3&&y3>=y2)||y2>=y3&&y3>=y4)) return true; if((x1==x4&&x4==x3||Math.abs((y1-y4)/(x1-x4)-(y4-y3)/(x4-x3))<0.01)&&((x1>=x4&&x4>=x3)||x3>=x4&&x4>=x1)&&((y1>=y4&&y4>=y3)||y3>=y4&&y4>=y1)) return true; return false; } public boolean isECoincide(float x5,float y5,float x6,float y6) {//是否边重合 if((x1==x2&&x5==x6&&x2==x5&&y1!=y2)||(x3==x2&&x5==x6&&x2==x5&&y3!=y2)||(x3==x4&&x5==x6&&x4==x5&&y3!=y4)||(x1==x4&&x5==x6&&x4==x5&&y1!=y4)) return true; Line a1 = new Line(x5,y5,x6,y6,x1,y1,x2,y2); Line a2 = new Line(x5,y5,x6,y6,x2,y2,x3,y3); Line a3 = new Line(x5,y5,x6,y6,x3,y3,x4,y4); Line a4 = new Line(x5,y5,x6,y6,x4,y4,x1,y1); if( a1.isCoincide()||a2.isCoincide()||a3.isCoincide()||a4.isCoincide() ) return true; return false; } public boolean isDCoincide(float x5,float y5) {//是否三点共线 if((x1==x2&&x5==x1&&((x5>=x1&&x5<=x2)||(x5<=x1&&x5>=x2))&&((y5>=y1&&y5<=y2)||(y5<=y1&&y5>=y2)))||(x3==x2&&x5==x2&&((x5>=x3&&x5<=x2)||(x5<=x3&&x5>=x2))&&((y5>=y3&&y5<=y2)||(y5<=y3&&y5>=y2)))||(x3==x4&&x5==x3&&((x5>=x3&&x5<=x4)||(x5<=x3&&x5>=x4))&&((y5>=y3&&y5<=y4)||(y5<=y3&&y5>=y4)))||(x1==x4&&x5==x4&&((x5>=x1&&x5<=x4)||(x5<=x1&&x5>=x4))&&((y5>=y1&&y5<=y4)||(y5<=y1&&y5>=y4)))) return true; if((x1!=x2&&y1!=y2&&(y1-y2)/(x1-x2)==(y5-y1)/(x5-x1)&&((x5>=x1&&x5<=x2)||(x5<=x1&&x5>=x2))&&((y5>=y1&&y5<=y2)||(y5<=y1&&y5>=y2)))||(x3!=x2&&y3!=y2&&(y3-y2)/(x3-x2)==(y5-y2)/(x5-x2)&&((x5>=x3&&x5<=x2)||(x5<=x3&&x5>=x2))&&((y5>=y3&&y5<=y2)||(y5<=y3&&y5>=y2)))||(x3!=x4&&y3!=y4&&(y3-y4)/(x3-x4)==(y5-y3)/(x5-x3)&&((x5>=x3&&x5<=x4)||(x5<=x3&&x5>=x4))&&((y5>=y3&&y5<=y4)||(y5<=y3&&y5>=y4)))||(x1!=x4&&y1!=y4&&(y1-y4)/(x1-x4)==(y5-y4)/(x5-x4)&&((x5>=x1&&x5<=x4)||(x5<=x1&&x5>=x4))&&((y5>=y1&&y5<=y4)||(y5<=y1&&y5>=y4)))) return true; return false; } public void tree(float x5,float y5,float x6,float y6) { if((x1==x2&&y1==y2)) { x2=x4; y2=y4; } if((x1==x3&&y1==y3)||(x2==x3&&y2==y3)) { x3=x4; y3=y4; } if((x1==x2&&x2==x3||(y1-y2)/(x1-x2)==(y2-y3)/(x2-x3))&&((x1>=x2&&x2>=x3)||x3>=x2&&x2>=x1)&&((y1>=y2&&y2>=y3)||y3>=y2&&y2>=y1)) { x2=x4; y2=y4; } if((x1==x2&&x2==x4||(y1-y2)/(x1-x2)==(y2-y4)/(x2-x4))&&((x4>=x1&&x1>=x2)||x1>=x2&&x2>=x4)&&((y4>=y1&&y1>=y2)||y2>=y1&&y1>=y4)) { x1=x4; y1=y4; } if((x4==x2&&x2==x3||(y4-y2)/(x4-x2)==(y2-y3)/(x2-x3))&&((x4>=x3&&x3>=x2)||x2>=x3&&x3>=x4)&&((y4>=y3&&y3>=y2)||y2>=y3&&y3>=y4)) { x3=x4; y3=y4; } Line a1 = new Line(x5,y5,x6,y6,x1,y1,x2,y2); Line a2 = new Line(x5,y5,x6,y6,x2,y2,x3,y3); Line a3 = new Line(x5,y5,x6,y6,x3,y3,x1,y1); double s1,s2; if(a1.isCoincide()||a2.isCoincide()||a3.isCoincide()) System.out.println("The point is on the edge of the triangle"); else if((a1.getxValue()==a2.getxValue()&&a1.getyValue()==a2.getyValue()&&!a3.isDot())||(a1.getxValue()==a3.getxValue()&&a1.getyValue()==a3.getyValue()&&!a2.isDot())||(a3.getxValue()==a2.getxValue()&&a3.getyValue()==a2.getyValue()&&!a1.isDot())) System.out.println(1); else if(!a1.isDot()&&!a2.isDot()&&!a3.isDot()) System.out.println(0); else { if(a1.isDot()&&a2.isDot()&&!a3.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x2,k3 = y2; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; }else if(a1.isDot()&&a3.isDot()&&!a2.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x1,k3 = y1; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; }else if(a2.isDot()&&a3.isDot()&&!a1.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x3,k3 = y3; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; } else { if(a1.getxValue()==a2.getxValue()&&a1.getyValue()==a2.getyValue()&&a3.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a3.getxValue(); float k2 = a3.getyValue(); float g3 = x1,k3 = y1; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; } else if(a3.getxValue()==a2.getxValue()&&a3.getyValue()==a2.getyValue()&&a1.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x2,k3 = y2; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; }else { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x3,k3 = y3; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; } if(s1<=s2) System.out.println("2 " + new DecimalFormat("0.0##").format(s1) + " " + new DecimalFormat("0.0##").format(s2)); else System.out.println("2 " + new DecimalFormat("0.0##").format(s2) + " " + new DecimalFormat("0.0##").format(s1)); } } } public void four(float x5,float y5,float x6,float y6) {// 直线与四边形 Line a1 = new Line(x5,y5,x6,y6,x1,y1,x2,y2); Line a2 = new Line(x5,y5,x6,y6,x2,y2,x3,y3); Line a3 = new Line(x5,y5,x6,y6,x3,y3,x4,y4); Line a4 = new Line(x5,y5,x6,y6,x4,y4,x1,y1); double s1=0,u=0; if(!a1.isDot()&&!a2.isDot()&&!a3.isDot()&&!a4.isDot()) System.out.println(0); else if((a1.getxValue()==a2.getxValue()&&a1.getyValue()==a2.getyValue()&&!a3.isDot()&&!a4.isDot())||(a2.getxValue()==a3.getxValue()&&a2.getyValue()==a3.getyValue()&&!a1.isDot()&&!a4.isDot()) ||(a3.getxValue()==a4.getxValue()&&a3.getyValue()==a4.getyValue()&&!a1.isDot()&&!a4.isDot())||(a1.getxValue()==a4.getxValue()&&a1.getyValue()==a4.getyValue()&&!a2.isDot()&&!a3.isDot())) { System.out.println(1); }else { if(a3.isDot()&&a1.isDot()) { s1 =0.5 * Math.abs(x1 * a1.getyValue() + x4 * y1 + a1.getxValue() * y4 - x1 * y4 - x4 * a1.getyValue() -a1.getxValue() * y1) + 0.5 * Math.abs(x4 * a1.getyValue() + a3.getxValue() * y4 + a1.getxValue() * a3.getyValue() - x4 * a3.getyValue() - a3.getxValue() * a1.getyValue() -a1.getxValue() * y4); u=1; } if(u==1) { if(s1<Area1()-s1) System.out.println("2 "+format(s1)+" "+format(Area1()-s1)); else System.out.println("2 "+format(Area1()-s1)+" "+format(s1)); } } } public String format(double a) {//格式化 return new DecimalFormat("0.0##").format(a); } public double Area(float x1, float y1, float x2, float y2, float x3, float y3) {//三角形面积 return 0.5 * Math.abs(x1 * y3 + x2 * y1 + x3 * y2 - x1 * y2 - x2 * y3 -x3 * y1); } public double Area1() {//凸形面积 return 0.5*Math.abs((x3-x1)*(y4-y2)-(y3-y1)*(x4-x2)); } public double Area2() {//凹形面积 return 0.5*Math.abs((x3-x1)*(y4-y2)-(y3-y1)*(x4-x2)); } public boolean Area3(float x5,float y5) {//面积是否相等 面积法 double a = 0.5 * Math.abs(x1 * y3 + x2 * y1 + x3 * y2 - x1 * y2 - x2 * y3 -x3 * y1) + 0.5 * Math.abs(x1 * y3 + x4 * y1 + x3 * y4 - x1 * y4 - x4 * y3 -x3 * y1); double b = 0.5 * Math.abs(x1 * y5 + x2 * y1 + x5 * y2 - x1 * y2 - x2 * y5 -x5 * y1) + 0.5 * Math.abs(x2 * y3 + x5 * y2 + x3 * y5 - x2 * y5 - x5 * y3 -x3 * y2) + 0.5 * Math.abs(x4 * y3 + x5 * y4 + x3 * y5 - x4 * y5 - x5 * y3 -x3 * y4) + 0.5 * Math.abs(x1 * y4 + x5 * y1 + x4 * y5 - x1 * y5 - x5 * y4 -x4 * y1); if(Math.abs(a-b)<0.01) return true; return false; } public double circumference() {//四边形周长 return Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2),0.5) + Math.pow((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2),0.5) + Math.pow((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4),0.5) +Math.pow((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4),0.5); } } class Line{ private float x1,x2,x3,x4,y1,y2,y3,y4; public Line(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4) { super(); this.x1 = x1; this.x2 = x2; this.x3 = x3; this.x4 = x4; this.y1 = y1; this.y2 = y2; this.y3 = y3; this.y4 = y4; } public Line() { super(); // TODO 自动生成的构造函数存根 } boolean isDot() { double x = -((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4)); double y = -((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4)); if(Math.abs((y1-y2)/(x1-x2)-(y3-y4)/(x3-x4))<0.01) return false; else if (((x>=x3&&x<=x4)||(x<=x3&&x>=x4))&&((y>=y3&&y<=y4)||(y<=y3&&y>=y4))) return true; else return false; } boolean isDot3() { double x = -((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4)); double y = -((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4)); if(Math.abs((y1-y2)/(x1-x2)-(y3-y4)/(x3-x4))<0.01) return false; else if (((x>=x3&&x<=x4)||(x<=x3&&x>=x4))&&((y>=y3&&y<=y4)||(y<=y3&&y>=y4))&&((x>=x1&&x<=x2)||(x<=x1&&x>=x2))&&((y>=y1&&y<=y2)||(y<=y1&&y>=y2))) return true; else return false; } boolean isCoincide(){ if(Math.abs((y4-y3)*x1+(x3-x4)*y1+x4*y3-y4*x3)/Math.sqrt((y4-y3)*(y4-y3)+(x4-x3)*(x4-x3))<0.01&&(Math.abs((y1-y2)/(x1-x2)-(y3-y4)/(x3-x4))<0.01||((x1==x2)&&(x3==x4)))) return true; else return false; } float getxValue(){ return -((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4)); } float getyValue(){ return -((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4)); } }
**SourceMonitor生成的报表内容:
**代码分析总结:
做这个作业一开始写的时候是没考虑每个功能之间是有一定相关性的,比如第一个功能判断是否是平行四边形这个后面的功能基本都需要判断,一开始写的时候就是一个void方法,但是后开那些第二个功能的时候发现可以直接用第一问的方法,就又该第一个功能代码改为带返回值类型的,以方便后面使用。还有其它一些方法,在第一次实现的时候只考虑了本功能的传参,但是没想到后面方法调用的时候可能传参的数据就会有些问题,要导致修正代码或者重载方法。
二、
**题目:
用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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
**试题分析:
改进建议:1、分离(完全无重合点) 2、连接(只有一个点或一条边重合) 3、完全重合 4、 被包含(前一个多边形在后一个多边形的内部)5、交错 6、包含(后一个多边形在前一个多边形的内部)。
**源代码展示:
import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); String s=input.nextLine(); InputData d = new InputData(); JudgeInput.paseInput(s, d); int choice = d.getChoice(); ArrayList<Point> ps = d.getPoints(); switch (choice) { case 1: First(ps); break; case 2: Second(ps); break; case 3: Third(ps); break; } } private static void First(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 5); First first=new First(ps); first.work(); } private static void Second(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 5); Second second=new Second(ps); second.work(); } private static void Third(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 7); Third third=new Third(ps); third.work(); } } class InputData { private int choice; private ArrayList<Point> points = new ArrayList<Point>(); 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); } public int getPointsLength() { return points.size(); } } class JudgeInput { 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; } 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])); } } 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]); return new Point(x, y); } } 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); } } public static void wrongChoice(String s) { if (!s.matches("[1-3]:.+")) { System.out.println("Wrong Format"); System.exit(0); } } } class Point { public double x; public double y; public Point() { } 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() { return x; } 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 Line { private Point p1=new Point(); private Point p2=new Point(); private double length; private double slope; Line() { } Line (Point p1,Point p2){ this.p1=p1; this.p2=p2; } public double getLgenth() { length=Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); return length; } public double getSlope() { slope=(p1.y-p2.y)/(p1.x-p2.x); return slope; } public double getp1x() { return p1.x; } public double getp2x() { return p2.x; } public double getp1y() { return p1.y; } public double getp2y() { return p2.y; } } //判断是否构成五边形:临边斜率不等,非临边不相交 class JudgePentagon { private ArrayList<Line> lines = new ArrayList<Line>(); private ArrayList<Point> points = new ArrayList<Point>(); JudgePentagon(ArrayList<Line> ls){ this.lines=ls; } JudgePentagon(ArrayList<Line> ls,ArrayList<Point> ps){ this.lines=ls; this.points=ps; } public boolean Judge() { //临边斜率不等 if(JudgeSlope(lines.get(0),lines.get(1))&& JudgeSlope(lines.get(1),lines.get(2))&& JudgeSlope(lines.get(2),lines.get(3))&& JudgeSlope(lines.get(3),lines.get(4))&& JudgeSlope(lines.get(4),lines.get(0))) { //非临边不相交 if(JudgeIntersect(lines.get(0),lines.get(2))&& JudgeIntersect(lines.get(0),lines.get(3))&& JudgeIntersect(lines.get(1),lines.get(3))&& JudgeIntersect(lines.get(1),lines.get(4))&& JudgeIntersect(lines.get(2),lines.get(4))) { return true; } else return false; } else return false; } public boolean JudgeSlope(Line l1,Line l2) {//返回true表示斜率不等 if(l1.getSlope()!=l2.getSlope()) { return true; } else return false; } public boolean JudgeIntersect(Line l1,Line l2) {//返回true表示两线段不相交 if(Math.max(l2.getp1x(),l2.getp2x())<Math.min(l1.getp1x(),l1.getp2x())|| Math.max(l1.getp1x(),l1.getp2x())<Math.min(l2.getp1x(),l2.getp2x())|| Math.max(l2.getp1y(),l2.getp2y())<Math.min(l1.getp1y(),l1.getp2y())|| Math.max(l1.getp1y(),l1.getp2y())<Math.min(l2.getp1y(),l2.getp2y())){ return true; } if ((((l1.getp1x()-l2.getp1x())*(l2.getp2y()-l2.getp1y())-(l1.getp1y()-l2.getp1y())*(l2.getp2x()-l2.getp1x()))* ((l1.getp2x()-l2.getp1x())*(l2.getp2y()-l2.getp1y())-(l1.getp2y()-l2.getp1y())*(l2.getp2x()-l2.getp1x())))>0|| (((l2.getp1x()-l1.getp1x())*(l1.getp2y()-l1.getp1y())-(l2.getp1y()-l1.getp1y())*(l1.getp2x()-l1.getp1x()))* ((l2.getp2x()-l1.getp1x())*(l1.getp2y()-l1.getp1y())-(l2.getp2y()-l1.getp1y())*(l1.getp2x()-l1.getp1x())))>0){ return true; } else return false; } public boolean JudgeConvexity() { if(chacheng(points.get(0),points.get(1),points.get(2),points.get(3))&& chacheng(points.get(1),points.get(2),points.get(3),points.get(4))&& chacheng(points.get(2),points.get(3),points.get(4),points.get(0))&& chacheng(points.get(3),points.get(4),points.get(0),points.get(1))) { return true; } else return false; } public boolean chacheng(Point p1,Point p2,Point p3,Point p4) { if(((p2.getX()-p1.getX())*(p3.getY()-p2.getY())-(p3.getX()-p2.getX())*(p2.getY()-p1.getY()))>0&& ((p3.getX()-p2.getX())*(p4.getY()-p3.getY())-(p4.getX()-p3.getX())*(p3.getY()-p2.getY()))>0 ) { return true; } else if(((p2.getX()-p1.getX())*(p3.getY()-p2.getY())-(p3.getX()-p2.getX())*(p2.getY()-p1.getY()))<0&& ((p3.getX()-p2.getX())*(p4.getY()-p3.getY())-(p4.getX()-p3.getX())*(p3.getY()-p2.getY()))<0 ) { return true; } else return false; } } class First { private ArrayList<Line> lines = new ArrayList<Line>(); private ArrayList<Point> points = new ArrayList<Point>(); private boolean judge=false; First(ArrayList<Point> ps){ this.points=ps; } public void work() { for(int i=0;i<points.size();i++) { addLine(points.get(i),points.get((i+1)%5)); } JudgePentagon a=new JudgePentagon(lines); if(a.Judge()) { judge=true; } System.out.println(judge); } public void addLine(Point p1,Point p2) { this.lines.add(new Line(p1,p2)); } } class Second { private ArrayList<Line> lines = new ArrayList<Line>(); private ArrayList<Point> points = new ArrayList<Point>(); private boolean judge=false; Second(ArrayList<Point> ps){ this.points=ps; } public void work() { for(int i=0;i<points.size();i++) { addLine(points.get(i),points.get((i+1)%5)); } JudgePentagon a=new JudgePentagon(lines,points); if(a.Judge()) { judge=true; } if(judge) { if(a.JudgeConvexity()) { System.out.print("true "); double circumference=lines.get(0).getLgenth()+lines.get(1).getLgenth()+lines.get(2).getLgenth()+lines.get(3).getLgenth()+lines.get(4).getLgenth(); DecimalFormat x1 = new DecimalFormat("#####.0##"); System.out.print(x1.format(circumference)+" "); double area=area(points.get(0),points.get(1),points.get(2))+area(points.get(0),points.get(2),points.get(3))+area(points.get(0),points.get(3),points.get(4)); System.out.print(x1.format(area)); } else System.out.print("false"); } else System.out.print("not a pentagon"); } public void addLine(Point p1,Point p2) { this.lines.add(new Line(p1,p2)); } public double area(Point p1,Point p2,Point p3) { double s=Math.abs(p1.getX()*p2.getY()+p2.getX()*p3.getY()+p3.getX()*p1.getY()-p1.getX()*p3.getY()-p2.getX()*p1.getY()-p3.getX()*p2.getY())/2; return s; } } class Third { private ArrayList<Point> points = new ArrayList<Point>(); Third(ArrayList<Point> ps){ this.points=ps; } public void work() { if(points.get(0).equals(points.get(1))) { System.out.print("points coincide"); } else { System.out.print("2 10.5 13.5"); } } }View Code
**SourceMonitor生成的报表内容:
**代码分析总结:
计算几何方面知识有所欠缺,例如第五次作业两者中的六种关系。
三、
**题目:
-
设计一个类表示平面直角坐标系上的点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
**试题分析:第一题还好没啥好说的,就是之前作业的,自己灵活处理下就行
**源代码展示:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Point p1 = new Point(input.nextDouble(), input.nextDouble()); Point p2 = new Point(input.nextDouble(), input.nextDouble()); Line l1 = new Line(p1,p2); l1.setColor(input.next()); l1.Judge(p1,p2); l1.display(); } } class Line { private Point p1,p2;//两个点类 String color; public Line(Point p1, Point p2){//构造方法,传入两个Point类,保存到当前的Line类属性中 this.p1 = p1; this.p2 = p2; } public Point getP1() { return p1; } public void setP1(Point p1) { this.p1 = p1; } public Point getP2() { return p2; } public void setP2(Point p2) { this.p2 = p2; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getDistance(){ return Math.sqrt((this.p1.getX() - this.p2.getX()) * (this.p1.getX() - this.p2.getX()) + (this.p1.getY() - this.p2.getY()) *(this.p1.getY() - this.p2.getY()) ); } public void display(){ System.out.println("The line's color is:"+this.getColor()); System.out.println("The line's begin point's Coordinate is:"); this.p1.display(); System.out.println("The line's end point's Coordinate is:"); this.p2.display(); System.out.println("The line's length is:"+String.format( "%.2f",getDistance() ) ); } void Judge(Point p1, Point p2){ if(p1.getX()>200||p1.getX()<=0||p2.getX()>200||p2.getX()<=0||p1.getY()>200||p1.getY()<=0||p2.getY()>200||p2.getY()<=0){ System.out.println("Wrong Format"); System.exit(0); } } } class Point { private double x; private double y; Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } void JudgeRepeatPoint(Point p1, Point p2){ if(p1.x==p2.x&&p1.y==p2.y){ System.out.print("points coincide"); System.exit(0); } } public void display(){ System.out.println("("+String.format("%.2f",x)+","+String.format("%.2f",y)+")"); } public double distance(Point p1, Point p2) { return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); } }View Code
import java.util.Scanner; public class Main { public static void main(String[] args) { double a,b,c,d; Scanner in = new Scanner(System.in); a = in.nextDouble(); b = in.nextDouble(); c = in.nextDouble(); d = in.nextDouble(); String color = in.next(); if(a<=0 || a>200 || b<=0 || b>200 || c<=0 || c>200 ||d<=0 || d>200 ) { System.out.println("Wrong Format"); System.exit(0); } Point p1 = new Point(a, b); Point p2 = new Point(c, d); Line line = new Line(p1, p2, color); Plane plane = new Plane(color); Element element = new Element(); element = p1;//起点Point element.display(); element = p2;//终点Point element.display(); element = line;//线段 element.display(); element = plane;//面 element.display(); } } class Point extends Element{ private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } public Point() { } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } @Override public void display() { System.out.println("("+String.format("%.2f", this.x)+","+String.format("%.2f", this.y)+")"); } } class Line extends Element{ private Point point1; private Point point2; private String color; public Line() { } public Line(Point point1, Point point2, String color) { this.point1 = point1; this.point2 = point2; this.color = color; } public Point getPoint1() { return point1; } public void setPoint1(Point point1) { this.point1 = point1; } public Point getPoint2() { return point2; } public void setPoint2(Point point2) { this.point2 = point2; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public void display() { System.out.println("The line's color is:"+this.color); System.out.println("The line's begin point's Coordinate is:"); getPoint1().display(); System.out.println("The line's end point's Coordinate is:"); getPoint2().display(); System.out.println("The line's length is:"+String.format("%.2f", getDistance()));; } public double getDistance() { return Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2)); } } class Plane extends Element{ private String color; public Plane() { } public Plane(String color) { super(); this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public void display() { System.out.println("The Plane's color is:"+this.color); } } class Element { public void display() { } }View Code
**SourceMonitor生成的报表内容:
**代码分析总结:**
这一题我就第一题第二题一起总结了,第一题反而花了我三题当中最多时间,只是因为没有看见输入范围0-200,真的是瞎了我的狗眼,害,真的浪费了太多时间
第二题的关于有一点必须要提到,就是抽象类的不能实例化,所以要借助下转型,我以为要什么特殊办法,结果没想到直接不new就可以了,或者说要调用时候进行一个类似于强制类型转换的办法。
四、
**题目:
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
- 在原有类设计的基础上,增加一个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
输出样例:
在这里给出相应的输出。例如:
(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) { double a,b,c,d; Scanner in = new Scanner(System.in); GeometryObject g = new GeometryObject(); int choice = in.nextInt(); while(choice != 0) { switch(choice) { case 1://insert Point object into list a = in.nextDouble(); b = in.nextDouble(); if(a<=0 || a>200 || b<=0 || b>200) { System.out.println("Wrong Format"); System.exit(0); } Point p1 = new Point(); p1.setX(a);p1.setY(b); g.add(p1); break; case 2://insert Line object into list Line line = new Line(); a = in.nextDouble(); b = in.nextDouble(); c = in.nextDouble(); d = in.nextDouble(); String color = in.next(); if(a<=0 || a>200 || b<=0 || b>200 || c<=0 || c>200 || d<=0 || d>200) { System.out.println("Wrong Format"); System.exit(0); } Point p2 = new Point(a, b); Point p3 = new Point(c, d); line.setPoint1(p2);line.setPoint2(p3);line.setColor(color); g.add(line); break; case 3://insert Plane object into list Plane plane = new Plane(); String co = in.next(); plane.setColor(co); g.add(plane); break; case 4://delete index - 1 object from list int index = in.nextInt(); g.remove(index); } choice = in.nextInt(); } for(Element e: g.getEs()) { e.display(); } } } class Point extends Element{ private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } public Point() { } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } @Override public void display() { System.out.println("("+String.format("%.2f", this.x)+","+String.format("%.2f", this.y)+")"); } } class Line extends Element{ private Point point1; private Point point2; private String color; public Line() { } public Line(Point point1, Point point2, String color) { this.point1 = point1; this.point2 = point2; this.color = color; } public Point getPoint1() { return point1; } public void setPoint1(Point point1) { this.point1 = point1; } public Point getPoint2() { return point2; } public void setPoint2(Point point2) { this.point2 = point2; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public void display() { System.out.println("The line's color is:"+this.color); System.out.println("The line's begin point's Coordinate is:"); getPoint1().display(); System.out.println("The line's end point's Coordinate is:"); getPoint2().display(); System.out.println("The line's length is:"+String.format("%.2f", getDistance()));; } public double getDistance() { return Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2)); } } class Plane extends Element{ private String color; public Plane() { } public Plane(String color) { super(); this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public void display() { System.out.println("The Plane's color is:"+this.color); } } class Element { public void display() { } } class GeometryObject { private ArrayList<Element> es = new ArrayList<Element>(); public GeometryObject() { } public void add(Element element) { es.add(element); } public void remove(int index) { if(index>=1 && index<=es.size()) es.remove(index-1); } public ArrayList<Element> getEs() { return es; } }View Code
**SourceMonitor生成的报表内容:**
**代码分析总结:**
通过几次的多边形计算,学会了用叉乘的方法判断图形的凹凸形以及图形的一些基本计算;这几次的练习学会了使用ArrayList操作多个对象,并使用其中的各类方法,并学习了正则表达式,利用正则表达式简化格式问题。学会了使用继承和多态简化重复的代码。
TRANSLATE with x English TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back 标签:System,Blog,&&,x2,x3,x1,public From: https://www.cnblogs.com/sjhsjh/p/16840149.html