PTA BLOG-2
前言:
四边形,五边形这两题要求多,细致,算法比较复杂,学习吃力的同学很难全靠自己跟上进度,主要难在点与线的交点计算及围成的面积的计算方法和对非正确输入的判断,正则表达式的使用。相比之下期中考试题目十分简单,只要简单运用继承和抽象类即可完成第二题,第三题。
7-2 点线形系列4-凸四边形的计算
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,输出"not a quadrilateral or triangle"。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出"on the triangle或者on the quadrilateral"。若后四个点不符合四边形或三角形,输出"not a quadrilateral or triangle"。
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。
选项4中,若前两个输入线的点重合,输出"points coincide"。
输入样例1:
选项1,点重合。例如:
1:-1,-1 -1,-1 1,2 1,-2
输出样例:
在这里给出相应的输出。例如:
points coincide
设计与分析:
设计主函数,点类,线类,三角形类,四边形类,用输入类简化main![]()





import java.util.*;
import java.util.ArrayList;
public class Main {
public static double siSheWuRu(double s) {
String str = String.valueOf(s);
String substring = str.substring(str.indexOf(".") + 1);
int str_len;
if (substring.length() >3) {
str_len =3;
} else {
str_len = substring.length();
}
String formats = "%." + str_len + "f";
String out = String.format(formats, s);
double res = Double.parseDouble(out);
return res;
}
public static void main(String[] args) {
Input in = new Input();
in.getlist();
switch(in.choice)
{
case '1': {
if(in.list.size()!=4) {
System.out.print("wrong number of points");
return;
}
Sibian shape=new Sibian(in.list.get(0),in.list.get(1),in.list.get(2),in.list.get(3));
if(shape.chonghe) {
System.out.println("points coincide");
return;
}
if(shape.sibianx())
System.out.print("true ");
else System.out.print("false ");
if(shape.pingxinsbx())
System.out.print("true");
else System.out.print("false");
break;
}
case '2': {
if(in.list.size()!=4) {
System.out.print("wrong number of points");
return;
}
Sibian shape=new Sibian(in.list.get(0),in.list.get(1),in.list.get(2),in.list.get(3));
if(shape.chonghe){
System.out.println("points coincide");
return;
}
if(shape.sibianx()) {
if(shape.diamond())
System.out.print("true ");
else System.out.print("false ");
if(shape.rectangle())
System.out.print("true ");
else System.out.print("false ");
if(shape.square())
System.out.print("true");
else System.out.print("false");
}
else System.out.print("not a quadrilateral");
break;
}
case '3': {
if(in.list.size()!=4) {
System.out.print("wrong number of points");
return;
}
Sibian shape=new Sibian(in.list.get(0),in.list.get(1),in.list.get(2),in.list.get(3));
if(shape.chonghe) {
System.out.println("points coincide");
return;
}
if(shape.sibianx()) {
if(shape.aosbx())
System.out.print("false ");
else System.out.print("true ");
System.out.print(siSheWuRu(shape.zhouchang)+" "+siSheWuRu(shape.mianji()));
}
else System.out.print("not a quadrilateral");
break;
}
case '4': {
if(in.list.size()!=6) {
System.out.print("wrong number of points");
return;
}
if(in.list.get(0).x==in.list.get(1).x&&in.list.get(0).y==in.list.get(1).y) {
System.out.println("points coincide");
return;
}
Line l=new Line(in.list.get(0),in.list.get(1));
Sibian shape=new Sibian(in.list.get(2),in.list.get(3),in.list.get(4),in.list.get(5));
if(!shape.sibianx()){
Sanjiao tra=new Sanjiao(in.list.get(2),in.list.get(3),in.list.get(4));
if(!tra.issanjiao)
System.out.println("not a quadrilateral or triangle");
}
break;
}
case '5': {
if(in.list.size()!=5) {
System.out.print("wrong number of points");
return;
}
break;
}
}
}
}
class Point {
double x;
double y;
void getx(double a){
this.x=a;
}
void gety(double b){
this.y=b;
}
double distance(Point a){
return Math.sqrt((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y));
}
}
class Line {
Point a, b;
double xielv;
double B;//y=kx+B
boolean chonghe;
double length;
Line(Point a, Point b) {
this.a = a;
this.b = b;
this.length=a.distance(b);
if (this.a.x == this.b.x && this.a.y == this.b.y)
//System.out.println("points coincide");
this.chonghe=true;
else {
this.xielv = (a.y - b.y) / (a.x - b.x);
B = a.y - this.xielv * a.x;
}
}
double juli(Point c) {
return Math.abs(xielv * c.x - c.y + this.B) / Math.sqrt(this.xielv * this.xielv + 1);
}
boolean pinxin(Line l1) {
if (this.xielv == l1.xielv)
return true;
else
return false;
}
}
class Sanjiao {
Point a,b,c,zhongxin;
double ab,bc,ca;
boolean issanjiao,isdengbian,isdengyao,dunjiao,zhijiao,ruijiao;
double zhouchang,mianji;
Sanjiao(Point a,Point b,Point c){
this.a=a;
this.b=b;
this.c=c;
this.ab=a.distance(b);
this.bc=b.distance(c);
this.ca=c.distance(a);
if(ab+bc>ca&&ab+ca>bc&&bc+ca>ab){//是否组成三角形
this.issanjiao=true;
if(ab==bc&&bc==ca)//判断等边
this.isdengbian=true;
if(ab==bc||bc==ca)
this.isdengyao=true;
else if((ab*ab+bc*bc-ca*ca<0.0001)||(ab*ab+ca*ca-bc*bc<0.00001)||(bc*bc+ca*ca-ab*ab<0.0001))//是否有直角
this.zhijiao=true;
else if(ab*ab+bc*bc<ca*ca)
this.dunjiao=true;
else if(ab*ab+bc*bc>ca*ca)
this.ruijiao=true;
this.zhouchang=ab+bc+ca;
this.mianji=Math.sqrt( ((this.ab+this.bc+this.ca)/2.0)*(((this.ab+this.bc+this.ca)/2.0)-this.ab)*(((this.ab+this.bc+this.ca)/2.0)-this.bc)*(((this.ab+this.bc+this.ca)/2.0)-this.ca));
this.zhongxin = new Point();
this.zhongxin.x= (this.a.x+this.b.x+this.c.x)/3.0;
this.zhongxin.y= (this.a.y+this.b.y+this.c.y)/3.0;
}
}
}
class Sibian {
Point a,b,c,d;
Line ab,bc,cd,da;
double zhouchang;
boolean chonghe;
Sibian(Point a,Point b,Point c,Point d){
this.a=a;
this.b=b;
this.c=c;
this.d=d;
ab=new Line(a,b);
bc=new Line(b,c);
cd=new Line(c,d);
da=new Line(d,a);
zhouchang=ab.length+bc.length+cd.length+ da.length;
if(ab.chonghe||bc.chonghe|| cd.chonghe|| da.chonghe)
this.chonghe=true;
}
boolean sibianx(){
double k1 = (this.a.y- this.b.y) / (this.a.x- this.b.x);
double k2 = (this.a.y- this.c.y) / (this.a.x- this.c.x);
double k3 = (this.a.y- this.d.y) / (this.a.x- this.d.x);
double k4 = (this.b.y- this.c.y) / (this.b.x- this.c.x);
double k5 = (this.b.y- this.d.y) / (this.b.x- this.d.x);
if(k1==k2||k1==k3||k2==k3)
{
return false;
}
else
{
if(k4 == k5)
{
return false;
}
return true;
}
}
boolean pingxinsbx(){
if(ab.xielv== cd.xielv&& ab.xielv!= da.xielv)
{
if(a.distance(b) == c.distance(d))
{
return true;
}
}
else if(da.xielv== bc.xielv && da.xielv!= ab.xielv)
{
if(a.distance(d) ==b.distance(c))
{
return true;
}
}
return false;
}
boolean diamond(){
double k2 = (this.a.y- this.c.y) / (this.a.x- this.c.x);//Kac
double k5 = (this.b.y- this.d.y) / (this.b.x- this.d.x);//Kbd
if( k2*k5 == -1)
{
return true;
}
else
return false;
}
boolean rectangle(){
if(a.distance(c)== b.distance(d)) //ac=bd
{
return true;
}
else
{
return false;
}
}
boolean square(){
double k2 = (this.a.y- this.c.y) / (this.a.x- this.c.x);//Kac
double k5 = (this.b.y- this.d.y) / (this.b.x- this.d.x);//Kbd
if(a.distance(c)== b.distance(d)&& k2*k5 == -1 )
{
return true;
}
else
{
return false;
}
}
boolean aosbx(){
Sanjiao tra1=new Sanjiao(a,b,c);
Sanjiao tra2=new Sanjiao(a,d,c);
Sanjiao tra3=new Sanjiao(a,b,d);
Sanjiao tra4=new Sanjiao(d,b,c);
if((tra1.mianji+tra2.mianji)==(tra4.mianji+tra3.mianji)){
return false;
}
return true;
}
double mianji(){
double s;
Sanjiao abc=new Sanjiao(a,b,c);
Sanjiao bcd=new Sanjiao(d,b,c);
if(this.aosbx())
s= 0.5*Math.abs((c.x-a.x)*(d.y-b.y)-(c.y-a.y)*(d.x-b.x));
else s=abc.mianji+bcd.mianji;
return s;
}
}
class Input {
char choice;
ArrayList<Point> list;
void getlist() {
Scanner input = new Scanner(System.in);
String s = input.nextLine();
String s2 = s.substring(2);
if(!s.matches("^[1-5][:](([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))[,]([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))\\s?)+")) {
System.out.println("Wrong Format");
return;
}
choice=s.charAt(0);
String[] num;
String[] point = s2.split(" ");//将s按照空格切分后放入point
ArrayList<Point> list0= new ArrayList<>();
for (int i = 0; i < point.length; i++) {
num = point[i].split(",");//将point按照,切分后放入num
double a = Double.parseDouble(num[0]);
double b = Double.parseDouble(num[1]);
Point p = new Point();
p.x = a;
p.y = b;
list0.add(p);
}
this.list=list0;
}
}
采坑心得:用数组只能存放固定数量的东西,使用arrylist可以自由的增减数量。
常用方法有:public boolean add(E e); 向集合当中添加元素,参数的类型和泛型一致。public E get(int index): 从集合当中获取元素,参数是索引编号,返回值就是对应位置的元素。public E remove(int index): 从集合当中删除元素,参数是索引编号,返回值就是被删除掉的元素。public int size(): 获取集合的尺寸长度,返回值是集合中包含的元素个数。
判断三角不能用a的平方加b的平方等于c的平方,因为小数的计算会不完整,通过a*a+b*b-c*c<0.0001)可以尽可能减小出错。
改进建议:没有用继承,多态等来简化代码,选项四和选项五没有完全实现。
7-1 点线形系列5-凸五边形的计算-1
用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出"not a pentagon"
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入,若前两点重合,输出"points coincide"。
以上3选项中,若输入的点无法构成多边形,则输出"not a polygon"。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
输入样例1:
选项1,点重合。例如:
1:-1,-1 1,2 -1,1 1,0
输出样例:
在这里给出相应的输出。例如:
wrong number of points
设计与分析:
![]()

