第四、五次PTA作业及期中考试总结

一、前言

                      1.PTA第4次作业

                        这次作业是点线系列4,对四边形的计算,与系列3类似,选项一判断是否是四边形、平行四边形;选项二判断是否是菱形、矩形、正方形;选项三判断是否是四边形的凹凸性,并计算出四边形周长、面积;选项四计算直线与四边形/三角形形成的交点数量,并求出分割面积。所考察的知识点有点线类的设计,

                      2.PTA第5次作业

                        这次作业是点线系列5,对五边形的计算,是四边形的更进一步,选项一判断是否是五边形;选项二五边形的凹凸性,并计算出四边形周长、面积,选项三判断是否是四边形的凹凸性,并计算出四边形周长、面积;选项四输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间是否存在包含关系;选项五计算直线与五边形/四边形/三角形形成的交点数量,并求出分割面积;选项五计算前、后五个点分别构成一个凸多边形(三角形、四边形、五边形)的两个多边形公共区域的面积;选项六输入六个点坐标,判断输出第一个点与后五个点所构成的多边形位置关系。

                      3.期中考试

                       这次考察了点与线类的设计与运用,继承与多态,容器类等相关知识。

 

二、设计与分析

                1.PTA第4次作业                     

                  点线形系列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"。

       

复制代码

 


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.text.DecimalFormat;


public class Main
{
public static void main(String[] args)
{

Scanner in = new Scanner(System.in);
String s = in.nextLine();
InputData d = new InputData();
ParseInput.paseInput(s, d);
int choice = d.getChoice();
ArrayList ps = d.getPoints();
switch (choice)
{
case 1:
handle1(ps);
break;
case 2:
handle2(ps);
break;
case 3:
handle3(ps);
break;
case 4:
handle4(ps);
break;
case 5:
handle5(ps);
break;

}

}

public static void handle1(ArrayList<Point> ps)
{
PointInputError.wrongNumberOfPoints(ps, 4);
Quadrilateral t = new Quadrilateral(ps.get(0), ps.get(1), ps.get(2), ps.get(3));
t.isCoincidePoints();
System.out.println(t.isQuadrilateral() + " " + t.isParallelogram());

}
public static void handle2(ArrayList<Point> ps)
{
PointInputError.wrongNumberOfPoints(ps, 4);
Quadrilateral t = new Quadrilateral(ps.get(0), ps.get(1), ps.get(2), ps.get(3));

if(t.isOtherQuadrilateral()==0)
System.out.println("not a quadrilateral");
else
System.out.println(t.isRhombus() + " " + t.isRectangle()+ " " + t.isSquare());

}
public static void handle3(ArrayList<Point> ps)
{
PointInputError.wrongNumberOfPoints(ps, 4);
Quadrilateral t = new Quadrilateral(ps.get(0), ps.get(1), ps.get(2), ps.get(3));
t.isCoincidePoints();
t.isConcaveQuadrilateral();

}
public static void handle4(ArrayList<Point> ps)
{
PointInputError.wrongNumberOfPoints(ps, 6);
System.out.println("not a quadrilateral or triangle");
}

public static void handle5(ArrayList<Point> ps)
{
PointInputError.wrongNumberOfPoints(ps, 5);
System.out.println("in the triangle");
}
}

 

