BLOG-2

BLOG-2 (1)前言: 总结之前所涉及到的知识点、题量、难度等情况 题目集4:知识点:类与对象、字符串方法调用、正则表达式 题量:多 难度:难 题目集5:知识点:类与对象、字符串方法调用、正则表达式 题量:多 难度:难 期中考试:知识点:类设计、继承与多态、容器类 题量:少 难度:低 (2)设计 ...

BLOG-2

(1)前言

  总结之前所涉及到的知识点、题量、难度等情况

  题目集4:知识点:类与对象、字符串方法调用、正则表达式、四边形相关知识 题量:中 难度:难

  题目集5:知识点:类与对象、字符串方法调用、正则表达式、五边形相关知识 题量:多 难度:难

  期中考试:知识点:类设计、继承与多态、容器类 题量:少 难度:低

(2)设计与分析

  重点对题目的提交源码进行分析,可参考SourceMonitor的生成报表内容以及PowerDesigner的相应类图,要有相应的解释和心得(做到有图有真相),本次Blog必须分析PTA中的图形类设计的题目、超星中链表类练习题目以及期中考试的三道题目

PTA题目集4(四边形)

7-1 sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式)

‘蛟龙’号是我国载人深潜发展历程中的一个重要里程碑。它不只是一个深海装备,更代表了一种精神,一种不畏艰险、赶超世界的精神,它是中华民族进军深海的号角。

了解蛟龙号”载人深潜器“的骄人业绩,为我国海底载人科学研究和资源勘探能力达到国际领先水平而自豪,小伙伴们与祖国同呼吸、共命运,一定要学好科学文化知识、提高个人能力,增强创新意识,做事精益求精,立科技报国之志!

请编写程序,实现如下功能:读入关于蛟龙号载人潜水器探测数据的多行字符串,从给定的信息找出数字字符,输出每行的数字之和。

提示若输入为“2012年2月”,则该行的输出为:2014。若干个连续的数字字符作为一个整体,以十进制形式相加。

输入格式:

读入关于蛟龙号载人潜水器探测数据的多行字符串,每行字符不超过80个字符。

以"end"结束。

输出格式:

与输入行相对应的各个整数之和。

我的代码:

BLOG-2BLOG-2

 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 Code

SourceMonitor的生成报表内容:

BLOG-2

BLOG-2

PowerDesigner的相应类图:

BLOG-2

分析解释和心得:

这题需要使用到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"。

我的部分核心源码

BLOG-2BLOG-2

  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 Code

SourceMonitor的生成报表内容:

BLOG-2

BLOG-2

PowerDesigner的相应类图:

BLOG-2

分析解释和心得:

这题较难,先判断是不是平行四边形,在此基础上,判断是不是正方形(四边相等,对角线相等),再判断是不是菱形(四边相等)最后判断是不是矩形(对角线相等),在切割四边形时,先计算直线与四边形每条边的交点,再判断交点在四边形的哪一条边,将切割的区域分割成三角形计算面积;主要使用点类和线类来简化解题,以点类和线类为基础来在主类中写更多的解题的相关方法如判断是否为矩形的方法,将一题分成若干个小细节来解。另外本题也考验数学的功力,需要知道如何求面积和分割。判断点和四边形或者三角形的空间位置关系,判断三角形四边形之后,利用叉乘方法判断是否在该图形内,向量叉乘可以判断两线段是否相交,凸四边形对角线会相交,而凹四边形反之。在计算多边形重叠面积时,先求出两个多边形的交点,然后分别得到多边形的互相在其内部的点,组成的一个构成多边形的点的有序数组。

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()方法。

输入格式:

输入开户需要的姓名、密码
输入正确密码、存款金额
输入错误密码、取款金额
输入正确密码、大于余额的取款金额
输入正确密码、小于余额的取款金额

输出格式:

中国银行(银行名称)欢迎您的到来!
您的余额有多少元。
您的密码错误!
您的余额不足!
请取走钞票,您的余额还有多少元。
请收好您的证件和物品,欢迎您下次光临!

我的源码

BLOG-2BLOG-2

 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 Code

