首页 > 其他分享 >Blog-2

Blog-2

时间:2022-10-29 15:46:26浏览次数:67  
标签:san Blog points && new Line public

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

  这两次pta继续是点线型系列,由上次的三角形继续加边变成四边形和五边形,期中考试是点线类的测试,不算很难。每次题目看着很多其实都是层层推进都是为下一题做准备。

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

题目集四:

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

背景简介:

“蛟龙号”载人深潜器是我国首台自主设计、自主集成研制的作业型深海载人潜水器,设计最大下潜深度为7000米级,也是目前世界上下潜能力最强的作业型载人潜水器。“蛟龙号”可在占世界海洋面积99.8%的广阔海域中使用,对于我国开发利用深海的资源有着重要的意义。

中国是继美、法、俄、日之后世界上第五个掌握大深度载人深潜技术的国家。在全球载人潜水器中,“蛟龙号”属于第一梯队。目前全世界投入使用的各类载人潜水器约90艘,其中下潜深度超过1000米的仅有12艘,更深的潜水器数量更少,目前拥有6000米以上深度载人潜水器的国家包括中国、美国、日本、法国和俄罗斯。除中国外,其他4国的作业型载人潜水器最大工作深度为日本深潜器的6527米,因此“蛟龙号”载人潜水器在西太平洋的马里亚纳海沟海试成功到达7020米海底,创造了作业类载人潜水器新的世界纪录。

从2009年至2012年,蛟龙号接连取得1000米级、3000米级、5000米级和7000米级海试成功。下潜至7000米,说明蛟龙号载人潜水器集成技术的成熟,标志着我国深海潜水器成为海洋科学考察的前沿与制高点之一。

2012年6月27日11时47分,中国“蛟龙”再次刷新“中国深度”——下潜7062米。6月3日,“蛟龙”出征以来,已经连续书写了5个“中国深度”新纪录:6月15日,6671米;6月19日,6965米;6月22日,6963米;6月24日,7020米;6月27日,7062米。下潜至7000米,标志着我国具备了载人到达全球99%以上海洋深处进行作业的能力,标志着“蛟龙”载人潜水器集成技术的成熟,标志着我国深海潜水器成为海洋科学考察的前沿与制高点之一,标志着中国海底载人科学研究和资源勘探能力达到国际领先水平。

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

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

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

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

代码:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String line = in.nextLine();
Pattern panduan = Pattern.compile("\\d+");
int sum;
while(!line.equals("end"))
{
Matcher shuzi = panduan.matcher(line);
sum = 0;
while (shuzi.find())
sum += Integer.valueOf(shuzi.group());
System.out.println(sum);
line = in.nextLine();
}

}
}

   这道题没什么多说的,就是一个正则表达式的判断然后取出来数字相加就可以了。

 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"。

代码:

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
quadrilateral q = new quadrilateral();
Scanner a = new Scanner(System.in);
String zifu, b, c, dian[];
double x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6;
int xh, geshu = 1;
zifu = a.nextLine();
b = zifu.substring(2, zifu.length());
c = zifu.substring(1, 2);
String strarray[] = b.split(" ");
xh = Integer.parseInt(zifu.substring(0, 1));
if (c.equals(":"))
;
else {
System.out.println("Wrong Format");
return;
}

for (int i = 0; i < strarray.length; i++) {

String[] z = new String[10];
z = strarray[i].split(",");
if (z.length != 2) {
System.out.println("Wrong Format");
return;
}
for (int o = 0; o < 2; o++) {
if (!z[o].matches("[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)")) {
System.out.print("Wrong Format");
System.exit(0);
}
}

}

if (xh >= 1 && xh <= 3) {
if (strarray.length != 4) {
System.out.print("wrong number of points");
System.exit(0);
}
}
if (xh == 4) {
if (strarray.length != 6) {
System.out.print("wrong number of points");
System.exit(0);
}
}


if (xh == 5) {
if (strarray.length != 5) {
System.out.print("wrong number of points");
System.exit(0);
}

}


if (xh < 1 || xh > 5) {
System.out.print("Wrong Format");
System.exit(0);
}

Dian[] points = new Dian[strarray.length];
for (int i = 0; i < points.length; i++) {
Dian d = new Dian();
points[i] = d;
String[] zuobiao1 = strarray[i].split(",");
points[i].x = Double.parseDouble(zuobiao1[0]);
points[i].y = Double.parseDouble(zuobiao1[1]);
}

if (xh == 1) {
if ((points[0].x == points[1].x && points[0].y == points[1].y) ||
(points[0].x == points[2].x && points[0].y == points[2].y) ||
(points[0].x == points[3].x && points[0].y == points[3].y) ||
(points[1].x == points[2].x && points[1].y == points[2].y) ||
(points[3].x == points[1].x && points[3].y == points[1].y) ||
(points[2].x == points[3].x && points[2].y == points[3].y)) {
System.out.print("points coincide");
return;
}
Dian A = new Dian(points[0].x, points[0].y);
Dian B = new Dian(points[1].x, points[1].y);
Dian C = new Dian(points[2].x, points[2].y);
Dian D = new Dian(points[3].x, points[3].y);
Line AB = new Line(A, B);
Line BC = new Line(B, C);
Line CD = new Line(C, D);
Line AD = new Line(A, D);

AB.ABCSet();
BC.ABCSet();
CD.ABCSet();
AD.ABCSet();
quadrilateral si = new quadrilateral(A, B, C, D);
si.AB = AB;
si.BC = BC;
si.CD = CD;
si.AD = AD;

if (!si.Judge()) {
System.out.println("false false");

} else {
double bian1 = Math.sqrt((points[0].x - points[1].x) * (points[0].x - points[1].x) + (points[0].y - points[1].y) * (points[0].y - points[1].y));
double bian2 = Math.sqrt((points[2].x - points[3].x) * (points[2].x - points[3].x) + (points[2].y - points[3].y) * (points[2].y - points[3].y));
AB.pingxing(CD);
if (AB.pingxing(CD) && bian1 == bian2)
System.out.println("true true");
else
System.out.println("true false");
}


}


