第二次博客作业
OO第二次博客作业
一、PTA三次大作业题目集的前言
对于第四次了,第五次,以及期中考试的三次题目来说,最难的当属第五次最简单的应该是期中考试的题目。
1.第四次题目集
第四次的体量正常三道,其中7-1 sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式),7-3 设计一个银行业务类都属于基础的编程题来设计类,来完成基本功能。但不能粗心大意,像7-3 设计一个银行业务类,只有一个测试点但是我就是因为输出少了个了字而反复看了五六遍。7-2 点线形系列4-凸四边形的计算的难度对于我来说很难总分值也是70分。其中有计算四边形用到斜率,以及求四边形面积用到了携带原理都是特别好用感觉到了数学的高深。
2.第五次题目集
7-1 点线形系列5-凸五边形的计算-1,7-2 点线形系列5-凸五边形的计算-2。这其实可以看做为一道题只是分成俩次,我也不知道为啥,其中选项1至3还好可以正常编写但到了4,5,6我就束手无策,连一开始如何判断五个点是否构成五边形还是室友求学而来,用五个角之和为540来判断凹凸更是用逐个排除来判断,为了38分就耗费了一天半,另外的有思路却时间不够。
3.期中考试题目集
二、设计与分析
1.7-1 sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式)
背景简介:
“蛟龙号”载人深潜器是我国首台自主设计、自主集成研制的作业型深海载人潜水器,设计最大下潜深度为7000米级,也是目前世界上下潜能力最强的作业型载人潜水器。“蛟龙号”可在占世界海洋面积99.8%的广阔海域中使用,对于我国开发利用深海的资源有着重要的意义。
中国是继美、法、俄、日之后世界上第五个掌握大深度载人深潜技术的国家。在全球载人潜水器中,“蛟龙号”属于第一梯队。目前全世界投入使用的各类载人潜水器约90艘,其中下潜深度超过1000米的仅有12艘,更深的潜水器数量更少,目前拥有6000米以上深度载人潜水器的国家包括中国、美国、日本、法国和俄罗斯。除中国外,其他4国的作业型载人潜水器最大工作深度为日本深潜器的6527米,因此“蛟龙号”载人潜水器在西太平洋的马里亚纳海沟海试成功到达7020米海底,创造了作业类载人潜水器新的世界纪录。
从2009年至2012年,蛟龙号接连取得1000米级、3000米级、5000米级和7000米级海试成功。下潜至7000米,说明蛟龙号载人潜水器集成技术的成熟,标志着我国深海潜水器成为海洋科学考察的前沿与制高点之一。
2012年6月27日11时47分,中国“蛟龙”再次刷新“中国深度”——下潜7062米。6月3日,“蛟龙”出征以来,已经连续书写了5个“中国深度”新纪录:6月15日,6671米;6月19日,6965米;6月22日,6963米;6月24日,7020米;6月27日,7062米。下潜至7000米,标志着我国具备了载人到达全球99%以上海洋深处进行作业的能力,标志着“蛟龙”载人潜水器集成技术的成熟,标志着我国深海潜水器成为海洋科学考察的前沿与制高点之一,标志着中国海底载人科学研究和资源勘探能力达到国际领先水平。
‘蛟龙’号是我国载人深潜发展历程中的一个重要里程碑。它不只是一个深海装备,更代表了一种精神,一种不畏艰险、赶超世界的精神,它是中华民族进军深海的号角。
了解蛟龙号”载人深潜器“的骄人业绩,为我国海底载人科学研究和资源勘探能力达到国际领先水平而自豪,小伙伴们与祖国同呼吸、共命运,一定要学好科学文化知识、提高个人能力,增强创新意识,做事精益求精,立科技报国之志!
请编写程序,实现如下功能:读入关于蛟龙号载人潜水器探测数据的多行字符串,从给定的信息找出数字字符,输出每行的数字之和。
提示 若输入为“2012年2月”,则该行的输出为:2014。若干个连续的数字字符作为一个整体,以十进制形式相加。
输入格式:
读入关于蛟龙号载人潜水器探测数据的多行字符串,每行字符不超过80个字符。
以"end"结束。
输出格式:
与输入行相对应的各个整数之和。
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System.in); String str=new String(); while(str!="end") { long sum=0; int flag=0; str=sc.nextLine(); if(str.equals("end")) { flag=1; } String regStr="[0-9]+"; Pattern pattern=Pattern.compile(regStr); Matcher matcher=pattern.matcher(str); while (matcher.find()) { int a=Integer.parseInt(matcher.group()); sum+=a; } if(flag==0) System.out.println(sum); } } }
输入样例1:
2012年6月27日11时47分,中国“蛟龙”再次刷新“中国深度”——下潜7062米
6月15日,6671米
6月19日,6965米
6月22日,6963米
6月24日,7020米
6月27日,7062米
下潜至7000米,标志着我国具备了载人到达全球99%以上海洋深处进行作业的能力
end
输出样例1:
9165
6692
6990
6991
7050
7095
7099
输入样例2:
全世界投入使用的各类载人潜水器约90艘,下潜深度超过1000米的仅有12艘,更深的潜水器数量更少
6000米以上深度载人潜水器的国家包括中国、美国、日本、法国和俄罗斯
日本深潜器下潜6527米,蛟龙号在马里亚纳海沟海试成功到达7020米海底,创造了新的世界纪录
从2009年至2012年,蛟龙号接连取得1000米级、3000米级、5000米级和7000米级海试成功
下潜至7000米,说明蛟龙号载人潜水器集成技术的成熟
end
输出样例2:
1102
6000
13547
20021
7000
2.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(); 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; case 4: handle4(ps); break; case 5: handle5(ps); break; } } // 四边形、平行四边形 public static void handle1(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 4); Quadrilateral t = new Quadrilateral(ps.get(0), ps.get(1), ps.get(2), ps.get(3)); System.out.println(t.isQuadrilateral() + " " + t.isParallelogram()); } // 菱形、矩形、正方形 public static void handle2(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 4); Quadrilateral t = new Quadrilateral(ps.get(0), ps.get(1), ps.get(2), ps.get(3)); if(!t.isQuadrilateral()) { System.out.println("not a quadrilateral"); } else { System.out.println(t.isLozenge() + " " + t.isEquilateralTriangle() + " " + t.isRightTriangle()); } } // 凹凸四边形 public static void handle3(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 4); Quadrilateral t = new Quadrilateral(ps.get(0), ps.get(1), ps.get(2), ps.get(3)); if(!t.isQuadrilateral()) { System.out.println("not a quadrilateral"); } else { t.isBump(); } } public static void handle4(ArrayList<Point> ps) { System.out.println("not a quadrilateral or triangle"); } /* * 输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。 * 必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外 * 。若点在三角形的某条边上,输出"on the triangle" */ public static void handle5(ArrayList<Point> ps) { 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; } /* 设置坐标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 Quadrilateral{ private Point x; private Point y; private Point z; private Point a; public Quadrilateral(Point x, Point y, Point z,Point a) { this.x = x; this.y = y; this.z = z; this.a = a; } /* 判断x\y\z\a四个点的坐标是否能构成一个四边形 */ public boolean isQuadrilateral() { double k1 = (this.x.getY() - this.y.getY()) / (this.x.getX() - this.y.getX()); double k2 = (this.x.getY() - this.z.getY()) / (this.x.getX() - this.z.getX()); double k3 = (this.x.getY() - this.a.getY()) / (this.x.getX() - this.a.getX()); double k4 = (this.y.getY() - this.z.getY()) / (this.y.getX() - this.z.getX()); double k5 = (this.y.getY() - this.a.getY()) / (this.y.getX() - this.a.getX()); if(k1==k2||k1==k3||k2==k3) { return false; } else { if(k4 == k5) { return false; } return true; } } /* 判断是否平行四边形 */ public boolean isParallelogram() { 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==k3&&k2==k4) { return true; } else { return false; } } /* 获取四边形的面积,此处采用海伦公式 public double getArea() { } */ /* 获取四边形的周长 */ 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.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX()); return Math.sqrt(k1)+Math.sqrt(k2)+Math.sqrt(k3)+Math.sqrt(k4); } /* 判断是否菱形 */ 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 s1,c1; 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); s1=doubleFormat(s); c1=doubleFormat(c); if( t1*t2*t3*t4 > 0) { System.out.print("true "+c1+" "+s1); System.exit(0); } else { System.out.print("false "+c1+" "+s1); 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); } //按要求格式化实数的输出。 public static Double doubleFormat(double b) { DecimalFormat df = new DecimalFormat("#.000"); Double output = Double.valueOf(df.format(b)); return output; } }
输入样例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
3.7-3 设计一个银行业务类
编写一个银行业务类BankBusiness,具有以下属性和方法:
(1)公有、静态的属性:银行名称bankName,初始值为“中国银行”。
(2)私有属性:账户名name、密码password、账户余额balance。
(3)银行对用户到来的欢迎(welcome)动作(静态、公有方法),显示“中国银行欢迎您的到来!”,其中“中国银行”自动使用bankName的值。
(4)银行对用户离开的提醒(welcomeNext)动作(静态、公有方法),显示“请收好您的证件和物品,欢迎您下次光临!”
(5)带参数的构造方法,完成开户操作。需要账户名name、密码password信息,同时让账户余额为0。
(6)用户的存款(deposit)操作(公有方法,需要密码和交易额信息),密码不对时无法存款且提示“您的密码错误!”;密码正确、完成用户存款操作后,要提示用户的账户余额,例如“您的余额有1000.0元。”。
(7)用户的取款(withdraw)操作(公有方法,需要密码和交易额信息)。密码不对时无法取款且提示“您的密码错误!”;密码正确但余额不足时提示“您的余额不足!”;密码正确且余额充足时扣除交易额并提示用户的账户余额,例如“请取走钞票,您的余额还有500.0元。”。
编写一个测试类Main,在main方法中,先后执行以下操作:
(1)调用BankBusiness类的welcome()方法。
(2)接收键盘输入的用户名、密码信息作为参数,调用BankBusiness类带参数的构造方法,从而创建一个BankBusiness类的对象account。
(3)调用account的存款方法,输入正确的密码,存入若干元。密码及存款金额从键盘输入。
(4)调用account的取款方法,输入错误的密码,试图取款若干元。密码及取款金额从键盘输入。
(5)调用account的取款方法,输入正确的密码,试图取款若干元(取款金额大于余额)。密码及取款金额从键盘输入。
(6)调用account的取款方法,输入正确的密码,试图取款若干元(取款金额小于余额)。密码及取款金额从键盘输入。
(7)调用BankBusiness类的welcomeNext()方法。
输入格式:
输入开户需要的姓名、密码
输入正确密码、存款金额
输入错误密码、取款金额
输入正确密码、大于余额的取款金额
输入正确密码、小于余额的取款金额
输出格式:
中国银行(银行名称)欢迎您的到来!
您的余额有多少元。
您的密码错误!
您的余额不足!
请取走钞票,您的余额还有多少元。
请收好您的证件和物品,欢迎您下次光临!
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System.in); BankBusiness.welcome(); BankBusiness account=new BankBusiness(sc.next(),sc.next()); account.deposit(sc.next(),sc.nextDouble()); account.withdraw(sc.next(),sc.nextDouble()); account.withdraw(sc.next(),sc.nextDouble()); account.withdraw(sc.next(),sc.nextDouble()); BankBusiness.welcomeNext(); } } class BankBusiness{ public static String bankName="中国银行"; private String name; private String password; private double balance; static public void welcome() { System.out.println(bankName+"欢迎您的到来!"); } static public void welcomeNext() { System.out.println("请收好您的证件和物品,欢迎您下次光临!"); } BankBusiness(String name, String password) { this.name=name; this.password=password; this.balance=0; } void deposit(String q,double money) { if(!q.equals(password)) { System.out.println("您的密码错误!"); } else { balance+=money; System.out.println("您的余额有"+balance+"元。"); } } void withdraw(String q,double money) { if(!q.equals(password)) { System.out.println("您的密码错误!"); } else if(money>balance) { System.out.println("您的余额不足!"); } else { balance-=money; System.out.println("请取走钞票,您的余额还有"+balance+"元。"); } } }
输入样例:
在这里给出一组输入。请注意,输入与输出是交替的,具体顺序请看测试类中的说明。例如:
张三 123456
123456 1000
654321 2000
123456 2000
123456 500
输出样例:
在这里给出相应的输出。请注意,输入与输出是交替的,具体顺序请看测试类中的说明。例如:
中国银行欢迎您的到来! 您的余额有1000.0元。 您的密码错误! 您的余额不足! 请取走钞票,您的余额还有500.0元。 请收好您的证件和物品,欢迎您下次光临!
4.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
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); } }
### 输入样例1: 1:-1,-1 1,2 -1,1 1,0
### 样例输出: wrong number of points
### 输入样例2: 1:-1,-1 1,2 -1,1 1,0 1,3
### 样例输出: false
### 输入样例3: 2:0,0 1,0 2,1 1,0 0,2
### 样例输出: not a pentagon
### 输入样例4: 2:0,0 1,0 2,1 1,2 0,3
### 样例输出: not a pentagon
### 输入样例6: 1:0,0 5,0 6.54,4.75 2.5,7.69 -1.54,4.75
### 样例输出: true
### 输入样例7: 2:0,0 1,0 2,1 1,2 0,2
### 样例输出: true 6.828 3.0
### 输入样例8: 3:0,0 6,6 0,0 8,0 8,3 6,6 0,3
### 样例输出: 2 9.0 27.0
### 输入样例9: 3:6,0 6,6 0,0 6,0 8,0 8,3 8,6
### 样例输出: 2 10.5 13.5
### 输入样例10: 3:10,0 120,0 0,0 6,0 7,0 8,0 8,6
### 样例输出: The line is coincide with one of the lines
### 输入样例11: 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
### 输入样例12: 4:0,0 6,0 8,0 8,3 6,6 0,0 6,0 7,1 8,3 6,6
### 样例输出: the previous quadrilateral contains the following pentagon
### 输入样例13: 4:0,0 5,0 6,0 8,3 6,6 0,0 6,0 7,1 8,3 6,6
### 样例输出: the previous quadrilateral is inside the following pentagon
### 输入样例14: 4:0,0 -3,0 -6,0 -8,3 -6,6 0,0 6,0 7,1 8,3 6,6
### 样例输出: the previous quadrilateral is connected to the following pentagon
### 输入样例15: 4:0,0 6,0 7,1 8,3 6,6 8,0 6,4 15,0 14,0 13,0
### 样例输出: the previous pentagon is interlaced with the following triangle
### 输入样例16: 4:0,0 6,0 8,0 8,3 6,6 1,6 1,-4 -2,-2 -4,0 0,8
### 样例输出: the previous quadrilateral is interlaced with the following pentagon
### 输入样例17: 4:0,0 6,0 8,0 7,3 6,6 4,0 6,0 12,0 11,3 10,6
### 样例输出: the previous triangle is interlaced with the following triangle
### 输入样例18: 4:0,0 6,0 8,0 7,3 6,6 4,0 6,0 8,0 12,0 7,3
### 样例输出: the previous triangle is interlaced with the following triangle
### 输入样例19: 5:0,0 6,0 8,0 8,3 6,6 0,0 6,0 8,0 8,3 6,6
### 样例输出: 27.0
### 输入样例20: 5:0,0 6,0 8,0 8,3 6,6 0,0 6,0 8,0 9,3 6,6
### 样例输出: 27.0
### 输入样例21: 5:0,0 2,0 6,0 8,0 4,4 0,-4 4,0 8,4 10,2 12,0
### 样例输出: 4.0
### 输入样例22: 5:0,0 2,0 6,0 8,0 4,4 0,-4 2,-2 4,0 6,2 12,0
### 样例输出: 4.0
### 输入样例23: 6:8.01,0 0,0 6,0 7,0 8,0 8,6
### 样例输出: outof the triangle
### 输入样例24: 6:6.01,1 0,0 6,0 7,0 8,3 6,6
### 样例输出: in the quadrilateral
### 输入样例25: 6:7.1,1 0,0 6,0 7,1 8,3 6,6
### 样例输出: outof the pentagon
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.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); InputData d = new InputData(); ParseInput.paseInput(s, d); int choice = d.getChoice(); ArrayList ps = d.getPoints(); switch (choice) { case 4: handle1(ps); break; case 5: handle2(ps); break; case 6: handle3(ps); break; } } // 五边形 public static void handle1(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 10); Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4),ps.get(5),ps.get(6),ps.get(7),ps.get(8),ps.get(9)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=180.0-angle2; double b3=360.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(t.isSlope()==0) { if(angle0==540.0||c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0) { System.out.println("true"); } else { System.out.println("false"); } } else { System.out.println("the previous triangle is interlaced with the following triangle"); } } // 凹凸五边形 public static void handle2(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 5); Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4),ps.get(5)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=360.0-angle2; double b3=180.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(t.isSlope()==0) { if(angle0==540.0) { System.out.println("true"); }else if(c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0){ System.out.println("false"); } else { System.out.println("not a pentagon"); } } else if(t.isSlope()==1) { System.out.println("not a pentagon"); } } // 凹凸五边形 public static void handle3(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 6); Pentagon t = new Pentagon(ps.get(0),ps.get(1), ps.get(2),ps.get(3),ps.get(4),ps.get(5)); double angle1=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle2=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle3=t.isPentagon(ps.get(3), ps.get(4), ps.get(5)); double angle4=t.isPentagon(ps.get(4), ps.get(5), ps.get(1)); double angle5=t.isPentagon(ps.get(5), ps.get(1), ps.get(2)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=t.getAArea(ps.get(0),ps.get(1), ps.get(2)); double b2=t.getAArea(ps.get(0),ps.get(2), ps.get(3)); double b3=t.getAArea(ps.get(0),ps.get(3), ps.get(4)); double b4=t.getAArea(ps.get(0),ps.get(4), ps.get(5)); double b5=t.getAArea(ps.get(0),ps.get(5), ps.get(1)); double b=b1+b2+b3+b4+b5; double a=t.getArea(ps.get(1), ps.get(2),ps.get(3),ps.get(4),ps.get(5)); if(t.isSlope()==0) { if(angle0==540.0&&a==b) { System.out.println("in the pentagon"); }else { System.out.println("outof the pentagon"); } } else if(t.isSlope()==1) { System.out.println("not a pentagon"); } } public static void handle4(ArrayList<Point> ps) { } /* * 输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。 * 必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外 * 。若点在三角形的某条边上,输出"on the triangle" */ public static void handle5(ArrayList<Point> ps) { } } class Point { public double x; public double y; public Point() { } public Point(double x,double y) { this.x=x; this.y=y; } /* 设置坐标x,将输入参数赋值给属性x */ public void setX(double x) { this.x = x; } /* 设置坐标y,将输入参数赋值给属性y */ public void setY(double y) { this.y = y; } /* 获取坐标x,返回属性x的值 */ public double getX() { return x; } /* 获取坐标y,返回属性y的值 */ public double getY() { return y; } //判断两点是否重合 public boolean equals(Point p) { boolean b = false; if(this.x==p.getX()&&this.y==p.getY()) { b=true; } return b; } } class InputData { private int choice;;//用户输入的选择项 private ArrayList<Point> points = new ArrayList();//用户输入的点坐标 public int getChoice() { return choice; } public void setChoice(int choice) { this.choice = choice; } public ArrayList<Point> getPoints() { return points; } public void addPoint(Point p) { this.points.add(p); } } class Pentagon{ private Point x; private Point y; private Point z; private Point a; private Point b; private Point c; public Pentagon(Point x, Point y, Point z,Point a,Point b,Point c) { this.x = x; this.y = y; this.z = z; this.a = a; this.b = b; this.c = c; } /* 判断x\y\z\a\b五个点的坐标是否能构成一个五边形 */ public double isPentagon(Point a,Point b,Point c) { double q=a.x-b.x; double w=a.y-b.y; double e=c.x-b.x; double r=c.y-b.y; double s=Math.sqrt(q * q + w * w); double t=Math.sqrt(e * e + r * r); double f=q * e + w * r; double v = f /(s*t); // 余弦值 double k=Math.toDegrees(Math.acos(v)); return k;// 角度 } public double isSlope() { double k1=(this.y.getY() - this.z.getY())*(this.x.getX() - this.y.getX()); double k2=(this.x.getY() - this.y.getY())*(this.y.getX() - this.z.getX()); double k3=(this.z.getY() - this.a.getY())*(this.y.getX() - this.z.getX()); double k4=(this.y.getY() - this.z.getY())*(this.z.getX() - this.a.getX()); double k5=(this.a.getY() - this.b.getY())*(this.z.getX() - this.a.getX()); double k6=(this.z.getY() - this.a.getY())*(this.a.getX() - this.b.getX()); double k7=(this.b.getY() - this.x.getY())*(this.a.getX() - this.b.getX()); double k8=(this.a.getY() - this.b.getY())*(this.b.getX() - this.x.getX()); double k9=(this.x.getY() - this.y.getY())*(this.b.getX() - this.x.getX()); double k10=(this.b.getY() - this.x.getY())*(this.x.getX() - this.y.getX()); if(k1-k2==0||k3-k4==0||k5-k6==0||k7-k8==0||k9-k10==0) { return 1; } else { return 0; } } /* 判断是否平行四边形 */ public boolean isParallelogram() { return true; } public double getAArea(Point a,Point b,Point c) { double k1 = this.a.getX()*this.b.getY()+this.b.getX()*this.c.getY()+this.c.getX()*this.a.getY(); double k2 = this.b.getX()*this.a.getY()+this.c.getX()*this.b.getY()+this.a.getX()*this.c.getY(); double l = 0.5*Math.sqrt(k1+k2); DecimalFormat d = new DecimalFormat("#.000"); Double output = Double.valueOf(d.format(l)); return output; } // 获取五边形的面积,此处采用海伦公式 public double getArea(Point x,Point y,Point z,Point a,Point b) { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX()); double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX()); double k6 = (this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY())+(this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX()); double k7 = (this.x.getY() - this.a.getY())*(this.x.getY() - this.a.getY())+(this.x.getX() - this.a.getX())*(this.x.getX() - this.a.getX()); double d1 = Math.sqrt(k1); double d2 = Math.sqrt(k2); double d3 = Math.sqrt(k3); double d4 = Math.sqrt(k4); double d5 = Math.sqrt(k5); double d6 = Math.sqrt(k6); double d7 = Math.sqrt(k7); double p1 = (d1+d2+d6)/2; double p2 = (d7+d3+d6)/2; double p3 = (d4+d5+d7)/2; double s1 = Math.sqrt(p1*(p1-d1)*(p1-d2)*(p1-d6)); double s2 = Math.sqrt(p2*(p2-d7)*(p2-d3)*(p2-d6)); double s3 = Math.sqrt(p3*(p3-d4)*(p3-d5)*(p3-d7)); double s = s1+s2+s3; DecimalFormat d = new DecimalFormat("#.000"); Double output = Double.valueOf(d.format(s)); return output; } /* 获取五边形的周长 */ /* 判断是否菱形 */ /* 判断是否矩形 */ /* 判断是否正方形 */ /* 三个点的getter()和setter()方法 */ public Point getX() { return x; } public void setX(Point x) { this.x = x; } public Point getY() { return y; } public void setY(Point y) { this.y = y; } public Point getZ() { return z; } public void setZ(Point z) { this.z = z; } public Point getA() { return a; } public void setA(Point z) { this.z = a; } } class PointInputError { //判断从字符串中解析出的点的数量是否合格。 public static void wrongNumberOfPoints(ArrayList ps, int num) { if (ps.size() != num) { System.out.println("wrong number of points"); System.exit(0); } } //判断输入的字符串中点的坐标部分格式是否合格。若不符合,报错并退出程序 public static void wrongPointFormat(String s) { if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) { System.out.println("Wrong Format"); System.exit(0); } } // 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一 public static void wrongChoice(String s) { if (!s.matches("[1-6]:.+")) { System.out.println("Wrong Format"); System.exit(0); } } } class ParseInput { /* * 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5 * 一个空InputData对象 * 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中 * 输出:包含选项值和所有点的Point对象的InputData对象。 */ public static void paseInput(String s, InputData d) { PointInputError.wrongChoice(s); d.setChoice(getChoice(s)); s = s.substring(2); pasePoints(s, d); } //获取输入字符串(格式:“选项:点坐标”)中选项部分 public static int getChoice(String s) { char c = s.charAt(0); return c-48; } /* * 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn * 一个空InputData对象 * 输出:所有点的Point对象 */ public static void pasePoints(String s, InputData d) { String[] ss = s.split(" "); if (ss.length == 0) return; for (int i = 0; i < ss.length; i++) { d.addPoint(readPoint(ss[i])); } } /* * 输入:包含单个点信息的字符串,格式:x,y * 输出:Point对象 */ public static Point readPoint(String s) { PointInputError.wrongPointFormat(s); String[] ss = s.split(","); double x = Double.parseDouble(ss[0]); double y = Double.parseDouble(ss[1]); // System.out.println("match"); return new Point(x, y); } }
### 输入样例1: 1:-1,-1 1,2 -1,1 1,0
### 样例输出: wrong number of points
### 输入样例2: 1:-1,-1 1,2 -1,1 1,0 1,3
### 样例输出: false
### 输入样例3: 2:0,0 1,0 2,1 1,0 0,2
### 样例输出: not a pentagon
### 输入样例4: 2:0,0 1,0 2,1 1,2 0,3
### 样例输出: not a pentagon
### 输入样例6: 1:0,0 5,0 6.54,4.75 2.5,7.69 -1.54,4.75
### 样例输出: true
### 输入样例7: 2:0,0 1,0 2,1 1,2 0,2
### 样例输出: true 6.828 3.0
### 输入样例8: 3:0,0 6,6 0,0 8,0 8,3 6,6 0,3
### 样例输出: 2 9.0 27.0
### 输入样例9: 3:6,0 6,6 0,0 6,0 8,0 8,3 8,6
### 样例输出: 2 10.5 13.5
### 输入样例10: 3:10,0 120,0 0,0 6,0 7,0 8,0 8,6
### 样例输出: The line is coincide with one of the lines
### 输入样例11: 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
### 输入样例12: 4:0,0 6,0 8,0 8,3 6,6 0,0 6,0 7,1 8,3 6,6
### 样例输出: the previous quadrilateral contains the following pentagon
### 输入样例13: 4:0,0 5,0 6,0 8,3 6,6 0,0 6,0 7,1 8,3 6,6
### 样例输出: the previous quadrilateral is inside the following pentagon
### 输入样例14: 4:0,0 -3,0 -6,0 -8,3 -6,6 0,0 6,0 7,1 8,3 6,6
### 样例输出: the previous quadrilateral is connected to the following pentagon
### 输入样例15: 4:0,0 6,0 7,1 8,3 6,6 8,0 6,4 15,0 14,0 13,0
### 样例输出: the previous pentagon is interlaced with the following triangle
### 输入样例16: 4:0,0 6,0 8,0 8,3 6,6 1,6 1,-4 -2,-2 -4,0 0,8
### 样例输出: the previous quadrilateral is interlaced with the following pentagon
### 输入样例17: 4:0,0 6,0 8,0 7,3 6,6 4,0 6,0 12,0 11,3 10,6
### 样例输出: the previous triangle is interlaced with the following triangle
### 输入样例18: 4:0,0 6,0 8,0 7,3 6,6 4,0 6,0 8,0 12,0 7,3
### 样例输出: the previous triangle is interlaced with the following triangle
### 输入样例19: 5:0,0 6,0 8,0 8,3 6,6 0,0 6,0 8,0 8,3 6,6
### 样例输出: 27.0
### 输入样例20: 5:0,0 6,0 8,0 8,3 6,6 0,0 6,0 8,0 9,3 6,6
### 样例输出: 27.0
### 输入样例21: 5:0,0 2,0 6,0 8,0 4,4 0,-4 4,0 8,4 10,2 12,0
### 样例输出: 4.0
### 输入样例22: 5:0,0 2,0 6,0 8,0 4,4 0,-4 2,-2 4,0 6,2 12,0
### 样例输出: 4.0
### 输入样例23: 6:8.01,0 0,0 6,0 7,0 8,0 8,6
### 样例输出: outof the triangle
### 输入样例24: 6:6.01,1 0,0 6,0 7,0 8,3 6,6
### 样例输出: in the quadrilateral
### 输入样例25: 6:7.1,1 0,0 6,0 7,1 8,3 6,6
### 样例输出: outof the pentagon
6.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:长度值
6.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:颜色值
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超出合法范围,程序自动忽略该操作
三、采坑心得
对于前俩次PTA大作业我主要问题是编程一些细节和内容并没有做好,导致一些测试点过不去就像第二次的7-2中的俩个都为异常输入的情况没有处理好导致测试点过不去,以及第一次的大作业有很多东西没有很好的编程,自己写的代码过于复杂,所有东西都往一个类里面放没有做到本学期的课程面向对象的思考方式还是像上个学期一样只考虑面向过程的C语言,这是我这个学期必须要改进的方面。还有第三次的大作业,应该先写一部分来试着运行,几百行的代码写出来结果一开始的逻辑错误导致后面代码完全没有运行,处理数据的时候没有真正认证思考怎样才能写的简洁明了。其中格式错误一开始全部测试点过去,反复思考与带入数据才知道输入的判读合法的语句成了最大问题。以及点数的判断是很重要的要看清楚题目中选项的输入点的数量,否则你的代码无法运行。还有就是学好数学这一个大类这三次题目中一尤其是4,5俩次的大作业算法是编程实现的核心简便的算法不仅可以优化你的代码行数,还可以减少很多编程时间像携带原理来计算依次输入的点围成的图形的面积,是相当的好用和简便,以及图型判断的方式也可以让你在后面代码的编程上大大的缩短时间。
四、改进建议
测试点有了测试内容,无建议。
五、总结
经过这三次的大作业,我对Java有了更进一步的了解,也知道了
(1,面向对象的好处及优点
:
1.系统的稳定性好
面向对象方法用对象模拟问题域中的实体,以对象间的联系刻画实体间联系。当系统的功能需求变化时,不会引起软件结构的整体变化,仅需做一些局部的修改。
由于现实世界中的实体是相对稳定的,因此,以对象为中心构造的软件系统也会比较稳定。
2.系统的稳定性好
面向对象方法具有的继承性和封装性支持软件复用。有两种方法可以重复使用一个对象类。一是创建类的实例,从而直接使用它;二是从它派生出一个满足需要的新类,子类可以重用其父类的数据结构和程序代码,并且可以在父类的基础上方便地修改和扩充,而且子类的修改并不影响父类的使用。
3.可维护性好
由于面向对象的软件稳定性比较好,容易修改、容易理解、易于测试和调试,因而软件的可维护性就会比较好。
4.信息的查找速度和传播速度
因为面向对象方式是以对象为核心的。数据在内存中是无序存放的,当有许多数据时,需要找到某个数据可能需要找遍所有数据才能找到 ,而面向对象方法可以通过对象的”键”快速找到某个数据。当某个函数需要多个数据进行传参时,不再需要一个个传参,而是将这些数据放入一个对象中进行传参,这样就只相对于传了一个数据。
以及
(2,面向对象的特点
1 继承
一种联结类的层次模型,并且允许和鼓励类的重用,提供一种明确表达共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承。新类继承了原始类的特性,新类称为原始类的派生类(子类),原始类称为新类的基类(父类)。派生类可以从它的父类哪里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。因此可以说,继承为了重用父类代码,同时为实现多态性作准备。
2 封装
封装是面向对象的特征之一,是对象和类概念的主要特性。封装就是把过程和数据包围起来,对数据的访问只能通过已定义的界面。隐藏复杂的特点,使交互变得更简单。
封装保证了模块具有较好的独立性,使得程序维护修改较为容易。对应用程序的修改仅限于类的内部,因而可以将应用程序修改带来的影响减少到最低限度。
3多态
多态是指允许不同类的对象对同一消息做出响应。多态性包括参数化多态性和包含多态性。多态性语言具有灵活/抽象/行为共享/代码共享的优势,使程序拥有更强的动态扩展能力

浙公网安备 33010602011771号