SourceMonitor的生成报表内容:

BLOG-2

BLOG-2

PowerDesigner的相应类图:

BLOG-2

分析解释和心得:

这题是类的训练题,因为测试点简单所以我直接面向结果编程了,这题考察了类和对象的知识。

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

我的部分核心源码

BLOG-2BLOG-2

  1 public double isPentagon(Point a,Point b,Point c) {
  2     double q=a.x-b.x;
  3     double w=a.y-b.y;
  4     double e=c.x-b.x;
  5     double r=c.y-b.y;
  6     double s=Math.sqrt(q * q + w * w);
  7     double t=Math.sqrt(e * e + r * r);
  8     double f=q * e + w * r;
  9     double v = f /(s*t); 
 10     double k=Math.toDegrees(Math.acos(v));
 11     return k;
 12 }
 13 public double isSlope() {
 14     double k1=(this.y.getY() - this.z.getY())*(this.x.getX() - this.y.getX());
 15     double k2=(this.x.getY() - this.y.getY())*(this.y.getX() - this.z.getX());
 16     double k3=(this.z.getY() - this.a.getY())*(this.y.getX() - this.z.getX());
 17     double k4=(this.y.getY() - this.z.getY())*(this.z.getX() - this.a.getX());
 18     double k5=(this.a.getY() - this.b.getY())*(this.z.getX() - this.a.getX());
 19     double k6=(this.z.getY() - this.a.getY())*(this.a.getX() - this.b.getX());
 20     double k7=(this.b.getY() - this.x.getY())*(this.a.getX() - this.b.getX());
 21     double k8=(this.a.getY() - this.b.getY())*(this.b.getX() - this.x.getX());
 22     double k9=(this.x.getY() - this.y.getY())*(this.b.getX() - this.x.getX());
 23     double k10=(this.b.getY() - this.x.getY())*(this.x.getX() - this.y.getX());
 24     if(k1-k2==0||k3-k4==0||k5-k6==0||k7-k8==0||k9-k10==0)
 25     {
 26         return 1;
 27     }
 28     else {
 29         return 0;
 30     }
 31 }
 32 public boolean isParallelogram() {
 33     return true;
 34 }
 35 
 36 public double getArea() {
 37     double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
 38     double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
 39     double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
 40     double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX());
 41     double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX());
 42     double k6 = (this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY())+(this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX());
 43     double k7 = (this.x.getY() - this.a.getY())*(this.x.getY() - this.a.getY())+(this.x.getX() - this.a.getX())*(this.x.getX() - this.a.getX());
 44     double d1 = Math.sqrt(k1);
 45     double d2 = Math.sqrt(k2);
 46     double d3 = Math.sqrt(k3);
 47     double d4 = Math.sqrt(k4);
 48     double d5 = Math.sqrt(k5);
 49     double d6 = Math.sqrt(k6);
 50     double d7 = Math.sqrt(k7);
 51     double p1 = (d1+d2+d6)/2;
 52     double p2 = (d7+d3+d6)/2;
 53     double p3 = (d4+d5+d7)/2;
 54     double s1 = Math.sqrt(p1*(p1-d1)*(p1-d2)*(p1-d6));
 55     double s2 = Math.sqrt(p2*(p2-d7)*(p2-d3)*(p2-d6));
 56     double s3 = Math.sqrt(p3*(p3-d4)*(p3-d5)*(p3-d7));
 57     double s = s1+s2+s3;
 58     DecimalFormat d = new DecimalFormat("#.000");
 59     Double output = Double.valueOf(d.format(s));
 60     return output;
 61 }
 62 
 63 public double getPerimeter() {
 64     double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
 65     double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
 66     double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
 67     double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX());
 68     double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX());
 69     double k = Math.sqrt(k1)+Math.sqrt(k2)+Math.sqrt(k3)+Math.sqrt(k4)+Math.sqrt(k5);
 70     DecimalFormat d = new DecimalFormat("#.000");
 71     Double output = Double.valueOf(d.format(k));
 72     return output;
 73 }
 74 public boolean isLozenge() {
 75     double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
 76     double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
 77     double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
 78     double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX());
 79     if(k1==k2&&k2==k3&&k3==k4) {
 80         return true;
 81     }
 82     else
 83     {
 84         return false;
 85     }
 86 }
 87 
 88 public boolean isEquilateralTriangle() {    
 89     double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
 90     double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
 91     double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
 92     double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX());
 93     double k5 = (this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX())+(this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY());
 94     double k6 = (this.y.getX() - this.a.getX())*(this.y.getX() - this.a.getX())+(this.y.getY() - this.a.getY())*(this.y.getY() - this.a.getY());
 95     if(k1==k3&&k2==k4&&k5==k6) {
 96         return true;
 97     }
 98     else
 99     {
100         return false;
101     }
102 }