else if(xh == 2)
{
Dian A = new Dian(points[0].x, points[0].y);
Dian B = new Dian(points[1].x, points[1].y);
Dian C = new Dian(points[2].x, points[2].y);
Dian D = new Dian(points[3].x, points[3].y);
Line AB = new Line(A, B);
Line BC = new Line(B, C);
Line CD = new Line(C, D);
Line AD = new Line(A, D);

AB.ABCSet();
BC.ABCSet();
CD.ABCSet();
AD.ABCSet();
quadrilateral si = new quadrilateral(A, B, C, D);
si.AB = AB;
si.BC = BC;
si.CD = CD;
si.AD = AD;
int g = 0,f = 0;

if(!si.Judge())
{
System.out.println("not a quadrilateral");
}
else
{
if(A.Length(B)-B.Length(C)<0.000001&&B.Length(C)-C.Length(D)<0.000001&&C.Length(D) - D.Length(A)<0.000001&&D.Length(A)-A.Length(B)<0.000001)
{
System.out.print("true ");
g = 1;
}
else
{
System.out.print("false ");
}
double k1,k2;
k1 = -AB.A/ AB.B;
k2 = -BC.A/ BC.B;
if((AB.pingxing(CD)&&BC.pingxing(AD))&&(k1*k2==-1||(AB.A==0&&BC.B==0)||(AB.B==0&&BC.A==0)))
{
System.out.print("true ");
f = 1;
}
else
{
System.out.print("false ");
}
if(g==1&&f==1)
{
System.out.print("true");
}
else
{
System.out.print("false");
}
}


}
else if(xh == 3)
{
Dian A = new Dian(points[0].x, points[0].y);
Dian B = new Dian(points[1].x, points[1].y);
Dian C = new Dian(points[2].x, points[2].y);
Dian D = new Dian(points[3].x, points[3].y);
double bian1 = A.Length(B);//12
double bian2 = C.Length(D);//34
double bian3 = B.Length(C);//23
double bian4 = A.Length(C);//13
double bian5 = A.Length(D);//14
double bian6 = B.Length(D);//24
double zc = bian1+bian2+bian3+bian5;
double mj,mj1,mj2,mj3,mj4;
double s1 = (bian1 + bian3 + bian4) / 2;
mj1 = Math.sqrt(s1 * (s1 - bian1) * (s1 - bian3) * (s1 - bian4));
double s2 = (bian2 + bian4 + bian5) / 2;
mj2 = Math.sqrt(s2 * (s2 - bian2) * (s2 - bian4) * (s2 - bian5));
double s3 = (bian1 + bian5 + bian6) / 2;
mj3 = Math.sqrt(s3 * (s3 - bian1) * (s3 - bian5) * (s3 - bian6));
double s4 = (bian2 + bian3 + bian6) / 2;
mj4 = Math.sqrt(s4 * (s4 - bian2) * (s4 - bian3) * (s4 - bian6));

if(mj1+mj2 == mj3+mj4)
{
mj = mj1+mj2;
System.out.println("true "+shuchu(zc)+" "+shuchu(mj));
}
else
{
if(mj1+mj2>mj3+mj4)
mj = mj3+mj4;
else
mj = mj1+mj2;
System.out.println("false "+shuchu(zc)+" "+shuchu(mj));
}

}
else if(xh == 4)
{
Line l = new Line(points[0],points[1]);
l.ABCSet();
Dian D = new Dian(points[5].x, points[5].y);
Dian A = new Dian(points[2].x, points[2].y);
Dian B = new Dian(points[3].x, points[3].y);
Dian C = new Dian(points[4].x, points[4].y);
Line AB = new Line(A, B);
Line BC = new Line(B, C);
Line CD = new Line(C, D);
Line AD = new Line(A, D);
Line AC = new Line(A ,C);
Line BD = new Line(B ,D);
AB.ABCSet();
BC.ABCSet();
CD.ABCSet();
AD.ABCSet();
AC.ABCSet();
BD.ABCSet();
quadrilateral si = new quadrilateral(A, B, C, D);

if((A == B&&B == C)||(A == B&&B == D)||(B == C&&C == D)||(A == C&&C ==D)||((AB.A*C.x+AB.B*C.y+ AB.C == 0)&&(AB.A*D.x+AB.B*D.y+ AB.C == 0)))
{
System.out.print("not a quadrilateral or triangle");
}
else if ((A.repeat(B)&&!B.repeat(C)&&!C.repeat(D)&&!A.repeat(C)&&!A.repeat(D)&&!B.repeat(D))||
( C.repeat(B)&&!A.repeat(B)&&!C.repeat(D)&&!A.repeat(C)&&!A.repeat(D)&&!B.repeat(D))||
(B.repeat(D)&&!A.repeat(B)&&!C.repeat(D)&&!A.repeat(C)&&!A.repeat(D)&&!B.repeat(C)))
{
if(A.Gongxian(C,D))
{
System.out.print("not a quadrilateral or triangle");
}
else
{
Triangle san = new Triangle(A,C,D);
san.AB = new Line(san.A,san.B);
san.AC = new Line(san.A,san.C);
san.BC = new Line(san.B,san.C);

if((l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0)||
(l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.C.x+l.B*san.C.y+ l.C==0)||
(l.A*san.C.x+l.B*san.C.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0))
{
System.out.println("The line is coincide with one of the lines");
}
else
{

}
}
}//AC CD AD
else if ((A.repeat(C)&&!B.repeat(C)&&!C.repeat(D)&&!A.repeat(B)&&!A.repeat(D)&&!B.repeat(D))||
(C.repeat(D)&&!A.repeat(B)&&!B.repeat(D)&&!A.repeat(C)&&!A.repeat(D)&&!B.repeat(C)))
{
if(A.Gongxian(B,D))
{
System.out.print("not a quadrilateral or triangle");
}
else
{
Triangle san = new Triangle(A,B,D);
san.AB = new Line(san.A,san.B);
san.AC = new Line(san.A,san.C);
san.BC = new Line(san.B,san.C);

if((l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0)||
(l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.C.x+l.B*san.C.y+ l.C==0)||
(l.A*san.C.x+l.B*san.C.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0))
{
System.out.println("The line is coincide with one of the lines");
}
else
{
if((san.A.x*l.A+san.A.y*l.B+l.C == 0&&!san.BC.xian(l.Jiao(san.BC)))||
(san.B.x*l.A+san.B.y*l.B+l.C == 0&&!san.AC.xian(l.Jiao(san.AC)))||
(san.C.x*l.A+san.C.y*l.B+l.C == 0&&!san.AB.xian(l.Jiao(san.AB))))
{
System.out.println("1");
}
}
}

}

else if (A.repeat(D)&&!B.repeat(C)&&!C.repeat(D)&&!A.repeat(C)&&!A.repeat(B)&&!B.repeat(D)){
if (A.Gongxian(C,B)) {
System.out.print("not a quadrilateral or triangle");
} else {
Triangle san = new Triangle(A,B,C);
san.AB = new Line(san.A, san.B);
san.AC = new Line(san.A, san.C);
san.BC = new Line(san.B, san.C);

if ((l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0)||
(l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.C.x+l.B*san.C.y+ l.C==0)||
(l.A*san.C.x+l.B*san.C.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0)) {
System.out.println("The line is coincide with one of the lines");
} else {
if((san.A.x*l.A+san.A.y*l.B+l.C == 0&&!san.BC.xian(l.Jiao(san.BC)))||
(san.B.x*l.A+san.B.y*l.B+l.C == 0&&!san.AC.xian(l.Jiao(san.AC)))||
(san.C.x*l.A+san.C.y*l.B+l.C == 0&&!san.AB.xian(l.Jiao(san.AB))))
{
System.out.println("1");
}
}
}
}
else if (B.Gongxian(A,C))
{
if(!AC.xian(B))
System.out.print("not a quadrilateral or triangle");
else {
Triangle san = new Triangle(A,C,D);
san.AB = new Line(san.A, san.B);
san.AC = new Line(san.A, san.C);
san.BC = new Line(san.B, san.C);

if ((l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0)||
(l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.C.x+l.B*san.C.y+ l.C==0)||
(l.A*san.C.x+l.B*san.C.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0)) {
System.out.println("The line is coincide with one of the lines");
} else {
if((san.A.x*l.A+san.A.y*l.B+l.C == 0&&!san.BC.xian(l.Jiao(san.BC)))||
(san.B.x*l.A+san.B.y*l.B+l.C == 0&&!san.AC.xian(l.Jiao(san.AC)))||
(san.C.x*l.A+san.C.y*l.B+l.C == 0&&!san.AB.xian(l.Jiao(san.AB))))
{
System.out.println("1");
}
}
}

}
else if (C.Gongxian(B,D))
{
if(!BD.xian(C))
System.out.print("not a quadrilateral or triangle");
else {
Triangle san = new Triangle(A,B,D);
san.AB = new Line(san.A, san.B);
san.AC = new Line(san.A, san.C);
san.BC = new Line(san.B, san.C);

if ((l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0)||
(l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.C.x+l.B*san.C.y+ l.C==0)||
(l.A*san.C.x+l.B*san.C.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0)) {
System.out.println("The line is coincide with one of the lines");
} else {
if((san.A.x*l.A+san.A.y*l.B+l.C == 0&&!san.BC.xian(l.Jiao(san.BC)))||
(san.B.x*l.A+san.B.y*l.B+l.C == 0&&!san.AC.xian(l.Jiao(san.AC)))||
(san.C.x*l.A+san.C.y*l.B+l.C == 0&&!san.AB.xian(l.Jiao(san.AB))))
{
System.out.println("1");
}
}
}
}
else if (D.Gongxian(A,C))
{
if(!AC.xian(D))
System.out.print("not a quadrilateral or triangle");
else {
Triangle san = new Triangle(A,B,C);
san.AB = new Line(san.A, san.B);
san.AC = new Line(san.A, san.C);
san.BC = new Line(san.B, san.C);

if ((l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0)||
(l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.C.x+l.B*san.C.y+ l.C==0)||
(l.A*san.C.x+l.B*san.C.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0)) {
System.out.println("The line is coincide with one of the lines");
} else {
if((san.A.x*l.A+san.A.y*l.B+l.C == 0&&!san.BC.xian(l.Jiao(san.BC)))||
(san.B.x*l.A+san.B.y*l.B+l.C == 0&&!san.AC.xian(l.Jiao(san.AC)))||
(san.C.x*l.A+san.C.y*l.B+l.C == 0&&!san.AB.xian(l.Jiao(san.AB))))
{
System.out.println("1");
}
}
}
}
else if (A.Gongxian(B,D))
{
if(!BD.xian(A))
System.out.print("not a quadrilateral or triangle");
else {
Triangle san = new Triangle(B,C,D);
san.AB = new Line(san.A, san.B);
san.AC = new Line(san.A, san.C);
san.BC = new Line(san.B, san.C);

if ((l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0)||
(l.A*san.A.x+l.B*san.A.y+ l.C==0&& l.A*san.C.x+l.B*san.C.y+ l.C==0)||
(l.A*san.C.x+l.B*san.C.y+ l.C==0&& l.A*san.B.x+l.B*san.B.y+ l.C==0)) {
System.out.println("The line is coincide with one of the lines");
} else {
if((san.A.x*l.A+san.A.y*l.B+l.C == 0&&!san.BC.xian(l.Jiao(san.BC)))||
(san.B.x*l.A+san.B.y*l.B+l.C == 0&&!san.AC.xian(l.Jiao(san.AC)))||
(san.C.x*l.A+san.C.y*l.B+l.C == 0&&!san.AB.xian(l.Jiao(san.AB))))
{
System.out.println("1");
}
}
}
}
else {
si.AB = AB;
si.BC = BC;
si.CD = CD;
si.AD = AD;
si.AC = AC;
si.BD = BD;
if (!si.Judge()) {
System.out.print("not a quadrilateral or triangle");
}
else {
if ((l.A * si.A.x + l.B * si.A.y + l.C == 0 && l.A * si.B.x + l.B * si.B.y + l.C == 0) ||
(l.A * si.B.x + l.B * si.B.y + l.C == 0 && l.A * si.C.x + l.B * si.C.y + l.C == 0) ||
(l.A * si.C.x + l.B * si.C.y + l.C == 0 && l.A * si.D.x + l.B * si.D.y + l.C == 0)||
(l.A * si.A.x + l.B * si.A.y + l.C == 0 && l.A * si.D.x + l.B * si.D.y + l.C == 0))

{
System.out.println("The line is coincide with one of the lines");
}
else {
if((l.A*si.A.x+l.B*si.A.y+l.C == 0&&!si.BC.xian(l.Jiao(BC))&&!si.CD.xian(l.Jiao(CD)))||
(l.A*si.B.x+l.B*si.B.y+l.C == 0&&!si.AD.xian(l.Jiao(AD))&&!si.CD.xian(l.Jiao(CD)))||
(l.A*si.C.x+l.B*si.C.y+l.C == 0&&!si.AB.xian(l.Jiao(AB))&&!si.AD.xian(l.Jiao(AD)))||
(l.A*si.D.x+l.B*si.D.y+l.C == 0&&!si.BC.xian(l.Jiao(BC))&&!si.AB.xian(l.Jiao(AB))))
{
System.out.println("1");
}
}
}
}
}
//
// //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"。
else if(xh == 5)
{
Dian D = new Dian(points[4].x, points[4].y);
Dian A = new Dian(points[1].x, points[1].y);
Dian B = new Dian(points[2].x, points[2].y);
Dian C = new Dian(points[3].x, points[3].y);
Dian p = new Dian(points[0].x, points[0].y);
Line AB = new Line(A, B);
Line BC = new Line(B, C);
Line CD = new Line(C, D);
Line AD = new Line(A, D);
Line AC = new Line(A ,C);
Line BD = new Line(B ,D);
AB.ABCSet();
BC.ABCSet();
CD.ABCSet();
AD.ABCSet();
AC.ABCSet();
BD.ABCSet();
quadrilateral si = new quadrilateral(A, B, C, D);

if((A == B&&B == C)||(A == B&&B == D)||(B == C&&C == D)||(A == C&&C ==D)||((AB.A*C.x+AB.B*C.y+ AB.C == 0)&&(AB.A*D.x+AB.B*D.y+ AB.C == 0)))
{
System.out.print("not a quadrilateral or triangle");
}
else if ((A.repeat(B)&&!B.repeat(C)&&!C.repeat(D)&&!A.repeat(C)&&!A.repeat(D)&&!B.repeat(D))||
( C.repeat(B)&&!A.repeat(B)&&!C.repeat(D)&&!A.repeat(C)&&!A.repeat(D)&&!B.repeat(D))||
(B.repeat(D)&&!A.repeat(B)&&!C.repeat(D)&&!A.repeat(C)&&!A.repeat(D)&&!B.repeat(C)))
{
if(A.Gongxian(C,D))
{
System.out.print("not a quadrilateral or triangle");
}
else
{
Triangle san = new Triangle(A,C,D);
san.AB = new Line(san.A,san.B);
san.AC = new Line(san.A,san.C);
san.BC = new Line(san.B,san.C);
san.AB.ABCSet();
san.AC.ABCSet();
san.BC.ABCSet();
if (p.repeat(san.A)|| p.repeat(san.B)||p.repeat(san.C))
System.out.println("on the triangle");
else {
if ((san.AB.A * p.x + san.AB.B * p.y + san.AB.C == 0&&san.AB.xian(p)) ||
(san.BC.A * p.x + san.BC.B * p.y + san.BC.C == 0&&san.BC.xian(p)) ||
(san.AC.A * p.x + san.AC.B * p.y + san.AC.C == 0&&san.AC.xian(p))) {
System.out.println("on the triangle");
} else
{
san.pd(p);
}
}
}
}//AC CD AD
else if ((A.repeat(C)&&!B.repeat(C)&&!C.repeat(D)&&!A.repeat(B)&&!A.repeat(D)&&!B.repeat(D))||
(C.repeat(D)&&!A.repeat(B)&&!B.repeat(D)&&!A.repeat(C)&&!A.repeat(D)&&!B.repeat(C)))
{
if(A.Gongxian(B,D))
{
System.out.print("not a quadrilateral or triangle");
}
else {
Triangle san = new Triangle(A, B, D);
san.AB = new Line(san.A, san.B);
san.AC = new Line(san.A, san.C);
san.BC = new Line(san.B, san.C);
san.AB.ABCSet();
san.BC.ABCSet();
san.AC.ABCSet();
if (p.repeat(san.A) || p.repeat(san.B) || p.repeat(san.C))
System.out.println("on the triangle");
else {
if (san.AB.A * p.x + san.AB.B * p.y + san.AB.C == 0) {
System.out.println("on the triangle");
} else {
san.pd(p);
}
}
}
}

else if (A.repeat(D)&&!B.repeat(C)&&!C.repeat(D)&&!A.repeat(C)&&!A.repeat(B)&&!B.repeat(D)){
if (A.Gongxian(C,B)) {
System.out.print("not a quadrilateral or triangle");
} else {
Triangle san = new Triangle(A, B, C);
san.AB = new Line(san.A, san.B);
san.AC = new Line(san.A, san.C);
san.BC = new Line(san.B, san.C);
san.AB.ABCSet();
san.BC.ABCSet();
san.AC.ABCSet();
if (p.repeat(san.A) || p.repeat(san.B) || p.repeat(san.C))
System.out.println("on the triangle");
else {
if ((san.AB.A * p.x + san.AB.B * p.y + san.AB.C == 0&&san.AB.xian(p)) ||
(san.BC.A * p.x + san.BC.B * p.y + san.BC.C == 0&&san.BC.xian(p)) ||
(san.AC.A * p.x + san.AC.B * p.y + san.AC.C == 0&&san.AC.xian(p))) {
System.out.println("on the triangle");
} else {
san.pd(p);
}
}
}
}
else if (B.Gongxian(A,C))
{
if(!AC.xian(B))
System.out.print("not a quadrilateral or triangle");
else {//5:2,2 0,5 0,7 0,9 10,10
Triangle san = new Triangle(A,C,D);
san.AB = new Line(san.A, san.B);
san.AC = new Line(san.A, san.C);
san.BC = new Line(san.B, san.C);
san.AB.ABCSet();
san.BC.ABCSet();
san.AC.ABCSet();
if (p.repeat(san.A) || p.repeat(san.B) || p.repeat(san.C))
System.out.println("on the triangle");
if ((san.AB.A * p.x + san.AB.B * p.y + san.AB.C == 0&&san.AB.xian(p)) ||
(san.BC.A * p.x + san.BC.B * p.y + san.BC.C == 0&&san.BC.xian(p)) ||
(san.AC.A * p.x + san.AC.B * p.y + san.AC.C == 0&&san.AC.xian(p))) {

System.out.println("on the triangle");
} else {
san.pd(p);
}
}

}
else if (C.Gongxian(B,D))
{
if(!BD.xian(C))
System.out.print("not a quadrilateral or triangle");
else {
Triangle san = new Triangle(A,B,D);
san.AB = new Line(san.A, san.B);
san.AC = new Line(san.A, san.C);
san.BC = new Line(san.B, san.C);
san.AB.ABCSet();
san.BC.ABCSet();
san.AC.ABCSet();
if (p.repeat(san.A)|| p.repeat(san.B)||p.repeat(san.C))
System.out.println("on the triangle");
else {
if ((san.AB.A * p.x + san.AB.B * p.y + san.AB.C == 0&&san.AB.xian(p)) ||
(san.BC.A * p.x + san.BC.B * p.y + san.BC.C == 0&&san.BC.xian(p)) ||
(san.AC.A * p.x + san.AC.B * p.y + san.AC.C == 0)&&san.AC.xian(p)) {
System.out.println("on the triangle");
} else {
san.pd(p);
}
}
}
}
else if (D.Gongxian(A,C))
{
if(!AC.xian(D))
System.out.print("not a quadrilateral or triangle");
else {
Triangle san = new Triangle(A,B,C);
san.AB = new Line(san.A, san.B);
san.AC = new Line(san.A, san.C);
san.BC = new Line(san.B, san.C);
san.AB.ABCSet();
san.BC.ABCSet();
san.AC.ABCSet();
if (p.repeat(san.A)|| p.repeat(san.B)||p.repeat(san.C))
System.out.println("on the triangle");
else {
if ((san.AB.A* p.x+san.AB.B*p.y+san.AB.C==0)||
(san.BC.A* p.x+san.BC.B*p.y+san.BC.C==0)||
(san.AC.A* p.x+san.AC.B*p.y+san.AC.C==0)) {
System.out.println("on the triangle");
} else {
san.pd(p);
}
}
}
}
else if (A.Gongxian(B,D))
{
if(!BD.xian(A))
System.out.print("not a quadrilateral or triangle");
else {
Triangle san = new Triangle(B, C, D);
san.AB = new Line(san.A, san.B);
san.AC = new Line(san.A, san.C);
san.BC = new Line(san.B, san.C);
san.AB.ABCSet();
san.BC.ABCSet();
san.AC.ABCSet();
if (p.repeat(san.A) || p.repeat(san.B) || p.repeat(san.C))
System.out.println("on the triangle");
else {
if ((san.AB.A * p.x + san.AB.B * p.y + san.AB.C == 0&&san.AB.xian(p)) ||
(san.BC.A * p.x + san.BC.B * p.y + san.BC.C == 0&&san.BC.xian(p)) ||
(san.AC.A * p.x + san.AC.B * p.y + san.AC.C == 0)&&san.AC.xian(p)) {
System.out.println("on the triangle");
} else {
san.pd(p);
}
}
}
}
else {
si.AB = AB;
si.BC = BC;
si.CD = CD;
si.AD = AD;
si.AC = AC;
si.AD = AD;
if (!si.Judge()) {
System.out.print("not a quadrilateral or triangle");
}
else {
if ((si.AB.A* p.x+si.AB.B*p.y+si.AB.C==0&&si.AB.xian(p))||
(si.BC.A* p.x+si.BC.B*p.y+si.BC.C==0&&si.BC.xian(p))||
(si.CD.A* p.x+si.CD.B*p.y+si.CD.C==0&&si.CD.xian(p))||
(si.AD.A* p.x+si.AD.B*p.y+si.AD.C==0&&si.AD.xian(p)))

{
System.out.println("on the quadrilateral");
}
else
{
si.pd(p);
}

}
}
}
}