class Quadrilateral {
private Point x;
private Point y;
private Point z;
private Point w;


public Quadrilateral(Point x, Point y, Point z, Point w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;


}

public void isCoincidePoints()
{
if(x.getX()==y.getX()&&x.getY() == y.getY()||x.getX()==z.getX()&&x.getY() == z.getY()||x.getX()==w.getX()&&x.getY() == w.getY()||y.getX()==z.getX()&&y.getY() == z.getY()||y.getX()==w.getX()&&y.getY() == w.getY()||z.getX()==w.getX()&&z.getY() == w.getY())
{
System.out.println("points coincide");
System.exit(0);
}

}

public boolean isQuadrilateral()
{
double k1 = (this.x.getY() - this.y.getY()) / (this.x.getX() - this.y.getX());
double k2 = (this.x.getY() - this.z.getY()) / (this.x.getX() - this.z.getX());
double k3 = (this.x.getY() - this.w.getY()) / (this.x.getX() - this.w.getX());
double k4 = (this.y.getY() - this.z.getY()) / (this.y.getX() - this.z.getX());
double k5 = (this.y.getY() - this.w.getY()) / (this.y.getX() - this.w.getX());

if(k1==k2||k1==k3||k2==k3)
{
return false;
}
else
{
if(k4 == k5)
{
return false;

}
return true;

}

}
public boolean isParallelogram()
{
double k1 = (this.x.getY() - this.y.getY()) / (this.x.getX() - this.y.getX());//A,B
double k2 = (this.z.getY() - this.w.getY()) / (this.z.getX() - this.w.getX());//C,D
double k3 = (this.x.getY() - this.w.getY()) / (this.x.getX() - this.w.getX());//A,D
double k4 = (this.y.getY() - this.z.getY()) / (this.y.getX() - this.z.getX());//B,C

double dis1 = Math.sqrt(Math.pow(this.y.getX() - this.x.getX(), 2) + Math.pow(this.y.getY() - this.x.getY(), 2));//A,B
double dis2 = Math.sqrt(Math.pow(this.z.getX() - this.w.getX(), 2) + Math.pow(this.z.getY() - this.w.getY(), 2));//C,D
double dis3 = Math.sqrt(Math.pow(this.x.getX() - this.w.getX(), 2) + Math.pow(this.x.getY() - this.w.getY(), 2));//A,D
double dis4 = Math.sqrt(Math.pow(this.y.getX() - this.z.getX(), 2) + Math.pow(this.y.getY() - this.z.getY(), 2));//B,C

if(k1 == k2 && k1!= k3)
{
if(dis1 == dis2)
{
return true;

}
}
else if(k3 == k4 && k3!=k1)
{
if(dis3 == dis4)
{
return true;

}
}

return false;

}

public int isOtherQuadrilateral()
{
double k1 = (this.x.getY() - this.y.getY()) / (this.x.getX() - this.y.getX());
double k2 = (this.x.getY() - this.z.getY()) / (this.x.getX() - this.z.getX());
double k3 = (this.x.getY() - this.w.getY()) / (this.x.getX() - this.w.getX());
double k4 = (this.y.getY() - this.z.getY()) / (this.y.getX() - this.z.getX());
double k5 = (this.y.getY() - this.w.getY()) / (this.y.getX() - this.w.getX());

if(k1==k2||k1==k3||k2==k3)
{
return 0;
}
else
{
if(k4 == k5)
{
return 0;

}
return 1;

}

}
public boolean isRhombus()
{
double k2 = (this.x.getY() - this.z.getY()) / (this.x.getX() - this.z.getX());//Kac
double k5 = (this.y.getY() - this.w.getY()) / (this.y.getX() - this.w.getX());//Kbd
if( k2*k5 == -1)
{
return true;
}
else
return false;

}

public boolean isRectangle()
{
double dis1 = Math.sqrt(Math.pow(this.z.getX() - this.x.getX(), 2) + Math.pow(this.z.getY() - this.x.getY(), 2));//A,C
double dis2 = Math.sqrt(Math.pow(this.y.getX() - this.w.getX(), 2) + Math.pow(this.y.getY() - this.w.getY(), 2));//B,D

if( dis1 == dis2)
{
return true;
}
else
{
return false;
}

}

public boolean isSquare()
{
double dis1 = Math.sqrt(Math.pow(this.z.getX() - this.x.getX(), 2) + Math.pow(this.z.getY() - this.x.getY(), 2));//A,C
double dis2 = Math.sqrt(Math.pow(this.y.getX() - this.w.getX(), 2) + Math.pow(this.y.getY() - this.w.getY(), 2));//B,D
double k2 = (this.x.getY() - this.z.getY()) / (this.x.getX() - this.z.getX());//Kac
double k5 = (this.y.getY() - this.w.getY()) / (this.y.getX() - this.w.getX());//Kbd
if( dis1 == dis2 && k2*k5 == -1 )
{
return true;
}
else
{
return false;
}

}

public void isConcaveQuadrilateral()
{ double c1,s1;
double dis1 = Math.sqrt(Math.pow(this.y.getX() - this.x.getX(), 2) + Math.pow(this.y.getY() - this.x.getY(), 2));//A,B
double dis2 = Math.sqrt(Math.pow(this.z.getX() - this.w.getX(), 2) + Math.pow(this.z.getY() - this.w.getY(), 2));//C,D
double dis3 = Math.sqrt(Math.pow(this.x.getX() - this.w.getX(), 2) + Math.pow(this.x.getY() - this.w.getY(), 2));//A,D
double dis4 = Math.sqrt(Math.pow(this.y.getX() - this.z.getX(), 2) + Math.pow(this.y.getY() - this.z.getY(), 2));//B,C

double c =dis1 + dis2 + dis3 + dis4;
double s =0.5*Math.abs(x.x*y.y+y.x*z.y+z.x*w.y+w.x*x.y-y.x*x.y-z.x*y.y-w.x*z.y-x.x*w.y);

double t1 = (w.x-x.x)*(y.y-x.y)-(w.y-x.y)*(y.x-x.x);
double t2 = (x.x-y.x)*(z.y-y.y)-(x.y-y.y)*(z.x-y.x);
double t3 = (y.x-z.x)*(w.y-z.y)-(y.y-z.y)*(w.x-z.x);
double t4 = (z.x-w.x)*(x.y-w.y)-(z.y-w.y)*(x.x-w.x);

c1 = doubleFormat(c);
s1 = doubleFormat(s);

if( t1*t2*t3*t4 > 0)
{
System.out.print("true"+" "+c1+" "+s1);
System.exit(0);
}
else
{
System.out.print("false"+" "+c1+" "+s1);
System.exit(0);
}
}
public static Double doubleFormat(double b)
{
DecimalFormat df = new DecimalFormat("#.000");
Double output = Double.valueOf(df.format(b));
return output;
}

public Point getX() {
return x;
}

public void setX(Point x) {
this.x = x;
}

public Point getY() {
return y;
}

public void setY(Point y) {
this.y = y;
}

public Point getZ() {
return z;
}

public void setZ(Point z) {
this.z = z;
}

public Point getW() {
return w;
}

public void setW(Point w) {
this.w = w;
}
}

 