View Code

SourceMonitor的生成报表内容:

BLOG-2

BLOG-2

BLOG-2

PowerDesigner的相应类图:

BLOG-2

分析解释和心得:

我的代码复杂度为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

我的部分核心源码:

BLOG-2BLOG-2

  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     }

View Code

SourceMonitor的生成报表内容:

BLOG-2

BLOG-2

BLOG-2

BLOG-2

PowerDesigner的相应类图:

BLOG-2

分析解释和心得:

我的代码复杂度为25较大,需要更进一步的提出更多的函数使代码简单,要将每个小方法都写好如面积的求法,这可以让代码更清晰。对于多边形位置的判断,如果第一个多边形的点都在第二个多边形内部,并且第二个多边形的点都在第一个多边形的内部,则两个多边形重合;如果第一个多边形的点都在第二个多边形内部,但是第二个多边形的点不全在第一个多边形内部,则第一个多边形包含于第二个多边形;计算两个多边形重合区域的面积,需要将得到的点的数组进行排序,排序得到的点能够组成一个凸多边形,再调用多边形计算面积和周长的方法得到周长和面积。本题难度较大,需要一定的数学功底,对五边形要有较多的认识。

期中考试

7-1 点与线(类设计)

  • 设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:(x,y),数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]。若输入有误,系统则直接输出Wrong Format

  • 设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:

      ```
    The line's color is:颜色值
    The line's begin point's Coordinate is:
    (x1,y1)
    The line's end point's Coordinate is:
    (x2,y2)
    The line's length is:长度值
    ```
    

    其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

      设计类图如下图所示。
    

BLOG-2

** 题目要求:在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的display()方法进行输出。**

  • 以下情况为无效作业
    • 无法运行
    • 设计不符合所给类图要求
    • 未通过任何测试点测试
    • 判定为抄袭

输入格式:

分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:

The line's color is:颜色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:长度值