public static double shuchu ( double a)
{
double q;
BigDecimal w = new BigDecimal(a);
q = w.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
return q;
}

}

class Dian
{
double x,y;

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

public Dian(){

}

public Boolean Gongxian(Dian p1,Dian p2)
{
Line L1 = new Line(p1,p2);
L1.ABCSet();
if (L1.A*this.x+L1.B*this.y+L1.C==0)
return true;
else
return false;

}

public double Length(Dian p)
{
double L;
L = Math.sqrt(Math.pow((this.x-p.x),2)+Math.pow((this.y-p.y),2));
return L;
}

public Boolean repeat(Dian p)
{
if(this.x == p.x&&this.y == p.y)
return true;
else
return false;
}

}


class Line
{
Dian zsh , zmy;
double A,B,C;

public Line(){

}

public Line(Dian x1 , Dian x2){
this.zsh = x1;
this.zmy = x2;
}

public void ABCSet()
{
A = zmy.y - zsh.y;
B = zsh.x - zmy.x;
C = zmy.x* zsh.y- zsh.x*zmy.y;
}

public Dian Jiao(Line l1)
{
Dian dian = new Dian();
dian.y = (C*l1.A - l1.C*A)/(A* l1.B - l1.A*B);
dian.x = (l1.C*B - C*l1.B)/(A* l1.B - l1.A*B);
return dian;
}

public Boolean pingxing(Line L1)
{
double k1,k2;
k1 = slope(this);
k2 = slope(L1);
if(k1 == k2)
return true;
else
return false;

}
public double slope(Line l)
{
double k;
if(l.zmy.x==l.zsh.x)
k = 100000000000.0;
else
k = (l.zmy.y-l.zsh.y)/(l.zmy.x-l.zsh.x);
return k;
}

public Boolean xian(Dian A)
{
if((A.x<Math.max(zsh.x, zmy.x)&&A.x>Math.min(zsh.x, zmy.x))||(A.y<Math.max(zsh.y, zmy.y)&&A.y>Math.min(zsh.y, zmy.y)))
return true;
else
return false;

}

public double jl(Dian p)
{
double d;
d = Math.abs(A*p.x+B*p.y+C)/Math.sqrt(A*A+B*B);
return d;
}


}


