一丶前言
题目集4和题目集5总体难度上相较于之前,确实有所提升。在题目集4,题目集5中都用到了继承丶构造丶封装等知识点,在题量上相较之前,有明显增加,这和题目中多边形的边数有一定关系。
二丶设计与分析
背景简介:
“蛟龙号”载人深潜器是我国首台自主设计、自主集成研制的作业型深海载人潜水器,设计最大下潜深度为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 input = new Scanner(System.in);
String s = input.nextLine();
Pattern p = Pattern.compile("[0-9]+");
while(!s.equals("end")) {
Matcher o = p.matcher(s);
int sum = 0;
while (o.find())
sum += Integer.valueOf(o.group());
System.out.println(sum);
s = input.nextLine();
}
}
}

本体比较简单,因此代码量也很少。
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); double x1,y1; double x2,y2; double x3,y3; double x4,y4; double x5,y5; double x6,y6; double x,y; String s = input.nextLine(); String s1,s2; String[] a = new String[10]; String[] b = new String[2]; if(!s.matches("^[0-9]:((\\-|\\+)?\\d+(\\.\\d+)?,(\\-|\\+)?\\d+(\\.\\d+)? ){1,4}(\\-|\\+)?\\d+(\\.\\d+)?,(\\-|\\+)?\\d+(\\.\\d+)?$")) { System.out.println("Wrong Format"); return; } s1 = s.substring(0, 1); s2 = s.substring(2); a = s2.split(" "); //x1,y1,x2,y2,x3,y3,x4,y4 if(s1.equals("1")) { if(!numme(a,4)) { System.out.println("wrong number of points"); return; } b = a[0].split(","); x1 = Double.valueOf(b[0]); y1 = Double.valueOf(b[1]); b = a[1].split(","); x2 = Double.valueOf(b[0]); y2 = Double.valueOf(b[1]); b = a[2].split(","); x3 = Double.valueOf(b[0]); y3 = Double.valueOf(b[1]); b = a[3].split(","); x4 = Double.valueOf(b[0]); y4 = Double.valueOf(b[1]); if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x3==x2&&y3==y2)||(x4==x2&&y4==y2)||(x3==x4&&y3==y4)) { System.out.println("points coincide"); return; } if(sibian(x1,y1,x2,y2,x3,y3,x4,y4)) { System.out.print("true "); if(pingxi(x1,y1,x2,y2,x3,y3,x4,y4)) System.out.print("true"); else System.out.print("false"); return; } System.out.println("false false"); } else if(s1.equals("2")) { if(!numme(a,4)) { System.out.println("wrong number of points"); return; } b = a[0].split(","); x1 = Double.valueOf(b[0]); y1 = Double.valueOf(b[1]); b = a[1].split(","); x2 = Double.valueOf(b[0]); y2 = Double.valueOf(b[1]); b = a[2].split(","); x3 = Double.valueOf(b[0]); y3 = Double.valueOf(b[1]); b = a[3].split(","); x4 = Double.valueOf(b[0]); y4 = Double.valueOf(b[1]); if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x3==x2&&y3==y2)||(x4==x2&&y4==y2)||(x3==x4&&y3==y4)) { System.out.println("points coincide"); return; } if(sibian(x1,y1,x2,y2,x3,y3,x4,y4)) { if(pingxi(x1,y1,x2,y2,x3,y3,x4,y4)) { if(zfxi(x1,y1,x2,y2,x3,y3,x4,y4)) { System.out.println("true true true"); return; } if(lingxi(x1,y1,x2,y2,x3,y3,x4,y4)) { System.out.println("true false false"); return; } if(juxi(x1,y1,x2,y2,x3,y3,x4,y4)) { System.out.println("false true false"); return; } } } else { System.out.println("not a quadrilateral"); return; } System.out.println("false false false"); } else if(s1.equals("3")) { if(!numme(a,4)) { System.out.println("wrong number of points"); return; } b = a[0].split(","); x1 = Double.valueOf(b[0]); y1 = Double.valueOf(b[1]); b = a[1].split(","); x2 = Double.valueOf(b[0]); y2 = Double.valueOf(b[1]); b = a[2].split(","); x3 = Double.valueOf(b[0]); y3 = Double.valueOf(b[1]); b = a[3].split(","); x4 = Double.valueOf(b[0]); y4 = Double.valueOf(b[1]); if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x3==x2&&y3==y2)||(x4==x2&&y4==y2)||(x3==x4&&y3==y4)) { System.out.println("points coincide"); return; } if(sibian(x1,y1,x2,y2,x3,y3,x4,y4)) { System.out.print(tosib(x1,y1,x2,y2,x3,y3,x4,y4)+" "); double len = chang(x1,y1,x2,y2)+chang(x3,y3,x2,y2)+chang(x3,y3,x4,y4)+chang(x1,y1,x4,y4); double S = 0; if(tosib(x1,y1,x2,y2,x3,y3,x4,y4)) S = sibim(x1,y1,x2,y2,x3,y3,x4,y4); else { double r1 = sanmian(x1,y1,x2,y2,x3,y3); double r2 = sanmian(x1,y1,x3,y3,x4,y4); if(sanmian(x1,y1,x2,y2,x3,y3)+sanmian(x3,y3,x4,y4,x1,y1)<sanmian(x4,y4,x2,y2,x3,y3)+sanmian(x2,y2,x4,y4,x1,y1)) S = sanmian(x1,y1,x2,y2,x3,y3)+sanmian(x3,y3,x4,y4,x1,y1); else S = sanmian(x4,y4,x2,y2,x3,y3)+sanmian(x2,y2,x4,y4,x1,y1); } System.out.printf("%.3f",len); System.out.println(" "+S); } else System.out.println("not a quadrilateral"); } else if(s1.equals("4")) { if(!numme(a,4)) { System.out.println("wrong number of points"); return; } b = a[0].split(","); x1 = Double.valueOf(b[0]); y1 = Double.valueOf(b[1]); b = a[1].split(","); x2 = Double.valueOf(b[0]); y2 = Double.valueOf(b[1]); b = a[2].split(","); x3 = Double.valueOf(b[0]); y3 = Double.valueOf(b[1]); b = a[3].split(","); x4 = Double.valueOf(b[0]); y4 = Double.valueOf(b[1]); if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x3==x2&&y3==y2)||(x4==x2&&y4==y2)||(x3==x4&&y3==y4)) { System.out.println("points coincide"); return; } } else if(s1.equals("5")) { if(!numme(a,4)) { System.out.println("wrong number of points"); return; } b = a[0].split(","); x1 = Double.valueOf(b[0]); y1 = Double.valueOf(b[1]); b = a[1].split(","); x2 = Double.valueOf(b[0]); y2 = Double.valueOf(b[1]); b = a[2].split(","); x3 = Double.valueOf(b[0]); y3 = Double.valueOf(b[1]); b = a[3].split(","); x4 = Double.valueOf(b[0]); y4 = Double.valueOf(b[1]); if((x1==x2&&y1==y2)||(x3==x4&&y3==y4)) { System.out.println("points coincide"); return; } if(xielv(x1,y1,x2,y2)==xielv(x3,y3,x4,y4)) { System.out.println("is parallel lines,have no intersection point"); return; } y = ((y2-y1)*(x4*y3-x3*y4)-(x2*y1-x1*y2)*(y4-y3))/((y2-y1)*(x4-x3)-(x2-x1)*(y4-y3)); x = (y*(x2-x1)-( x2*y1-x1*y2))/(y2-y1); if((x==x1&&y1==y)||(x==x2&&y2==y)||(x==x3&&y3==y)||(x==x4&&y4==y)) { System.out.println(x+","+y+" flase"); } else { System.out.println(x+","+y+" true"); } } else { System.out.println("Wrong Format"); return; } } static double xielv(double x1,double y1,double x2,double y2) { return (y2-y1)/(x2-x1); } static boolean numme(String []s,int n) { if(s.length!=n) return false; else return true; } static boolean sibian(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { if(judge(x1,y1,x2,y2,x3,y3,x4,y4)||sanjia(x1,y1,x2,y2,x3,y3,x4,y4)) return false; else return true; } static boolean judge(double Ax1,double Ay1,double Ax2,double Ay2,double Bx1,double By1,double Bx2,double By2) { if( ( Math.max(Ax1,Ax2)>=Math.min(Bx1,Bx2)&&Math.min(Ax1,Ax2)<=Math.max(Bx1,Bx2) )&& //判断x轴投影 ( Math.max(Ay1,Ay2)>=Math.min(By1,By2)&&Math.min(Ay1,Ay2)<=Math.max(By1,By2) ) //判断y轴投影 ) { if( ( (Bx1-Ax1)*(Ay2-Ay1)-(By1-Ay1)*(Ax2-Ax1) ) * //判断B是否跨过A ( (Bx2-Ax1)*(Ay2-Ay1)-(By2-Ay1)*(Ax2-Ax1) ) <=0 && ( (Ax1-Bx1)*(By2-By1)-(Ay1-By1)*(Bx2-Bx1) ) * //判断A是否跨过B ( (Ax2-Bx1)*(By2-By1)-(Ay2-By1)*(Bx2-Bx1) ) <=0 ) { return true; } else return false; } else return false; } static boolean sanjia(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { if(san(x1,y1,x2,y2,x3,y3)||san(x4,y4,x2,y2,x3,y3)||san(x1,y1,x2,y2,x4,y4)||san(x1,y1,x4,y4,x3,y3)) return true; else return false; } static boolean san(double x1,double y1,double x2,double y2,double x3,double y3) { if(panxielv(x1,y1,x2,y2)&&panxielv(x3,y3,x2,y2)) return true; else if(panxielv(x1,y1,x2,y2)||panxielv(x3,y3,x2,y2)) return false; else if(xielv(x1,y1,x2,y2)==xielv(x3,y3,x2,y2)) return true; else return false; } static boolean panxielv(double x1,double y1,double x2,double y2) { if(x1==x2) return true; else return false; } static boolean pingxi(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { if((panxielv(x1,y1,x2,y2)&&panxielv(x3,y3,x4,y4))||(xielv(x1,y1,x2,y2)==xielv(x3,y3,x4,y4))) { if((panxielv(x1,y1,x4,y4)&&panxielv(x2,y2,x3,y3))||(xielv(x1,y1,x4,y4)==xielv(x2,y2,x3,y3))) return true; else return false; } else return false; } static boolean lingxi(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { if(panxielv(x1,y1,x3,y3)) { if(xielv(x2,y2,x4,y4)==0) return true; } if(panxielv(x2,y2,x4,y4)) { if(xielv(x1,y1,x3,y3)==0) return true; } if(xielv(x1,y1,x3,y3)*xielv(x2,y2,x4,y4)==-1) return true; return false; } static boolean juxi(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { if(panxielv(x1,y1,x2,y2)) { if(xielv(x2,y2,x3,y3)==0) { if(panxielv(x3,y3,x4,y4)) { if(xielv(x2,y2,x3,y3)==0) return true; } if(panxielv(x2,y2,x3,y3)) { if(xielv(x3,y3,x4,y4)==0) return true; } if(xielv(x3,y3,x4,y4)*xielv(x2,y2,x3,y3)==-1) return true; } } if(panxielv(x2,y2,x3,y3)) { if(xielv(x1,y1,x2,y2)==0) { if(panxielv(x3,y3,x4,y4)) { if(xielv(x2,y2,x3,y3)==0) return true; } if(panxielv(x2,y2,x3,y3)) { if(xielv(x3,y3,x4,y4)==0) return true; } if(xielv(x3,y3,x4,y4)*xielv(x2,y2,x3,y3)==-1) return true; } } if(xielv(x1,y1,x2,y2)*xielv(x2,y2,x3,y3)==-1) { if(panxielv(x3,y3,x4,y4)) { if(xielv(x2,y2,x3,y3)==0) return true; } if(panxielv(x2,y2,x3,y3)) { if(xielv(x3,y3,x4,y4)==0) return true; } if(xielv(x3,y3,x4,y4)*xielv(x2,y2,x3,y3)==-1) return true; } return false; } static boolean zfxi(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { if(lingxi(x1,y1,x2,y2,x3,y3,x4,y4)&&juxi(x1,y1,x2,y2,x3,y3,x4,y4)) return true; else return false; } static double sanmian(double x1,double y1,double x2,double y2,double x3,double y3) { return Math.abs(0.5*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))); } static boolean tosib(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { double s1 = sanmian(x1,y1,x2,y2,x3,y3); double s2 = sanmian(x3,y3,x4,y4,x1,y1); double s3 = sanmian(x4,y4,x2,y2,x3,y3); double s4 = sanmian(x2,y2,x4,y4,x1,y1); if(sanmian(x1,y1,x2,y2,x3,y3)+sanmian(x3,y3,x4,y4,x1,y1)==sanmian(x4,y4,x2,y2,x3,y3)+sanmian(x2,y2,x4,y4,x1,y1)) return true; else return false; } static double chang(double x1,double y1,double x2,double y2) { return Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2)); } static double sibim(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { /*double k1 = xielv(x1,y1,x3,y3); double k2 = xielv(x2,y2,x4,y4); double s1 = Math.abs(k1-k2); double s2 = chang(x1,y1,x3,y3); double s3 = chang(x2,y2,x4,y4); double s4 = Math.pow((1+Math.pow(k1,2)*Math.pow(k2,2)+Math.pow(k1,2)+Math.pow(k2,2)),1/2); double s5 = Math.abs(k1-k2);*/ double S = sanmian(x1,y1,x2,y2,x4,y4)+sanmian(x3,y3,x4,y4,x2,y2); return S; //1/2*chang(x1,y1,x3,y3)*chang(x2,y2,x4,y4)*Math.abs(k1-k2)/Math.pow((1+Math.pow(k1,2)*Math.pow(k2,2)+Math.pow(k1,2)+Math.pow(k2,2)) //,1/2) } }
本体我写的很混乱,因为根本没有用到类等其他方法,所以在写代码的过程中也是感觉很艰难。