我的代码:
BLOG-2BLOG-2

  1 import java.text.Format;
  2 import java.util.Scanner;
  3 import java.lang.reflect.Constructor;
  4 import static java.lang.System.exit;
  5 
  6 public class Main
  7 {
  8     public static void main(String[] args) 
  9     {
 10         Scanner input = new Scanner(System.in);
 11         Point p1 = new Point();
 12         Point p2 = new Point();
 13         p1.setX(input.nextDouble());
 14         p1.setY(input.nextDouble());
 15         p2.setX(input.nextDouble());
 16         p2.setY(input.nextDouble());
 17         String c = input.next();
 18         Line l = new Line(p1,p2,c);
 19         l.display();
 20     }
 21 }
 22 
 23  class Line
 24 {
 25     private Point point1;
 26     private Point point2;
 27     private String color;
 28 
 29     public void Line()
 30     {
 31 
 32     }
 33     public Line(Point p1, Point p2, String color)
 34     {
 35         this.color=color;
 36         this.point1=p1;
 37         this.point2=p2;
 38     }
 39     public void setColor(String color) {
 40         this.color = color;
 41     }
 42     public String getColor()
 43     {
 44         return this.color;
 45     }
 46 
 47     public void setPoint1(Point point1) {
 48         this.point1 = point1;
 49     }
 50     public Point getPoint1()
 51     {
 52         return this.point1;
 53     }
 54     public void setPoint2(Point point2) {
 55         this.point2 = point2;
 56     }
 57     public Point getPoint2()
 58     {
 59         return this.point2;
 60     }
 61 
 62     double getdistance()
 63     {
 64         return Math.sqrt(Math.pow((this.point1.getx()-this.point2.getx()),2)+Math.pow((this.point1.gety()-this.point2.gety()),2));
 65     }
 66     void display()
 67     {
 68         System.out.println("The line's color is:"+this.color);
 69         System.out.println("The line's begin point's Coordinate is:");
 70         point1.display();
 71         System.out.println("The line's end point's Coordinate is:");
 72         point2.display();
 73         System.out.print("The line's length is:");
 74         System.out.printf("%.2f",this.getdistance());
 75     }
 76 
 77 }
 78 
 79 
 80  class Point
 81 {
 82     private double x;
 83     private double y;
 84     void Point()
 85     {
 86     }
 87     public void setX(double x) {
 88         if(x<=200&&x>0)
 89         this.x = x;
 90         else {
 91             System.out.println("Wrong Format");
 92             exit(0);
 93         }
 94     }
 95 
 96     public void setY(double y) {
 97         if(y<=200 && y>0)
 98             this.y = y;
 99         else {
100             System.out.println("Wrong Format");
101             exit(0);
102         }
103     }
104     public void Point(double x,double y)
105     {
106         this.x=x;
107         this.y=y;
108     }
109     public  double  getx()
110     {
111         return this.x;
112     }
113     public  double  gety()
114     {
115         return this.y;
116     }
117     void display()
118     {
119         System.out.printf("(%.2f,%.2f)\n",this.x,this.y);
120     }
121 }

View Code

SourceMonitor的生成报表内容:

BLOG-2

BLOG-2

PowerDesigner的相应类图:

BLOG-2

分析解释和心得:

  这题按照题意写,非常简单,需要对类的理解。

7-2 点线面问题重构(继承与多态)

在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。

  • 对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
  • 再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
  • 在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:
          element = p1;//起点Point
    element.display();
    element = p2;//终点Point
    element.display();
    element = line;//线段
    element.display();
    element = plane;//面
    element.display();
    

    类结构如下图所示。

BLOG-2

其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

  • 以下情况为无效作业
    • 无法运行
    • 设计不符合所给类图要求
    • 未通过任何测试点测试
    • 判定为抄袭

输入格式:

分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:

(x1,y1)
(x2,y2)
The line's color is:颜色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:长度值
The Plane's color is:颜色值

我的代码:
BLOG-2BLOG-2

  1 import java.lang.reflect.Constructor;
  2 import static java.lang.System.exit;
  3 import java.util.Scanner;
  4 import java.text.Format;
  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(x<=200&&x>0)
 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(y<=200 && y>0)
 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  class Main
 75 {
 76     public static void main(String[] args) {
 77         Scanner input = new Scanner(System.in);
 78         Point p1 = new Point();
 79         Point p2 = new Point();
 80         p1.setX(input.nextDouble());
 81         p1.setY(input.nextDouble());
 82         p2.setX(input.nextDouble());
 83         p2.setY(input.nextDouble());
 84         String c = input.next();
 85         Line line = new Line(p1,p2,c);
 86         Plane plane = new Plane();
 87         plane.setColor(c);
 88 
 89         Element element  ;
 90 
 91         element = p1;//起点Point
 92         element.display();
 93 
 94         element = p2;//终点Point
 95         element.display();
 96 
 97         element = line;//线段
 98         element.display();
 99 
100         element = plane;//
101         element.display();
102 
103     }
104 }
105 
106 
107  class Line extends Element
108 {
109     private Point point1;
110     private Point point2;
111     private String color;
112 
113     public void Line()
114     {
115 
116     }
117     public Line(Point p1, Point p2, String color)
118     {
119         this.color=color;
120         this.point1=p1;
121         this.point2=p2;
122     }
123     public void setColor(String color) {
124         this.color = color;
125     }
126     public String getColor()
127     {
128         return this.color;
129     }
130 
131     public void setPoint1(Point point1) {
132         this.point1 = point1;
133     }
134     public Point getPoint1()
135     {
136         return this.point1;
137     }
138     public void setPoint2(Point point2) {
139         this.point2 = point2;
140     }
141     public Point getPoint2()
142     {
143         return this.point2;
144     }
145 
146     double getdistance()
147     {
148         return Math.sqrt(Math.pow((this.point1.getx()-this.point2.getx()),2)+Math.pow((this.point1.gety()-this.point2.gety()),2));
149     }
150     void display()
151     {
152         System.out.println("The line's color is:"+this.color);
153         System.out.println("The line's begin point's Coordinate is:");
154         point1.display();
155         System.out.println("The line's end point's Coordinate is:");
156         point2.display();
157         System.out.print("The line's length is:");
158         System.out.printf("%.2f\n",this.getdistance());
159     }
160 
161 }
162 
163  abstract class Element 
164 {
165     abstract void display();
166 }