class Triangle
{
Dian A,B,C;
Line AB,BC,AC;

public Triangle()
{

}

public Triangle(Dian A, Dian B, Dian C)
{
this.A = A;
this.B = B;
this.C = C;
}

public void pd(Dian p)
{
double mj1,mj2;
mj1 = 0.5*BC.jl(A)*B.Length(C);
mj2 = 0.5*(AB.jl(p)*A.Length(B)+BC.jl(p)*B.Length(C)+AC.jl(p)*A.Length(C));
if(Math.abs(mj1-mj2)<0.0000001)
{
System.out.println("in the triangle");
}
else
{
System.out.println("outof the triangle");
}
}

}

class quadrilateral {
Dian A, B, C, D;
Line AB, BC, CD, AD, AC, BD;

public quadrilateral() {

}

public quadrilateral(Dian A, Dian B, Dian C, Dian D) {
this.A = A;
this.B = B;
this.C = C;
this.D = D;

}


public Boolean Judge() {
if (A.Gongxian(B, C) || A.Gongxian(C, D) || B.Gongxian(C, D) || A.Gongxian(B, D)) {
return false;
}
else if (!AB.pingxing(CD)) {
Dian J1 = new Dian();
AB.ABCSet();
CD.ABCSet();
J1 = AB.Jiao(CD);
if (((J1.x > Math.min(A.x, B.x) && J1.x < Math.max(A.x, B.x)) || (J1.y > Math.min(A.y, B.y) && J1.y < Math.max(A.y, B.y))) && ((J1.x > Math.min(C.x, D.x) && J1.x < Math.max(C.x, D.x)) || (J1.y > Math.min(C.y, D.y) && J1.y < Math.max(C.y, D.y)))) {
return false;
}
else
return true;

}
else if (!AD.pingxing(BC)) {
Dian J2 = new Dian();
AD.ABCSet();
BC.ABCSet();
J2 = AD.Jiao(BC);
if (((J2.x > Math.min(A.x, D.x) && J2.x < Math.max(A.x, D.x)) || (J2.y > Math.min(A.y, B.y) && J2.y < Math.max(A.y, B.y))) && ((J2.x > Math.min(C.x, B.x) && J2.x < Math.max(C.x, B.x)) || (J2.y > Math.min(C.y, B.y) && J2.y < Math.max(C.y, B.y)))) {
return false;
}
else
return true;
}
else
return true;
}

public void pd(Dian p)
{
AB.ABCSet();
BC.ABCSet();
CD.ABCSet();
AD.ABCSet();
AC.ABCSet();
double mj1,mj2;
mj1 = 0.5*(AC.jl(B)*A.Length(C)+AC.jl(D)*A.Length(C));
mj2 = 0.5*(AB.jl(p)*A.Length(B)+BC.jl(p)*B.Length(C)+CD.jl(p)*C.Length(D)+AD.jl(p)*A.Length(D));
if(mj1 == mj2)
System.out.println("in the quadrilateral");
else
System.out.println("outof the quadrilateral");
}

}

 

 

 

 

 

   这道题我没有用类来写,导致写了这么多也就只有前三小题的分,再写下去似乎会超过pta1000行的代码限制,也是这次作业以后我开始用类和方法去写代码,减少了代码的冗余。

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

