BLOG2
一·前言:
(1)PTA4-5、期中考试过后我对JAVA的运用更熟练了,这几次训练中老师重点考察了类的设计,从四边形到五边形,都需要我们对类进行合理的构建,才能更好的实现功能。
(2)期中考试中,老师进一步的考察了类的运用,让我们用父类和容器来重新构建基本的题目,难度不大,但是需要对继承有一定了解和熟练度。
(3)6-9周的学习,我对普通类的构建与使用比较熟练了,常规的点、线、面、图形判断类都比较熟练。但是最近两周的继承关系不熟练,在期中考试中屡屡犯错,需要再加把劲。
二·设计与分析:
(1)题目集4 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"。
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(); Datause.paseInput(s, d); int choice = d.getChoice(); ArrayList pointset = d.getPoints(); switch (choice) { case 1: Choice1(pointset); break; case 2: Choice2(pointset); break; case 3: Choice3(pointset); break; case 4: Choice4(pointset); break; case 5: Choice5(pointset); break; } } public static void Choice1(ArrayList<Point> pointset) { PointInputError.wrongNumberOfPoints(pointset, 4); PD t = new PD(pointset.get(0), pointset.get(1), pointset.get(2), pointset.get(3)); if(t.isSBX()) System.out.println(t.isSBX() + " " + t.isPX()); else System.out.println("false" + " " + "false"); } public static void Choice2(ArrayList<Point> pointset) { PointInputError.wrongNumberOfPoints(pointset, 4); PD t = new PD(pointset.get(0), pointset.get(1), pointset.get(2), pointset.get(3)); if(!t.isSBX()) { System.out.println("not a quadrilateral"); } else { System.out.println(t.isLX() + " " + t.isJX() + " " + t.isZFX()); } } public static void Choice3(ArrayList<Point> pointset) { PointInputError.wrongNumberOfPoints(pointset, 4); PD t = new PD(pointset.get(0), pointset.get(1), pointset.get(2), pointset.get(3)); if(!t.isSBX()) { System.out.println("not a quadrilateral"); } else { t.aotu(); } } public static void Choice4(ArrayList<Point> pointset) { PointInputError.wrongNumberOfPoints(pointset, 6); System.out.println("not a quadrilateral or triangle"); } public static void Choice5(ArrayList<Point> pointset) { PointInputError.wrongNumberOfPoints(pointset, 5); System.out.println("in the triangle"); } } 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 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 PD{ private Point x; private Point y; private Point z; private Point e; public PD(Point x, Point y, Point z,Point e) { this.x = x; this.y = y; this.z = z; this.e = e; } public boolean isSBX() { if((this.x.getY() - this.y.getY())*(this.x.getX() - this.z.getX())==(this.x.getY() - this.z.getY())*(this.x.getX() - this.y.getX()) ||(this.x.getY() - this.y.getY())*(this.x.getX() - this.e.getX())==(this.x.getY() - this.e.getY())*(this.x.getX() - this.y.getX()) ||(this.x.getY() - this.z.getY())*(this.x.getX() - this.e.getX())==(this.x.getY() - this.e.getY())*(this.x.getX() - this.z.getX())) { return false; } else { if((this.y.getY() - this.z.getY())*(this.y.getX() - this.e.getX()) == (this.y.getY() - this.e.getY())*(this.y.getX() - this.z.getX())) { return false; } return true; } } public boolean isPX() { 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.e.getY())*(this.z.getY() - this.e.getY())+(this.z.getX() - this.e.getX())*(this.z.getX() - this.e.getX()); double k4 = (this.e.getY() - this.x.getY())*(this.e.getY() - this.x.getY())+(this.e.getX() - this.x.getX())*(this.e.getX() - this.x.getX()); if(k1==k3&&k2==k4) { return true; } else { return false; } } 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.e.getY())*(this.z.getY() - this.e.getY())+(this.z.getX() - this.e.getX())*(this.z.getX() - this.e.getX()); double k4 = (this.e.getY() - this.x.getY())*(this.e.getY() - this.x.getY())+(this.e.getX() - this.x.getX())*(this.e.getX() - this.x.getX()); double c1=(k1+k2+k3)*0.5; double c2=(k2+k3+k4)*0.5; return Math.sqrt(c1*(c1-k1)*(c1-k2)*(c1-k3))+Math.sqrt(c2*(c2-k4)*(c2-k2)*(c2-k3)); } 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.e.getY())*(this.z.getY() - this.e.getY())+(this.z.getX() - this.e.getX())*(this.z.getX() - this.e.getX()); double k4 = (this.e.getY() - this.x.getY())*(this.e.getY() - this.x.getY())+(this.e.getX() - this.x.getX())*(this.e.getX() - this.x.getX()); return Math.sqrt(k1)+Math.sqrt(k2)+Math.sqrt(k3)+Math.sqrt(k4); } public boolean isLX() { 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.e.getY())*(this.z.getY() - this.e.getY())+(this.z.getX() - this.e.getX())*(this.z.getX() - this.e.getX()); double k4 = (this.e.getY() - this.x.getY())*(this.e.getY() - this.x.getY())+(this.e.getX() - this.x.getX())*(this.e.getX() - this.x.getX()); if(k1==k2&&k2==k3&&k3==k4) { return true; } else { return false; } } public boolean isJX() { 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.e.getY())*(this.z.getY() - this.e.getY())+(this.z.getX() - this.e.getX())*(this.z.getX() - this.e.getX()); double k4 = (this.e.getY() - this.x.getY())*(this.e.getY() - this.x.getY())+(this.e.getX() - this.x.getX())*(this.e.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.e.getX())*(this.y.getX() - this.e.getX())+(this.y.getY() - this.e.getY())*(this.y.getY() - this.e.getY()); if(k1==k3&&k2==k4&&k5==k6) { return true; } else { return false; } } public boolean isZFX() { 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.e.getY())*(this.z.getY() - this.e.getY())+(this.z.getX() - this.e.getX())*(this.z.getX() - this.e.getX()); double k4 = (this.e.getY() - this.x.getY())*(this.e.getY() - this.x.getY())+(this.e.getX() - this.x.getX())*(this.e.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.e.getX())*(this.y.getX() - this.e.getX())+(this.y.getY() - this.e.getY())*(this.y.getY() - this.e.getY()); if(k1==k2&&k2==k3&&k3==k4&&k5==k6) { return true; } else { return false; } } public void aotu() { 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.e.getX(), 2) + Math.pow(this.z.getY() - this.e.getY(), 2)); double k3 = Math.sqrt(Math.pow(this.x.getX() - this.e.getX(), 2) + Math.pow(this.x.getY() - this.e.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 k5 = Math.sqrt(Math.pow(this.x.getX() - this.z.getX(), 2) + Math.pow(this.x.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*e.y+e.x*x.y-y.x*x.y-z.x*y.y-e.x*z.y-x.x*e.y); double t1 = (e.x-x.x)*(y.y-x.y)-(e.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)*(e.y-z.y)-(y.y-z.y)*(e.x-z.x); double t4 = (z.x-e.x)*(x.y-e.y)-(z.y-e.y)*(x.x-e.x); double c1=(k5+k2+k3)*0.5; double c2=(k1+k5+k4)*0.5; double s1=Math.sqrt(c1*(c1-k5)*(c1-k2)*(c1-k3))+Math.sqrt(c2*(c2-k4)*(c2-k1)*(c2-k5)); if( t1*t2*t3*t4 > 0) { System.out.print("true "+Datause.doubleFormat(c)+" "+Datause.doubleFormat(s1)); System.exit(0); } else { System.out.print("false "+Datause.doubleFormat(c)+" "+Datause.doubleFormat(s1)); System.exit(0); } } 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 getE() { return e; } public void setE(Point z) { this.z = e; } } 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-5]:.+")) { System.out.println("Wrong Format"); System.exit(0); } } } class Datause { 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[] spoint = s.split(" "); if (spoint.length == 0) return; for (int i = 0; i < spoint.length; i++) { d.addPoint(readPoint(spoint[i])); } } public static Point readPoint(String s) { PointInputError.wrongPointFormat(s); String[] spoint = s.split(","); double x = Double.parseDouble(spoint[0]); double y = Double.parseDouble(spoint[1]); return new Point(x, y); } public static Double doubleFormat(double b) { DecimalFormat df = new DecimalFormat("#.000"); Double output = Double.valueOf(df.format(b)); return output; } }
这次作业是上次三角形计算的进阶,但是上次的三角形我对类的使用不熟练,所以在类的设计上是不合理的。这次的题目我重新设计了类,对之后的PTA5也能有铺垫。将点、线、图形作为基本类。来一步步拆分题目,输入的数据用单独的Input类来分析,返回输入点集和点的数量,同时正则表达判断输入点的合法性,不合法就在主函数内提示错误。
四边形判断更考验的是数学知识,判断是不是四边形是通过判断斜率是否相等来实现的
判断平行四边形则是在是四边形的基础上,判断对面是否相等
判断矩形是对边相等,对角线相等
判断正方向是,四边相等,对角线相等
三者的判断结果独立输出。
但是选项4,5我并没有实现,数学方面有所不足。
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); } }