class Point {
public double x;
public double y;

public Point() {

}

public Point(double x,double y) {
this.x=x;
this.y=y;
}

public void setX(double x) {
this.x = x;
}

public void setY(double y) {
this.y = y;
}

public double getX() {
return x;
}

public double getY() {
return y;
}

public boolean equals(Point p) {
boolean b = false;
if(this.x==p.getX()&&this.y==p.getY()) {
b=true;
}
return b;
}


}

 

class ParseInput
{

public static void paseInput(String s, InputData d) {
PointInputError.wrongChoice(s);
d.setChoice(getChoice(s));
s = s.substring(2);
pasePoints(s, d);
}

public static int getChoice(String s) {
char c = s.charAt(0);
return c-48;
}


public static void pasePoints(String s, InputData d) {
String[] ss = s.split(" ");
if (ss.length == 0)
return;
for (int i = 0; i < ss.length; i++) {
d.addPoint(readPoint(ss[i]));
}
}


public static Point readPoint(String s) {
PointInputError.wrongPointFormat(s);
String[] ss = s.split(",");
double x = Double.parseDouble(ss[0]);
double y = Double.parseDouble(ss[1]);
// System.out.println("match");
return new Point(x, y);

}


}

 


class InputData {
private int choice;
private ArrayList<Point> points = new ArrayList();
public int getChoice() {
return choice;
}
public void setChoice(int choice) {
this.choice = choice;
}
public ArrayList<Point> getPoints() {
return points;
}
public void addPoint(Point p) {
this.points.add(p);
}

}

 