编写一个银行业务类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; class User{ String useName; String password; float balance; } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); BankBusiness.welcome(); BankBusiness use = new BankBusiness(input.next(),input.next()); use.deposit(input.next(), input.nextFloat()); use.withdrawmoney(input.next(), input.nextFloat()); use.withdrawmoney(input.next(), input.nextFloat()); use.withdrawmoney(input.next(), input.nextFloat()); BankBusiness.welcomeNext(); } } class BankBusiness{ public static String bankName = "中国银行"; public static void welcome() { System.out.println(bankName+"欢迎您的到来!"); } public static void welcomeNext() { System.out.println("请收好您的证件和物品,欢迎您下次光临!"); } private User us = new User(); public BankBusiness(String name,String password) { this.us.useName = name; this.us.password = password; this.us.balance = 0; } public void withdrawmoney(String password,float balance) { if(!(this.us.password.equals(password))) { System.out.println("您的密码错误!"); return; } if(this.us.balance < balance) { System.out.println("您的余额不足!"); return; } this.us.balance-=balance; System.out.println("请取走钞票,您的余额还有"+this.us.balance+"元。"); } public void deposit(String password,float balance) { if(!(this.us.password.equals(password))) { System.out.println("您的密码错误!"); return; } this.us.balance+=balance; System.out.println("您的余额有"+this.us.balance+"元。"); } }
代码较简单,但没有用到类。

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 7 Scanner in = new Scanner(System.in); 8 input In = new input(in.nextLine()); 9 judge jud = new judge(); 10 if(!jud.Is_input(In.s))//格式检测 11 { 12 System.out.println("Wrong Format"); 13 return; 14 } 15 if(In.s1.equals("1")) 16 option01(In); 17 else if(In.s1.equals("2")) 18 option02(In); 19 else if(In.s1.equals("3")) 20 option03(In); 21 else 22 System.out.println("Wrong Format"); 23 24 return; 25 } 26 static void option01(input In) { 27 if(!In.jud.Is_point_num(In.a, 5))//点个数检测 28 { 29 System.out.println("wrong number of points"); 30 return; 31 } 32 judge jud = new judge(); 33 pentagon pent = new pentagon(); 34 int i; 35 //System.out.println(In.a.length); 36 37 for(i=0;i<5;i++)//初始化五边形 38 { 39 Point temp = new Point(); 40 In.b = In.a[i].split(","); 41 temp.x = Double.valueOf(In.b[0]); 42 temp.y = Double.valueOf(In.b[1]); 43 pent.poin[i] = temp; 44 } 45 pent.Regularity_point_line();//完善五边形边信息 46 if(!jud.polygon(pent))//是否为多边形 47 { 48 System.out.println("false"); 49 return; 50 } 51 if(!jud.Is_pentagon(pent))//是否为五边形 52 { 53 System.out.println("false"); 54 return; 55 } 56 System.out.println(jud.Is_pentagon(pent)); 57 58 } 59 static void option02(input In) { 60 if(!In.jud.Is_point_num(In.a, 5))//点个数检测 61 { 62 System.out.println("wrong number of points"); 63 return; 64 } 65 judge jud = new judge(); 66 pentagon pent = new pentagon(); 67 int i; 68 69 for(i=0;i<5;i++)//五边形初始化 70 { 71 In.b = In.a[i].split(","); 72 Point temp = new Point(); 73 temp.x = Double.valueOf(In.b[0]); 74 temp.y = Double.valueOf(In.b[1]); 75 pent.poin[i] = temp; 76 } 77 pent.Regularity_point_line();//完善边信息 78 pent.Regularity_point_triangle();//把五边形分成三个三角形,方便计算面积 79 if(!jud.polygon(pent))//是否为多边形 80 { 81 System.out.println("not a pentagon"); 82 return; 83 } 84 if(!jud.Is_pentagon(pent))//是否为五边形 85 { 86 System.out.println("not a pentagon"); 87 return; 88 } 89 System.out.print(jud.Is_pentagon_Convex(pent));//判断五边形的凹凸性 90 if(jud.Is_pentagon_Convex(pent)) {//凸五边形打印周长、面积 91 System.out.printf(" %.3f %.1f",pent.Perimeter(),pent.area()); 92 } 93 } 94 static void option03(input In) { 95 if(!In.jud.Is_point_num(In.a, 7))//点数检测 96 { 97 System.out.println("wrong number of points"); 98 return; 99 } 100 int i; 101 judge jud = new judge(); 102 pentagon pent = new pentagon(); 103 Line L = new Line(); 104 105 for(i=0;i<2;i++) {//初始化直线 106 In.b = In.a[i].split(","); 107 Point pol = new Point(); 108 pol.x = Double.valueOf(In.b[0]); 109 pol.y = Double.valueOf(In.b[1]); 110 L.point[i] = pol; 111 } 112 for(i=0;i<5;i++)//初始化五边形 113 { 114 In.b = In.a[i+2].split(","); 115 Point temp = new Point(); 116 temp.x = Double.valueOf(In.b[0]); 117 temp.y = Double.valueOf(In.b[1]); 118 pent.poin[i] = temp; 119 } 120 pent.Regularity_point_line();//完善边信息 121 pent.Regularity_point_triangle();//把五边形分为三个三角形 122 L.Regularity();//完善直线的k、b值 123 if(!jud.polygon(pent)) 124 { 125 System.out.println("not a pontagon"); 126 return; 127 }//判断是否为多边形 128 129 if(jud.point_coincidence(L.point[0], L.point[1])) 130 { 131 System.out.println("points coincide"); 132 return; 133 }//判断直线两点是否重合 134 for(i=0;i<5;i++) { 135 if(L.coin_line(pent.lin[i])) 136 { 137 System.out.println("The line is coincide with one of the lines"); 138 return; 139 } 140 }//判断直线与多边形的边是否重合 141 if(jud.Is_pentagon(pent))//五边形 142 { 143 double area_pent = pent.area(); 144 double area_pent01; 145 double area_pent02; 146 Point[] p2 = new Point[2]; 147 System.out.print(pent.pentagon_straight_coin(L));//打印直线与五边形的交点个数 148 if(pent.pentagon_straight_coin(L)==2) {//直线与五边形的交点数为2 149 p2 = pent.pentagon_straight_find(L);//五边形与直线交点坐标 150 if(pent.Select_through_point()==2) {//直线与五边形角的交点个数为2 151 152 triangle tri = new triangle(); 153 tri = pent.through_give_triangle02();//求直线与五边形围成的三角形 154 area_pent01 = tri.triangle_area();//求面积 155 } 156 else if(pent.Select_through_point()==1&&pent.Select_through_line()==1) {//直线与五边形角的交点个数为1 157 if(pent.pent_create_triangle02()[2]==1)//直线与五边形围成的图案有三角形 158 { 159 triangle tri = new triangle(); 160 tri = pent.through_give_triangle01(L);//求直线与五边形围成的三角形 161 area_pent01 = tri.triangle_area(); 162 } 163 else {//直线与五边形围成的图案没有三角形 164 quadrilateral qua = new quadrilateral(); 165 qua = pent.give_quadrilateral01(L); 166 area_pent01 = qua.quadrilateral_area(); 167 } 168 } 169 else {//直线与五边形角的交点个数为0 170 if(pent.pent_create_triangle00()[2]==1) {//直线与五边形围成的图案有三角形 171 triangle tri = new triangle(); 172 tri = pent.through_give_triangle00(L); 173 area_pent01 = tri.triangle_area(); 174 } 175 else {//直线与五边形围成的图案没有三角形 176 quadrilateral qua = new quadrilateral(); 177 qua = pent.give_quadrilateral00(L); 178 area_pent01 = qua.quadrilateral_area(); 179 } 180 } 181 area_pent02 = area_pent-area_pent01; 182 if(area_pent02>area_pent01) 183 System.out.println(" "+area_pent01+" "+area_pent02); 184 else 185 System.out.println(" "+area_pent02+" "+area_pent01);//求面积 186 } 187 } 188 else if(jud.Is_point5_quadrilateral(pent)) {//五个点围成了一个四边形 189 quadrilateral qua = new quadrilateral(); 190 qua = pent.pentagon_reduce_quadrilateral(); 191 qua.Regularity(); 192 qua.Regularity_point_triangle(); 193 double area_pent = qua.quadrilateral_area(); 194 double area_pent01; 195 double area_pent02; 196 System.out.print(qua.quadrilateral_straight_coin(L)); 197 if(qua.quadrilateral_straight_coin(L)==2) { 198 if(qua.Select_through_point()==2) { 199 triangle tri = new triangle(); 200 tri = qua.through_give_triangle02(L); 201 area_pent01 = tri.triangle_area(); 202 } 203 else if(qua.Select_through_point()==1) { 204 triangle tri = new triangle(); 205 tri = qua.through_give_triangle01(L); 206 area_pent01 = tri.triangle_area(); 207 } 208 else { 209 if(qua.quadrilateral_create_triangle00()[2]==1) { 210 triangle tri = new triangle(); 211 tri = qua.through_give_triangle00(L); 212 area_pent01 = tri.triangle_area(); 213 } 214 else { 215 quadrilateral qua01 = new quadrilateral(); 216 qua01 = qua.through_give_quadrilateral(L); 217 area_pent01 = qua01.quadrilateral_area(); 218 } 219 220 } 221 area_pent02 = area_pent-area_pent01; 222 if(area_pent02>area_pent01) 223 System.out.println(" "+area_pent01+" "+area_pent02); 224 else 225 System.out.println(" "+area_pent02+" "+area_pent01);//求面积 226 } 227 } 228 else if(jud.Is_point5_triangle(pent)) { 229 triangle tri = new triangle(); 230 tri = pent.pentagon_reduce_triangle(); 231 tri.Regularity_point_line(); 232 double area_pent = tri.triangle_area(); 233 double area_pent01; 234 double area_pent02; 235 System.out.print(tri.triangle_straight_coin(L)); 236 if(tri.triangle_straight_coin(L)==2) { 237 if(tri.Select_through_point()==1) { 238 triangle tri01 = new triangle(); 239 tri01 = tri.through_give_triangle01(L); 240 area_pent01 = tri01.triangle_area(); 241 } 242 else { 243 triangle tri01 = new triangle(); 244 tri01 = tri.through_give_triangle00(L); 245 area_pent01 = tri01.triangle_area(); 246 } 247 area_pent02 = area_pent-area_pent01; 248 if(area_pent02>area_pent01) 249 System.out.println(" "+area_pent01+" "+area_pent02); 250 else 251 System.out.println(" "+area_pent02+" "+area_pent01);//求面积 252 } 253 254 } 255 /*for(i=0;i<5;i++) { 256 if(L.straight_point_existence(pent.poin[i])) 257 { 258 System.out.println("1"); 259 return; 260 } 261 }*/ 262 263 } 264 } 265 266 class Point { 267 double x; 268 double y; 269 int through; 270 int flag; 271 public Point() { 272 flag = 0; 273 through = 0; 274 } 275 public Point(double x,double y) { 276 this.x = x; 277 this.y = y; 278 flag = 0; 279 through = 0; 280 } 281 282 } 283 class Line{ 284 Point[] point = new Point[2]; 285 Point vector = new Point(); 286 double k; 287 double b; 288 int through; 289 public Line() { 290 through = 0; 291 } 292 public Line(Point[] poin) { 293 through = 0; 294 this.point = poin; 295 } 296 297 public double Line_distance() { 298 return (Math.pow((Math.pow(this.point[1].y-this.point[0].y, 2))+(Math.pow(this.point[1].x-this.point[0].x, 2)), 0.5)); 299 } 300 public boolean Is_Line_Slope() { 301 if(this.point[0].x==this.point[1].x) 302 return false; 303 else 304 return true; 305 } 306 public double Line_Slope() { 307 return ((this.point[1].y-this.point[0].y)/(this.point[1].x-this.point[0].x)); 308 } 309 public void vector_get() { 310 Point Ls = new Point(); 311 Ls = this.vector; 312 Ls.x = this.point[1].x-this.point[0].x; 313 Ls.y = this.point[1].y-this.point[0].y; 314 } 315 public double Crossproduct(Line l) { 316 Point Ls = new Point(); 317 Ls = this.vector; 318 Point le = new Point(); 319 le = l.vector; 320 return (Ls.x*le.x+Ls.y*le.y); 321 } 322 public void Regularity() { 323 if(!this.Is_Line_Slope()) 324 this.b = this.point[0].x; 325 else { 326 this.k = this.Line_Slope(); 327 this.b = this.point[0].y-this.k*this.point[0].x; 328 } 329 } 330 public boolean coin_line(Line l) { 331 if(!(this.Is_Line_Slope()||l.Is_Line_Slope())) 332 { 333 if(this.b==l.b) 334 return true; 335 } 336 else if(this.k==l.k) 337 { 338 if(this.b==l.b) 339 return true; 340 } 341 342 return false; 343 } 344 public boolean straight_point_existence(Point p) { 345 if(!this.Is_Line_Slope()) 346 { 347 if(p.x==this.b) 348 return true; 349 } 350 else 351 { 352 if(p.y==this.k*p.x+this.b) 353 return true; 354 } 355 356 return false; 357 } 358 public boolean segment_point_existence(Point p) { 359 if(straight_point_existence(p)) 360 { 361 if(this.k==0&&p.y==this.point[0].y) 362 return true; 363 364 if((p.y<this.point[0].y&&p.y>this.point[1].y)||(p.y>this.point[0].y&&p.y<this.point[1].y)) 365 return true; 366 } 367 368 return false; 369 } 370 public Point get_intersection(Line l) { 371 Point p = new Point(); 372 if(!this.Is_Line_Slope()) { 373 p.x = this.b; 374 p.y = l.k*p.x+l.b; 375 } 376 else if(!l.Is_Line_Slope()) { 377 p.x = l.b; 378 p.y = this.k*p.x+this.b; 379 } 380 else { 381 p.y = (this.b-l.b)/(l.k-this.k); 382 p.x = (p.y-this.b)/this.k; 383 } 384 385 return p; 386 } 387 } 388 389 class input { 390 String s; 391 String s1,s2; 392 String[] a = new String[20]; 393 String[] b = new String[2]; 394 395 public input(String s) {//输入数据处理 396 this.s = s; 397 this.s1 = s.substring(0, 1); 398 this.s2 = s.substring(2); 399 this.a = s2.split(" "); 400 } 401 judge jud = new judge(); 402 403 404 } 405 class judge { 406 Line l = new Line(); 407 public boolean Point3_Collinear(Line l1,Line l2) {//判断三点是否在同一直线 408 if(!(l1.Is_Line_Slope()||l2.Is_Line_Slope())) 409 return true; 410 else if(!(l1.Is_Line_Slope()&&l2.Is_Line_Slope())) 411 return false; 412 else if(l1.Line_Slope()==l2.Line_Slope()) 413 return true; 414 else 415 return false; 416 } 417 public boolean polygon(pentagon p) {//判断是否为多边形 418 419 p.poin[5]=p.poin[0]; 420 for(int i=0;i<5;++i)//枚举第一条线段 421 for(int j=i+1;j<5;++j)//枚举第二条线段 422 { 423 if(j==i+1 || (j+1)%5==i ) 424 continue;//两线段本来就相邻 425 if(Is_intersect_segment(p.lin[i],p.lin[j])) 426 return false; 427 } 428 return true; 429 } 430 public boolean Is_pentagon(pentagon p) {//判断是否为五边形 431 //Point3_Collinear 432 if(Is_pentagon_pointcoin(p)) 433 return false; 434 if(!(Point3_Collinear(p.lin[0],p.lin[1])||Point3_Collinear(p.lin[1],p.lin[2])||Point3_Collinear(p.lin[2],p.lin[3])||Point3_Collinear(p.lin[3],p.lin[4])||Point3_Collinear(p.lin[4],p.lin[0]))) 435 return true; 436 else 437 return false; 438 } 439 public boolean Is_pentagon_Convex(pentagon p) {//判断是否为凸五边形 440 int i; 441 for(i=0;i<5;i++) { 442 if(p.lin[i].Crossproduct(p.lin[(i+1)%5])<0) 443 return false; 444 } 445 return true; 446 } 447 public boolean Is_pentagon_concave(pentagon p) {//判断是否为凹五边形 448 if(!Is_pentagon_Convex(p)) 449 return true; 450 else 451 return false; 452 } 453 public boolean Is_triangle(triangle t) {//判断是否为三角形 454 if(!Point3_Collinear(t.lin[0], t.lin[1])) 455 return true; 456 else 457 return false; 458 } 459 public boolean Is_quadrilateral(quadrilateral q) {//判断是否为四边形 460 if(!(Point3_Collinear(q.lin[0],q.lin[1])||Point3_Collinear(q.lin[1],q.lin[2])||Point3_Collinear(q.lin[2],q.lin[3])||Point3_Collinear(q.lin[3],q.lin[0]))) 461 return true; 462 else 463 return false; 464 } 465 public boolean Is_input(String s) {//正则表达式判断输入字符串格式是否正确 466 if(!s.matches("[1-5]\\:([+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?\\ ?)+")) 467 return false; 468 else 469 return true; 470 } 471 public boolean Is_point_num(String[] s,int n) {//判断点的个数是否正确 472 if(s.length!=n) 473 return false; 474 else 475 return true; 476 } 477 public boolean Is_intersect_segment(Line l1,Line l2) {//判断两线段是否相交 478 double Ax1 = l1.point[0].x; 479 double Ay1 = l1.point[0].y; 480 double Ax2 = l1.point[1].x; 481 double Ay2 = l1.point[1].y; 482 double Bx1 = l2.point[0].x; 483 double By1 = l2.point[0].y; 484 double Bx2 = l2.point[1].x; 485 double By2 = l2.point[1].y; 486 if( 487 ( Math.max(Ax1,Ax2)>=Math.min(Bx1,Bx2)&&Math.min(Ax1,Ax2)<=Math.max(Bx1,Bx2) )&& //判断x轴投影 488 ( Math.max(Ay1,Ay2)>=Math.min(By1,By2)&&Math.min(Ay1,Ay2)<=Math.max(By1,By2) ) //判断y轴投影 489 ) 490 { 491 if( 492 ( (Bx1-Ax1)*(Ay2-Ay1)-(By1-Ay1)*(Ax2-Ax1) ) * //判断B是否跨过A 493 ( (Bx2-Ax1)*(Ay2-Ay1)-(By2-Ay1)*(Ax2-Ax1) ) <=0 && 494 ( (Ax1-Bx1)*(By2-By1)-(Ay1-By1)*(Bx2-Bx1) ) * //判断A是否跨过B 495 ( (Ax2-Bx1)*(By2-By1)-(Ay2-By1)*(Bx2-Bx1) ) <=0 496 ) 497 { 498 return true; 499 } 500 else 501 return false; 502 } 503 else 504 return false; 505 506 507 } 508 public boolean point_coincidence(Point p1,Point p2) {//判断两点是否重合 509 if(p1.x==p2.x&&p1.y==p2.y) 510 return true; 511 else 512 return false; 513 } 514 public boolean straight_segment_intersect(Line l1,Line l2) {//判断直线与线段是否相交 515 516 if(!Point3_Collinear(l1,l2)) 517 { 518 double x1,y1; 519 double x2,y2; 520 double y3,y4; 521 x1 = l2.point[0].x; 522 x2 = l2.point[1].x; 523 if(!l1.Is_Line_Slope()) 524 { 525 if((x1>l1.b&&x2<l1.b)||(x1<l1.b&&x2>l1.b)) 526 return true; 527 } 528 else if(!l2.Is_Line_Slope()) 529 { 530 y1 = l1.k*x1+l1.b; 531 if((l2.point[0].y>y1&&l2.point[1].y<y1)||(l2.point[0].y<y1&&l2.point[1].y>y1)) 532 return true; 533 } 534 else { 535 l2.k = l2.Line_Slope(); 536 l2.b = l2.point[0].y-l2.k*l2.point[0].x; 537 y1 = l2.k*x1+l2.b; 538 y2 = l2.k*x2+l2.b; 539 y3 = l1.k*x1+l1.b; 540 y4 = l1.k*x2+l1.b; 541 if((y1>y3&&y2<y4)||(y1>y3&&y2<y4)||(y1<y3&&y2>y4)||(y1<y3&&y2>y4)) 542 return true; 543 } 544 } 545 546 return false; 547 } 548 public boolean Is_pentagon_pointcoin(pentagon p) {//判断五边形的五个点是否有重合 549 for(int i=0;i<5;i++) { 550 for(int o =i+1;o<5;o++) { 551 if(point_coincidence(p.poin[i],p.poin[o])) 552 return true; 553 } 554 } 555 return false; 556 } 557 public boolean Is_point5_quadrilateral(pentagon p) {//判断五个点是否能形成四边形 558 int tmp=0; 559 for(int o=0;o<5;o++) { 560 if(this.point_coincidence(p.poin[o], p.poin[(o+1)%5])) { 561 tmp++; 562 p.poin[o].flag = 1; 563 } 564 565 } 566 if(tmp==1) 567 return true; 568 tmp = 0; 569 for(int i=5;i<10;i++) { 570 if(this.Point3_Collinear(p.lin[(i-1)%5], p.lin[i%5])) { 571 tmp++; 572 p.poin[i%5].flag = 1; 573 } 574 575 } 576 if(tmp==1) 577 return true; 578 579 return false; 580 } 581 public boolean Is_point5_triangle(pentagon p) {//判断五个点能否形成三角形 582 int tmp=0; 583 584 for(int o=0;o<5;o++) { 585 if(this.point_coincidence(p.poin[o], p.poin[(o+1)%5])) { 586 tmp++; 587 p.poin[o].flag = 1; 588 } 589 590 } 591 if(tmp==2) 592 return true; 593 tmp = 0; 594 for(int i=5;i<10;i++) { 595 if(this.Point3_Collinear(p.lin[(i-1)%5], p.lin[i%5])) { 596 tmp++; 597 p.poin[i%5].flag = 1; 598 } 599 600 } 601 if(tmp==2) 602 return true; 603 604 return false; 605 } 606 } 607 class pentagon extends polygon { 608 public pentagon(){//无参构造器 609 super(); 610 } 611 public pentagon(Point p1,Point p2,Point p3,Point p4,Point p5) {//带参构造器 612 super(); 613 this.poin[0] = p1; 614 this.poin[1] = p2; 615 this.poin[2] = p3; 616 this.poin[3] = p4; 617 this.poin[4] = p5; 618 } 619 judge jud = new judge(); 620 void Regularity_point_line() {//五边形边初始化 621 622 for(int i=0;i<5;i++) { 623 Line l = new Line(); 624 Point p = new Point(); 625 l.point[0] = super.poin[i%5]; 626 l.point[1] = super.poin[(i+1)%5]; 627 super.lin[i] = l; 628 p = super.lin[i].vector; 629 p.x = super.poin[i%5].x-super.poin[(i+1)%5].x; 630 p.y = super.poin[i%5].y-super.poin[(i+1)%5].y; 631 } 632 for(int o=0;o<5;o++) { 633 if(!this.lin[o].Is_Line_Slope()){ 634 this.lin[o].b = this.lin[o].point[0].x; 635 } 636 else { 637 this.lin[o].k = this.lin[o].Line_Slope(); 638 this.lin[o].b = this.lin[o].point[0].y-this.lin[o].k*this.lin[o].point[0].x; 639 } 640 } 641 } 642 double Perimeter() {//五边形周长 643 double sum = 0; 644 for(int i=0;i<5;i++) 645 { 646 sum+=this.lin[i].Line_distance(); 647 } 648 return sum; 649 } 650 triangle tri1 = new triangle(); 651 triangle tri2 = new triangle(); 652 triangle tri3 = new triangle(); 653 public void Regularity_point_triangle() {//五边形的三角形初始化 654 this.tri1.triangle_point_give(this.poin[0], this.poin[1], this.poin[2]); 655 this.tri2.triangle_point_give(this.poin[2], this.poin[3], this.poin[4]); 656 this.tri3.triangle_point_give(this.poin[0], this.poin[4], this.poin[2]); 657 } 658 //this.poin[0],this.poin[1],this.poin[2] 659 //this.poin[2],this.poin[3],this.poin[4] 660 // this.poin[0],this.poin[4],this.poin[2] 661 662 double area() {//计算五边形面积 663 return(this.tri1.triangle_area()+this.tri2.triangle_area()+this.tri3.triangle_area()); 664 } 665 public int pentagon_straight_coin(Line l) {//五边形与直线交点个数(未作标记) 666 int num01 = 0; 667 int num02 = 0; 668 int i; 669 for(i=0;i<5;i++) { 670 if(l.straight_point_existence(this.poin[i])) { 671 num01++; 672 this.poin[i].through = 1; 673 } 674 675 } 676 for(i=0;i<5;i++) { 677 if(jud.straight_segment_intersect(l, this.lin[i])) { 678 num02++; 679 this.lin[i].through = 1; 680 } 681 682 } 683 684 return num01+num02; 685 } 686 public Point[] pentagon_straight_find(Line l) {//寻找直线与五边形交点坐标 687 Point[] p = new Point[2]; 688 int i; 689 int m = 0; 690 for(i=0;i<5;i++) { 691 if(l.straight_point_existence(this.poin[i])) 692 { 693 p[m] = this.poin[i]; 694 this.poin[i].through = 1; 695 m++; 696 } 697 } 698 for(i=0;i<5;i++) { 699 if(jud.straight_segment_intersect(l, this.lin[i])) { 700 p[m] = l.get_intersection(this.lin[i]); 701 this.lin[i].through = 1; 702 m++; 703 } 704 705 } 706 return p; 707 } 708 public int Select_through_point() {//查询直线与角交点个数 709 int sum = 0; 710 for(int i=0;i<5;i++) { 711 if(this.poin[i].through==1) 712 sum++; 713 } 714 715 return sum; 716 } 717 public int Select_through_line() {//查询直线与边交点个数 718 int sum = 0; 719 for(int i=0;i<5;i++) { 720 if(this.lin[i].through==1) 721 sum++; 722 } 723 724 return sum; 725 } 726 public triangle through_give_triangle00(Line L){//与五边形的角没有交点的直线与五边形围成的三角形 727 triangle t = new triangle(); 728 int i1 = this.pent_create_triangle00()[0]; 729 int i2 = this.pent_create_triangle00()[1]; 730 int i; 731 if((i1==0&&i2==4)||(i1==4&&i2==0)) 732 i=0; 733 else { 734 i = (i1+i2+1)/2; 735 } 736 t.triangle_point_give(L.get_intersection(this.lin[i1]), L.get_intersection(this.lin[i2]), this.poin[i]); 737 return t; 738 739 } 740 public triangle through_give_triangle02() {//与五边形的角有两个交点的直线与五边形围成的三角形 741 triangle t = new triangle(); 742 int m=0; 743 double j = 0; 744 int o; 745 for(int i=0;i<5;i++) { 746 if(this.poin[i].through==1) { 747 t.poin[m] = this.poin[i]; 748 j+=i; 749 m++; 750 } 751 752 } 753 j = j/2; 754 if(j==2.5) 755 o = 0; 756 else if(j==1.5) 757 o = 4; 758 else 759 o = (int)j; 760 t.poin[2] = this.poin[o]; 761 return t; 762 } 763 public triangle through_give_triangle01(Line L) {//与五边形的角有一个交点的直线与五边形围成的三角形 764 triangle t = new triangle(); 765 int m = 0; 766 int n = 0; 767 int l; 768 for(int i=0;i<5;i++) { 769 if(this.poin[i].through==1) 770 m = i; 771 else if(this.lin[i].through==1) 772 n = i; 773 } 774 if(n==(m+1)%5) 775 l = n; 776 else { 777 l = n+1; 778 } 779 t.triangle_point_give(this.poin[m], this.poin[n], L.get_intersection(this.lin[l])); 780 return t; 781 } 782 public int[] pent_create_triangle02() {//与五边形的角有一个交点的直线与五边形是否围成一个三角形以及交点信息 783 int[] fb = new int[3]; 784 int i1=0; 785 for(int i=0;i<5;i++) { 786 if(this.poin[i].through==1) 787 { 788 fb[i1]=i; 789 i1++; 790 } 791 else if(this.lin[i].through==1) 792 fb[1]=i; 793 } 794 if((fb[1]-fb[0]==2)||(fb[1]-fb[0]==-3)) 795 fb[2] = 0; 796 else 797 fb[2] = 1; 798 799 return fb; 800 } 801 public quadrilateral give_quadrilateral01(Line l) {//与五边形的角有一个交点的直线与五边形围成的四边形的信息 802 quadrilateral qua = new quadrilateral(); 803 Point p = new Point(); 804 p = l.get_intersection(this.lin[this.pent_create_triangle02()[1]]); 805 qua.poin[0] = p; 806 for(int i=1;i<4;i++) { 807 qua.poin[i] = this.poin[(this.pent_create_triangle02()[1]+1)%5]; 808 } 809 return qua; 810 } 811 public quadrilateral give_quadrilateral00(Line l) {//与五边形的角没有交点的直线与五边形围成的四边形的信息 812 quadrilateral qua = new quadrilateral(); 813 int i1 = this.pent_create_triangle00()[0]; 814 int i2 = this.pent_create_triangle00()[1]; 815 int o1,o2; 816 if(Math.abs((i1+1)%5-i2)==1) { 817 o1 = (i1+1)%5; 818 o2 = i2; 819 } 820 else { 821 o1 = i1; 822 o2 = (i2+1)%5; 823 } 824 qua.quadrilateral_point_give(l.get_intersection(this.lin[i1]), this.poin[o1], this.poin[o2], l.get_intersection(this.lin[i2])); 825 return qua; 826 } 827 public int[] pent_create_triangle00() {//与五边形的角没有个交点的直线与五边形是否围成一个三角形以及交点信息 828 int[] fb = new int[3]; 829 int i1=0; 830 for(int i=0;i<5;i++) { 831 if(this.lin[i].through==1) 832 { 833 fb[i1]=i; 834 i1++; 835 } 836 } 837 if(Math.abs(fb[0]-fb[1])==1) 838 fb[2] = 1; 839 else 840 fb[2] = 0; 841 842 return fb; 843 } 844 public quadrilateral pentagon_reduce_quadrilateral() {//五边形信息转换为四边形 845 int m=0; 846 quadrilateral qua = new quadrilateral(); 847 for(int i=0;i<5;i++) { 848 if(this.poin[i].flag==0) { 849 qua.poin[m] = this.poin[i]; 850 m++; 851 } 852 853 } 854 855 return qua; 856 } 857 public triangle pentagon_reduce_triangle() { 858 int m = 0; 859 triangle tri = new triangle(); 860 for(int i=0;i<5;i++) { 861 if(this.poin[i].flag==0) { 862 tri.poin[m] = this.poin[i]; 863 m++; 864 } 865 866 } 867 return tri; 868 } 869 } 870 class polygon {//多边形 871 Point[] poin = new Point[10]; 872 Line[] lin = new Line[10]; 873 public polygon() { 874 875 } 876 } 877 class quadrilateral extends polygon{//四边形 878 judge jud = new judge(); 879 triangle tri1 = new triangle(); 880 triangle tri2 = new triangle(); 881 882 public void Regularity_point_triangle() {//四边形被分成两个三角形 883 this.tri1.triangle_point_give(this.poin[0], this.poin[1], this.poin[2]); 884 this.tri2.triangle_point_give(this.poin[2], this.poin[3], this.poin[0]); 885 } 886 public void quadrilateral_point_give(Point p1,Point p2,Point p3,Point p4) {//给四边形赋予信息 887 this.poin[0] = p1; 888 this.poin[1] = p2; 889 this.poin[2] = p3; 890 this.poin[3] = p4; 891 } 892 void Regularity() {//四边形边初始化 893 for(int i=0;i<4;i++) { 894 Line l = new Line(); 895 l.point[0] = super.poin[i%4]; 896 l.point[1] = super.poin[(i+1)%4]; 897 super.lin[i] = l; 898 } 899 } 900 public int quadrilateral_straight_coin(Line l) {//四边形与直线交点个数 901 int num01 = 0; 902 int num02 = 0; 903 int i; 904 for(i=0;i<4;i++) { 905 if(l.straight_point_existence(this.poin[i])) { 906 num01++; 907 this.poin[i].through = 1; 908 } 909 } 910 for(i=0;i<4;i++) { 911 if(jud.straight_segment_intersect(l, this.lin[i])) { 912 num02++; 913 this.lin[i].through = 1; 914 } 915 916 } 917 918 return num01+num02; 919 } 920 public int Select_through_point() {//查询直线与角交点个数 921 int sum = 0; 922 for(int i=0;i<4;i++) { 923 if(this.poin[i].through==1) 924 sum++; 925 } 926 927 return sum; 928 } 929 public int Select_through_line() {//查询直线与边交点个数 930 int sum = 0; 931 for(int i=0;i<4;i++) { 932 if(this.lin[i].through==1) 933 sum++; 934 } 935 936 return sum; 937 } 938 public double quadrilateral_area() {//四边形面积 939 return (this.tri1.triangle_area()+this.tri2.triangle_area()); 940 } 941 public int[] quadrilateral_create_triangle02() { 942 int[] p = new int[3]; 943 int m=0; 944 for(int i=0;i<4;i++) { 945 if(this.poin[i].through==1) { 946 p[m] = i; 947 m++; 948 } 949 } 950 return p; 951 } 952 public int[] quadrilateral_create_triangle01() { 953 int[] p = new int[3]; 954 int m=0; 955 for(int i=0;i<4;i++) { 956 if(this.poin[i].through==1) { 957 p[m] = i; 958 m++; 959 } 960 if(this.lin[i].through==1) { 961 p[m] = i; 962 p[2] = m; 963 m++; 964 } 965 } 966 return p; 967 } 968 public int[] quadrilateral_create_triangle00() { 969 int[] p = new int[3]; 970 int m=0; 971 for(int i=0;i<4;i++) { 972 if(this.lin[i].through==1) { 973 p[m] = i; 974 m++; 975 } 976 } 977 if((p[0]+1)%4==p[1]||(p[1]+1)%4==p[0]) 978 p[2] = 1; 979 else 980 p[2] = 0; 981 return p; 982 } 983 public triangle through_give_triangle02(Line l) {//找出直线与四边形中两个角相交的三角形 984 triangle tri = new triangle(); 985 int i1 = this.quadrilateral_create_triangle02()[0]; 986 int i2 = this.quadrilateral_create_triangle02()[1]; 987 tri.triangle_point_give(this.poin[i1], this.poin[i2], this.poin[(i1+1)%4]); 988 return tri; 989 } 990 public triangle through_give_triangle01(Line l) {//找出直线与四边形中一个角相交的三角形 991 triangle tri = new triangle(); 992 int i1,i2,i; 993 if(this.quadrilateral_create_triangle01()[2]==0) { 994 i2 = this.quadrilateral_create_triangle01()[0]; 995 i1 = this.quadrilateral_create_triangle01()[1]; 996 } 997 else { 998 i1 = this.quadrilateral_create_triangle01()[0]; 999 i2 = this.quadrilateral_create_triangle01()[1]; 1000 } 1001 Point p =new Point(); 1002 p = l.get_intersection(this.lin[i2]); 1003 if((i1+1)%4==i2) 1004 i = i2; 1005 else { 1006 i = (i2+1)%4; 1007 } 1008 tri.triangle_point_give(this.poin[i1], p, this.poin[i]); 1009 return tri; 1010 } 1011 public triangle through_give_triangle00(Line l) { 1012 triangle tri = new triangle(); 1013 int i1 = this.quadrilateral_create_triangle00()[0]; 1014 int i2 = this.quadrilateral_create_triangle00()[1]; 1015 int i; 1016 if((i1==0&&i2==3)||(i2==0&&i1==3)) 1017 i=0; 1018 else if(i1>i2) 1019 i = i1; 1020 else 1021 i = i2; 1022 Point[] p = new Point[2]; 1023 p[0] = l.get_intersection(this.lin[i1]); 1024 p[1] = l.get_intersection(this.lin[i2]); 1025 tri.triangle_point_give(p[0], p[1], this.poin[i]); 1026 return tri; 1027 } 1028 public quadrilateral through_give_quadrilateral(Line l) { 1029 quadrilateral qua = new quadrilateral(); 1030 int i1 = this.quadrilateral_create_triangle00()[0]; 1031 int i2 = this.quadrilateral_create_triangle00()[1]; 1032 Point[] p = new Point[2]; 1033 p[0] = l.get_intersection(this.lin[i1]); 1034 p[1] = l.get_intersection(this.lin[i2]); 1035 1036 qua.quadrilateral_point_give(p[0], this.poin[(i1+1)%4], this.poin[(i1+2)%4], p[1]); 1037 return qua; 1038 } 1039 } 1040 class triangle extends polygon{//三角形 1041 public triangle() { 1042 super(); 1043 } 1044 public triangle(Point p1,Point p2,Point p3) {//构造器 1045 super(); 1046 this.poin[0] = p1; 1047 this.poin[1] = p2; 1048 this.poin[2] = p3; 1049 } 1050 judge jud = new judge(); 1051 public void triangle_point_give(Point p1,Point p2,Point p3) {//三角形赋予信息 1052 this.poin[0] = p1; 1053 this.poin[1] = p2; 1054 this.poin[2] = p3; 1055 } 1056 void Regularity_point_line() {//三角形边初始化 1057 for(int i=0;i<3;i++) { 1058 Line l = new Line(); 1059 l.point[0] = super.poin[i%3]; 1060 l.point[1] = super.poin[(i+1)%3]; 1061 super.lin[i] = l; 1062 } 1063 for(int o=0;o<3;o++) { 1064 if(!this.lin[o].Is_Line_Slope()){ 1065 this.lin[o].b = this.lin[o].point[0].x; 1066 } 1067 else { 1068 this.lin[o].k = this.lin[o].Line_Slope(); 1069 this.lin[o].b = this.lin[o].point[0].y-this.lin[o].k*this.lin[o].point[0].x; 1070 } 1071 } 1072 } 1073 public double triangle_area() {//三角形面积 1074 double r1 = this.poin[0].x*(this.poin[1].y-this.poin[2].y); 1075 double r2 = this.poin[1].x*(this.poin[2].y-this.poin[0].y); 1076 double r3 = this.poin[2].x*(this.poin[0].y-this.poin[1].y); 1077 double r = Math.abs(0.5*(r1+r2+r3)); 1078 return r; 1079 } 1080 public int triangle_straight_coin(Line l) {//直线与三角形交点个数 1081 int num01 = 0; 1082 int num02 = 0; 1083 int i; 1084 for(i=0;i<3;i++) { 1085 if(l.straight_point_existence(this.poin[i])) { 1086 num01++; 1087 this.poin[i].through = 1; 1088 } 1089 1090 } 1091 for(i=0;i<3;i++) { 1092 if(jud.straight_segment_intersect(l, this.lin[i])) { 1093 num02++; 1094 this.lin[i].through = 1; 1095 } 1096 1097 } 1098 1099 return num01+num02; 1100 } 1101 public int Select_through_point() {//查询直线与角交点个数 1102 int sum = 0; 1103 for(int i=0;i<3;i++) { 1104 if(this.poin[i].through==1) 1105 sum++; 1106 } 1107 1108 return sum; 1109 } 1110 public int Select_through_line() {//查询直线与边交点个数 1111 int sum = 0; 1112 for(int i=0;i<3;i++) { 1113 if(this.lin[i].through==1) 1114 sum++; 1115 } 1116 1117 return sum; 1118 } 1119 public int[] triangle_create_triangle01() { 1120 int[] p = new int[3]; 1121 int m=0; 1122 for(int i=0;i<3;i++) { 1123 if(this.poin[i].through==1) { 1124 p[m] = i; 1125 m++; 1126 } 1127 if(this.lin[i].through==1) { 1128 p[m] = i; 1129 p[2] = m; 1130 m++; 1131 } 1132 1133 } 1134 return p; 1135 } 1136 public int[] triangle_create_triangle00() { 1137 int[] p = new int[3]; 1138 int m=0; 1139 for(int i=0;i<3;i++) { 1140 if(this.lin[i].through==1) { 1141 p[m] = i; 1142 m++; 1143 } 1144 } 1145 return p; 1146 } 1147 public triangle through_give_triangle01(Line l) { 1148 triangle tri = new triangle(); 1149 int i1,i2,i; 1150 if(this.triangle_create_triangle01()[2]==0) 1151 { 1152 i2 = this.triangle_create_triangle01()[0]; 1153 i1 = this.triangle_create_triangle01()[1]; 1154 } 1155 else { 1156 i1 = this.triangle_create_triangle01()[0]; 1157 i2 = this.triangle_create_triangle01()[1]; 1158 } 1159 i = i2; 1160 tri.triangle_point_give(this.poin[i1], l.get_intersection(this.lin[i2]), this.poin[i2]); 1161 return tri; 1162 } 1163 public triangle through_give_triangle00(Line l) { 1164 triangle tri = new triangle(); 1165 int i1,i2,i; 1166 i1 = this.triangle_create_triangle00()[0]; 1167 i2 = this.triangle_create_triangle00()[1]; 1168 if((i1==0&&i2==2)||(i1==0&&i2==2)) 1169 i=0; 1170 else if(i1>i2) 1171 i = i2; 1172 else { 1173 i = i1; 1174 } 1175 tri.triangle_point_give(this.poin[i], l.get_intersection(this.lin[i1]), l.get_intersection(this.lin[i2])); 1176 return tri; 1177 } 1178 }








