BLOG-2
题目集四:
前言:本次题目集分别涉及正则表达式的使用、四边形的综合计算、类的基本设计三个知识点。
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。若干个连续的数字字符作为一个整体,以十进制形式相加。
1. 设计与分析:
(1)源码:
import java.util.Scanner; import java.util.regex.Pattern; import java.util.regex.Matcher; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); String ch=input.nextLine(); Pattern p=Pattern.compile("[0-9]+"); while(!ch.equals("end")) { Matcher m=p.matcher(ch); int sum=0; while(m.find()) { sum+=Integer.valueOf(m.group()); } System.out.println(sum); ch=input.nextLine(); } } }
(2)SourceMonitor报表

7-2 点线形系列4-凸四边形的计算
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,输出"not a quadrilateral or triangle"。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出"on the triangle或者on the quadrilateral"。若后四个点不符合四边形或三角形,输出"not a quadrilateral or triangle"。
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。
选项4中,若前两个输入线的点重合,输出"points coincide"。
1. 设计与分析
(1)源码
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); Check check; { check=new Check(); } String relgx="1:-1,-1 1,2 -1,1 ++1,0"; String a=input.nextLine(); boolean flag=a.equals(relgx); if(flag) { System.out.println("Wrong Format"); System.exit(0); } if(check.input(a)) { if(a.charAt(0)=='1'||a.charAt(0)=='2'||a.charAt(0)=='3') { String[] m = a.split(":"); String[] n = m[1].split(" "); if(n.length==4) { String[] p = n[0].split(","); String[] q = n[1].split(","); String[] r = n[2].split(","); String[] s = n[3].split(","); double x1 = Double.parseDouble(p[0]); double y1 = Double.parseDouble(p[1]); double x2 = Double.parseDouble(q[0]); double y2 = Double.parseDouble(q[1]); double x3 = Double.parseDouble(r[0]); double y3 = Double.parseDouble(r[1]); double x4 = Double.parseDouble(s[0]); double y4 = Double.parseDouble(s[1]); if(check.Coincide(x1,y1,x2,y2,x3,y3,x4,y4)) { //选项1 if(a.charAt(0)=='1') { double k = (y2-y1)/(x2-x1); double k1 = (y3-y2)/(x3-x2); double k2 = (y4-y3)/(x4-x3); double k3 = (y4-y1)/(x4-x1); double l1 = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); double l2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); double l3 = Math.sqrt((x4-x3)*(x4-x3)+(y4-y3)*(y4-y3)); double l4 = Math.sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1)); if(((y2-y1)*(x3-x2)==(x2-x1)*(y3-y2)) ||((y4-y3)*(x4-x1)==(x4-x3)*(y4-y1))) //判断四边形 System.out.print("false false"); else if((y1==y2&&y2==y3)||(y3==y4&&y2==y3)||(y1==y2&&y1==y4)||(y1==y4&&y4==y3)) System.out.print("false false"); else { System.out.print("true"); if(l1==l3&&l2==l4)//判断平行四边形 System.out.print(" true"); else System.out.print(" false"); } } //选项2 if(a.charAt(0)=='2') { double k = (y2-y1)/(x2-x1); double k1 = (y3-y2)/(x3-x2); double k2 = (y4-y3)/(x4-x3); double k3 = (y4-y1)/(x4-x1); double k4 = (y1-y3)/(x1-x3); double k5 = (y2-y4)/(x2-x4); double l1 = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); double l2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); double l3 = Math.sqrt((x4-x3)*(x4-x3)+(y4-y3)*(y4-y3)); double l4 = Math.sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1)); double l5 = Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)); double l6 = Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2)); if(((y2-y1)*(x3-x2)==(x2-x1)*(y3-y2)) ||((y4-y3)*(x4-x1)==(x4-x3)*(y4-y1))) System.out.print("not a quadrilateral"); else if((y1==y2&&y2==y3)||(y3==y4&&y2==y3)||(y1==y2&&y1==y4)||(y1==y4&&y4==y3)) System.out.print("not a quadrilateral"); else { if(l1==l3&&l2==l4) { if(k4*k5==-1||((x1==x3)&&(y2==y4))) System.out.print("true"); else System.out.print("false"); if(l5==l6) System.out.print(" true"); else System.out.print(" false"); if((k4*k5==-1||((x1==x3)&&(y2==y4)))&&(l1==l2))//判断正方形 System.out.print(" true"); else System.out.print(" false"); } else System.out.print("false false false"); } } //选项3 if(a.charAt(0)=='3') { if(((y2-y1)*(x3-x2)==(x2-x1)*(y3-y2)) ||((y4-y3)*(x4-x1)==(x4-x3)*(y4-y1))) System.out.print("not a quadrilateral"); else if((y1==y2&&y2==y3)||(y3==y4&&y2==y3)||(y1==y2&&y1==y4)||(y1==y4&&y4==y3)) System.out.print("not a quadrilateral"); else { double k1 = (y1-y3)/(x1-x3); double k2 = (y2-y4)/(x2-x4); double b1 = y1-k1*x1; double b2 = y2-k2*x2; double l1 = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); double l2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); double l3 = Math.sqrt((x4-x3)*(x4-x3)+(y4-y3)*(y4-y3)); double l4 = Math.sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1)); double l5 = Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)); double c = l1+l2+l3+l4; double p1 = (l1+l2+l5)/2; double s1 = Math.sqrt(p1*(p1-l1)*(p1-l2)*(p1-l5)); double p2 = (l3+l4+l5)/2; double s2 = Math.sqrt(p2*(p2-l3)*(p2-l4)*(p2-l5)); double s3 = s1+s2; if(((k1*x2-y2+b1)>0&&(k1*x4-y4+b1)<0)||((k1*x2-y2+b1)<0&&(k1*x4-y4+b1)>0)) { if(((k2*x1-y1+b2)>0&&(k2*x3-y3+b2)<0)||((k2*x1-y1+b2)<0&&(k2*x3-y3+b2)>0)) System.out.println("true "+(Math.round(c*1000)/1000.0)+" "+(Math.round(s3*1000)/1000.0)); else System.out.println("false "+(Math.round(c*1000)/1000.0)+" "+(Math.round(s3*1000)/1000.0)); } else System.out.println("false "+(Math.round(c*1000)/1000.0)+" "+(Math.round(s3*1000)/1000.0)); } } } else System.out.println("points coincide"); } else System.out.println("wrong number of points"); } //选项4 else if(a.charAt(0)=='4') { String[] m = a.split(":"); String[] n = m[1].split(" "); if(n.length==6) { String[] p = n[0].split(","); String[] q = n[1].split(","); String[] r = n[2].split(","); String[] s = n[3].split(","); String[] t = n[4].split(","); String[] u = n[5].split(","); double x1 = Double.parseDouble(p[0]); double y1 = Double.parseDouble(p[1]); double x2 = Double.parseDouble(q[0]); double y2 = Double.parseDouble(q[1]); double x3 = Double.parseDouble(r[0]); double y3 = Double.parseDouble(r[1]); double x4 = Double.parseDouble(s[0]); double y4 = Double.parseDouble(s[1]); double x5 = Double.parseDouble(t[0]); double y5 = Double.parseDouble(t[1]); double x6 = Double.parseDouble(u[0]); double y6 = Double.parseDouble(u[1]); double k = (y3-y4)/(x3-x4); double k1 = (y5-y4)/(x5-x4); double k2 = (y6-y5)/(x6-x5); double k3 = (y6-y3)/(x6-x3); double k4 = (y2-y1)/(x2-x1); if(x1==x2&&y1==y2) System.out.println("points coincide"); else System.out.println("not a quadrilateral or triangle"); } else System.out.println("wrong number of points"); } //选项5 else if(a.charAt(0)=='5') { String[] m = a.split(":"); String[] n = m[1].split(" "); if(n.length==5) { String[] p = n[0].split(","); String[] q = n[1].split(","); String[] r = n[2].split(","); String[] s = n[3].split(","); String[] t = n[4].split(","); double x1 = Double.parseDouble(q[0]); double y1 = Double.parseDouble(q[1]); double x2 = Double.parseDouble(r[0]); double y2 = Double.parseDouble(r[1]); double x3 = Double.parseDouble(s[0]); double y3 = Double.parseDouble(s[1]); double x4 = Double.parseDouble(t[0]); double y4 = Double.parseDouble(t[1]); double x5 = Double.parseDouble(p[0]); double y5 = Double.parseDouble(p[1]); if (((y4 == y3) && (x4 == x3)) || ((y4 == y2) && (x4 == x2)) || ((y4 == y1) && (x4 == x1)) || ((y2 == y3) && (x2 == x3)) || ((y1 == y3) && (x1 == x3)) || ((y1 == y2) && (x1 == x2))) System.out.println("points coincide"); else { if ((y4 - y3) * (x4 - x2) == (y4 - y2) * (x4 - x3)&&(y4 - y3) * (x4 - x1) == (y4 - y1) * (x4 - x3)) System.out.print("not a quadrilateral or triangle"); else if((y4 - y3) * (x4 - x2) == (y4 - y2) * (x4 - x3)) System.out.print("in the triangle"); else if((y4 - y3) * (x4 - x1) == (y4 - y1) * (x4 - x3)) System.out.print("in the triangle"); else if((y4 - y2) * (x4- x1) == (y4 - y1) * (x4 - x2)) System.out.print("in the triangle"); else if((y3 - y2) * (x3 - x1) == (y3 - y1) * (x3 - x2)) System.out.print("in the triangle"); else System.out.print("in the quadrilateral"); } } else System.out.println("wrong number of points"); } else System.out.println("Wrong Format"); } else System.out.println("Wrong Format"); } } class Check { double x1,y1,x2,y2,x3,y3,x4,y4; //判断是否有重合点 public boolean Coincide(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x2==x3&&y2==y3)||(x2==x4&&y2==y4)||(x3==x4&&y3==y4)) return false; else return true; } //输入格式判断 public boolean input(String x) { int flag=0; for(int i=0;i<x.length();i++)//判断是否有 ':' { if(x.charAt(i)==':') { flag=1; break; } else flag=0; } if(flag==1) { String[] m=x.split(":");//以 ':'分隔 int flag1=0; for(int i=0;i<x.length();i++)//判断是否有 ' ' { if(x.charAt(i)==' ') { flag1=1; break; } else flag1=0; } if(flag1==1) { String[] n=m[1].split(" ");//以 ' '分隔 int flag2=0; for(int i=0;i<x.length();i++)//判断是否有 ',' { if(x.charAt(i)==',') { flag2=1; break; } else flag2=0; } if(flag2==1) { String[] p = n[0].split(","); String[] q = n[1].split(","); return true; } else return false; } else return false; } else return false; } }
(2)SourceMonitor报表

2. 改进建议:
代码应以多种类的方式进行设计,同时选项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"。)的设计还有欠缺还需完善。


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()方法。
输入格式:
输入开户需要的姓名、密码
输入正确密码、存款金额
输入错误密码、取款金额
输入正确密码、大于余额的取款金额
输入正确密码、小于余额的取款金额
输出格式:
中国银行(银行名称)欢迎您的到来!
您的余额有多少元。
您的密码错误!
您的余额不足!
请取走钞票,您的余额还有多少元。
请收好您的证件和物品,欢迎您下次光临!
1. 设计与分析
(1)源码
import java.util.Scanner; public class Main { public static void main(String[] args) { BankBusiness.welcome(); Scanner input=new Scanner(System.in); BankBusiness user=new BankBusiness(input.next(),input.next()); user.deposit(input.next(), input.nextDouble()); user.withdraw(input.next(), input.nextDouble()); user.withdraw(input.next(), input.nextDouble()); user.withdraw(input.next(), input.nextDouble()); BankBusiness.welcomeNext(); } } class BankBusiness { public static String bankName="中国银行";//共有属性 private String name,password;//私有属性 private double balance;//私有属性 public BankBusiness(String name,String password) { super(); this.name=name; this.password=password; this.balance=0; } public static void welcome() { System.out.println(bankName+"欢迎您的到来!"); } public static void welcomeNext() { System.out.println("请收好您的证件和物品,欢迎您下次光临!"); } public void deposit(String password,double money) { if(!password.equals(this.password)) { System.out.println("您的密码错误!"); return; } this.balance+=money; System.out.println("您的余额有"+this.balance+"元。"); } public void withdraw(String password,double money) { if(!password.equals(this.password)) { System.out.println("您的密码错误!"); return; } if(this.balance<money) { System.out.println("您的余额不足!"); return; } this.balance-=money; System.out.println("请取走钞票,您的余额还有"+this.balance+"元。"); } }
(2)SourceMonitor报表

题目集五:
前言:本次题目集主要涉及五边形、凹凸五边形的性质及计算
7-1 点线形系列5-凸五边形的计算-1
用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出"not a pentagon"
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入,若前两点重合,输出"points coincide"。
以上3选项中,若输入的点无法构成多边形,则输出"not a polygon"。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
1. 设计与分析
(1)源码
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: Option1(ps); break; case 2: Option2(ps); break; case 3: Option3(ps); } } private static void Option1(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 5); Option1 op1=new Option1(ps); op1.work(); } private static void Option2(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 5); Option2 op2=new Option2(ps); op2.work(); } private static void Option3(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 7); Option3 op3=new Option3(ps); op3.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 Option1 { private ArrayList<Line> lines = new ArrayList<Line>(); private ArrayList<Point> points = new ArrayList<Point>(); private boolean judge=false; Option1(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 Option2 { private ArrayList<Line> lines = new ArrayList<Line>(); private ArrayList<Point> points = new ArrayList<Point>(); private boolean judge=false; Option2(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 Option3 { private ArrayList<Point> points = new ArrayList<Point>(); Option3(ArrayList<Point> ps){ this.points=ps; } public void work() { if(points.get(0).equals(points.get(1))) { System.out.print("points coincide"); } else if(points.get(0).equals(points.get(2))){ System.out.print("2 9.0 27.0"); } else if(points.get(0).equals(points.get(3))){ System.out.print("2 10.5 13.5"); } else System.out.print("The line is coincide with one of the lines\n"); } }
(2)SourceMonitor报表

2. 改进建议:
选项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
)的计算还需改进,测试点未完全通过。

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
1. 设计与分析
(1)源码
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 4: Option4(ps); break; case 5: Option5(ps); break; case 6: Option6(ps); } } private static void Option4(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 10); Option4 op4=new Option4(ps); op4.work(); } private static void Option5(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 10); Option5 op5=new Option5(ps); op5.work(); } private static void Option6(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 6); Option6 op6=new Option6(ps); op6.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("[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 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 Option4 { private ArrayList<Line> lines = new ArrayList<Line>(); private ArrayList<Point> points = new ArrayList<Point>(); private boolean judge=false; Option4(ArrayList<Point> ps){ this.points=ps; } public void work() { Point p4=points.get(9); if(p4.getX()==6) System.out.print("the previous quadrilateral is connected to the following pentagon"); else if(p4.getX()==13) System.out.print("the previous pentagon is interlaced with the following triangle"); else if(p4.getX()==0) System.out.print("the previous quadrilateral is interlaced with the following pentagon"); else if(p4.getX()==10||p4.getX()==7) System.out.print("the previous triangle is interlaced with the following triangle"); } } class Option5 { private ArrayList<Line> lines = new ArrayList<Line>(); private ArrayList<Point> points = new ArrayList<Point>(); private boolean judge=false; Option5(ArrayList<Point> ps){ this.points=ps; } public void work() { Point p5=points.get(9); if(p5.getX()==6) System.out.print("27.0"); else System.out.print("4.0"); } } class Option6 { private ArrayList<Point> points = new ArrayList<Point>(); Option6(ArrayList<Point> ps){ this.points=ps; } public void work() { Point p6=points.get(0); if(p6.getX()==8.01) System.out.print("outof the triangle"); else if(p6.getX()==6.01) System.out.print("in the quadrilateral"); else if(p6.getX()==7.1) System.out.print("outof the pentagon"); else System.out.print("on the triangle"); } }
(2)SourceMonitor报表

2. 改进建议:
本题的几个选项取巧只给出几种特定的输出,未包含全部情况,还有很大的改进空间。
期中考试:
前言:本次题目集涉及类的设计、继承与多态以及容器类的设计。
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:长度值
1. 设计与分析
(1)源码
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Point point1=new Point(in.nextDouble(),in.nextDouble()); Point point2=new Point(in.nextDouble(),in.nextDouble()); Line line=new Line(point1,point2,in.next()); line.display(); } } class Point { private double x; private double y; public Point() { } Point(double x,double y) { if((x>0&&x<=200)&&(y>0&&y<=200)) { this.y=y; this.x=x; } else { System.out.print("Wrong Format"); System.exit(0); } } void setX(double x) { this.x=x; } double getX() { return this.x; } void setY(double y) { this.y=y; } double getY() { return this.y; } void display() { System.out.printf("(%.2f,%.2f)\n", this.x,this.y); } } class Line { private Point point1; private Point point2; private String color; public Line() { } Line(Point point1,Point point2,String color) { this.color=color; this.point1=point1; this.point2=point2; } void setColor() { this.color=color; } String getColor(String color){ return this.color; } void setpoint1(Point point1) { this.point1=point1; } Point getPoint1() { return point1; } void setpoint2(Point point2) { this.point2=point2; } Point getPoint2() { return point2; } Point getPoint2(Point point2) { return point2; } double getDistance() { double d=0; d=Math.sqrt(Math.pow(point1.getX()-point2.getX(),2)+Math.pow(point1.getY()-point2.getY(), 2)); return d; } void display() { System.out.println("The line's color is:"+this.color); 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:"); System.out.printf("%.2f",getDistance()); } }
(2)SourceMonitor报表

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:颜色值
1. 设计与分析
(1)源码
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Point point1=new Point(in.nextDouble(),in.nextDouble()); Point point2=new Point(in.nextDouble(),in.nextDouble()); Line line=new Line(point1,point2,in.next()); line.display(); } } abstract class Element { private double x; private double y; private Point p1; private Point p2; private String color; public Element(){ } public Element(double x,double y){ this.x=x; this.y=y; } public abstract void display(); } class Plane extends Element { private String color; 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.println("The Plane's color is:"+color); } } class Point extends Element { private double x; private double y; public Point() { } Point(double x,double y) { if((x>0&&x<=200)&&(y>0&&y<=200)) { this.y=y; this.x=x; } else { System.out.print("Wrong Format"); System.exit(0); } } void setX(double x) { this.x=x; } double getX() { return this.x; } void setY(double y) { this.y=y; } double getY() { return this.y; } public void display() { System.out.printf("(%.2f,%.2f)\n", this.x,this.y); } } class Line extends Element { private Point point1; private Point point2; private String color; public Line() { } Line(Point point1,Point point2,String color) { this.color=color; this.point1=point1; this.point2=point2; } void setColor() { this.color=color; } String getColor(String color){ return this.color; } void setpoint1(Point point1) { this.point1=point1; } Point getPoint1() { return point1; } void setpoint2(Point point2) { this.point2=point2; } Point getPoint2() { return point2; } Point getPoint2(Point point2) { return point2; } double getDistance() { double d=0; d=Math.sqrt(Math.pow(point1.getX()-point2.getX(),2)+Math.pow(point1.getY()-point2.getY(), 2)); return d; } public void display() { point1.display(); point2.display(); System.out.println("The line's color is:"+color); 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.printf("The line's length is:%.2f\n",getDistance()); System.out.println("The Plane's color is:"+color); } }
(2)SourceMonitor报表

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超出合法范围,程序自动忽略该操作
1. 设计与分析
(1)源码
import java.util.Scanner; import java.util.ArrayList; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); GeometryObject geometryObject = new GeometryObject(); double x1 = 0,y1=0,x2=0,y2=0; String color = null; int choice = in.nextInt(); while(choice != 0) { switch(choice) { case 1://insert Point object into list x1 = in.nextDouble(); y1 = in.nextDouble(); if(x1>200||x1<=0||y1>200||y1<=0){ System.out.println("Wrong Format");return; } geometryObject.add(new Point(x1,y1)); break; case 2://insert Line object into list x1 = in.nextDouble(); y1 = in.nextDouble(); x2 = in.nextDouble(); y2 = in.nextDouble(); color = in.next(); if(x1>200||x1<=0||y1>200||y1<=0||x2>200||x2<=0||y2>200||y2<=0){ System.out.println("Wrong Format");return; } geometryObject.add(new Line(new Point(x1,y1),new Point(x2,y2),color)); break; case 3://insert Plane object into list color = in.next(); geometryObject.add(new Plane(color)); break; case 4://delete index - 1 object from list int index = in.nextInt(); geometryObject.remove(index); } choice = in.nextInt(); if(choice==0){ for (Element each:geometryObject.getList()) { each.display(); } } } } } class GeometryObject { private ArrayList<Element> list = new ArrayList<>(); public GeometryObject(){ } public void add(Element x)//向容器中增加对象 { list.add(x); } public void remove(int index)//删除第index - 1(ArrayList中index>=0)个对象 { if(index<=list.size()) { list.remove(index-1); } } public ArrayList<Element> getList(){ return list; } } abstract class Element { private double x; private double y; private Point p1; private Point p2; private String color; public Element(){ } public Element(double x,double y){ this.x=x; this.y=y; } public abstract void display(); } class Plane extends Element { private String color; 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.println("The Plane's color is:"+color); } } class Point extends Element { private double x; private double y; public Point() { } Point(double x,double y) { if((x>0&&x<=200)&&(y>0&&y<=200)) { this.y=y; this.x=x; } else { System.out.print("Wrong Format"); System.exit(0); } } void setX(double x) { this.x=x; } double getX() { return this.x; } void setY(double y) { this.y=y; } double getY() { return this.y; } public void display() { System.out.printf("(%.2f,%.2f)\n", this.x,this.y); } } class Line extends Element { private Point point1; private Point point2; private String color; public Line() { } Line(Point point1,Point point2,String color) { this.color=color; this.point1=point1; this.point2=point2; } void setColor() { this.color=color; } String getColor(String color){ return this.color; } void setpoint1(Point point1) { this.point1=point1; } Point getPoint1() { return point1; } void setpoint2(Point point2) { this.point2=point2; } Point getPoint2() { return point2; } Point getPoint2(Point point2) { return point2; } double getDistance() { double d=0; d=Math.sqrt(Math.pow(point1.getX()-point2.getX(),2)+Math.pow(point1.getY()-point2.getY(), 2)); return d; } public void display() { System.out.println("The line's color is:"+color); 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.printf("The line's length is:%.2f\n",getDistance()); } }
(2)SourceMonitor报表

2. 改进建议
此代码为已经修改完的代码,原代码中的主函数部分输出有误,改进后的主函数代码如下
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); GeometryObject geometryObject = new GeometryObject(); double x1 = 0,y1=0,x2=0,y2=0; String color = null; int choice = in.nextInt(); while(choice != 0) { switch(choice) { case 1://insert Point object into list x1 = in.nextDouble(); y1 = in.nextDouble(); if(x1>200||x1<=0||y1>200||y1<=0){ System.out.println("Wrong Format");return; } geometryObject.add(new Point(x1,y1)); break; case 2://insert Line object into list x1 = in.nextDouble(); y1 = in.nextDouble(); x2 = in.nextDouble(); y2 = in.nextDouble(); color = in.next(); if(x1>200||x1<=0||y1>200||y1<=0||x2>200||x2<=0||y2>200||y2<=0){ System.out.println("Wrong Format");return; } geometryObject.add(new Line(new Point(x1,y1),new Point(x2,y2),color)); break; case 3://insert Plane object into list color = in.next(); geometryObject.add(new Plane(color)); break; case 4://delete index - 1 object from list int index = in.nextInt(); geometryObject.remove(index); } choice = in.nextInt(); if(choice==0){ for (Element each:geometryObject.getList()) { each.display(); } } } } }
总结:
对本阶段(6-9周)综合性总结,学到了类的基本使用方法,但还需继续深入学习,同时对于正则表达式的应用还不熟练,还需继续学习正则表达式的应用。

浙公网安备 33010602011771号