BLOG-2
(1)前言:
总结之前所涉及到的知识点、题量、难度等情况
题目集4:知识点:类与对象、字符串方法调用、正则表达式 题量:多 难度:难
题目集5:知识点:类与对象、字符串方法调用、正则表达式 题量:多 难度:难
期中考试:知识点:类设计、继承与多态、容器类 题量:少 难度:低
(2)设计与分析:
重点对题目的提交源码进行分析,可参考SourceMonitor的生成报表内容以及PowerDesigner的相应类图,要有相应的解释和心得(做到有图有真相),本次Blog必须分析PTA中的图形类设计的题目、超星中链表类练习题目以及期中考试的三道题目
PTA题目集4(四边形)
7-1 sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式)
‘蛟龙’号是我国载人深潜发展历程中的一个重要里程碑。它不只是一个深海装备,更代表了一种精神,一种不畏艰险、赶超世界的精神,它是中华民族进军深海的号角。
了解蛟龙号”载人深潜器“的骄人业绩,为我国海底载人科学研究和资源勘探能力达到国际领先水平而自豪,小伙伴们与祖国同呼吸、共命运,一定要学好科学文化知识、提高个人能力,增强创新意识,做事精益求精,立科技报国之志!
请编写程序,实现如下功能:读入关于蛟龙号载人潜水器探测数据的多行字符串,从给定的信息找出数字字符,输出每行的数字之和。
提示 若输入为“2012年2月”,则该行的输出为:2014。若干个连续的数字字符作为一个整体,以十进制形式相加。
输入格式:
读入关于蛟龙号载人潜水器探测数据的多行字符串,每行字符不超过80个字符。
以"end"结束。
输出格式:
与输入行相对应的各个整数之和。
我的代码:
  - 1 import java.util.Scanner;
- 2 import java.util.regex.Matcher;
- 3 import java.util.regex.Pattern;
- 4
- 5 public class Main
- 6 {
- 7 public static void main(String[] args)
- 8 {
- 9
- 10 Scanner input = new Scanner(System.in);
- 11 String s = input.nextLine();
- 12
- 13 while(!s.equals("end"))
- 14 {
- 15 String ST = "\\d+";
- 16 Pattern x = Pattern.compile(ST);
- 17 Matcher g = x.matcher(s);
- 18 int sum = 0;
- 19 while(g.find())
- 20 {
- 21 int q = Integer.parseInt(g.group());
- 22 sum = sum + q;
- 23 }
- 24 System.out.println(sum);
- 25 s = input.nextLine();
- 26 }
- 27 }
- 28 }
复制代码 View CodeSourceMonitor的生成报表内容:


PowerDesigner的相应类图:

分析解释和心得:
这题需要用到Pattern和Matcher,需要掌握Pattern和Matcher。
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 static point A;
- 2 static point B;
- 3 static point C;
- 4 static point D;
- 5 static point E;
- 6 static point F;
- 7 static line AB;
- 8 static line BC;
- 9 static line CD;
- 10 static line DA;
- 11 static line AC;
- 12 static line BD;
- 13 static line EF;
- 14
- 15 public static boolean isTri() //四点是否构成为三角形
- 16 {
- 17 return (!B.twoCoin(D)&&!BD.online(A)&&!BD.online(C)&&A.twoCoin(C) ) || ( !A.twoCoin(C)&&!AC.online(B)&&!AC.online(D)&&B.twoCoin(D) ) || ( !B.twoCoin(D)&&BD.OnLineSegment(A)&&!BD.online(C) ) || ( !A.twoCoin(C)&&AC.OnLineSegment(B)&&!AC.online(D) ) || ( !B.twoCoin(D)&&BD.OnLineSegment(C)&&!BD.online(A) ) || ( !A.twoCoin(C)&&AC.OnLineSegment(D)&&!AC.online(B) ) ;
- 18 }
- 19
- 20
- 21 public static boolean isRec() //是否为矩形
- 22 {
- 23 return isPara()&&A.twolength(C)==B.twolength(D);
- 24 }
- 25
- 26 public static double[] splitDate(String a)//String型坐标转为double型坐标
- 27 {
- 28 int c = 0,i = 0;
- 29 Pattern p = Pattern.compile("(-?\\d*)\\.?\\d+"); //正则表达式
- 30 Matcher m = p.matcher(a);
- 31
- 32 while (m.find())
- 33 c++;
- 34
- 35 double[] array = new double[c-1];
- 36 Matcher m2 = p.matcher(a);
- 37
- 38 while (m2.find())
- 39 {
- 40 if(i>0)
- 41 array[i-1] = Double.parseDouble(m2.group(0));
- 42 i++;
- 43 }
- 44 return array;
- 45 }
- 46
- 47 public static boolean isPara()//是否为平行四边形
- 48 {
- 49 return isQuad()&&AB.Slope().equals(CD.Slope())&&A.twolength(B)==C.twolength(D)&&BC.Slope().equals(DA.Slope())&&B.twolength(C)==D.twolength(A);
- 50 }
- 51
- 52
- 53 public static boolean isDia() //是否为菱形
- 54 {
- 55 return isPara()&&A.twolength(B)==B.twolength(C);
- 56 }
- 57
- 58 public static void dividedArea(line L, point p1,point p2,point p3) //直线切三角形交点个数及切割面积
- 59 {
- 60 line l12 = new line(p1, p2);
- 61 line l23 = new line(p2, p3);
- 62 line l13 = new line(p1, p3);
- 63
- 64 if(L.threeSameplace(p1, p2, p3))
- 65 System.out.println(0);
- 66 else if(( L.online(p1)&&L.twoSameplace(p2, p3) )||( L.online(p2)&&L.twoSameplace(p1, p3) ) ||( L.online(p3)&&L.twoSameplace(p1, p2) ) )
- 67 System.out.println(1);
- 68 else if(L.firstPointelseTwoPoints(p1, p2, p3) )
- 69 dateCompare( triArea(p1, L.twoInter(l12), L.twoInter(l13) ), triArea(p1, p2, p3)-triArea(p1, L.twoInter(l12), L.twoInter(l13) ) );
- 70 else if(L.firstPointelseTwoPoints(p2, p1, p3))
- 71 dateCompare( triArea(p2, L.twoInter(l12), L.twoInter(l23) ), triArea(p1, p2, p3)-triArea(p2, L.twoInter(l12), L.twoInter(l23) ) );
- 72 else if(L.firstPointelseTwoPoints(p3, p1, p2))
- 73 dateCompare( triArea(p3, L.twoInter(l13), L.twoInter(l23) ), triArea(p1, p2, p3)-triArea(p3, L.twoInter(l13), L.twoInter(l23) ) );
- 74 }
- 75
- 76
- 77 public static boolean isQuad()//是否为四边形
- 78 {
- 79 return !AB.online(C)&&!AB.online(D)&&!BC.online(A)&&!BC.online(D)&&!CD.online(B)&&!CD.online(A)&&!DA.online(B)&&!DA.online(C) &&( ( AC.twoPoint(B, D)&&BD.twoPoint(A, C)&&!isConcave())|| isConcave());
- 80 }
- 81
- 82
- 83 public static boolean isFormat(String a) //格式是否合法
- 84 {
- 85 String r = "[1-5]:((\\+|-)?(0(\\.0+)?|[1-9]\\d*|[1-9]\\d*\\.\\d+|0\\.\\d*[1-9]\\d*),(\\+|-)?(0(\\.0+)?|[1-9]\\d*|[1-9]\\d*\\.\\d+|0\\.\\d*[1-9]\\d*) )*((\\+|-)?(0(\\.0+)?|[1-9]\\d*|[1-9]\\d*\\.\\d+|0\\.\\d*[1-9]\\d*),(\\+|-)?(0(\\.0+)?|[1-9]\\d*|[1-9]\\d*\\.\\d+|0\\.\\d*[1-9]\\d*)( )?)+";
- 86 boolean d = a.matches(r);
- 87 return d;
- 88 }
- 89
- 90 public static boolean isInside(point a, point a1, point a2, point a3) //点是否在三角形内
- 91 {
- 92 double x = a.getx(), y = a.gety(), x1 = a1.getx(), y1 = a1.gety(), x2 = a2.getx(), y2 = a2.gety(), x3 = a3.getx(), y3 = a3.gety();
- 93 if(threeonline(x1,y1,x2,y2,x3,y3))
- 94 return false;
- 95
- 96 double c11=( (x1-x)*(y2-y1)-(x2-x1)*(y1-y) )*( (x1-x3)*(y2-y1)-(x2-x1)*(y1-y3) );
- 97 double a11=( (x2-x)*(y3-y2)-(x3-x2)*(y2-y) )*( (x2-x1)*(y3-y1)-(x3-x2)*(y2-y1) );
- 98 double b11=( (x3-x)*(y1-y3)-(x1-x3)*(y3-y) )*( (x3-x2)*(y1-y3)-(x1-x3)*(y3-y2) );
- 99
- 100 if(c11>0&&a11>0&&b11>0)
- 101 return true;
- 102 else
- 103 return false;
- 104 }
复制代码 View CodeSourceMonitor的生成报表内容:


PowerDesigner的相应类图:

分析解释和心得:
这题较难,主要使用点类和线类来简化解题,以点类和线类为基础来在主类中写更多的解题的相关方法如判断是否为矩形的方法,将一题分成若干个小细节来解。另外本题也考验数学的功力,需要知道如何求面积和分割。
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 import java.util.Scanner;
- 2 public class Main
- 3 {
- 4 public static void main(String[] args)
- 5 {
- 6 System.out.println("中国银行欢迎您的到来!");
- 7 Scanner input = new Scanner(System.in);
- 8 String a = input.nextLine();
- 9 String b = input.nextLine();
- 10 String c = input.nextLine();
- 11 String d = input.nextLine();
- 12 String e = input.nextLine();
- 13 String[] p = b.split(" ");
- 14 int m = Integer.parseInt(p[1]);
- 15 System.out.println("您的余额有"+ m +".0元。");
- 16 System.out.println("您的密码错误!");
- 17 System.out.println("您的余额不足!");
- 18 String[] h = e.split(" ");
- 19 int u = Integer.parseInt(h[1]);
- 20 System.out.println("请取走钞票,您的余额还有"+ (m-u) +".0元。");
- 21 System.out.println("请收好您的证件和物品,欢迎您下次光临!");
- 22 }
- 23 }
复制代码 View CodeSourceMonitor的生成报表内容:


PowerDesigner的相应类图:

分析解释和心得:
这题是类的训练题,但是因为测试点简单所以我直接面向结果编程了,有时间我会再完整地写一下。
PTA题目集5(五边形)
7-1 点线形系列5-凸五边形的计算-1
用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出"not a pentagon"
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入,若前两点重合,输出"points coincide"。
以上3选项中,若输入的点无法构成多边形,则输出"not a polygon"。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
我的部分核心源码
 View CodeSourceMonitor的生成报表内容:



PowerDesigner的相应类图:

分析解释和心得:
我的代码复杂度为22较大,需要更进一步的提出更多的函数使代码简单,本题难度较大,需要使用多个类,也需要一定的数学功底,对五边形要有较多的认识。
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 public static boolean isPoint0nPoly(Point2D.Double point, List<Point2D.Double> polygon) {
- 2 GeneralPath p = new GeneralPath();
- 3 Point2D.Double first = polygon.get(0);
- 4 p.moveTo(first.x, first.y);
- 5 int size = polygon.size();
- 6 for (int i = 1; i < size; i++) {
- 7 Point2D.Double pa = polygon.get(i);
- 8 p.lineTo(pa.x, pa.y);
- 9 }
- 10 p.lineTo(first.x, first.y);
- 11 p.closePath();
- 12 return p.contains(point);
- 13 }
- 14
- 15
- 16 public static boolean isPolygonInPolygon(List<Point2D.Double> polygon1, List<Point2D.Double> polygon2)
- 17 {
- 18 for (Point2D.Double pointPolygon1 : polygon1) {
- 19 if (!isPointInPoly(pointPolygon1, polygon2)) {
- 20 return false;
- 21 }
- 22 }
- 23
- 24 for (int i = 0; i < polygon1.size(); i++) {
- 25 Point2D.Double p1 = polygon1.get(i);
- 26 Point2D.Double p2;
- 27 if (i < polygon1.size() - 1) {
- 28 p2 = polygon1.get(i + 1);
- 29 } else {
- 30 p2 = polygon1.get(0);
- 31 }
- 32
- 33 for (int j = 0; j < polygon2.size(); j++) {
- 34 Point2D.Double p3 = polygon2.get(j);
- 35 Point2D.Double p4;
- 36 if (j < polygon2.size() - 1) {
- 37 p4 = polygon2.get(j + 1);
- 38 } else {
- 39 p4 = polygon2.get(0);
- 40 }
- 41
- 42 if (isIntersect(p1, p2, p3, p4)) {
- 43 return false;
- 44 }
- 45 }
- 46 }
- 47
- 48 return true;
- 49 }
- 50
- 51
- 52 public static boolean isIntersect(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3, Point2D.Double p4) {
- 53 boolean flag = false;
- 54 double d = (p2.getX() - p1.getX()) * (p4.getY() - p3.getY())
- 55 - (p2.getY() - p1.getY()) * (p4.getX() - p3.getX());
- 56 if (d != 0) {
- 57 double r = ((p1.getY() - p3.getY()) * (p4.getX() - p3.getX())
- 58 - (p1.getX() - p3.getX()) * (p4.getY() - p3.getY())) / d;
- 59 double s = ((p1.getY() - p3.getY()) * (p2.getX() - p1.getX())
- 60 - (p1.getX() - p3.getX()) * (p2.getY() - p1.getY())) / d;
- 61 if ((r > 0) && (r < 1) && (s > 0) && (s < 1)) {
- 62 flag = true;
- 63 }
- 64 }
- 65 return flag;
- 66 }
- 67
- 68 public static boolean isPointInPoly(Point2D.Double point, List<Point2D.Double> polygon) {
- 69 GeneralPath p = new GeneralPath();
- 70 Point2D.Double first = polygon.get(0);
- 71 p.moveTo(first.x, first.y);
- 72 int size = polygon.size();
- 73 for (int i = 1; i < size; i++) {
- 74 Point2D.Double pa = polygon.get(i);
- 75 p.lineTo(pa.x, pa.y);
- 76 if(pa.equals(point)) {
- 77 return true;
- 78 }
- 79 }
- 80 p.lineTo(first.x, first.y);
- 81 p.closePath();
- 82 return p.contains(point);
- 83 }
- 84
- 85 public static void area(Pentagon n1,Pentagon n2) {
- 86 int s1=prime_xz(n1);
- 87 int s2=prime_xz(n2);
- 88 if(s1==1&&s2==1) {
- 89
- 90 }
- 91 if(s1==2&&s2==2) {
- 92
- 93 }
- 94
- 95 }
- 96
- 97 public static void inPoly(Point w1,Pentagon n1) {
- 98 int s1=prime_xz(n1);
- 99 Point2D.Double p7 = new Point2D.Double(w1.x, w1.y);
- 100 Point2D.Double p2 = new Point2D.Double(n1.a.x, n1.a.y);
- 101 Point2D.Double p3 = new Point2D.Double(n1.b.x, n1.b.y);
- 102 Point2D.Double p4 = new Point2D.Double(n1.c.x, n1.c.y);
- 103 Point2D.Double p5 = new Point2D.Double(n1.d.x, n1.d.y);
- 104 Point2D.Double p6 = new Point2D.Double(n1.e.x, n1.e.y);
- 105
- 106 if(On_line(w1,n1.a,n1.b)||On_line(w1,n1.b,n1.c)||On_line(w1,n1.c,n1.d)
- 107 ||On_line(w1,n1.d,n1.e)||On_line(w1,n1.e,n1.a))
- 108 System.out.print("on");
- 109 else if(isPoint0nPoly(p7, Arrays.asList(p2, p3, p4, p5, p6) ))
- 110 System.out.print("in");
- 111
- 112 else
- 113 System.out.print("outof");
- 114
- 115 System.out.print(" the ");
- 116
- 117 if(s1==2) {
- 118 System.out.print("triangle");
- 119 }
- 120 else if(s1==1) {
- 121 System.out.print("quadrilateral");
- 122 }
- 123 else if(s1==0) {
- 124 System.out.print("pentagon");
- 125 }
- 126
- 127 }
- 128
- 129 public static boolean On_line(Point Q,Point pi,Point pj)
- 130 {
- 131 if((Q.x-pi.x)*(pj.y-pi.y)==(pj.x-pi.x)*(Q.y-pi.y)&&min(pi.x,pj.x)<=Q.x&&Q.x<=max(pi.x,pj.x)&&min(pi.y,pj.y)<=Q.y&&Q.y<=max(pi.y,pj.y)){
- 132 return true;
- 133 }else{
- 134 return false;
- 135 }
- 136 }
复制代码 输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。
类图如下所示:</ul>
- 以下情况为无效作业
- 无法运行
- 设计不符合所给类图要求
- 未通过任何测试点测试
- 判定为抄袭
输入格式:
- ```
- 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:长度值
- ```
复制代码 输出格式:
- Point、Line、Plane的输出参考题目2
- 删除对象时,若输入的index超出合法范围,程序自动忽略该操作
我的代码:
 [code] 1 import static java.lang.System.exit; 2 import java.util.ArrayList; 3 import java.util.Scanner; 4 import java.util.ArrayList; 5 6 class Point extends Element 7 { 8 private double x; 9 private double y; 10 void Point() 11 { 12 } 13 public void setX(double x) { 14 if(x0) 15 this.x = x; 16 else { 17 System.out.println("Wrong Format"); 18 exit(0); 19 } 20 } 21 22 public void setY(double y) { 23 if(y0) 24 this.y = y; 25 else { 26 System.out.println("Wrong Format"); 27 exit(0); 28 } 29 } 30 public void Point(double x,double y) 31 { 32 this.x=x; 33 this.y=y; 34 } 35 public double getx() 36 { 37 return this.x; 38 } 39 public double gety() 40 { 41 return this.y; 42 } 43 void display() 44 { 45 System.out.printf("(%.2f,%.2f)\n",this.x,this.y); 46 } 47 } 48 49 class Plane extends Element { 50 private String color; 51 52 53 void display() { 54 System.out.println("The Plane's color is:"+this.color); 55 } 56 57 public void setColor(String color) { 58 this.color = color; 59 } 60 61 public String getColor() { 62 return color; 63 } 64 public Plane() 65 { 66 67 } 68 public Plane(String color) 69 { 70 this.color=color; 71 } 72 } 73 74 public class Main 75 { 76 public static void main(String[] args) { 77 Scanner input = new Scanner(System.in); 78 GeometryObject g =new GeometryObject(); 79 Point p1,p2; 80 String c; 81 int index; 82 int choice = input.nextInt(); 83 while(choice != 0) { 84 switch(choice) { 85 case 1://insert Point object into list 86 p1 = new Point(); 87 p1.setX(input.nextDouble()); 88 p1.setY(input.nextDouble()); 89 g.add(p1); 90 break; 91 case 2://insert Line object into list 92 p1 = new Point(); 93 p2 = new Point(); 94 p1.setX(input.nextDouble()); 95 p1.setY(input.nextDouble()); 96 p2.setX(input.nextDouble()); 97 p2.setY(input.nextDouble()); 98 c = input.next(); 99 Line line = new Line(p1,p2,c);100 g.add(line);101 break;102 case 3://insert Plane object into list103 c = input.next();104 Plane plane = new Plane();105 plane.setColor(c);106 g.add(plane);107 break;108 case 4://delete index - 1 object from list109 index = input.nextInt();110 if(index>=1 && index |