本次代码题量很大,同时我也用到了类等方法,发现确实和普通面向过程相比,更加的有条理。
用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 7 Scanner in = new Scanner(System.in); 8 input In = new input(in.nextLine()); 9 judge jud = new judge(); 10 if(!jud.Is_input(In.s))//格式检测 11 { 12 System.out.println("Wrong Format"); 13 return; 14 } 15 if(In.s1.equals("1")) 16 option01(In); 17 else if(In.s1.equals("2")) 18 option02(In); 19 else if(In.s1.equals("3")) 20 option03(In); 21 else if(In.s1.equals("4")) 22 option04(In); 23 else if(In.s1.equals("5")) 24 option05(In); 25 else if(In.s1.equals("6")) 26 option06(In); 27 else 28 System.out.println("Wrong Format"); 29 30 return; 31 } 32 static void option01(input In) { 33 if(!In.jud.Is_point_num(In.a, 5))//点个数检测 34 { 35 System.out.println("wrong number of points"); 36 return; 37 } 38 judge jud = new judge(); 39 pentagon pent = new pentagon(); 40 int i; 41 //System.out.println(In.a.length); 42 43 for(i=0;i<5;i++)//初始化五边形 44 { 45 Point temp = new Point(); 46 In.b = In.a[i].split(","); 47 temp.x = Double.valueOf(In.b[0]); 48 temp.y = Double.valueOf(In.b[1]); 49 pent.poin[i] = temp; 50 } 51 pent.Regularity_point_line();//完善五边形边信息 52 if(!jud.polygon(pent))//是否为多边形 53 { 54 System.out.println("false"); 55 return; 56 } 57 if(!jud.Is_pentagon(pent))//是否为五边形 58 { 59 System.out.println("false"); 60 return; 61 } 62 System.out.println(jud.Is_pentagon(pent)); 63 64 } 65 static void option02(input In) { 66 if(!In.jud.Is_point_num(In.a, 5))//点个数检测 67 { 68 System.out.println("wrong number of points"); 69 return; 70 } 71 judge jud = new judge(); 72 pentagon pent = new pentagon(); 73 int i; 74 75 for(i=0;i<5;i++)//五边形初始化 76 { 77 In.b = In.a[i].split(","); 78 Point temp = new Point(); 79 temp.x = Double.valueOf(In.b[0]); 80 temp.y = Double.valueOf(In.b[1]); 81 pent.poin[i] = temp; 82 } 83 pent.Regularity_point_line();//完善边信息 84 pent.Regularity_point_triangle();//把五边形分成三个三角形,方便计算面积 85 if(!jud.polygon(pent))//是否为多边形 86 { 87 System.out.println("not a pentagon"); 88 return; 89 } 90 if(!jud.Is_pentagon(pent))//是否为五边形 91 { 92 System.out.println("not a pentagon"); 93 return; 94 } 95 System.out.print(jud.Is_pentagon_Convex(pent));//判断五边形的凹凸性 96 if(jud.Is_pentagon_Convex(pent)) {//凸五边形打印周长、面积 97 System.out.printf(" %.3f %.1f",pent.Perimeter(),pent.area()); 98 } 99 } 100 static void option03(input In) { 101 if(!In.jud.Is_point_num(In.a, 7))//点数检测 102 { 103 System.out.println("wrong number of points"); 104 return; 105 } 106 int i; 107 judge jud = new judge(); 108 pentagon pent = new pentagon(); 109 Line L = new Line(); 110 111 for(i=0;i<2;i++) {//初始化直线 112 In.b = In.a[i].split(","); 113 Point pol = new Point(); 114 pol.x = Double.valueOf(In.b[0]); 115 pol.y = Double.valueOf(In.b[1]); 116 L.point[i] = pol; 117 } 118 for(i=0;i<5;i++)//初始化五边形 119 { 120 In.b = In.a[i+2].split(","); 121 Point temp = new Point(); 122 temp.x = Double.valueOf(In.b[0]); 123 temp.y = Double.valueOf(In.b[1]); 124 pent.poin[i] = temp; 125 } 126 pent.Regularity_point_line();//完善边信息 127 pent.Regularity_point_triangle();//把五边形分为三个三角形 128 L.Regularity();//完善直线的k、b值 129 if(!jud.polygon(pent)) 130 { 131 System.out.println("not a pontagon"); 132 return; 133 }//判断是否为多边形 134 135 if(jud.point_coincidence(L.point[0], L.point[1])) 136 { 137 System.out.println("points coincide"); 138 return; 139 }//判断直线两点是否重合 140 for(i=0;i<5;i++) { 141 if(L.coin_line(pent.lin[i])) 142 { 143 System.out.println("The line is coincide with one of the lines"); 144 return; 145 } 146 }//判断直线与多边形的边是否重合 147 if(jud.Is_pentagon(pent))//五边形 148 { 149 double area_pent = pent.area(); 150 double area_pent01; 151 double area_pent02; 152 Point[] p2 = new Point[2]; 153 System.out.print(pent.pentagon_straight_coin(L));//打印直线与五边形的交点个数 154 if(pent.pentagon_straight_coin(L)==2) {//直线与五边形的交点数为2 155 p2 = pent.pentagon_straight_find(L);//五边形与直线交点坐标 156 if(pent.Select_through_point()==2) {//直线与五边形角的交点个数为2 157 158 triangle tri = new triangle(); 159 tri = pent.through_give_triangle02();//求直线与五边形围成的三角形 160 area_pent01 = tri.triangle_area();//求面积 161 } 162 else if(pent.Select_through_point()==1&&pent.Select_through_line()==1) {//直线与五边形角的交点个数为1 163 if(pent.pent_create_triangle02()[2]==1)//直线与五边形围成的图案有三角形 164 { 165 triangle tri = new triangle(); 166 tri = pent.through_give_triangle01(L);//求直线与五边形围成的三角形 167 area_pent01 = tri.triangle_area(); 168 } 169 else {//直线与五边形围成的图案没有三角形 170 quadrilateral qua = new quadrilateral(); 171 qua = pent.give_quadrilateral01(L); 172 area_pent01 = qua.quadrilateral_area(); 173 } 174 } 175 else {//直线与五边形角的交点个数为0 176 if(pent.pent_create_triangle00()[2]==1) {//直线与五边形围成的图案有三角形 177 triangle tri = new triangle(); 178 tri = pent.through_give_triangle00(L); 179 area_pent01 = tri.triangle_area(); 180 } 181 else {//直线与五边形围成的图案没有三角形 182 quadrilateral qua = new quadrilateral(); 183 qua = pent.give_quadrilateral00(L); 184 area_pent01 = qua.quadrilateral_area(); 185 } 186 } 187 area_pent02 = area_pent-area_pent01; 188 if(area_pent02>area_pent01) 189 System.out.println(" "+area_pent01+" "+area_pent02); 190 else 191 System.out.println(" "+area_pent02+" "+area_pent01);//求面积 192 } 193 } 194 else if(jud.Is_point5_quadrilateral(pent)) {//五个点围成了一个四边形 195 quadrilateral qua = new quadrilateral(); 196 qua = pent.pentagon_reduce_quadrilateral(); 197 qua.Regularity(); 198 qua.Regularity_point_triangle(); 199 double area_pent = qua.quadrilateral_area(); 200 double area_pent01; 201 double area_pent02; 202 System.out.print(qua.quadrilateral_straight_coin(L)); 203 if(qua.quadrilateral_straight_coin(L)==2) { 204 if(qua.Select_through_point()==2) { 205 triangle tri = new triangle(); 206 tri = qua.through_give_triangle02(L); 207 area_pent01 = tri.triangle_area(); 208 } 209 else if(qua.Select_through_point()==1) { 210 triangle tri = new triangle(); 211 tri = qua.through_give_triangle01(L); 212 area_pent01 = tri.triangle_area(); 213 } 214 else { 215 if(qua.quadrilateral_create_triangle00()[2]==1) { 216 triangle tri = new triangle(); 217 tri = qua.through_give_triangle00(L); 218 area_pent01 = tri.triangle_area(); 219 } 220 else { 221 quadrilateral qua01 = new quadrilateral(); 222 qua01 = qua.through_give_quadrilateral(L); 223 area_pent01 = qua01.quadrilateral_area(); 224 } 225 226 } 227 area_pent02 = area_pent-area_pent01; 228 if(area_pent02>area_pent01) 229 System.out.println(" "+area_pent01+" "+area_pent02); 230 else 231 System.out.println(" "+area_pent02+" "+area_pent01);//求面积 232 } 233 } 234 else if(jud.Is_point5_triangle(pent)) { 235 triangle tri = new triangle(); 236 tri = pent.pentagon_reduce_triangle(); 237 tri.Regularity_point_line(); 238 double area_pent = tri.triangle_area(); 239 double area_pent01; 240 double area_pent02; 241 System.out.print(tri.triangle_straight_coin(L)); 242 if(tri.triangle_straight_coin(L)==2) { 243 if(tri.Select_through_point()==1) { 244 triangle tri01 = new triangle(); 245 tri01 = tri.through_give_triangle01(L); 246 area_pent01 = tri01.triangle_area(); 247 } 248 else { 249 triangle tri01 = new triangle(); 250 tri01 = tri.through_give_triangle00(L); 251 area_pent01 = tri01.triangle_area(); 252 } 253 area_pent02 = area_pent-area_pent01; 254 if(area_pent02>area_pent01) 255 System.out.println(" "+area_pent01+" "+area_pent02); 256 else 257 System.out.println(" "+area_pent02+" "+area_pent01);//求面积 258 } 259 260 } 261 /*for(i=0;i<5;i++) { 262 if(L.straight_point_existence(pent.poin[i])) 263 { 264 System.out.println("1"); 265 return; 266 } 267 }*/ 268 269 } 270 static void option04(input In) { 271 if(!In.jud.Is_point_num(In.a, 10))//点数检测 272 { 273 System.out.println("wrong number of points"); 274 return; 275 } 276 int intpent01; 277 int intpent02; 278 int i; 279 String[] pol = {"pentagon","quadrilateral","triangle"}; 280 int mag01,mag02; 281 judge jud = new judge(); 282 pentagon pent01 = new pentagon(); 283 pentagon pent02 = new pentagon(); 284 285 for(i=0;i<5;i++)//初始化五边形 286 { 287 In.b = In.a[i+2].split(","); 288 Point temp = new Point(); 289 temp.x = Double.valueOf(In.b[0]); 290 temp.y = Double.valueOf(In.b[1]); 291 pent01.poin[i] = temp; 292 } 293 for(i=0;i<5;i++)//初始化五边形 294 { 295 In.b = In.a[i+2].split(","); 296 Point temp = new Point(); 297 temp.x = Double.valueOf(In.b[0]); 298 temp.y = Double.valueOf(In.b[1]); 299 pent02.poin[i] = temp; 300 } 301 pent01.Regularity_point_line();//完善边信息 302 pent01.Regularity_point_triangle();//把五边形分为三个三角形 303 if(!jud.polygon(pent01)) 304 { 305 System.out.println("not a pontagon"); 306 return; 307 }//判断是否为多边形 308 pent02.Regularity_point_line();//完善边信息 309 pent02.Regularity_point_triangle();//把五边形分为三个三角形 310 if(!jud.polygon(pent02)) 311 { 312 System.out.println("not a pontagon"); 313 return; 314 }//判断是否为多边形 315 int diag = pent01.polygon_in_polygon(pent02); 316 if(jud.Is_pentagon(pent01)) 317 intpent01 = 0; 318 else if(jud.Is_point5_quadrilateral(pent01)) 319 intpent01 = 1; 320 else 321 intpent01 = 2; 322 323 if(jud.Is_pentagon(pent02)) 324 intpent02 = 0; 325 else if(jud.Is_point5_quadrilateral(pent02)) 326 intpent02 = 1; 327 else 328 intpent02 = 2; 329 330 if(diag==1) 331 System.out.println("no overlapping area between the previous "+pol[intpent01]+" and the following "+pol[intpent02]); 332 else if(diag==2) 333 System.out.println("the previous "+pol[intpent01]+" is connected to the following "+pol[intpent02]); 334 else if(diag==3) 335 System.out.println("the previous "+pol[intpent01]+" coincides with the following "+pol[intpent02]); 336 else if(diag==4) 337 System.out.println("the previous "+pol[intpent01]+" is inside the following "+pol[intpent02]); 338 else if(diag==5) 339 System.out.println("the previous "+pol[intpent01]+" is interlaced with the following "+pol[intpent02]); 340 else if(diag==6) 341 System.out.println("the previous "+pol[intpent01]+" contains the following "+pol[intpent02]); 342 343 344 } 345 static void option05(input In) { 346 347 } 348 static void option06(input In) { 349 350 } 351 } 352 353 class Point { 354 double x; 355 double y; 356 int through; 357 int flag; 358 public Point() { 359 flag = 0; 360 through = 0; 361 } 362 public Point(double x,double y) { 363 this.x = x; 364 this.y = y; 365 flag = 0; 366 through = 0; 367 } 368 369 } 370 class Line{ 371 Point[] point = new Point[2]; 372 Point vector = new Point(); 373 double k; 374 double b; 375 int through; 376 public Line() { 377 through = 0; 378 } 379 public Line(Point[] poin) { 380 through = 0; 381 this.point = poin; 382 } 383 384 public double Line_distance() { 385 return (Math.pow((Math.pow(this.point[1].y-this.point[0].y, 2))+(Math.pow(this.point[1].x-this.point[0].x, 2)), 0.5)); 386 } 387 public boolean Is_Line_Slope() { 388 if(this.point[0].x==this.point[1].x) 389 return false; 390 else 391 return true; 392 } 393 public double Line_Slope() { 394 return ((this.point[1].y-this.point[0].y)/(this.point[1].x-this.point[0].x)); 395 } 396 public void vector_get() { 397 Point Ls = new Point(); 398 Ls = this.vector; 399 Ls.x = this.point[1].x-this.point[0].x; 400 Ls.y = this.point[1].y-this.point[0].y; 401 } 402 public double Crossproduct(Line l) { 403 Point Ls = new Point(); 404 Ls = this.vector; 405 Point le = new Point(); 406 le = l.vector; 407 return (Ls.x*le.x+Ls.y*le.y); 408 } 409 public void Regularity() { 410 if(!this.Is_Line_Slope()) 411 this.b = this.point[0].x; 412 else { 413 this.k = this.Line_Slope(); 414 this.b = this.point[0].y-this.k*this.point[0].x; 415 } 416 } 417 public boolean coin_line(Line l) { 418 if(!(this.Is_Line_Slope()||l.Is_Line_Slope())) 419 { 420 if(this.b==l.b) 421 return true; 422 } 423 else if(this.k==l.k) 424 { 425 if(this.b==l.b) 426 return true; 427 } 428 429 return false; 430 } 431 public boolean straight_point_existence(Point p) { 432 if(!this.Is_Line_Slope()) 433 { 434 if(p.x==this.b) 435 return true; 436 } 437 else 438 { 439 if(p.y==this.k*p.x+this.b) 440 return true; 441 } 442 443 return false; 444 } 445 public boolean segment_point_existence(Point p) { 446 if(straight_point_existence(p)) 447 { 448 if(this.k==0&&p.y==this.point[0].y) 449 return true; 450 451 if((p.y<this.point[0].y&&p.y>this.point[1].y)||(p.y>this.point[0].y&&p.y<this.point[1].y)) 452 return true; 453 } 454 455 return false; 456 } 457 public Point get_intersection(Line l) { 458 Point p = new Point(); 459 if(!this.Is_Line_Slope()) { 460 p.x = this.b; 461 p.y = l.k*p.x+l.b; 462 } 463 else if(!l.Is_Line_Slope()) { 464 p.x = l.b; 465 p.y = this.k*p.x+this.b; 466 } 467 else { 468 p.y = (this.b-l.b)/(l.k-this.k); 469 p.x = (p.y-this.b)/this.k; 470 } 471 472 return p; 473 } 474 } 475 476 class input { 477 String s; 478 String s1,s2; 479 String[] a = new String[20]; 480 String[] b = new String[2]; 481 482 public input(String s) {//输入数据处理 483 this.s = s; 484 this.s1 = s.substring(0, 1); 485 this.s2 = s.substring(2); 486 this.a = s2.split(" "); 487 } 488 judge jud = new judge(); 489 490 491 } 492 class judge { 493 Line l = new Line(); 494 public boolean Point3_Collinear(Line l1,Line l2) {//判断三点是否在同一直线 495 if(!(l1.Is_Line_Slope()||l2.Is_Line_Slope())) 496 return true; 497 else if(!(l1.Is_Line_Slope()&&l2.Is_Line_Slope())) 498 return false; 499 else if(l1.Line_Slope()==l2.Line_Slope()) 500 return true; 501 else 502 return false; 503 } 504 public boolean polygon(pentagon p) {//判断是否为多边形 505 506 p.poin[5]=p.poin[0]; 507 for(int i=0;i<5;++i)//枚举第一条线段 508 for(int j=i+1;j<5;++j)//枚举第二条线段 509 { 510 if(j==i+1 || (j+1)%5==i ) 511 continue;//两线段本来就相邻 512 if(Is_intersect_segment(p.lin[i],p.lin[j])) 513 return false; 514 } 515 return true; 516 } 517 public boolean Is_pentagon(pentagon p) {//判断是否为五边形 518 //Point3_Collinear 519 if(Is_pentagon_pointcoin(p)) 520 return false; 521 if(!(Point3_Collinear(p.lin[0],p.lin[1])||Point3_Collinear(p.lin[1],p.lin[2])||Point3_Collinear(p.lin[2],p.lin[3])||Point3_Collinear(p.lin[3],p.lin[4])||Point3_Collinear(p.lin[4],p.lin[0]))) 522 return true; 523 else 524 return false; 525 } 526 public boolean Is_pentagon_Convex(pentagon p) {//判断是否为凸五边形 527 int i; 528 for(i=0;i<5;i++) { 529 if(p.lin[i].Crossproduct(p.lin[(i+1)%5])<0) 530 return false; 531 } 532 return true; 533 } 534 public boolean Is_pentagon_concave(pentagon p) {//判断是否为凹五边形 535 if(!Is_pentagon_Convex(p)) 536 return true; 537 else 538 return false; 539 } 540 public boolean Is_triangle(triangle t) {//判断是否为三角形 541 if(!Point3_Collinear(t.lin[0], t.lin[1])) 542 return true; 543 else 544 return false; 545 } 546 public boolean Is_quadrilateral(quadrilateral q) {//判断是否为四边形 547 if(!(Point3_Collinear(q.lin[0],q.lin[1])||Point3_Collinear(q.lin[1],q.lin[2])||Point3_Collinear(q.lin[2],q.lin[3])||Point3_Collinear(q.lin[3],q.lin[0]))) 548 return true; 549 else 550 return false; 551 } 552 public boolean Is_input(String s) {//正则表达式判断输入字符串格式是否正确 553 if(!s.matches("[1-5]\\:([+|-]?(0|[1-9][0-9]*)(\\.\\d+)?,[+|-]?(0|[1-9][0-9]*)(\\.\\d+)?\\ ?)+")) 554 return false; 555 else 556 return true; 557 } 558 public boolean Is_point_num(String[] s,int n) {//判断点的个数是否正确 559 if(s.length!=n) 560 return false; 561 else 562 return true; 563 } 564 public boolean Is_intersect_segment(Line l1,Line l2) {//判断两线段是否相交 565 double Ax1 = l1.point[0].x; 566 double Ay1 = l1.point[0].y; 567 double Ax2 = l1.point[1].x; 568 double Ay2 = l1.point[1].y; 569 double Bx1 = l2.point[0].x; 570 double By1 = l2.point[0].y; 571 double Bx2 = l2.point[1].x; 572 double By2 = l2.point[1].y; 573 if( 574 ( Math.max(Ax1,Ax2)>=Math.min(Bx1,Bx2)&&Math.min(Ax1,Ax2)<=Math.max(Bx1,Bx2) )&& //判断x轴投影 575 ( Math.max(Ay1,Ay2)>=Math.min(By1,By2)&&Math.min(Ay1,Ay2)<=Math.max(By1,By2) ) //判断y轴投影 576 ) 577 { 578 if( 579 ( (Bx1-Ax1)*(Ay2-Ay1)-(By1-Ay1)*(Ax2-Ax1) ) * //判断B是否跨过A 580 ( (Bx2-Ax1)*(Ay2-Ay1)-(By2-Ay1)*(Ax2-Ax1) ) <=0 && 581 ( (Ax1-Bx1)*(By2-By1)-(Ay1-By1)*(Bx2-Bx1) ) * //判断A是否跨过B 582 ( (Ax2-Bx1)*(By2-By1)-(Ay2-By1)*(Bx2-Bx1) ) <=0 583 ) 584 { 585 return true; 586 } 587 else 588 return false; 589 } 590 else 591 return false; 592 593 594 } 595 public boolean point_coincidence(Point p1,Point p2) {//判断两点是否重合 596 if(p1.x==p2.x&&p1.y==p2.y) 597 return true; 598 else 599 return false; 600 } 601 public boolean straight_segment_intersect(Line l1,Line l2) {//判断直线与线段是否相交 602 603 if(!Point3_Collinear(l1,l2)) 604 { 605 double x1,y1; 606 double x2,y2; 607 double y3,y4; 608 x1 = l2.point[0].x; 609 x2 = l2.point[1].x; 610 if(!l1.Is_Line_Slope()) 611 { 612 if((x1>l1.b&&x2<l1.b)||(x1<l1.b&&x2>l1.b)) 613 return true; 614 } 615 else if(!l2.Is_Line_Slope()) 616 { 617 y1 = l1.k*x1+l1.b; 618 if((l2.point[0].y>y1&&l2.point[1].y<y1)||(l2.point[0].y<y1&&l2.point[1].y>y1)) 619 return true; 620 } 621 else { 622 l2.k = l2.Line_Slope(); 623 l2.b = l2.point[0].y-l2.k*l2.point[0].x; 624 y1 = l2.k*x1+l2.b; 625 y2 = l2.k*x2+l2.b; 626 y3 = l1.k*x1+l1.b; 627 y4 = l1.k*x2+l1.b; 628 if((y1>y3&&y2<y4)||(y1>y3&&y2<y4)||(y1<y3&&y2>y4)||(y1<y3&&y2>y4)) 629 return true; 630 } 631 } 632 633 return false; 634 } 635 public boolean Is_pentagon_pointcoin(pentagon p) {//判断五边形的五个点是否有重合 636 for(int i=0;i<5;i++) { 637 for(int o =i+1;o<5;o++) { 638 if(point_coincidence(p.poin[i],p.poin[o])) 639 return true; 640 } 641 } 642 return false; 643 } 644 public boolean Is_point5_quadrilateral(pentagon p) {//判断五个点是否能形成四边形 645 int tmp=0; 646 for(int o=0;o<5;o++) { 647 if(this.point_coincidence(p.poin[o], p.poin[(o+1)%5])) { 648 tmp++; 649 p.poin[o].flag = 1; 650 } 651 652 } 653 if(tmp==1) 654 return true; 655 tmp = 0; 656 for(int i=5;i<10;i++) { 657 if(this.Point3_Collinear(p.lin[(i-1)%5], p.lin[i%5])) { 658 tmp++; 659 p.poin[i%5].flag = 1; 660 } 661 662 } 663 if(tmp==1) 664 return true; 665 666 return false; 667 } 668 public boolean Is_point5_triangle(pentagon p) {//判断五个点能否形成三角形 669 int tmp=0; 670 671 for(int o=0;o<5;o++) { 672 if(this.point_coincidence(p.poin[o], p.poin[(o+1)%5])) { 673 tmp++; 674 p.poin[o].flag = 1; 675 } 676 677 } 678 if(tmp==2) 679 return true; 680 tmp = 0; 681 for(int i=5;i<10;i++) { 682 if(this.Point3_Collinear(p.lin[(i-1)%5], p.lin[i%5])) { 683 tmp++; 684 p.poin[i%5].flag = 1; 685 } 686 687 } 688 if(tmp==2) 689 return true; 690 691 return false; 692 } 693 694 } 695 class pentagon extends polygon { 696 public pentagon(){//无参构造器 697 super(); 698 } 699 public pentagon(Point p1,Point p2,Point p3,Point p4,Point p5) {//带参构造器 700 super(); 701 this.poin[0] = p1; 702 this.poin[1] = p2; 703 this.poin[2] = p3; 704 this.poin[3] = p4; 705 this.poin[4] = p5; 706 } 707 judge jud = new judge(); 708 void Regularity_point_line() {//五边形边初始化 709 710 for(int i=0;i<5;i++) { 711 Line l = new Line(); 712 Point p = new Point(); 713 l.point[0] = super.poin[i%5]; 714 l.point[1] = super.poin[(i+1)%5]; 715 super.lin[i] = l; 716 p = super.lin[i].vector; 717 p.x = super.poin[i%5].x-super.poin[(i+1)%5].x; 718 p.y = super.poin[i%5].y-super.poin[(i+1)%5].y; 719 } 720 for(int o=0;o<5;o++) { 721 if(!this.lin[o].Is_Line_Slope()){ 722 this.lin[o].b = this.lin[o].point[0].x; 723 } 724 else { 725 this.lin[o].k = this.lin[o].Line_Slope(); 726 this.lin[o].b = this.lin[o].point[0].y-this.lin[o].k*this.lin[o].point[0].x; 727 } 728 } 729 } 730 double Perimeter() {//五边形周长 731 double sum = 0; 732 for(int i=0;i<5;i++) 733 { 734 sum+=this.lin[i].Line_distance(); 735 } 736 return sum; 737 } 738 triangle tri1 = new triangle(); 739 triangle tri2 = new triangle(); 740 triangle tri3 = new triangle(); 741 public void Regularity_point_triangle() {//五边形的三角形初始化 742 this.tri1.triangle_point_give(this.poin[0], this.poin[1], this.poin[2]); 743 this.tri2.triangle_point_give(this.poin[2], this.poin[3], this.poin[4]); 744 this.tri3.triangle_point_give(this.poin[0], this.poin[4], this.poin[2]); 745 } 746 //this.poin[0],this.poin[1],this.poin[2] 747 //this.poin[2],this.poin[3],this.poin[4] 748 // this.poin[0],this.poin[4],this.poin[2] 749 750 double area() {//计算五边形面积 751 return(this.tri1.triangle_area()+this.tri2.triangle_area()+this.tri3.triangle_area()); 752 } 753 public int pentagon_straight_coin(Line l) {//五边形与直线交点个数(未作标记) 754 int num01 = 0; 755 int num02 = 0; 756 int i; 757 for(i=0;i<5;i++) { 758 if(l.straight_point_existence(this.poin[i])) { 759 num01++; 760 this.poin[i].through = 1; 761 } 762 763 } 764 for(i=0;i<5;i++) { 765 if(jud.straight_segment_intersect(l, this.lin[i])) { 766 num02++; 767 this.lin[i].through = 1; 768 } 769 770 } 771 772 return num01+num02; 773 } 774 public Point[] pentagon_straight_find(Line l) {//寻找直线与五边形交点坐标 775 Point[] p = new Point[2]; 776 int i; 777 int m = 0; 778 for(i=0;i<5;i++) { 779 if(l.straight_point_existence(this.poin[i])) 780 { 781 p[m] = this.poin[i]; 782 this.poin[i].through = 1; 783 m++; 784 } 785 } 786 for(i=0;i<5;i++) { 787 if(jud.straight_segment_intersect(l, this.lin[i])) { 788 p[m] = l.get_intersection(this.lin[i]); 789 this.lin[i].through = 1; 790 m++; 791 } 792 793 } 794 return p; 795 } 796 public int Select_through_point() {//查询直线与角交点个数 797 int sum = 0; 798 for(int i=0;i<5;i++) { 799 if(this.poin[i].through==1) 800 sum++; 801 } 802 803 return sum; 804 } 805 public int Select_through_line() {//查询直线与边交点个数 806 int sum = 0; 807 for(int i=0;i<5;i++) { 808 if(this.lin[i].through==1) 809 sum++; 810 } 811 812 return sum; 813 } 814 public triangle through_give_triangle00(Line L){//与五边形的角没有交点的直线与五边形围成的三角形 815 triangle t = new triangle(); 816 int i1 = this.pent_create_triangle00()[0]; 817 int i2 = this.pent_create_triangle00()[1]; 818 int i; 819 if((i1==0&&i2==4)||(i1==4&&i2==0)) 820 i=0; 821 else { 822 i = (i1+i2+1)/2; 823 } 824 t.triangle_point_give(L.get_intersection(this.lin[i1]), L.get_intersection(this.lin[i2]), this.poin[i]); 825 return t; 826 827 } 828 public triangle through_give_triangle02() {//与五边形的角有两个交点的直线与五边形围成的三角形 829 triangle t = new triangle(); 830 int m=0; 831 double j = 0; 832 int o; 833 for(int i=0;i<5;i++) { 834 if(this.poin[i].through==1) { 835 t.poin[m] = this.poin[i]; 836 j+=i; 837 m++; 838 } 839 840 } 841 j = j/2; 842 if(j==2.5) 843 o = 0; 844 else if(j==1.5) 845 o = 4; 846 else 847 o = (int)j; 848 t.poin[2] = this.poin[o]; 849 return t; 850 } 851 public triangle through_give_triangle01(Line L) {//与五边形的角有一个交点的直线与五边形围成的三角形 852 triangle t = new triangle(); 853 int m = 0; 854 int n = 0; 855 int l; 856 for(int i=0;i<5;i++) { 857 if(this.poin[i].through==1) 858 m = i; 859 else if(this.lin[i].through==1) 860 n = i; 861 } 862 if(n==(m+1)%5) 863 l = n; 864 else { 865 l = n+1; 866 } 867 t.triangle_point_give(this.poin[m], this.poin[n], L.get_intersection(this.lin[l])); 868 return t; 869 } 870 public int[] pent_create_triangle02() {//与五边形的角有一个交点的直线与五边形是否围成一个三角形以及交点信息 871 int[] fb = new int[3]; 872 int i1=0; 873 for(int i=0;i<5;i++) { 874 if(this.poin[i].through==1) 875 { 876 fb[i1]=i; 877 i1++; 878 } 879 else if(this.lin[i].through==1) 880 fb[1]=i; 881 } 882 if((fb[1]-fb[0]==2)||(fb[1]-fb[0]==-3)) 883 fb[2] = 0; 884 else 885 fb[2] = 1; 886 887 return fb; 888 } 889 public quadrilateral give_quadrilateral01(Line l) {//与五边形的角有一个交点的直线与五边形围成的四边形的信息 890 quadrilateral qua = new quadrilateral(); 891 Point p = new Point(); 892 p = l.get_intersection(this.lin[this.pent_create_triangle02()[1]]); 893 qua.poin[0] = p; 894 for(int i=1;i<4;i++) { 895 qua.poin[i] = this.poin[(this.pent_create_triangle02()[1]+1)%5]; 896 } 897 return qua; 898 } 899 public quadrilateral give_quadrilateral00(Line l) {//与五边形的角没有交点的直线与五边形围成的四边形的信息 900 quadrilateral qua = new quadrilateral(); 901 int i1 = this.pent_create_triangle00()[0]; 902 int i2 = this.pent_create_triangle00()[1]; 903 int o1,o2; 904 if(Math.abs((i1+1)%5-i2)==1) { 905 o1 = (i1+1)%5; 906 o2 = i2; 907 } 908 else { 909 o1 = i1; 910 o2 = (i2+1)%5; 911 } 912 qua.quadrilateral_point_give(l.get_intersection(this.lin[i1]), this.poin[o1], this.poin[o2], l.get_intersection(this.lin[i2])); 913 return qua; 914 } 915 public int[] pent_create_triangle00() {//与五边形的角没有个交点的直线与五边形是否围成一个三角形以及交点信息 916 int[] fb = new int[3]; 917 int i1=0; 918 for(int i=0;i<5;i++) { 919 if(this.lin[i].through==1) 920 { 921 fb[i1]=i; 922 i1++; 923 } 924 } 925 if(Math.abs(fb[0]-fb[1])==1) 926 fb[2] = 1; 927 else 928 fb[2] = 0; 929 930 return fb; 931 } 932 public quadrilateral pentagon_reduce_quadrilateral() {//五边形信息转换为四边形 933 int m=0; 934 quadrilateral qua = new quadrilateral(); 935 for(int i=0;i<5;i++) { 936 if(this.poin[i].flag==0) { 937 qua.poin[m] = this.poin[i]; 938 m++; 939 } 940 941 } 942 943 return qua; 944 } 945 public triangle pentagon_reduce_triangle() { 946 int m = 0; 947 triangle tri = new triangle(); 948 for(int i=0;i<5;i++) { 949 if(this.poin[i].flag==0) { 950 tri.poin[m] = this.poin[i]; 951 m++; 952 } 953 954 } 955 return tri; 956 } 957 public boolean point_in_pentagon(Point p) { 958 double area = this.area(); 959 double areap = 0; 960 for(int i=0;i<5;i++) { 961 triangle tri = new triangle(p,this.poin[i],this.poin[(i+1)%5]); 962 areap+=tri.triangle_area(); 963 } 964 if(area==areap) 965 return true; 966 else 967 return false; 968 } 969 public int polygon_in_polygon(pentagon p) { 970 judge jud = new judge(); 971 int flag = 1; 972 int n1 = 0; 973 for(int i=0;i<5;i++) { 974 if(this.point_in_pentagon(p.poin[i])) 975 n1++; 976 } 977 if(n1==5) { 978 flag=6; 979 return flag; 980 } 981 982 else if(n1!=0) 983 flag = 2; 984 985 986 int n2=0; 987 for(int i=0;i<5;i++) { 988 if(p.point_in_pentagon(this.poin[i])) 989 n2++; 990 } 991 if(n2==5){ 992 flag=4; 993 return flag; 994 } 995 else if(n2!=0) 996 flag = 2; 997 if(n1==0&&n2==0) 998 { 999 flag=1; 1000 return flag; 1001 } 1002 1003 for(int o=0;o<5;o++) { 1004 int l=0; 1005 for(int u=0;u<5;u++) { 1006 if(jud.point_coincidence(this.poin[(o+u)%5], p.poin[u])) 1007 l++; 1008 } 1009 if(l==5) 1010 { 1011 flag=3; 1012 return flag; 1013 } 1014 } 1015 for(int i=0;i<5;i++) { 1016 if(this.point_in_pentagon(p.poin[i])) { 1017 int m=0; 1018 for(int o=0;o<5;o++) { 1019 if(this.lin[o].segment_point_existence(p.poin[i])) 1020 m++; 1021 if(jud.point_coincidence(this.poin[o], p.poin[i])) 1022 m++; 1023 } 1024 if(m==0) 1025 { 1026 flag =5; 1027 return flag; 1028 } 1029 } 1030 if(p.point_in_pentagon(this.poin[i])) { 1031 int m=0; 1032 for(int o=0;o<5;o++) { 1033 if(p.lin[o].segment_point_existence(this.poin[i])) 1034 m++; 1035 if(jud.point_coincidence(p.poin[o], this.poin[i])) 1036 m++; 1037 } 1038 if(m==0) 1039 { 1040 flag =5; 1041 return flag; 1042 } 1043 } 1044 1045 } 1046 flag = 2; 1047 return flag; 1048 } 1049 } 1050 class polygon {//多边形 1051 Point[] poin = new Point[10]; 1052 Line[] lin = new Line[10]; 1053 public polygon() { 1054 1055 } 1056 } 1057 class quadrilateral extends polygon{//四边形 1058 judge jud = new judge(); 1059 triangle tri1 = new triangle(); 1060 triangle tri2 = new triangle(); 1061 1062 public void Regularity_point_triangle() {//四边形被分成两个三角形 1063 this.tri1.triangle_point_give(this.poin[0], this.poin[1], this.poin[2]); 1064 this.tri2.triangle_point_give(this.poin[2], this.poin[3], this.poin[0]); 1065 } 1066 public void quadrilateral_point_give(Point p1,Point p2,Point p3,Point p4) {//给四边形赋予信息 1067 this.poin[0] = p1; 1068 this.poin[1] = p2; 1069 this.poin[2] = p3; 1070 this.poin[3] = p4; 1071 } 1072 void Regularity() {//四边形边初始化 1073 for(int i=0;i<4;i++) { 1074 Line l = new Line(); 1075 l.point[0] = super.poin[i%4]; 1076 l.point[1] = super.poin[(i+1)%4]; 1077 super.lin[i] = l; 1078 } 1079 } 1080 public int quadrilateral_straight_coin(Line l) {//四边形与直线交点个数 1081 int num01 = 0; 1082 int num02 = 0; 1083 int i; 1084 for(i=0;i<4;i++) { 1085 if(l.straight_point_existence(this.poin[i])) { 1086 num01++; 1087 this.poin[i].through = 1; 1088 } 1089 } 1090 for(i=0;i<4;i++) { 1091 if(jud.straight_segment_intersect(l, this.lin[i])) { 1092 num02++; 1093 this.lin[i].through = 1; 1094 } 1095 1096 } 1097 1098 return num01+num02; 1099 } 1100 public int Select_through_point() {//查询直线与角交点个数 1101 int sum = 0; 1102 for(int i=0;i<4;i++) { 1103 if(this.poin[i].through==1) 1104 sum++; 1105 } 1106 1107 return sum; 1108 } 1109 public int Select_through_line() {//查询直线与边交点个数 1110 int sum = 0; 1111 for(int i=0;i<4;i++) { 1112 if(this.lin[i].through==1) 1113 sum++; 1114 } 1115 1116 return sum; 1117 } 1118 public double quadrilateral_area() {//四边形面积 1119 return (this.tri1.triangle_area()+this.tri2.triangle_area()); 1120 } 1121 public int[] quadrilateral_create_triangle02() { 1122 int[] p = new int[3]; 1123 int m=0; 1124 for(int i=0;i<4;i++) { 1125 if(this.poin[i].through==1) { 1126 p[m] = i; 1127 m++; 1128 } 1129 } 1130 return p; 1131 } 1132 public int[] quadrilateral_create_triangle01() { 1133 int[] p = new int[3]; 1134 int m=0; 1135 for(int i=0;i<4;i++) { 1136 if(this.poin[i].through==1) { 1137 p[m] = i; 1138 m++; 1139 } 1140 if(this.lin[i].through==1) { 1141 p[m] = i; 1142 p[2] = m; 1143 m++; 1144 } 1145 } 1146 return p; 1147 } 1148 public int[] quadrilateral_create_triangle00() { 1149 int[] p = new int[3]; 1150 int m=0; 1151 for(int i=0;i<4;i++) { 1152 if(this.lin[i].through==1) { 1153 p[m] = i; 1154 m++; 1155 } 1156 } 1157 if((p[0]+1)%4==p[1]||(p[1]+1)%4==p[0]) 1158 p[2] = 1; 1159 else 1160 p[2] = 0; 1161 return p; 1162 } 1163 public triangle through_give_triangle02(Line l) {//找出直线与四边形中两个角相交的三角形 1164 triangle tri = new triangle(); 1165 int i1 = this.quadrilateral_create_triangle02()[0]; 1166 int i2 = this.quadrilateral_create_triangle02()[1]; 1167 tri.triangle_point_give(this.poin[i1], this.poin[i2], this.poin[(i1+1)%4]); 1168 return tri; 1169 } 1170 public triangle through_give_triangle01(Line l) {//找出直线与四边形中一个角相交的三角形 1171 triangle tri = new triangle(); 1172 int i1,i2,i; 1173 if(this.quadrilateral_create_triangle01()[2]==0) { 1174 i2 = this.quadrilateral_create_triangle01()[0]; 1175 i1 = this.quadrilateral_create_triangle01()[1]; 1176 } 1177 else { 1178 i1 = this.quadrilateral_create_triangle01()[0]; 1179 i2 = this.quadrilateral_create_triangle01()[1]; 1180 } 1181 Point p =new Point(); 1182 p = l.get_intersection(this.lin[i2]); 1183 if((i1+1)%4==i2) 1184 i = i2; 1185 else { 1186 i = (i2+1)%4; 1187 } 1188 tri.triangle_point_give(this.poin[i1], p, this.poin[i]); 1189 return tri; 1190 } 1191 public triangle through_give_triangle00(Line l) { 1192 triangle tri = new triangle(); 1193 int i1 = this.quadrilateral_create_triangle00()[0]; 1194 int i2 = this.quadrilateral_create_triangle00()[1]; 1195 int i; 1196 if((i1==0&&i2==3)||(i2==0&&i1==3)) 1197 i=0; 1198 else if(i1>i2) 1199 i = i1; 1200 else 1201 i = i2; 1202 Point[] p = new Point[2]; 1203 p[0] = l.get_intersection(this.lin[i1]); 1204 p[1] = l.get_intersection(this.lin[i2]); 1205 tri.triangle_point_give(p[0], p[1], this.poin[i]); 1206 return tri; 1207 } 1208 public quadrilateral through_give_quadrilateral(Line l) { 1209 quadrilateral qua = new quadrilateral(); 1210 int i1 = this.quadrilateral_create_triangle00()[0]; 1211 int i2 = this.quadrilateral_create_triangle00()[1]; 1212 Point[] p = new Point[2]; 1213 p[0] = l.get_intersection(this.lin[i1]); 1214 p[1] = l.get_intersection(this.lin[i2]); 1215 1216 qua.quadrilateral_point_give(p[0], this.poin[(i1+1)%4], this.poin[(i1+2)%4], p[1]); 1217 return qua; 1218 } 1219 public boolean point_in_pentagon(Point p) { 1220 double area = this.quadrilateral_area(); 1221 double areap = 0; 1222 for(int i=0;i<4;i++) { 1223 triangle tri = new triangle(p,this.poin[i],this.poin[(i+1)%5]); 1224 areap+=tri.triangle_area(); 1225 } 1226 if(area==areap) 1227 return true; 1228 else 1229 return false; 1230 } 1231 } 1232 class triangle extends polygon{//三角形 1233 public triangle() { 1234 super(); 1235 } 1236 public triangle(Point p1,Point p2,Point p3) {//构造器 1237 super(); 1238 this.poin[0] = p1; 1239 this.poin[1] = p2; 1240 this.poin[2] = p3; 1241 } 1242 judge jud = new judge(); 1243 public void triangle_point_give(Point p1,Point p2,Point p3) {//三角形赋予信息 1244 this.poin[0] = p1; 1245 this.poin[1] = p2; 1246 this.poin[2] = p3; 1247 } 1248 void Regularity_point_line() {//三角形边初始化 1249 for(int i=0;i<3;i++) { 1250 Line l = new Line(); 1251 l.point[0] = super.poin[i%3]; 1252 l.point[1] = super.poin[(i+1)%3]; 1253 super.lin[i] = l; 1254 } 1255 for(int o=0;o<3;o++) { 1256 if(!this.lin[o].Is_Line_Slope()){ 1257 this.lin[o].b = this.lin[o].point[0].x; 1258 } 1259 else { 1260 this.lin[o].k = this.lin[o].Line_Slope(); 1261 this.lin[o].b = this.lin[o].point[0].y-this.lin[o].k*this.lin[o].point[0].x; 1262 } 1263 } 1264 } 1265 public double triangle_area() {//三角形面积 1266 double r1 = this.poin[0].x*(this.poin[1].y-this.poin[2].y); 1267 double r2 = this.poin[1].x*(this.poin[2].y-this.poin[0].y); 1268 double r3 = this.poin[2].x*(this.poin[0].y-this.poin[1].y); 1269 double r = Math.abs(0.5*(r1+r2+r3)); 1270 return r; 1271 } 1272 public int triangle_straight_coin(Line l) {//直线与三角形交点个数 1273 int num01 = 0; 1274 int num02 = 0; 1275 int i; 1276 for(i=0;i<3;i++) { 1277 if(l.straight_point_existence(this.poin[i])) { 1278 num01++; 1279 this.poin[i].through = 1; 1280 } 1281 1282 } 1283 for(i=0;i<3;i++) { 1284 if(jud.straight_segment_intersect(l, this.lin[i])) { 1285 num02++; 1286 this.lin[i].through = 1; 1287 } 1288 1289 } 1290 1291 return num01+num02; 1292 } 1293 public int Select_through_point() {//查询直线与角交点个数 1294 int sum = 0; 1295 for(int i=0;i<3;i++) { 1296 if(this.poin[i].through==1) 1297 sum++; 1298 } 1299 1300 return sum; 1301 } 1302 public int Select_through_line() {//查询直线与边交点个数 1303 int sum = 0; 1304 for(int i=0;i<3;i++) { 1305 if(this.lin[i].through==1) 1306 sum++; 1307 } 1308 1309 return sum; 1310 } 1311 public int[] triangle_create_triangle01() { 1312 int[] p = new int[3]; 1313 int m=0; 1314 for(int i=0;i<3;i++) { 1315 if(this.poin[i].through==1) { 1316 p[m] = i; 1317 m++; 1318 } 1319 if(this.lin[i].through==1) { 1320 p[m] = i; 1321 p[2] = m; 1322 m++; 1323 } 1324 1325 } 1326 return p; 1327 } 1328 public int[] triangle_create_triangle00() { 1329 int[] p = new int[3]; 1330 int m=0; 1331 for(int i=0;i<3;i++) { 1332 if(this.lin[i].through==1) { 1333 p[m] = i; 1334 m++; 1335 } 1336 } 1337 return p; 1338 } 1339 public triangle through_give_triangle01(Line l) { 1340 triangle tri = new triangle(); 1341 int i1,i2,i; 1342 if(this.triangle_create_triangle01()[2]==0) 1343 { 1344 i2 = this.triangle_create_triangle01()[0]; 1345 i1 = this.triangle_create_triangle01()[1]; 1346 } 1347 else { 1348 i1 = this.triangle_create_triangle01()[0]; 1349 i2 = this.triangle_create_triangle01()[1]; 1350 } 1351 i = i2; 1352 tri.triangle_point_give(this.poin[i1], l.get_intersection(this.lin[i2]), this.poin[i2]); 1353 return tri; 1354 } 1355 public triangle through_give_triangle00(Line l) { 1356 triangle tri = new triangle(); 1357 int i1,i2,i; 1358 i1 = this.triangle_create_triangle00()[0]; 1359 i2 = this.triangle_create_triangle00()[1]; 1360 if((i1==0&&i2==2)||(i1==0&&i2==2)) 1361 i=0; 1362 else if(i1>i2) 1363 i = i2; 1364 else { 1365 i = i1; 1366 } 1367 tri.triangle_point_give(this.poin[i], l.get_intersection(this.lin[i1]), l.get_intersection(this.lin[i2])); 1368 return tri; 1369 } 1370 public boolean point_in_pentagon(Point p) { 1371 double area = this.triangle_area(); 1372 double areap = 0; 1373 for(int i=0;i<3;i++) { 1374 triangle tri = new triangle(p,this.poin[i],this.poin[(i+1)%5]); 1375 areap+=tri.triangle_area(); 1376 } 1377 if(area==areap) 1378 return true; 1379 else 1380 return false; 1381 } 1382 }