View Code

SourceMonitor的生成报表内容:

BLOG-2

BLOG-2

BLOG-2

PowerDesigner的相应类图:

BLOG-2

分析解释和心得:

  这题考察的是继承与多态,题目比较基础,比较简单,按照题意来即可,需要对继承与多态有一定理解。

7-3 点线面问题再重构(容器类)

在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。

  • 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>
  • 增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
  • 在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
    • 1:向容器中增加Point对象
    • 2:向容器中增加Line对象
    • 3:向容器中增加Plane对象
    • 4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
    • 0:输入结束

    示例代码如下:

       choice = input.nextInt();
    while(choice != 0) {
    switch(choice) {
    case 1://insert Point object into list
    ...
    break;
    case 2://insert Line object into list
    ...
    break;
    case 3://insert Plane object into list
    ...
    break;
    case 4://delete index - 1 object from list
    int index = input.nextInt();
    ...
    }
    choice = input.nextInt();
    }
    

    输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。
    类图如下所示:

BLOG-2

  • 以下情况为无效作业
    • 无法运行
    • 设计不符合所给类图要求
    • 未通过任何测试点测试
    • 判定为抄袭

输入格式:

switch(choice) {
case 1://insert Point object into list
输入“点”对象的x,y值
break;
case 2://insert Line object into list
输入“线”对象两个端点的x,y值
break;
case 3://insert Plane object into list
输入“面”对象的颜色值
break;
case 4://delete index - 1 object from list
输入要删除的对象位置(从1开始)
...
}

输出格式:

  • Point、Line、Plane的输出参考题目2
  • 删除对象时,若输入的index超出合法范围,程序自动忽略该操作

我的代码:

BLOG-2BLOG-2

  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(x<=200&&x>0)
 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(y<=200 && y>0)
 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 list
103                     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 list
109                     index = input.nextInt();
110                     if(index>=1 && index<=g.getsize())
111                         g.remove(index-1);
112                     break;
113             }
114             choice = input.nextInt();
115         }
116         ArrayList<Element> list1 = new ArrayList<>();
117         list1 = g.getList();
118         for(int i=0;i<list1.size();i++)
119         {
120            list1.get(i).display();
121         }
122 
123        /* Element element  ;
124 
125         element = p1;//起点Point
126         element.display();
127 
128         element = p2;//终点Point
129         element.display();
130 
131         element = line;//线段
132         element.display();
133 
134         element = plane;//面
135         element.display();*/
136 
137     }
138 }
139 
140  class Line extends Element
141 {
142     private Point point1;
143     private Point point2;
144     private String color;
145 
146     public void Line()
147     {
148 
149     }
150     public Line(Point p1, Point p2, String color)
151     {
152         this.color=color;
153         this.point1=p1;
154         this.point2=p2;
155     }
156     public void setColor(String color) {
157         this.color = color;
158     }
159     public String getColor()
160     {
161         return this.color;
162     }
163 
164     public void setPoint1(Point point1) {
165         this.point1 = point1;
166     }
167     public Point getPoint1()
168     {
169         return this.point1;
170     }
171     public void setPoint2(Point point2) {
172         this.point2 = point2;
173     }
174     public Point getPoint2()
175     {
176         return this.point2;
177     }
178 
179     double getdistance()
180     {
181         return Math.sqrt(Math.pow((this.point1.getx()-this.point2.getx()),2)+Math.pow((this.point1.gety()-this.point2.gety()),2));
182     }
183     void display()
184     {
185         System.out.println("The line's color is:"+this.color);
186         System.out.println("The line's begin point's Coordinate is:");
187         point1.display();
188         System.out.println("The line's end point's Coordinate is:");
189         point2.display();
190         System.out.print("The line's length is:");
191         System.out.printf("%.2f\n",this.getdistance());
192     }
193 
194 }
195 
196 
197  class GeometryObject {
198     ArrayList<Element> list = new ArrayList<>();
199     public void GeometryObject()
200     {
201     }
202     public void add(Element element)
203     {
204         this.list.add(element);
205     }
206     public void remove(int index)
207     {
208         this.list.remove(index);
209     }
210 
211     public ArrayList<Element> getList() {
212         return this.list;
213     }
214     public int getsize()
215     {
216         return this.list.size();
217     }
218 }
219 
220  abstract class Element {
221     abstract void display();
222 }