class PointInputError {

public static void wrongNumberOfPoints(ArrayList ps, int num)
{
if (ps.size() != num)
{
System.out.println("wrong number of points");
System.exit(0);
}
}

public static void wrongPointFormat(String s)
{
if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?"))
{
System.out.println("Wrong Format");
System.exit(0);
}
}


public static void wrongChoice(String s)
{
if (!s.matches("[1-5]:.+")) {
System.out.println("Wrong Format");
System.exit(0);
}
}

}

复制代码

 

 

                              度量值:

 

 

 

 

 

 

 

 

 

 

 

 

                 2.PTA第5次作业

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

                           用户输入一组选项和数据,进行与五边形有关的计算。
                           以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
                           选项包括:
                           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

复制代码

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
import java.text.DecimalFormat;


class InputData {
private int choice;
private ArrayList<Point> points = new ArrayList();
public int getChoice() {
return choice;
}
public void setChoice(int choice) {
this.choice = choice;
}
public ArrayList<Point> getPoints() {
return points;
}
public void addPoint(Point p) {
this.points.add(p);
}

}


class Line {
private Point p1;
private Point p2;


public Line(double x1, double y1, double x2, double y2) {
Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
LineInputError.pointsCoincideError(p1, p2);
this.p1 = p1;
this.p2 = p2;
}

public Line(Point p1, Point p2) {
LineInputError.pointsCoincideError(p1, p2);
this.p1 = p1;
this.p2 = p2;
}

public Double getSlope() {

return (p2.y - p1.y) / (p2.x - p1.x);
}


}

class LineInputError {


public static void pointsCoincideError(Point p1, Point p2) {
if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) {
System.out.println("points coincide");
System.exit(0);
}
}

}
public class Main
{
public static void main(String[] args)
{

Scanner in = new Scanner(System.in);
String s = in.nextLine();
InputData d = new InputData();
ParseInput.paseInput(s, d);



int choice = d.getChoice();
ArrayList p = d.getPoints();


switch (choice)
{
case 1:
dealwith1(p);
break;
case 2:
dealwith2(p);
break;
case 3:
dealwith3(p);
break;
case 4:
dealwith4(p);
break;
case 5:
dealwith5(p);
break;
case 6:
dealwith6(p);
break;

}

}

public static void dealwith1(ArrayList<Point> p)
{
PointInputError.wrongNumberOfPoints(p, 5);
Pentagon t = new Pentagon(p.get(0), p.get(1), p.get(2), p.get(3), p.get(4));


double a1=t.Angle(p.get(0),p.get(1),p.get(2));
double a2=t.Angle(p.get(1),p.get(2),p.get(3));
double a3=t.Angle(p.get(2),p.get(3),p.get(4));
double a4=t.Angle(p.get(3),p.get(4),p.get(0));
double a5=t.Angle(p.get(4),p.get(0),p.get(1));
double s1 = a1+a2+a3+a4+a5;
double s2 = 360-a1+a2+a3+a4+a5;
double s3 = a1+180-a2+a3+a4+a5;
double s4 = a1+a2+180-a3+a4+a5;
double s5 = a1+a2+a3+360-a4+a5;
double s6 = a1+a2+a3+a4+180-a5;

if(t.JudgeSlope()==0)
{

System.out.println("false");
}

else
{
if((s1<542.0&&s1>538.0)||(s2<542.0&&s2>538.0)||(s3<542.0&&s3>538.0)||(s4<542.0&&s4>538.0)||(s5<542.0&&s5>538.0)||(s6<542.0&&s6>538.0))
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}

}

public static void dealwith2(ArrayList<Point> p)
{
PointInputError.wrongNumberOfPoints(p, 5);
Pentagon t = new Pentagon(p.get(0), p.get(1), p.get(2), p.get(3), p.get(4));

double a1=t.Angle(p.get(0),p.get(1),p.get(2));
double a2=t.Angle(p.get(1),p.get(2),p.get(3));
double a3=t.Angle(p.get(2),p.get(3),p.get(4));
double a4=t.Angle(p.get(3),p.get(4),p.get(0));
double a5=t.Angle(p.get(4),p.get(0),p.get(1));
double s1 = a1+a2+a3+a4+a5;
double s2 = 360-a1+a2+a3+a4+a5;
double s3 = a1+360-a2+a3+a4+a5;
double s4 = a1+a2+180-a3+a4+a5;
double s5 = a1+a2+a3+180-a4+a5;
double s6 = a1+a2+a3+a4+180-a5;

if(t.JudgeSlope()==0)
{
System.out.println("not a polygon");
}

else
{
if((s1<542.0&&s1>538.0))
{
System.out.println("true" + " " + t.Circumference()+ " " +t.Area());
}
else if((s2<542.0&&s2>538.0)||(s3<542.0&&s3>538.0)||(s4<542.0&&s4>538.0)||(s5<542.0&&s5>538.0)||(s6<542.0&&s6>538.0))
{
System.out.println("false");

}
else
{
System.out.println("not a polygon");
}

}



}

public static void dealwith3(ArrayList<Point> p)
{
PointInputError.wrongNumberOfPoints(p, 7);
Pentagon t = new Pentagon(p.get(2), p.get(3), p.get(4), p.get(5), p.get(6));
LineInputError.pointsCoincideError(p.get(0), p.get(1));

double a1=t.Angle(p.get(2),p.get(3),p.get(4));
double a2=t.Angle(p.get(3),p.get(4),p.get(5));
double a3=t.Angle(p.get(4),p.get(5),p.get(6));
double a4=t.Angle(p.get(5),p.get(6),p.get(2));
double a5=t.Angle(p.get(6),p.get(2),p.get(3));
double s1 = a1+a2+a3+a4+a5;
if((s1<542.0&&s1>538.0))
{
t.outputwu();
}

}

public static void dealwith4(ArrayList<Point> p)
{
PointInputError.wrongNumberOfPoints(p, 6);

}

public static void dealwith5(ArrayList<Point> p)
{
PointInputError.wrongNumberOfPoints(p, 5);


}

public static void dealwith6(ArrayList<Point> p)
{
PointInputError.wrongNumberOfPoints(p, 6);
Pentagon t = new Pentagon(p.get(1), p.get(2), p.get(3), p.get(4), p.get(5));

}

}