和四边形的设计几乎一样,所需算法不同,而算法在这几题里尤为重要。如何满足选项要求,充分考虑。
采坑心得:要为四舍五入写一个方法以便调用
double b = Double.parseDouble(num[1]);注意类型转换
改进建议:类的属性不安全,会被更改,应该设为private然后通过方法进行增删改
属性,类的命名不严谨,最好用英文
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
设计与分析:
在5-1的基础上将switch内修改
case '4': {
if(in.list.size()!=10) {
System.out.print("wrong number of points");
return;
}
System.out.println("the previous quadrilateral is connected to the following pentagon");
/*else if()
System.out.println("the previous pentagon coincides with the following pentagon");
else if()
System.out.println("the previous pentagon is interlaced with the following triangle");
else if()
System.out.println("the previous quadrilateral is inside the following pentagon");
else if()
System.out.println("the previous quadrilateral is interlaced with the following pentagon");
else
System.out.println("the previous triangle is interlaced with the following triangle");*/
break;
}
case '5': {
if(in.list.size()!=10) {
System.out.print("wrong number of points");
return;
}
break;
}
case '6': {
if(in.list.size()!=6) {
System.out.print("wrong number of points");
return;
}
Wubian wubian=new Wubian(in.list.get(1),in.list.get(2),in.list.get(3),in.list.get(4),in.list.get(5));
if(wubian.ab.juli(in.list.get(0))==0||wubian.bc.juli(in.list.get(0))==0||wubian.cd.juli(in.list.get(0))==0||wubian.de.juli(in.list.get(0))==0||wubian.ea.juli(in.list.get(0))==0)
System.out.println("on the pentagon");
break;
}
}
采坑心得:
不会写算法,不知道如何计算交点及之后的操作
改进建议:
代码质量严重不达标,不能满足基本条件。
期中考试
7-1 点与线(类设计)
设计与分析:设计点类,线类
class Point{
private double x,y;
void setx(double x){
this.x=x;
}
void sety(double y){
this.y=y;
}
double getx(){
return x;
}
double gety(){
return y;
}
void display(){
System.out.println("("+String.format("%.2f",getx())+","+String.format("%.2f",gety())+")");
}
}
class Line{
private Point p1,p2;
private String color;
void setp1(Point p){
this.p1=p;
}
void setp2(Point p){
this.p2=p;
}
void setcolor(String color){
this.color=color;
}
double getDistance(){
return Math.sqrt((p1.getx()-p2.getx())*(p1.getx()-p2.getx())+(p1.gety()- p2.gety())*(p1.gety()- p2.gety()));
}
void display(){
System.out.println("The line's color is:"+color);
System.out.println("The line's begin point's Coordinate is:");
p1.display();
System.out.println("The line's end point's Coordinate is:");
p2.display();
System.out.println("The line's length is:"+String.format("%.2f",getDistance()));
}
}
采坑心得:注意边界值
if(x1>200||x1<=0||x2>200||x2<=0||y1>200||y1<=0||y2>200||y2<=0){
System.out.println("Wrong Format");
return;
}
所有数值均保留两位小数,建议可用String.format("%.2f", data)方法
改进建议:无
7-2在7-1的基础上加一个父类并将其他类作为他的子类即可。
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:输入结束
设计与分析:
import java.util.Scanner;
import java.util.Formatter;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
GeometryObject con=new GeometryObject();
int choice=in.nextInt();
while(choice!=0) {
switch (choice) {
case 1: {
Point p = new Point();
double x1= in.nextDouble();
double y1= in.nextDouble();
if(x1>200||x1<=0||y1>200||y1<=0){
System.out.println("Wrong Format");
return;
}
p.setx(x1);
p.sety(y1);
con.add(p);
break;
}
case 2: {
Point p1 = new Point();
Point p2 = new Point();
Line l = new Line();
double x1= in.nextDouble();
double y1= in.nextDouble();
double x2= in.nextDouble();
double y2= in.nextDouble();
if(x1>200||x1<=0||x2>200||x2<=0||y1>200||y1<=0||y2>200||y2<=0){
System.out.println("Wrong Format");
return;
}
p1.setx(x1);
p1.sety(y1);
p2.setx(x2);
p2.sety(y2);
l.setp1(p1);
l.setp2(p2);
String color= in.next();
l.setcolor(color);
con.add(l);
break;
}
case 3: {
Plane plane = new Plane();
String color= in.next();
plane.setcolor(color);
con.add(plane);
break;
}
case 4: {
int index = in.nextInt();
if(index >= 0 &&index <= con.elements.size())
con.remove(index);
}
}
choice = in.nextInt();
}
for(int i=0;i<con.elements.size();i++){
con.elements.get(i).display();
}
}
}
abstract class Element{
abstract void display();
}
class Point extends Element{
private double x,y;
void setx(double x){
this.x=x;
}
void sety(double y){
this.y=y;
}
double getx(){
return x;
}
double gety(){
return y;
}
void display(){
System.out.println("("+String.format("%.2f",getx())+","+String.format("%.2f",gety())+")");
}
}
class Line extends Element{
private Point p1,p2;
private String color;
void setp1(Point p){
this.p1=p;
}
void setp2(Point p){
this.p2=p;
}
void setcolor(String color){
this.color=color;
}
double getDistance(){
return Math.sqrt((p1.getx()-p2.getx())*(p1.getx()-p2.getx())+(p1.gety()- p2.gety())*(p1.gety()- p2.gety()));
}
void display(){
System.out.println("The line's color is:"+color);
System.out.println("The line's begin point's Coordinate is:");
p1.display();
System.out.println("The line's end point's Coordinate is:");
p2.display();
System.out.println("The line's length is:"+String.format("%.2f",getDistance()));
}
}
class Plane extends Element{
private String color;
void setcolor(String color){
this.color=color;
}
@Override
void display() {
System.out.println("The Plane's color is:"+color);
}
}
class GeometryObject{
ArrayList<Element> elements=new ArrayList<>();
void add(Element e){
elements.add(e);
}
void remove(int index){
elements.remove(index-1);
}
}

![]()
采坑心得:
- 4:删除容器中第index - 1个数据,若index数据非法,则无视此操作,需要加上一个判断长度是否大于index-1
if(index >= 0 &&index <= con.elements.size())
con.remove(index);
改进建议:main函数过于复杂,需要切分开,精简化
总结:学到了类的正确形式,要多调用类的方法进行编程。正则表达式功能强大,但是需要仔细,有逻辑地分析后才能正确使用。Java编程
应该化繁为简,不断分开,使代码各司其职。目前我只学会了继承和抽象类的简单使用,还需继续学习,加强自己。在完成作业的过程中,我练习
了各种字符串的操作,如indexof,split,substring,还学到了除数组外的存储方法arrylist。设计算法能力还是欠缺。
因为没有Java基础,完成作业的过程中,遇到了各种不知道有什么方法可以调用的情况,希望以后老师在新学期开课前提前通知学生自学。




浙公网安备 33010602011771号