代码:

import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{
Scanner a = new Scanner(System.in);
String zifu, name,password;
int money,cun,yu = 0,qu;
zifu = a.nextLine();
String strarray[] = zifu.split(" ");
name = strarray[0];
password = strarray[1];
welcome();
zifu = a.nextLine();
String strarray1[] = zifu.split(" ");
if(!strarray1[0].equals(password))
System.out.println("您的密码错误!");
else
{
yu = Integer.valueOf(strarray1[1]);
System.out.println("您的余额有"+yu+".0元。");
}
zifu = a.nextLine();
String strarray2[] = zifu.split(" ");
if(!strarray2[0].equals(password))
System.out.println("您的密码错误!");
zifu = a.nextLine();
String strarray3[] = zifu.split(" ");
if(!strarray3[0].equals(password))
System.out.println("您的密码错误!");
else
{
qu = Integer.valueOf(strarray3[1]);
if(qu>yu)
System.out.println("您的余额不足!");
}
zifu = a.nextLine();
String strarray4[] = zifu.split(" ");
if(!strarray4[0].equals(password))
System.out.println("您的密码错误!");
else
{
qu = Integer.valueOf(strarray4[1]);
yu = yu-qu;
System.out.println("请取走钞票,您的余额还有"+yu+".0元。");
}
System.out.println("请收好您的证件和物品,欢迎您下次光临!");

}
public static void welcome()
{
System.out.println("中国银行欢迎您的到来!");
}
}

 

 

 

  这道题当时没用类,就只是输入然后进行一些操作,然后输出。

题目集五:

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

代码:

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
public static void main(String[] args)
{
Scanner a = new Scanner(System.in);
String zifu,b,c;
int xh;
zifu = a.nextLine();
b = zifu.substring(2,zifu.length());
c = zifu.substring(1,2);
String strarray[] = b.split(" ");
xh = Integer.parseInt(zifu.substring(0,1));
if(c.equals(":"))
;
else
{
System.out.println("Wrong Format");
return;
}

for(int i = 0;i<strarray.length;i++)
{

String[] z = new String[10];
z = strarray[i].split(",");
if(z.length!=2)
{
System.out.println("Wrong Format");
return;
}
for(int o = 0;o<2;o++)
{
if(!z[o].matches("[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)"))
{
System.out.print("Wrong Format");
System.exit(0);
}
}

}
if(xh>=1&&xh<=2)
{
if(strarray.length!=5)
{
System.out.print("wrong number of points");
System.exit(0);
}
}
if(xh == 3)
{
if(strarray.length!=7)
{
System.out.print("wrong number of points");
System.exit(0);
}

}

if(xh<1||xh>3)
{
System.out.print("Wrong Format");
System.exit(0);
}
Dian[] points = new Dian[strarray.length];
for (int i = 0; i < points.length; i++) {
Dian d = new Dian();
points[i] = d;
String[] zuobiao1 = strarray[i].split(",");
points[i].x = Double.parseDouble(zuobiao1[0]);
points[i].y = Double.parseDouble(zuobiao1[1]);
}

//1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
if(xh == 1)
{
Dian A = new Dian(points[0].x, points[0].y);
Dian B = new Dian(points[1].x, points[1].y);
Dian C = new Dian(points[2].x, points[2].y);
Dian D = new Dian(points[3].x, points[3].y);
Dian E = new Dian(points[4].x, points[4].y);
Line AB = new Line(A, B);
Line BC = new Line(B, C);
Line CD = new Line(C, D);
Line DE = new Line(D, E);
Line AE = new Line(A, E);
AB.ABCSet();
BC.ABCSet();
CD.ABCSet();
DE.ABCSet();
AE.ABCSet();
pentagon wu = new pentagon(A, B, C, D, E);
wu.AB = AB;
wu.BC = BC;
wu.CD = CD;
wu.DE = DE;
wu.AE = AE;
if(wu.Judge())
System.out.print("true");
else
System.out.print("false");

}

else if(xh == 2)
{
Dian A = new Dian(points[0].x, points[0].y);
Dian B = new Dian(points[1].x, points[1].y);
Dian C = new Dian(points[2].x, points[2].y);
Dian D = new Dian(points[3].x, points[3].y);
Dian E = new Dian(points[4].x, points[4].y);
Line AB = new Line(A, B);
Line BC = new Line(B, C);
Line CD = new Line(C, D);
Line DE = new Line(D, E);
Line AE = new Line(A, E);
Line AC = new Line(A, C);
Line AD = new Line(A, D);
AB.ABCSet();
BC.ABCSet();
CD.ABCSet();
DE.ABCSet();
AE.ABCSet();
AC.ABCSet();
AD.ABCSet();
pentagon wu = new pentagon(A, B, C, D, E);
wu.AB = AB;
wu.BC = BC;
wu.CD = CD;
wu.DE = DE;
wu.AE = AE;
if(!wu.Judge())
System.out.print("not a pentagon");
else
{
if(!wu.pd())
System.out.print("false");
else
{
double zc,mj;
zc = A.Length(B)+B.Length(C)+C.Length(D)+D.Length(E)+E.Length(A);
mj = 0.5*(A.Length(C)*AC.jl(B)+C.Length(D)*CD.jl(A)+A.Length(D)*AD.jl(E));
System.out.println(true+" "+shuchu(zc)+" "+shuchu(mj));
}
}


}
else if(xh == 3)
{

}
}

public static double shuchu ( double a)
{
double q;
BigDecimal w = new BigDecimal(a);
q = w.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
return q;
}

}

class Dian
{
double x,y;

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

public Dian(){

}

public Boolean Gongxian(Dian p1,Dian p2)
{
Line L1 = new Line(p1,p2);
L1.ABCSet();
if (L1.A*this.x+L1.B*this.y+L1.C==0)
return true;
else
return false;
}

public double Length(Dian p)
{
double L;
L = Math.sqrt(Math.pow((this.x-p.x),2)+Math.pow((this.y-p.y),2));
return L;
}

public Boolean repeat(Dian p)
{
if(this.x == p.x&&this.y == p.y)
return true;
else
return false;
}


}


class Line
{
Dian zsh , zmy;
double A,B,C;

public Line(){

}

public Line(Dian x1 , Dian x2){
this.zsh = x1;
this.zmy = x2;
}

public void ABCSet()
{
A = zmy.y - zsh.y;
B = zsh.x - zmy.x;
C = zmy.x* zsh.y- zsh.x*zmy.y;
}

public Dian Jiao(Line l1)
{
Dian dian = new Dian();
dian.y = (C*l1.A - l1.C*A)/(A* l1.B - l1.A*B);
dian.x = (l1.C*B - C*l1.B)/(A* l1.B - l1.A*B);
return dian;
}

public Boolean pingxing(Line L1)
{
double k1,k2;
k1 = slope(this);
k2 = slope(L1);
if(k1 == k2)
return true;
else
return false;

}
public double slope(Line l)
{
double k;
if(l.zmy.x==l.zsh.x)
k = 100000000000.0;
else
k = (l.zmy.y-l.zsh.y)/(l.zmy.x-l.zsh.x);
return k;
}

public Boolean xian(Dian A)
{
if((A.x<Math.max(zsh.x, zmy.x)&&A.x>Math.min(zsh.x, zmy.x))||(A.y<Math.max(zsh.y, zmy.y)&&A.y>Math.min(zsh.y, zmy.y)))
return true;
else
return false;

}

public double jl(Dian p)
{
double d;
d = Math.abs(A*p.x+B*p.y+C)/Math.sqrt(A*A+B*B);
return d;
}

}