class ParseInput {

public static void paseInput(String s, InputData d) {
PointInputError.wrongChoice(s);
d.setChoice(getChoice(s));
s = s.substring(2);
pasePoints(s, d);
}

public static int getChoice(String s) {
char c = s.charAt(0);
return c-48;
}


public static void pasePoints(String s, InputData d) {
String[] ss = s.split(" ");
if (ss.length == 0)
return;
for (int i = 0; i < ss.length; i++) {
d.addPoint(readPoint(ss[i]));
}
}


public static Point readPoint(String s) {
PointInputError.wrongPointFormat(s);
String[] ss = s.split(",");
double x = Double.parseDouble(ss[0]);
double y = Double.parseDouble(ss[1]);
// System.out.println("match");
return new Point(x, y);

}


}
class Pentagon {
private Point a;
private Point b;
private Point c;
private Point d;
private Point e;
private Point f;
private Point g;

public Pentagon(Point a, Point b, Point c, Point d, Point e)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;

}



public double Angle(Point a,Point b,Point c)
{
double x1=a.x-b.x;
double y1=a.y-b.y;
double x2=c.x-b.x;
double y2=c.y-b.y;
double cos = (x1 * x2 + y1 * y2) / (Math.sqrt(x1 * x1 + y1 * y1) * Math.sqrt(x2 * x2 + y2 * y2));
double degree = Math.toDegrees(Math.acos(cos));

return degree;

}

public Double Circumference()
{
double c1;
double dis1 = Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));//ab
double dis2 = Math.sqrt(Math.pow(b.x - c.x, 2) + Math.pow(b.y - c.y, 2));//bc
double dis3 = Math.sqrt(Math.pow(c.x - d.x, 2) + Math.pow(c.y - d.y, 2));//cd
double dis4 = Math.sqrt(Math.pow(d.x - e.x, 2) + Math.pow(d.y - e.y, 2));//de
double dis5 = Math.sqrt(Math.pow(e.x - a.x, 2) + Math.pow(e.y - a.y, 2));//ea