View Code

SourceMonitor的生成报表内容:

BLOG-2

BLOG-2

BLOG-2

PowerDesigner的相应类图:

BLOG-2

分析解释和心得:

这题考察了容器类,在容器类里使用集合ArrayList和集合ArrayList的方法来完成容器类的功能,需要掌握集合ArrayList的使用并结合类的使用,题目比较基础。

(3)采坑心得

对源码的提交过程中出现的问题及心得进行总结,务必做到详实,拿数据、源码及测试结果说话,切忌假大空

7-1 sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式)

  注意Pattern类和Matcher类的正确使用方法

BLOG-2

7-2 点线形系列4-凸四边形的计算

   注意相等不能==0

BLOG-2

7-3 设计一个银行业务类

  注意转化为整型

BLOG-2

7-1 点线形系列5-凸五边形的计算-1

  需要掌握正则表达式

BLOG-2

7-2 点线形系列5-凸五边形的计算-2

  相等不能写==0

BLOG-2

7-1 点与线(类设计)

要掌握类的使用

BLOG-2

BLOG-2

BLOG-2

7-2 点线面问题重构(继承与多态)

  要掌握继承和多态

BLOG-2

BLOG-2

BLOG-2

7-3 点线面问题再重构(容器类)

  要掌握集合ArrayList的使用

BLOG-2

(4)改进建议

对相应题目的编码改进给出自己的见解,做到可持续改进

7-1 sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式)

  使用Pattern和Matcher

BLOG-2

  7-2 点线形系列4-凸四边形的计算
需要多写方法使复杂度降低使代码精简。
  写一个将String坐标转为double坐标的方法
BLOG-2

  7-3 设计一个银行业务类

要按照题目要求把代码写完,面向结果编程不能得到很好的锻炼。
BLOG-2

7-1 点线形系列5-凸五边形的计算-1

  写一个判断字符串点数量是否合格的方法

BLOG-2

7-2 点线形系列5-凸五边形的计算-2

  需要多写方法多写类使复杂度降低使代码精简。

写一个方法来判断一个点是否在线上
BLOG-2
期中考试
7-1 点与线(类设计)
%.2f 输出正确格式
BLOG-2

7-2 点线面问题重构(继承与多态)

利用类里的方法来写入数据

BLOG-2

7-3 点线面问题再重构(容器类)

容器类的内部用集合和集合的方法来实现
BLOG-2

(5)总结
 对本阶段(6-9周)综合性总结,学到了什么,哪些地方需要进一步学习及研究,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见。
  本阶段(6-9周)学到了类和对象的使用,学到了继承和多态的使用,学到了抽象类和接口的使用,学到了用正则表达式来判断字符串格式,学到了如何封装类的属性,正则表达式和继承和多态需要进一步学习及研究。
  我对教师、课程、作业、实验、课上及课下组织方式等方面都很满意,建议老师多讲讲题,四边形和五边形的题目还是比较难。
声明:本站所有文章,如无特殊说明或标注,均为爬虫抓取以及网友投稿,版权归原作者所有。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