本次代码同过软件的分析结果明显比以前的更加好。
-
设计一个类表示平面直角坐标系上的点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)方法。
在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。
- 对题目中的点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();
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
- 在原有类设计的基础上,增加一个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()方法进行输出。
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Main { 5 6 public static void main(String[] args) { 7 Scanner input=new Scanner(System.in); 8 int num; 9 int index; 10 GeometryObject g = new GeometryObject(); 11 num=input.nextInt(); 12 while(num!=0) 13 { 14 switch(num) 15 { 16 case 1:{ 17 Point p = new Point(input.nextDouble(),input.nextDouble()); 18 g.add(p); 19 } 20 break; 21 case 2:{ 22 Point p1 = new Point(input.nextDouble(),input.nextDouble()); 23 Point p2 = new Point(input.nextDouble(),input.nextDouble()); 24 Line l = new Line(p1,p2,input.next()); 25 g.add(l); 26 27 } 28 break; 29 case 3: 30 { 31 Plane p = new Plane(input.next()); 32 g.add(p); 33 } 34 break; 35 case 4: 36 { 37 index = input.nextInt(); 38 if(g.getList().size()>=index) 39 g.remove(index-1); 40 } 41 } 42 num = input.nextInt(); 43 } 44 ArrayList<Element> list = g.getList(); 45 for(int i=0;i<list.size();i++) 46 { 47 list.get(i).display(); 48 } 49 } 50 51 } 52 abstract class Element { 53 public abstract void display(); 54 } 55 class GeometryObject { 56 private ArrayList<Element> list = new ArrayList<Element>(); 57 public GeometryObject() { 58 59 } 60 public GeometryObject(ArrayList<Element> list) { 61 this.list = list; 62 } 63 public void add(Element element) { 64 list.add(element); 65 } 66 public void remove(int index) { 67 list.remove(index); 68 } 69 public ArrayList<Element> getList(){ 70 return this.list; 71 } 72 public void setList(ArrayList<Element> list){ 73 this.list = list; 74 } 75 } 76 class Line extends Element{ 77 private Point point1; 78 private Point point2; 79 String color; 80 public Line() { 81 82 } 83 public Line(Point point1,Point point2,String color) { 84 this.point1 = point1; 85 this.point2 = point2; 86 this.color = color; 87 } 88 public Point getPoint1() { 89 return this.point1; 90 } 91 public void setPoint1(Point point1) { 92 this.point1 = point1; 93 } 94 public Point getPoint2() { 95 return this.point1; 96 } 97 public void setPoint2(Point point2) { 98 this.point2 = point2; 99 } 100 public String getColor() { 101 return this.color; 102 } 103 public void setColor(String color) { 104 this.color = color; 105 } 106 public double getDistance() { 107 return Math.pow((Math.pow(point1.getY()-point2.getY(), 2)+Math.pow(point1.getX()-point2.getX(), 2)), 0.5); 108 } 109 public void display() { 110 System.out.println("The line's color is:"+color); 111 System.out.println("The line's begin point's Coordinate is:"); 112 point1.display(); 113 System.out.println("The line's end point's Coordinate is:"); 114 point2.display(); 115 System.out.println("The line's length is:"+String.format("%.2f", this.getDistance())); 116 } 117 118 } 119 class Plane extends Element{ 120 private String color; 121 public Plane() { 122 123 } 124 public Plane(String color) { 125 this.color = color; 126 } 127 public String getColor() { 128 return this.color; 129 } 130 public void setColor(String color) { 131 this.color = color; 132 } 133 @Override 134 public void display() { 135 System.out.println("The Plane's color is:"+this.color); 136 } 137 138 } 139 class Point extends Element{ 140 private double x; 141 private double y; 142 public Point() { 143 144 } 145 public Point(double x,double y) { 146 this.x = x; 147 this.y = y; 148 if(x<=0||x>200) 149 { 150 System.out.println("Wrong Format"); 151 System.exit(1); 152 } 153 else if(y<=0||y>200) { 154 System.out.println("Wrong Format"); 155 System.exit(1); 156 } 157 } 158 public double getX() { 159 return this.x; 160 } 161 public void setX(double x) { 162 this.x = x; 163 } 164 public double getY() { 165 return this.y; 166 } 167 public void setY(double y) { 168 this.y = y; 169 } 170 public void display() { 171 System.out.println("("+String.format("%.2f", this.x)+","+String.format("%.2f", this.y)+")"); 172 } 173 }
本次代码主要是为了考察继承,多态,容器等知识,同时我也找到了自己漏学的知识点。







三丶踩坑心得
要多用get set函数
四丶改进建议
我自己写的也不好,所以没有建议。
五丶总结
在前几次作业中我并没有使用类,虽然有的也过了,但越到后面的题目发现越来越吃力,自从使用了类之后发现这几次的题目是可以一起用的,而且条例更加的清晰,没有之前的混乱,还有继承,多态等也非常的好用。
浙公网安备 33010602011771号