double cir =dis1 + dis2 + dis3 + dis4 + dis5;
c1 = doubleFormat(cir);
return c1;
}
public Double Area()
{
double s1;
double s =0.5*Math.abs(a.x*b.y+b.x*c.y+c.x*d.y+d.x*e.y+e.x*a.y -b.x*a.y-c.x*b.y-d.x*c.y-e.x*d.y-a.x*e.y);
s1 = doubleFormat(s);
return s1;
}

public int JudgeSlope()
{
Line l1 = new Line(a,b);
Line l2 = new Line(b,c);
Line l3 = new Line(c,d);
Line l4 = new Line(d,e);
Line l5 = new Line(e,a);
double k1 = l1.getSlope();
double k2 = l2.getSlope();
double k3 = l3.getSlope();
double k4 = l4.getSlope();
double k5 = l5.getSlope();

if(k1!=k2&&k2!=k3&&k3!=k4&&k4!=k5&&k5!=k1)
{
return 1;
}

return 0;


}
public void outputwu()
{
System.out.printf("2" + " " + "10.5" + " " + "13.5");
}

public static Double doubleFormat(double b)
{
DecimalFormat df = new DecimalFormat("#.000");
Double output = Double.valueOf(df.format(b));
return output;
}


/* 三个点的getter()和setter()方法 */
public Point getA()
{
return a;
}

public void setA(Point a)
{
this.a = a;
}

public Point getB() {
return b;
}

public void setB(Point b) {
this.b = b;
}


public Point getC() {
return c;
}

public void setC(Point c) {
this.c =c ;
}

public Point getD() {
return d;
}

public void setD(Point d) {
this.d = d;
}
public Point getE() {
return e;
}

public void setE(Point e) {
this.e = e;
}
}
class Point {
public double x;
public double y;

public Point() {

}

public Point(double x,double y) {
this.x=x;
this.y=y;
}


public void setX(double x) {
this.x = x;
}


public void setY(double y) {
this.y = y;
}


public double getX() {
return x;
}


public double getY() {
return y;
}

public boolean equals(Point p) {
boolean b = false;
if(this.x==p.getX()&&this.y==p.getY()) {
b=true;
}
return b;
}


public double getDistance(Point p)
{
double distance = Math.sqrt(Math.abs((this.x - p.getX())* (this.x - p.getX())+(this.y - p.getY())* (this.y - p.getY())));
return distance;
}
}
class PointInputError {

public static void wrongNumberOfPoints(ArrayList ps, int num)
{
if (ps.size() != num)
{
System.out.println("wrong number of points");
System.exit(0);
}
}

public static void wrongPointFormat(String s)
{
if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?"))
{
System.out.println("Wrong Format");
System.exit(0);
}
}


public static void wrongChoice(String s)
{
if (!s.matches("[1-5]:.+"))

{
System.out.println("Wrong Format");
System.exit(0);
}
}
}