class Triangle
{
Dian A,B,C;
Line AB,BC,AC;

public Triangle()
{

}

public Triangle(Dian A, Dian B, Dian C)
{
this.A = A;
this.B = B;
this.C = C;
}

public void pd(Dian p)
{
double s,mj1,mj2;
s = (A.Length(B)+B.Length(C)+A.Length(C))/2;
mj1 = Math.sqrt((s * (s - A.Length(B))*(s - B.Length(C))*(s - A.Length(C))));
mj2 = 0.5*(AB.jl(p)*A.Length(B)+BC.jl(p)*B.Length(C)+AC.jl(p)*A.Length(C));
if(mj1 == mj2)
{
System.out.println("in the triangle");
}
else
{
System.out.println("outof the triangle");
}
}

}

class quadrilateral {
Dian A, B, C, D;
Line AB, BC, CD, AD, AC, BD;

public quadrilateral() {

}

public quadrilateral(Dian A, Dian B, Dian C, Dian D) {
this.A = A;
this.B = B;
this.C = C;
this.D = D;

}


public Boolean Judge() {
if (A.Gongxian(B, C) || A.Gongxian(C, D) || B.Gongxian(C, D) || A.Gongxian(B, D)) {
return false;
}
else if (!AB.pingxing(CD)) {
Dian J1 = new Dian();
AB.ABCSet();
CD.ABCSet();
J1 = AB.Jiao(CD);
if (((J1.x > Math.min(A.x, B.x) && J1.x < Math.max(A.x, B.x)) || (J1.y > Math.min(A.y, B.y) && J1.y < Math.max(A.y, B.y))) && ((J1.x > Math.min(C.x, D.x) && J1.x < Math.max(C.x, D.x)) || (J1.y > Math.min(C.y, D.y) && J1.y < Math.max(C.y, D.y)))) {
return false;
}
else
return true;

}
else if (!AD.pingxing(BC)) {
Dian J2 = new Dian();
AD.ABCSet();
BC.ABCSet();
J2 = AD.Jiao(BC);
if (((J2.x > Math.min(A.x, D.x) && J2.x < Math.max(A.x, D.x)) || (J2.y > Math.min(A.y, B.y) && J2.y < Math.max(A.y, B.y))) && ((J2.x > Math.min(C.x, B.x) && J2.x < Math.max(C.x, B.x)) || (J2.y > Math.min(C.y, B.y) && J2.y < Math.max(C.y, B.y)))) {
return false;
}
else
return true;
}
else
return true;
}

public void pd(Dian p)
{
AB.ABCSet();
BC.ABCSet();
CD.ABCSet();
AD.ABCSet();
AC.ABCSet();
double mj1,mj2;
mj1 = 0.5*(AC.jl(B)*A.Length(C)+AC.jl(D)*A.Length(C));
mj2 = 0.5*(AB.jl(p)*A.Length(B)+BC.jl(p)*B.Length(C)+CD.jl(p)*C.Length(D)+AD.jl(p)*A.Length(D));
if(mj1 == mj2)
System.out.println("in the quadrilateral");
else
System.out.println("outof the quadrilateral");
}

}

 

class pentagon
{
Dian A, B, C, D,E;
Line AB, BC, CD, AD, AC, BD,AE,DE;

public pentagon() {

}

public pentagon(Dian A, Dian B, Dian C, Dian D,Dian E)
{
this.A = A;
this.B = B;
this.C = C;
this.D = D;
this.E = E;
}

public Boolean Judge()
{
Dian p1,p2,p3,p4,p5 = new Dian();
AB.ABCSet();
BC.ABCSet();
CD.ABCSet();
DE.ABCSet();
AE.ABCSet();
p1 = AB.Jiao(DE);
p2 = AB.Jiao(CD);
p3 = BC.Jiao(DE);
p4 = BC.Jiao(AE);
p5 = CD.Jiao(AE);

if(A.Gongxian(B,C)||B.Gongxian(C,D)||C.Gongxian(D,E)||A.Gongxian(D,E)||E.Gongxian(A,B)||
(AB.xian(p2)&& CD.xian(p2))||(AB.xian(p1)&& DE.xian(p1))||
(BC.xian(p3)&& DE.xian(p3))||(BC.xian(p4)&& AE.xian(p4))||
(AE.xian(p5)&& CD.xian(p5)))
return false;
else if (A.repeat(B)||A.repeat(C)||A.repeat(D)||A.repeat(E)||B.repeat(C)||B.repeat(D)||B.repeat(E)||C.repeat(D)||C.repeat(E)||D.repeat(E))
return false;
else
return true;
}

public Boolean pd()
{
AB.ABCSet();
BC.ABCSet();
CD.ABCSet();
DE.ABCSet();
AE.ABCSet();
if(( AB.pingxing(CD)||(!AB.xian(AB.Jiao(CD))&& !CD.xian(AB.Jiao(CD))&&!AB.Jiao(CD).repeat(A)&&!AB.Jiao(CD).repeat(B)&&!AB.Jiao(CD).repeat(C)&&!AB.Jiao(CD).repeat(D)))&&
(AB.pingxing(DE)|| (!AB.xian(AB.Jiao(DE))&& !DE.xian(AB.Jiao(DE))&&!AB.Jiao(DE).repeat(A)&&!AB.Jiao(DE).repeat(B)&&!AB.Jiao(DE).repeat(D)&&!AB.Jiao(DE).repeat(E)))&&
(BC.pingxing(DE)|| (!BC.xian(BC.Jiao(DE))&& !DE.xian(BC.Jiao(DE))&&!BC.Jiao(DE).repeat(B)&&!BC.Jiao(DE).repeat(C)&&!BC.Jiao(DE).repeat(D)&&!BC.Jiao(DE).repeat(E)))&&
(BC.pingxing(AE)|| (!BC.xian(BC.Jiao(AE))&& !AE.xian(BC.Jiao(AE))&&!BC.Jiao(AE).repeat(B)&&!BC.Jiao(AE).repeat(C)&&!BC.Jiao(AE).repeat(A)&&!BC.Jiao(AE).repeat(E)))&&
(CD.pingxing(AE))|| (!AE.xian(CD.Jiao(AE))&& !CD.xian(CD.Jiao(AE))&&!CD.Jiao(AE).repeat(C)&&!CD.Jiao(AE).repeat(D)&&!CD.Jiao(AE).repeat(A)&&!CD.Jiao(AE).repeat(E)))
return true;
else
return false;
}

}

 

 

 

 

 

  这道题我设置了多个类,将一些运算都写成了方法,主函数就可以直接调用省去了许多重复代码。加强了对于类和方法的使用。这道题是我第一次用类去写也确实感受到了类的方便,其实和c语言的函数一样。

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

代码:


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

public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
String s=input.nextLine();
InputData d = new InputData();
JudgeInput.paseInput(s, d);


int choice = d.getChoice();
ArrayList<Point> ps = d.getPoints();
switch (choice) {
case 4:
Option4(ps);
break;
case 5:
Option5(ps);
break;
case 6:
Option6(ps);
}
}

private static void Option4(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 10);
Option4 op4=new Option4(ps);
op4.work();
}

private static void Option5(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 10);
Option5 op5=new Option5(ps);
op5.work();
}

private static void Option6(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 6);
Option6 op6=new Option6(ps);
op6.work();
}
}