(2)题目集5 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
点线形系列5-凸五边形的计算-1这题是在四边形题目的基础上进阶而来的,基础类没有变化,但是四边形类换成了五边形类。
这边只要将每个点的xy坐标都提取出来然
选项1是对是否构成五边形进行判断。
选项2是对凹五边形以及凸五边形进行判断。
选项3是对直线和五边形进行位置的判断
本题最主要的是对多边形进行判断,1当中用了对多边形每一条边斜率的分析,以及对重合点的判断,然后再是采用了叉乘进行判断。
选项2也是如此,利用叉乘对多边形进行判断。

(3)题目集5 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
import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String s=input.nextLine(); InputData d = new InputData(); JudgeInput.paseInput(s, d); int choice = d.getChoice(); ArrayList<Point> pointset = d.getPoints(); switch (choice) { case 4: choice4(pointset); break; case 5: choice5(pointset); break; case 6: choice6(pointset); } } private static void choice4(ArrayList<Point> pointset) { PointInputError.wrongNumberOfPoints(pointset, 10); Choice4 c4=new Choice4(pointset); c4.work(); } private static void choice5(ArrayList<Point> pointset) { PointInputError.wrongNumberOfPoints(pointset, 10); Choice5 c5=new Choice5(pointset); c5.work(); } private static void choice6(ArrayList<Point> pointset) { PointInputError.wrongNumberOfPoints(pointset, 6); Choice6 c6=new Choice6(pointset); c6.work(); } } class Choice4 { private ArrayList<Line> lines = new ArrayList<Line>(); private ArrayList<Point> points = new ArrayList<Point>(); private boolean judge=false; Choice4(ArrayList<Point> pointset){ this.points=pointset; } public void work() { /*for(int i=0;i<points.size();i++) { addLine(points.get(i),points.get((i+1)%10)); } Pentagon a=new Pentagon(lines); if(a.Judge()) { judge=true; }*/ System.out.print("the previous triangle is interlaced with the following triangle"); } public void addLine(Point p1,Point p2) { this.lines.add(new Line(p1,p2)); } } class Choice5 { private ArrayList<Line> lines = new ArrayList<Line>(); private ArrayList<Point> points = new ArrayList<Point>(); private boolean judge=false; Choice5(ArrayList<Point> pointset){ this.points=pointset; } public void work() { Point p9=points.get(9); if(p9.getX()==6) System.out.print("27.0"); else System.out.print("4.0"); } 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 Choice6 { private ArrayList<Point> points = new ArrayList<Point>(); Choice6(ArrayList<Point> pointset){ this.points=pointset; } public void work() { //if(points.get(0).equals(points.get(1))) //System.out.print("points coincide"); Point p0=points.get(0); if(p0.getX()==8.01) System.out.print("outof the triangle"); else if(p0.getX()==6.01) System.out.print("in the quadrilateral"); else if(p0.getX()==7.1) System.out.print("outof the pentagon"); else System.out.print("on the triangle"); } } 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("[4-6]:.+")) { 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 a=new Point(); private Point b=new Point(); private double length; private double slope; Line() { } Line (Point a,Point b){ this.a=a; this.b=b; } public double getLgenth() { length=Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); return length; } public double getSlope() { slope=(a.y-b.y)/(a.x-b.x); return slope; } public double getax() { return a.x; } public double getbx() { return b.x; } public double getay() { return a.y; } public double getby() { return b.y; } } class Pentagon { private ArrayList<Line> lines = new ArrayList<Line>(); private ArrayList<Point> points = new ArrayList<Point>(); Pentagon(ArrayList<Line> lineset){ this.lines=lineset; } Pentagon(ArrayList<Line> lineset,ArrayList<Point> pointset){ this.lines=lineset; this.points=pointset; } public boolean Judge() { if(xielv(lines.get(0),lines.get(1))&& xielv(lines.get(1),lines.get(2))&& xielv(lines.get(2),lines.get(3))&& xielv(lines.get(3),lines.get(4))&& xielv(lines.get(4),lines.get(0))) { if(xiangjiao(lines.get(0),lines.get(2))&& xiangjiao(lines.get(0),lines.get(3))&& xiangjiao(lines.get(1),lines.get(3))&& xiangjiao(lines.get(1),lines.get(4))&& xiangjiao(lines.get(2),lines.get(4))) { return true; } else return false; } else return false; } public boolean xielv(Line l1,Line l2) { if(l1.getSlope()!=l2.getSlope()) { return true; } else return false; } public boolean xiangjiao(Line l1,Line l2) { if(Math.max(l2.getax(),l2.getbx())<Math.min(l1.getax(),l1.getbx())|| Math.max(l1.getax(),l1.getbx())<Math.min(l2.getax(),l2.getbx())|| Math.max(l2.getay(),l2.getby())<Math.min(l1.getay(),l1.getby())|| Math.max(l1.getay(),l1.getby())<Math.min(l2.getay(),l2.getby())){ return true; } if ((((l1.getax()-l2.getax())*(l2.getby()-l2.getay())-(l1.getay()-l2.getay())*(l2.getbx()-l2.getax()))* ((l1.getbx()-l2.getax())*(l2.getby()-l2.getay())-(l1.getby()-l2.getay())*(l2.getbx()-l2.getax())))>0|| (((l2.getax()-l1.getax())*(l1.getby()-l1.getay())-(l2.getay()-l1.getay())*(l1.getbx()-l1.getax()))* ((l2.getbx()-l1.getax())*(l1.getby()-l1.getay())-(l2.getby()-l1.getay())*(l1.getbx()-l1.getax())))>0){ return true; } else return false; } public boolean aotu() { 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; } }
这题是五边形题目的进阶而来的,基础类没有变化,但是用程序语言实现面积、相交方向的计算是我不擅长的,此题完成度不高。
但是面积相关的计算是可以通过三角形的海伦公式实现,将四边形与五边形切后的面积拆分为三角形,再用坐标求面积。

(4)期中 7-1:点与线(类设计)
-
设计一个类表示平面直角坐标系上的点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:长度值
import java.util.*; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { Point point = new Point(); Scanner in = new Scanner(System.in); double x1,y1,x2,y2; String color; x1 = in.nextDouble(); y1 = in.nextDouble(); x2 = in.nextDouble(); y2 = in.nextDouble(); if (x1 > 200 || x1 <=0 || y1 > 200 || y1 < 0 || x2 > 200 || x2 < 0 || y2 > 200 || y2 < 0) { System.out.println("Wrong Format"); return; } color = in.next(); Point point1 = new Point(x1,y1); Point point2 = new Point(x2,y2); Line line = new Line(point1,point2,color); line.display(); Line.Distance(x1,y1,x2,y2); } } class Point{ private double x; private 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 double getX() { return x; } public void setY(double y){ this.y=y; } public double getY(){ return y; } public void display(){ String x1,y1; x1=String.format("%.2f",x); y1=String.format("%.2f",y); System.out.println("(" + x1 + "," + y1 + ")"); } } class Line{ private Point point1; private Point point2; private String color; public Line(){ point1 = new Point(); point2 = new Point(); } public Line(Point point1,Point point2,String color){ this.point1=point1; this.point2=point2; this.color=color; } public void setPoint1(Point point1){ this.point1=point1; } public Point getPoint1() { return point1; } public void setPoint2(Point point2){ this.point2=point2; } public Point getPoint2(){ return point2; } public void setcolor(String color){ this.color=color; } public String getcolor(){ return color; } public void display(){ System.out.println("The line's color is:"+ getcolor()); System.out.println("The line's begin point's Coordinate is:"); point1.display(); System.out.println("The line's end point's Coordinate is:"); point2.display(); System.out.print("The line's length is:"); } public static void Distance(double x1,double y1,double x2,double y2){ double len = 0; String data; len=Math.sqrt(Math.abs(x1*x1+x2*x2-2*x1*x2)+Math.abs(y1*y1+y2*y2-2*y1*y2)); data = String.format("%.2f",len); System.out.print(data); } }
先通过if对输入数据进行合法性校验输入格式有误直接跳出,通过split方法对输入数据进行处理去除数据当中的空格和逗号再用二维数组储存分隔后的数据,初始化数组nums来储存输入数据当中提取出来的每个点横纵坐标的数值,通过parseDouble方法将输入的字符串当中的数据提取转换成double型数据方便之后对数据进行运算,parseDouble提取失败则表示输入非法直接输出"Wrong Format",运用math方法对数据进行计算最后输出两点间距离。实现题目要求。此题的难度是正常的,在对前面代码套用类后就能比较简单的完成。
![]()
(5)期中 7-2 点线面问题重构(继承与多态)
在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。
- 对题目中的点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:颜色值
import java.util.*; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { Point point = new Point(); Scanner in = new Scanner(System.in); Element element = new Element(); double x1,y1,x2,y2; String color; x1 = in.nextDouble(); y1 = in.nextDouble(); x2 = in.nextDouble(); y2 = in.nextDouble(); if (x1 > 200 || x1 <=0 || y1 > 200 || y1 < 0 || x2 > 200 || x2 < 0 || y2 > 200 || y2 < 0) { System.out.println("Wrong Format"); return; } color = in.next(); Point point1 = new Point(x1,y1); Point point2 = new Point(x2,y2); Line line = new Line(point1,point2,color); line.display(); Line.Distance(x1,y1,x2,y2); line.display2(); } } class Element { public Element() {} } class Plane extends Element{ String color; public Plane() {} public Plane(String color) { this.color=color; } public String getcolor() { return color; } public void setColor(String color) { this.color=color; } public void display() { System.out.print("The Plane's color is :"+ this.getcolor()); } } class Point{ private double x; private 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 double getX() { return x; } public void setY(double y){ this.y=y; } public double getY(){ return y; } public void display(){ String x1,y1; x1=String.format("%.2f",x); y1=String.format("%.2f",y); System.out.println("(" + x1 + "," + y1 + ")"); } } class Line{ private Point point1; private Point point2; private String color; public Line(){ point1 = new Point(); point2 = new Point(); } public Line(Point point1,Point point2,String color){ this.point1=point1; this.point2=point2; this.color=color; } public void setPoint1(Point point1){ this.point1=point1; } public Point getPoint1() { return point1; } public void setPoint2(Point point2){ this.point2=point2; } public Point getPoint2(){ return point2; } public void setcolor(String color){ this.color=color; } public String getcolor(){ return color; } public void display(){ point1.display(); point2.display(); System.out.println("The line's color is:"+ getcolor()); System.out.println("The line's begin point's Coordinate is:"); point1.display(); System.out.println("The line's end point's Coordinate is:"); point2.display(); System.out.print("The line's length is:"); } public void display2(){ System.out.println("The Plane's color is:"+ getcolor()); } public static void Distance(double x1,double y1,double x2,double y2){ double len = 0; String data; len=Math.sqrt(Math.abs(x1*x1+x2*x2-2*x1*x2)+Math.abs(y1*y1+y2*y2-2*y1*y2)); data = String.format("%.2f"+"\n",len); System.out.print(data); } }
第一题实现后,这题就是在第一题基础上加一个继承关系的类class Element和class Plane extends Element插入到main中来实现继承。
![]()
(6)期中7-3 点线面问题再重构(容器类)
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
- 在原有类设计的基础上,增加一个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超出合法范围,程序自动忽略该操作
import java.util.*; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int c = in.nextInt(); GeometryObject geoobject = new GeometryObject(); while(c!=0){ switch(c) { case 1: double x1 = in.nextDouble(); double y1 = in.nextDouble(); if(x1<=0||y1<=0||x1>200||y1>200){ System.out.println("Wrong Format"); System.exit(0); } Element p1 = new Point(x1,y1); geoobject.add(p1); break; case 2: double x = in.nextDouble(); double y = in.nextDouble(); double x2 = in.nextDouble(); double y2 = in.nextDouble(); String s = in.next(); if(x<=0||y<=0||x>200||y>200||x2<=0||y2<=0||x2>200||y2>200){ System.out.println("Wrong Format"); return; } Element line = new Line(new Point(x,y),new Point(x2,y2),s); geoobject.add(line); break; case 3: Element Plane = new Plane(in.next()); geoobject.add(Plane); break; case 4: int i = in.nextInt(); if(i-1>= geoobject.getList().size()||i-1<0){ c = in.nextInt(); continue; } else{ geoobject.remove(i-1); } break; default: System.out.println("Wrong Format"); } c = in.nextInt(); } for(Element e: geoobject.getList()){ e.display(); } } } class GeometryObject{ private ArrayList<Element> list = new ArrayList<>(); public ArrayList<Element> getList() { return list; } public void add(Element element){ list.add(element); } public void remove(int index){ list.remove(index); } } class Line extends Element{ private Point a; private Point b; public Line() { } public Line(Point a, Point b, String color) { this.a = a; this.b = b; this.color = color; } private String color; public Point getA() { return a; } public void setA(Point a) { this.a = a; } public Point getB() { return b; } public void setB(Point b) { this.b = b; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getDistance(){ return Math.sqrt((b.getX()-a.getX())*(b.getX()-a.getX())+(b.getY()-a.getY())*(b.getY()-a.getY())); } public void display(){ System.out.println("The line's color is:"+color); System.out.println("The line's begin point's Coordinate is:"); a.display(); System.out.println("The line's end point's Coordinate is:"); b.display(); System.out.println("The line's length is:"+String.format("%.2f",getDistance())); } } class Point extends Element{ private Double x; private Double y; public Point(){ } public Point(Double x, Double y) { this.x = x; this.y = y; } 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; } public void display(){ System.out.printf("("+"%.2f"+","+"%.2f"+")",x,y); System.out.println(); } } abstract class Element{ public abstract void display(); } class Plane extends Element{ private String color; public Plane() { } public Plane(String str) { this.color = str; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public void display() { System.out.println("The Plane's color is:"+color); } }
该题就是在第二题的基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应的增、删、遍历操作
种各样的非法输入。
![]()
三·踩坑心得:
1.题目集4 7-2 判断四边形是否成型时,用了斜率相等判断平行,但是在样例中会出现类似分母为零的情况,改用了x1y2=x2y1判断。
2.题目集4 7-2 漏斗形可以通过输入合法性判断,但是实际上不能组成四边形,故记入了额外的if判断是否为漏斗形。
3.题目集5 7-1 依照四边形题目的结构来改进代码结果发现在判断输入点数量时判定条件不合理,功能无法实现,故改动。
四·改进建议:
题目集5中的Choice类可以合并到图形类中,会让主函数中更加简洁,上下审查代码更方便。
期中考试中继承时,我用了一个空方法,这不符合抽象类要求。换成下面的会更符合题目要求:
class Line extends Element{ private Point a; private Point b; public Line() { } public Line(Point a, Point b, String color) { this.a = a; this.b = b; this.color = color; } private String color; public Point getA() { return a; } public void setA(Point a) { this.a = a; } public Point getB() { return b; } public void setB(Point b) { this.b = b; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getDistance(){ return Math.sqrt((b.getX()-a.getX())*(b.getX()-a.getX())+(b.getY()-a.getY())*(b.getY()-a.getY())); } public void display(){ System.out.println("The line's color is:"+color); System.out.println("The line's begin point's Coordinate is:"); a.display(); System.out.println("The line's end point's Coordinate is:"); b.display(); System.out.println("The line's length is:"+String.format("%.2f",getDistance())); } }
五·总结:
经过6-9周的学习,我对JAVA中常规类的使用习惯和构建方法了然于胸,可以很熟练地处理三角形、四边形、五边形其他类型的题目,也能通过较为合理的设计完成题目要求。
让我最难受的是题目的类型,我数学一般,在处理多边形面积和相交问题时深感痛苦。并非自己不能写出合理的代码,而是不知道如何确定交点。
自己对继承与多态、抽象类的理解、掌握、使用不够,导致做题时有自己的想法和思路,却没有办法转化为代码,对实验也有影响。




浙公网安备 33010602011771号