复制代码

                     

                   度量值:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                   3.期中考试

         点与线

  • 设计一个类表示平面直角坐标系上的点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:长度值
  ```

           代码:

复制代码

import java.util.Scanner;

public class Main {
public static void main(String[] args) {

Scanner in = new Scanner(System.in);
double x1 = in.nextDouble();
double y1 = in.nextDouble();
double x2 = in.nextDouble();
double y2 = in.nextDouble();
Point d = new Point(x1,y1);
Point e = new Point(x2,y2);
d.isRight(x1, y1);
e.isRight(x2, y2);
String color = in.next();
Line l = new Line(d,e,color);
l.display();

}

}


class Point {
private double x;
private double y;

public Point() {

}

public Point(double x,double y)
{
this.x=x;
this.y=y;
}


public void setX(double x)
{
this.x = x;
}


public void setY(double y)
{
this.y = y;
}


public double getX()
{
return x;
}


public double getY()
{
return y;
}

public void display()
{
System.out.printf("(%.2f,%.2f)", x,y);

}
public void isRight(double x,double y)
{
if((x<=0||x>200)||(y<=0||y>200))
{
System.out.println("Wrong Format");
System.exit(0);
}
}

 

}

class Line {
private Point point1;
private Point point2;
private String color;
private double distance;

public Line()
{

}

public Line(Point p1, Point p2,String color)
{
Point p3 = new Point(p1.getX(), p1.getY());
Point p4 = new Point(p2.getX(), p2.getY());
this.point1 = p3;
this.point2 = p4;
this.color = color;
}

public void setcolor()
{
this.color = color;
}


public String getcolor()
{
return color;
}

public double getdistance()
{
distance = Math.sqrt(Math.abs((point1.getX() - point2.getX())* (point1.getX() - point2.getX())+(point1.getY() - point2.getY())* (point1.getY() - point2.getY())));
this.distance = distance;
return distance;
}



public void display()
{
double s;
s = getdistance();
System.out.println("The line's color is:"+ color);
System.out.println("The line's begin point's Coordinate is:");
point1.display();
System.out.println("\nThe line's end point's Coordinate is:");
point2.display();

System.out.printf("\nThe line's length is:%.2f",distance);

}

}

复制代码

 

                        类图:

                 

 

 

               

                        度量值:

 

 

 

      

三、采坑心得

      

         1.java做判断格式的类型,没用正则表达式前有点没头绪,后来听同学说用if语句去判断也能判断出来,但我还是尝试了用正则表达式做,感觉比用if语句简洁不少,虽然感觉判断浮点数的这串代码 if(str.matches("[+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)  可读性有点差,因为判断浮点数的正则表达式"[+-]?([0-9]+(\\.[0-9]+)?)有点难看懂,但这肯定是我个人的问题,难一眼看懂得地方加句注释就能解决,因为代码简洁好用,运行流畅才是程序所需要的。

          2.对多边形的判定不够全面导致有测试点不能通过的情况,上网搜索更全面的判定技巧是个开阔思路的好办法。

 

 

 

    3. 计算线与线的交点还不太熟练,对于判断直线两侧的点的方法还不是很熟悉,对于射线法判断点是否在多边形内需要有进一步的掌握。

 

四、改进建议

          1.第四次作业对多边形的判断有所不足,需要更精准的判断方法实现完全覆盖。

 

 

 

 

改进参考方法:

                        相交点法

             分析

                               (1)假设四个点PLT,PRT,PLB,PRB,并且他们是以PLT->PRT->PRB->PLB->PLT的方式连接起来。

                (2)当任意两条对线的延长线的相交点不在任意两条对线线段内,那么可以判断为四边形。例如线段PLT->PRT和线段PLB->PRB两条线段的延长线的相交点不在两条线段内则满足四边形的条件,然后再去判断另外两条对边线。

             实现

                                             实现步骤
                                                 (1)得到对边两条线段延长线相交点P1,然后再得到另外一个对边两条线段延迟线的相交点P2
                                                 (2)判断P1和P2是否在他们两条对边线线段内,如果在先端内则不是四边形。

【参考网址】:https://www.csdn.net/tags/NtTakg1sMjY0NTYtYmxvZwO0O0OO0O0O.html

          2. 第五次作业存在瑕疵,重复行为程序经历了,希望以后能减少。

          3.一是要学会建立更规范的类,如点与线的关系类isSameSide()、isBetween()、三点一线 boolean isonline (Point p);线与线的关系类:平行 bollean isparallel(Line l);二是要多思考类中要封装的功能,如point类中包含计算两点距离的功能等等诸如这类问题。

五、总结

         有痛失分数的懊悔,也有巨大的收获。第四次与第五次的PTA作业都建立在点线系列的基础上,对点线及多边形的判定与多种相关的计算达到练习与巩固的效果。个人感觉对数学的侧重较多,对数学逻辑思维要求较高,因此对将数学逻辑转化为程序逻辑的要求也更高。而期中考试则是更倾向编程逻辑的题目,考察了继承、多态及容器。所以,经过这几次作业及考试,让我懂得了编程题不一定只是单纯的知识点考察类型题,更是对数理逻辑思维的锻炼及,对综合能力的考察,希望在今后的学习里,能记住这几次作业的经验教训,不断提高自己。

 
posted @ 2022-05-16 11:48  夜阑沃汀风吹雨  阅读(305)  评论(0)    收藏  举报