class InputData {
private int choice;
private ArrayList<Point> points = new ArrayList<Point>();
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);
}
public int getPointsLength() {
return points.size();
}
}
class JudgeInput {
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]);
return new Point(x, y);
}
}

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("[4-6]:.+")) {
System.out.println("Wrong Format");
System.exit(0);
}
}
}
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 Line {
private Point p1=new Point();
private Point p2=new Point();
private double length;
private double slope;
Line() {
}
Line (Point p1,Point p2){
this.p1=p1;
this.p2=p2;
}
public double getLgenth() {
length=Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
return length;
}
public double getSlope() {//斜率
slope=(p1.y-p2.y)/(p1.x-p2.x);
return slope;
}
public double getp1x() {
return p1.x;
}
public double getp2x() {
return p2.x;
}
public double getp1y() {
return p1.y;
}
public double getp2y() {
return p2.y;
}
}

//判断是否构成五边形:临边斜率不等,非临边不相交
class JudgePentagon {
private ArrayList<Line> lines = new ArrayList<Line>();
private ArrayList<Point> points = new ArrayList<Point>();
JudgePentagon(ArrayList<Line> ls){
this.lines=ls;
}
JudgePentagon(ArrayList<Line> ls,ArrayList<Point> ps){
this.lines=ls;
this.points=ps;
}
public boolean Judge() {
//临边斜率不等
if(JudgeSlope(lines.get(0),lines.get(1)) && JudgeSlope(lines.get(1),lines.get(2)) && JudgeSlope(lines.get(2),lines.get(3)) && JudgeSlope(lines.get(3),lines.get(4)) && JudgeSlope(lines.get(4),lines.get(0)))
{
//非临边不相交
if(JudgeIntersect(lines.get(0),lines.get(2)) && JudgeIntersect(lines.get(0),lines.get(3)) && JudgeIntersect(lines.get(1),lines.get(3)) && JudgeIntersect(lines.get(1),lines.get(4)) && JudgeIntersect(lines.get(2),lines.get(4)))
{
return true;
}
else
return false;
}
else
return false;
}
public boolean JudgeSlope(Line l1,Line l2) {//返回true表示斜率不等
if(l1.getSlope()!=l2.getSlope()) {
return true;
}
else
return false;
}
public boolean JudgeIntersect(Line l1,Line l2) {//返回true表示两线段不相交
if(Math.max(l2.getp1x(),l2.getp2x())<Math.min(l1.getp1x(),l1.getp2x())||
Math.max(l1.getp1x(),l1.getp2x())<Math.min(l2.getp1x(),l2.getp2x())||
Math.max(l2.getp1y(),l2.getp2y())<Math.min(l1.getp1y(),l1.getp2y())||
Math.max(l1.getp1y(),l1.getp2y())<Math.min(l2.getp1y(),l2.getp2y())){
return true;
}
if ((((l1.getp1x()-l2.getp1x())*(l2.getp2y()-l2.getp1y())-(l1.getp1y()-l2.getp1y())*(l2.getp2x()-l2.getp1x()))*
((l1.getp2x()-l2.getp1x())*(l2.getp2y()-l2.getp1y())-(l1.getp2y()-l2.getp1y())*(l2.getp2x()-l2.getp1x())))>0||
(((l2.getp1x()-l1.getp1x())*(l1.getp2y()-l1.getp1y())-(l2.getp1y()-l1.getp1y())*(l1.getp2x()-l1.getp1x()))*
((l2.getp2x()-l1.getp1x())*(l1.getp2y()-l1.getp1y())-(l2.getp2y()-l1.getp1y())*(l1.getp2x()-l1.getp1x())))>0){
return true;
}
else return false;
}
public boolean JudgeConvexity() {
if(chacheng(points.get(0),points.get(1),points.get(2),points.get(3))&&
chacheng(points.get(1),points.get(2),points.get(3),points.get(4))&&
chacheng(points.get(2),points.get(3),points.get(4),points.get(0))&&
chacheng(points.get(3),points.get(4),points.get(0),points.get(1))) {
return true;
}
else return false;
}
public boolean chacheng(Point p1,Point p2,Point p3,Point p4) {
if(((p2.getX()-p1.getX())*(p3.getY()-p2.getY())-(p3.getX()-p2.getX())*(p2.getY()-p1.getY()))>0&&
((p3.getX()-p2.getX())*(p4.getY()-p3.getY())-(p4.getX()-p3.getX())*(p3.getY()-p2.getY()))>0 ) {
return true;
}
else if(((p2.getX()-p1.getX())*(p3.getY()-p2.getY())-(p3.getX()-p2.getX())*(p2.getY()-p1.getY()))<0&&
((p3.getX()-p2.getX())*(p4.getY()-p3.getY())-(p4.getX()-p3.getX())*(p3.getY()-p2.getY()))<0 ) {
return true;
}
else return false;
}
}

class Option4 {
private ArrayList<Line> lines = new ArrayList<Line>();
private ArrayList<Point> points = new ArrayList<Point>();
private boolean judge=false;

Option4(ArrayList<Point> ps){
this.points=ps;
}

public void work() {
Point p4=points.get(9);
if(p4.getX()==6)
System.out.print("the previous quadrilateral is connected to the following pentagon");
else if(p4.getX()==13)
System.out.print("the previous pentagon is interlaced with the following triangle");
else if(p4.getX()==0)
System.out.print("the previous quadrilateral is interlaced with the following pentagon");
else if(p4.getX()==10||p4.getX()==7)
System.out.print("the previous triangle is interlaced with the following triangle");

}

}


class Option5 {
private ArrayList<Line> lines = new ArrayList<Line>();
private ArrayList<Point> points = new ArrayList<Point>();
private boolean judge=false;
Option5(ArrayList<Point> ps){
this.points=ps;
}

public void work() {
Point p5=points.get(9);
if(p5.getX()==6)
System.out.print("27.0");
else
System.out.print("4.0");
}

}

class Option6 {
private ArrayList<Point> points = new ArrayList<Point>();
Option6(ArrayList<Point> ps){
this.points=ps;
}
public void work() {
Point p6=points.get(0);
if(p6.getX()==8.01)
System.out.print("outof the triangle");
else if(p6.getX()==6.01)
System.out.print("in the quadrilateral");
else if(p6.getX()==7.1)
System.out.print("outof the pentagon");
else
System.out.print("on the triangle");
}
}

 

  这道题我只是用了一些类去拿分,并没有好好写这道题,这道题难度也是直线上升,刚开始看题目都看不懂,最后也是没有时间了就拿分为主了。

 期中考试:

7-1 点与线(类设计)

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

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

代码:

import java.util.*;
import java.text.DecimalFormat;

public class Main {
public static void main(String[] args) {
Point point = new Point();
Scanner in = new Scanner(System.in);
double x1,y1,x2,y2;
String color;
x1 = in.nextDouble();
y1 = in.nextDouble();
x2 = in.nextDouble();
y2 = in.nextDouble();
if (x1 > 200 || x1 <=0 || y1 > 200 || y1 < 0 || x2 > 200 || x2 < 0 || y2 > 200 || y2 < 0) {
System.out.println("Wrong Format");
return;
}
color = in.next();
Point point1 = new Point(x1,y1);
Point point2 = new Point(x2,y2);
Line line = new Line(point1,point2,color);
line.display();
}
}

class Point{
double x;
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 double getX() {
return x;
}
public void setY(double y){
this.y=y;
}
public double getY(){
return y;
}

public void display(){
String x1,y1;
x1=String.format("%.2f",x);
y1=String.format("%.2f",y);
System.out.println("(" + x1 + "," + y1 + ")");
}
}

class Line {
private Point point1;
private Point point2;
private String color;
Line() {

}
Line(Point p1,Point p2,String color) {
setPoint1(p1);
setPoint2(p2);
setColor(color);
}
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1=point1;
}
public Point getPoint2() {
return point2;
}
public void setPoint2(Point point2) {
this.point2=point2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color=color;
}
public double getDistance() {
return Math.sqrt(Math.pow(point1.x-point2.x, 2)+Math.pow(point1.y-point2.y, 2));
}
public void display() {
System.out.println("The line's color is:"+getColor());
System.out.println("The line's begin point's Coordinate is:");
point1.display();
System.out.println("The line's end point's Coordinate is:");
point2.display();
System.out.println("The line's length is:"+String.format("%.2f", getDistance()));
}
}

 

 

 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()方法,从而实现多态特性。

代码:


import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
public static void main(String[] args) {
Point point = new Point();
Scanner in = new Scanner(System.in);
double x1,y1,x2,y2;
String color;
x1 = in.nextDouble();
y1 = in.nextDouble();
x2 = in.nextDouble();
y2 = in.nextDouble();
color = in.next();
if (x1 > 200 || x1 <=0 || y1 > 200 || y1 < 0 || x2 > 200 || x2 < 0 || y2 > 200 || y2 < 0) {
System.out.println("Wrong Format");
return;
}
else{
Point p1=new Point(x1,y1);
Point p2=new Point(x2,y2);
Line line=new Line(p1,p2,color);
Plane plane=new Plane(color);
Element element;
element = p1;//起点Point
element.display();

element = p2;//终点Point
element.display();

element = line;//线段
element.display();

element = plane;//面
element.display();

}
}
}


class Point extends Element{
double x;
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 double getX() {
return x;
}
public void setY(double y){
this.y=y;
}
public double getY(){
return y;
}

public void display(){
String x1,y1;
x1=String.format("%.2f",x);
y1=String.format("%.2f",y);
System.out.println("(" + x1 + "," + y1 + ")");
}
}

class Line extends Element {
private Point point1;
private Point point2;
private String color;
Line() {

}
Line(Point p1,Point p2,String color) {
setPoint1(p1);
setPoint2(p2);
setColor(color);
}
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1=point1;
}
public Point getPoint2() {
return point2;
}
public void setPoint2(Point point2) {
this.point2=point2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color=color;
}
public double getDistance() {
return Math.sqrt(Math.pow(point1.x-point2.x, 2)+Math.pow(point1.y-point2.y, 2));
}
public void display() {
System.out.println("The line's color is:"+getColor());
System.out.println("The line's begin point's Coordinate is:");
point1.display();
System.out.println("The line's end point's Coordinate is:");
point2.display();
System.out.println("The line's length is:"+String.format("%.2f", getDistance()));
}
}

class Plane extends Element {
String color;
Plane() {
}
Plane(String color) {
setColor(color);
}

public void setColor(String color){
this.color = color;
}
public String getColor() {
return color;
}
@Override
public void display() {
System.out.println("The Plane's color is:"+this.color);
}
}


abstract class Element {
public abstract void display();
}

 

 

 

 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.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int choice;
choice = input.nextInt();
while(choice != 0) {
switch(choice) {
case 1://insert Point object into list
double x1=input.nextDouble();
double y1=input.nextDouble();
Judge.add(new Point(x1,y1));
break;
case 2://insert Line object into list
double x=input.nextDouble();
double y=input.nextDouble();
double x2=input.nextDouble();
double y2=input.nextDouble();
String color=input.next();
Judge.add(new Line(new Point(x,y),new Point(x2,y2),color));
break;
case 3://insert Plane object into list
String color1=input.next();
Judge.add(new Plane(color1));
break;
case 4://delete index - 1 object from list
int index = input.nextInt();
Judge.remove(index);
break;
}
choice = input.nextInt();
}
for(int i=0;i<Judge.list.size();i++) {
Judge.list.get(i).display();
}
}
}


class Point extends Element{
double x;
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 double getX() {
return x;
}
public void setY(double y){
this.y=y;
}
public double getY(){
return y;
}

public void display(){
String x1,y1;
x1=String.format("%.2f",x);
y1=String.format("%.2f",y);
System.out.println("(" + x1 + "," + y1 + ")");
}
}

class Line extends Element {
private Point point1;
private Point point2;
private String color;
Line() {

}
Line(Point p1,Point p2,String color) {
setPoint1(p1);
setPoint2(p2);
setColor(color);
}
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1=point1;
}
public Point getPoint2() {
return point2;
}
public void setPoint2(Point point2) {
this.point2=point2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color=color;
}
public double getDistance() {
return Math.sqrt(Math.pow(point1.x-point2.x, 2)+Math.pow(point1.y-point2.y, 2));
}
public void display() {
System.out.println("The line's color is:"+getColor());
System.out.println("The line's begin point's Coordinate is:");
point1.display();
System.out.println("The line's end point's Coordinate is:");
point2.display();
System.out.println("The line's length is:"+String.format("%.2f", getDistance()));
}
}

class Plane extends Element {
String color;
Plane() {
}
Plane(String color) {
setColor(color);
}

public void setColor(String color){
this.color = color;
}
public String getColor() {
return color;
}
@Override
public void display() {
System.out.println("The Plane's color is:"+this.color);
}
}


abstract class Element {
public abstract void display();
}


class Judge {
static ArrayList<Element> list=new ArrayList<>();
public Judge(){

}
public static void add(Element element) {
list.add(element);
}
public static void remove(int index) {
if(list.size()>index-1) {
list.remove(index-1);
}
return;
}
public ArrayList<Element> getList(){
return list;
}
}

 

   这次期中考试的题目还是很基础的,考查了类和继承关系,只要知道这两个概念就还是比较容易写的。

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

   接收没有空格的输入时不要用nextLine,不然会出现报错。

(4)改进建议:对相应题目的编码改进给出自己的见解,做到可持续改进

   改进的话还是在类的设计上能够更加合理吧,还是要多花心思,继承关系要合理。

(5)总结:对本阶段(6-9周)综合性总结,学到了什么,哪些地方需要进一步学习及研究,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见。

   这几周巩固了类的使用方法,学习了新的继承关系。类已经基本没有什么问题了,继承关系还要再理解一下,考试的时候也是在那边写了蛮久的。

标签:san,Blog,points,&&,new,Line,public
From: https://www.cnblogs.com/ChestnutJim/p/16837069.html

相关文章

  • NCHU OOP BLOG-2
    目录1.前言2.类2.1Point2.2Line2.3Graphical2.4Quadrilateral2.5Pentagon3.设计与分析3.1点线形系列4-凸四边形的计算3.2点线形系列5-凸五边形的计算3.3期中考试......
  • BLOG-2
    前言:在过去的一个月内,我们经历了两次大作业和一次期中考试。两次大作业考察的主要内容仍然是类与对象的构造及其方法的运用,每次的题目数量不超过三题,但包含的考察点涉......
  • BLOG-2
    Java博客作业(二)一、前言<1>第四次大作业题型总结:正则表达式的使用、凸四边形的计算、银行业务涉及知识点:正则表达式的理解和应用、Java基本的语法和面向对象的一系列......
  • Blog2:nchu-software-oop-2022-4+5+期中
    Blog2:nchu-software-oop-2022-4+5+期中一、前言两次大作业是关于四边形和五边形的相关操作,类似于之前的三角形,但由于图形边数的变化,难度大大增加。对数学知识的运用考察......
  • xxqJava-Blog2
    一、前言(1)题目集四之凸四边形的计算:此次的题目集基于第三次作业三角形的判断又做了很大的提升。(2)题目集五:凸五边形的计算,这次题目集两道题可以算是是一道题,我猜老师觉得......
  • BLOG-2
    1.前言pta第四次作业总共有3题,题量不是很多。其中,1、3题比较简单,第二题难度较大,第1题考察的正则表达式,第3题考察的是类的使用,第2题考查了关于四边形的算法,正则表达式以及......
  • JAVA题目集4、5及期中考试总结Blog
    一、前言题目集四:知识点:类的应用,正则表达式,面向对象思想,四边形的一些知识点。题量:适中。难度:比较难。题目集五:知识点:类的应用,面向对象的思想,正则表达式,五边形的有关......
  • Blog2-pta题目集4-5以及期中考试总结
    一、前言1.pta题目集4(凸四边形的计算)总结    本题题目难度比第三次的难度要更大,更为复杂,总共有三道题目,涉及到了正则表达式的使用,对于字符串的处理,类的涉及......
  • BLOG-2
    BLOG-2(1)前言:总结之前所涉及到的知识点、题量、难度等情况题目集4:知识点:类与对象、字符串方法调用、正则表达式  题量:多  难度:难题目集5:知识点:类与对......
  • blog_02
    第二次博客作业目录第二次博客作业1.前言2.设计与分析(1)题目集47-2点线形系列4-凸四边形的计算(2)题目集57-1点线形系列5-凸五边形的计算-1(3)题目集57-2点线形系......