首页 > 其他分享 >BLOG-2

BLOG-2

时间:2022-10-29 22:56:33浏览次数:65  
标签:getX ps double get BLOG getY public

1.前言

第四次作业:第一题:考察正则表达式。第三题:考察java构造方法的创建和使用。

 

四边形,五边形需要复杂的算法和细致的思路,考虑到每个可能的情况,计算面积比较困难。

期中考试:
第一题考察构造方法,第二题考察继承与多态的使用,第三题当时来不及了,考察的是对象数组的使用。

 

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。若干个连续的数字字符作为一个整体,以十进制形式相加。
输入格式:
读入关于蛟龙号载人潜水器探测数据的多行字符串,每行字符不超过80个字符。
以"end"结束。

 

输出格式:
与输入行相对应的各个整数之和。

 

输入样例1:

 

Copy

 

2012年6月27日11时47分,中国“蛟龙”再次刷新“中国深度”——下潜7062米
6月15日,6671米
6月19日,6965米
6月22日,6963米
6月24日,7020米
6月27日,7062米
下潜至7000米,标志着我国具备了载人到达全球99%以上海洋深处进行作业的能力
end

 

输出样例1:

 

Copy

 


9165
6692
6990
6991
7050
7095
7099

 

输入样例2:

 

Copy

 

全世界投入使用的各类载人潜水器约90艘,下潜深度超过1000米的仅有12艘,更深的潜水器数量更少
6000米以上深度载人潜水器的国家包括中国、美国、日本、法国和俄罗斯
日本深潜器下潜6527米,蛟龙号在马里亚纳海沟海试成功到达7020米海底,创造了新的世界纪录
从2009年至2012年,蛟龙号接连取得1000米级、3000米级、5000米级和7000米级海试成功
下潜至7000米,说明蛟龙号载人潜水器集成技术的成熟
end

 

输出样例2:

 

Copy

 

1102
6000
13547
20021
7000
源代码:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true)
{
String line = sc.nextLine(); // 读入字符串
if(line.equals("end"))
break;
String[] split = line.split("\\D+");
//System.out.println(Arrays.toString(split));
int i;
long sum = 0;
for (i = 0; i < split.length; i++) {
if (!split[i].equals("")) {
int digit = Integer.parseInt(split[i]);
sum += digit;
}
}
System.out.println(sum);
}
}
}

我的idea是社区版,无法生成类图

 

 本题从每行字符串中提取数字,求得每行数字的和。

 

 

 

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
package pta4;


import java.util.Scanner;
import java.util.Arrays;

public class two {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();

//判断输入格式是否正确
if(!str.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");
System.exit(0);
}

//取出cmd,将串转化为浮点型
int cmd = str.charAt(0)-'0';
str = str.substring(2).trim();
String[] tmpstr = str.split("[ ,]");
double[] num = new double[30];
int cnt = 0;
for(String s:tmpstr){
if(!check(s)){
System.out.println("Wrong Format");
System.exit(0);
}
num[cnt++] = Double.parseDouble(s);
}

//将浮点型转化为坐标点型
Point[] p = new Point[10];
for(int i=0;i<cnt;i+=2){
p[i/2] = new Point(num[i],num[i+1]);
}


//点数不合法
if(cmd==1 || cmd==2 || cmd==3){
if(cnt != 8){
System.out.println("wrong number of points");
return;
}
}
if(cmd==4){
if(cnt != 12){
System.out.println("wrong number of points");
return;
}
}
if(cmd == 5){
if(cnt != 10){
System.out.println("wrong number of points");
return;
}
}


if(cmd == 1){
try{
Quadrilateral q = new Quadrilateral(p[0],p[1],p[2],p[3]);
System.out.printf("true %s\n",q.isParallelQuadrilateral());
}catch (Exception e){
if(e.getMessage().equals("not a quadrilateral"))
System.out.println("false false");
else System.out.println(e.getMessage());
}
}
if(cmd == 2){
try{
Quadrilateral q = new Quadrilateral(p[0],p[1],p[2],p[3]);
System.out.printf("%s %s %s\n",q.isDiamond(),q.isRectangle(),q.isSquare());
}catch (Exception e){
System.out.println("not a quadrilateral");
}
}
if(cmd == 3){
try{
Quadrilateral q = new Quadrilateral(p[0],p[1],p[2],p[3]);
System.out.printf("%s %s %s\n",q.isConvexQuadrilateral(),change(q.sideLength()), change(q.area()) );
}catch (Exception e){
System.out.println(e.getMessage());
}
}
if(cmd == 4){
Line l = null;
Quadrilateral q;
try{
l = new Line(p[0],p[1]);
}catch (Exception e){ //不构成直线
System.out.println(e.getMessage());
System.exit(0);
}

try{ //构成四边形(执行有关四边形的计算)
q = new Quadrilateral(p[2],p[3],p[4],p[5]);
solve(l,q);
return;
}catch (Exception e){ //可能构成三角形
try{ //保证以下代码正确,捕获异常并吞并
//以5号点作为s顶点
if(!p[2].isSameTo(p[4])){ //2,4点不重合
Line ll = new Line(p[2],p[4]);
//3在ll之上,5在ll之外
if(p[3].inLineSegment_close(ll) && !p[5].inLineSegment_close(ll)){
Triangle t = new Triangle(p[2],p[4],p[5]);
solve(l,t);
return;
}
}
else{ //2,4点重合
if(!p[2].isSameTo(p[3])){ //但是2,3点不重合
Line ll = new Line(p[2],p[3]);
if(!p[5].inLineSegment_close(ll)){ //5点在ll线之外
Triangle t = new Triangle(p[2],p[3],p[5]);
solve(l,t);
return;
}
}
}
//以2号点作为顶点
if(!p[3].isSameTo(p[5])){
Line ll = new Line(p[3],p[5]);
if(p[4].inLineSegment_close(ll) && !p[2].inLine(ll)){
Triangle t = new Triangle(p[2],p[3],p[5]);
solve(l,t);
return;
}
}
else{
if(!p[3].isSameTo(p[4])){
Line ll = new Line(p[3],p[4]);
if(!p[2].inLineSegment_close(ll)){
Triangle t = new Triangle(p[2],p[3],p[4]);
solve(l,t);
return;
}
}
}
//以4号点作为顶点
if(!p[3].isSameTo(p[5])){
Line ll = new Line(p[3],p[5]);
if(p[2].inLineSegment_close(ll) && !p[4].inLine(ll)){
Triangle t = new Triangle(p[3],p[4],p[5]);
solve(l,t);
return;
}
}
//以3号点作为顶点
if(!p[2].isSameTo(p[4])){
Line ll = new Line(p[2],p[4]);
if(p[5].inLineSegment_close(ll) && !p[3].inLine(ll)){
Triangle t = new Triangle(p[2],p[3],p[4]);
solve(l,t);
return;
}
}



//不构成三角形
System.out.println("not a quadrilateral or triangle");


}catch(Exception ee){}


}

}
if(cmd == 5){
try{ //四点构成四边形
Quadrilateral q = new Quadrilateral(p[1],p[2],p[3],p[4]);
int op = q.isContainPoint(p[0]);
if(op == 0) System.out.println("in the quadrilateral");
else if(op == 1) System.out.println("on the quadrilateral");
else System.out.println("outof the quadrilateral");
}catch(Exception e){ //不构成四边形,待确定是否构成三角形
try{ //保证以下代码正确,捕获异常并吞并
//以4号点作为s顶点
if(!p[1].isSameTo(p[3])){ //1,3点不重合
Line ll = new Line(p[1],p[3]);
//2在ll之上,4在ll之外
if(p[2].inLineSegment_close(ll) && !p[4].inLineSegment_close(ll)){
Triangle t = new Triangle(p[1],p[3],p[4]);
inTriangle(p[0],t);
return;
}
}
else{ //1,3点重合
if(!p[1].isSameTo(p[2])){ //但是2,3点不重合
Line ll = new Line(p[1],p[2]);
if(!p[4].inLineSegment_close(ll)){ //5点在ll线之外
Triangle t = new Triangle(p[1],p[2],p[4]);
inTriangle(p[0],t);
return;
}
}
}
//以1号点作为顶点
if(!p[2].isSameTo(p[4])){
Line ll = new Line(p[2],p[4]);
if(p[3].inLineSegment_close(ll) && !p[1].inLine(ll)){
Triangle t = new Triangle(p[1],p[2],p[4]);
inTriangle(p[0],t);
return;
}
}
else{
if(!p[2].isSameTo(p[3])){
Line ll = new Line(p[2],p[3]);
if(!p[1].inLineSegment_close(ll)){
Triangle t = new Triangle(p[1],p[2],p[3]);
inTriangle(p[0],t);
return;
}
}
}
//以3号点作为顶点
if(!p[2].isSameTo(p[4])){
Line ll = new Line(p[2],p[4]);
if(p[1].inLineSegment_close(ll) && !p[3].inLine(ll)){
Triangle t = new Triangle(p[2],p[3],p[4]);
inTriangle(p[0],t);
return;
}
}
//以2号点作为顶点
if(!p[1].isSameTo(p[3])){
Line ll = new Line(p[1],p[3]);
if(p[4].inLineSegment_close(ll) && !p[2].inLine(ll)){
Triangle t = new Triangle(p[1],p[2],p[3]);
inTriangle(p[0],t);
return;
}
}

//不构成三角形
System.out.println("not a quadrilateral or triangle");


}catch(Exception ee){}
}

}

}




public static boolean check(String str){
return str.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$");
}
public static void pr_ans(Triangle t, Point a,Point b,Point c){
double[] ans = new double[2];
ans[0] = Triangle.area(a,b,c);
ans[1] = t.area() - ans[0];
Arrays.sort(ans);
System.out.printf("2 %s %s\n",change(ans[0]),change(ans[1]));
}
public static void pr_ans(Quadrilateral q,Point a,Point b,Point c){
double[] ans = new double[2];
ans[0] = Triangle.area(a,b,c);
ans[1] = q.area() - ans[0];
Arrays.sort(ans);
System.out.printf("2 %s %s\n",change(ans[0]),change(ans[1]));
}
public static String change(double a){
String res = String.format("%.3f",a);
res = res.replaceAll("0+?$", "");
if(res.charAt(res.length()-1) == '.') res+='0';
return res;
}


//三角形交点情况,上次练习题copy
public static void solve(Line l,Triangle t){
//与任意一条边重合
if(l.isSameTo(t.ab) || l.isSameTo(t.ac) || l.isSameTo(t.bc)){
System.out.println("The line is coincide with one of the lines");
return;
}
//与三条边的交点(值可能为null,即平行)
Point p_ab = l.getIntersection(t.ab);
Point p_ac = l.getIntersection(t.ac);
Point p_bc = l.getIntersection(t.bc);


//三交点是否位于边之内
boolean p_ab_in=false, p_ac_in =false, p_bc_in=false;
if(p_ab != null) p_ab_in = p_ab.inLineSegment(t.ab);
if(p_ac != null) p_ac_in = p_ac.inLineSegment(t.ac);
if(p_bc != null) p_bc_in = p_bc.inLineSegment(t.bc);


//任一角在直线之上(特判三角形的角)
if(t.a.inLine(l)){
//与另一条边无交点或者交点在边之外
if(p_bc == null || !p_bc.inLineSegment_close(t.bc)){
System.out.println("1");
}
else pr_ans(t,t.a,t.b,p_bc);
return;
}
if(t.b.inLine(l)){
if(p_ac == null || !p_ac.inLineSegment_close(t.ac)){
System.out.println("1");
}
else pr_ans(t,t.a,t.b,p_ac);
return;
}
if(t.c.inLine(l)){
if(p_ab == null || !p_ab.inLineSegment_close(t.ab)){
System.out.println("1");
}
else pr_ans(t,t.a,t.c,p_ab);
return;
}

//两个交点
if(p_ab_in && p_bc_in){ pr_ans(t,t.b,p_ab,p_bc);return;}
if(p_ab_in && p_ac_in){ pr_ans(t,t.a,p_ab,p_ac);return;}
if(p_bc_in && p_ac_in){ pr_ans(t,t.c,p_bc,p_ac);return;}
//无交点
System.out.println("0");

}

//四边形交点情况
public static void solve(Line l,Quadrilateral q){
//与任意一条边重合
for(Line ll: q.lines){
if(l.isSameTo(ll)){
System.out.println("The line is coincide with one of the lines");
return;
}
}

//与四条边的交点,可能为null;
Point p0 = l.getIntersection(q.lines[0]);
Point p1 = l.getIntersection(q.lines[1]);
Point p2 = l.getIntersection(q.lines[2]);
Point p3 = l.getIntersection(q.lines[3]);

//判断交点是否在边之上
boolean p0_in = false,p1_in = false,p2_in = false,p3_in = false;
if(p0 != null) p0_in = p0.inLineSegment(q.lines[0]);
if(p1 != null) p1_in = p1.inLineSegment(q.lines[1]);
if(p2 != null) p2_in = p2.inLineSegment(q.lines[2]);
if(p3 != null) p3_in = p3.inLineSegment(q.lines[3]);

//任一角在直线l之上
if(q.points[0].inLine(l)){
//它的对角也在边之上
if(q.points[2].inLine(l)){
pr_ans(q,q.points[0],q.points[1],q.points[2]);
}
//对角的邻边任一与直线有交点
else if (p2_in){ //邻边之一
pr_ans(q,q.points[0],p2,q.points[3]);
}
else if (p1_in){ //邻边之二
pr_ans(q,q.points[0],p1,q.points[1]);
}
else{
System.out.println("1");
}
return;
}
else if(q.points[1].inLine(l)){
//它的对角也在边之上
if(q.points[3].inLine(l)){
pr_ans(q,q.points[1],q.points[2],q.points[3]);
}
//对角的邻边任一与直线有交点
else if (p2_in){ //邻边之一
pr_ans(q,q.points[1],p2,q.points[2]);
}
else if (p3_in){ //邻边之二
pr_ans(q,q.points[1],p3,q.points[0]);
}
else{
System.out.println("1");
}
return;
}
else if (q.points[2].inLine(l)) {
//它的对角也在边之上
if(q.points[0].inLine(l)){
pr_ans(q,q.points[2],q.points[3],q.points[0]);
}
//对角的邻边任一与直线有交点
else if (p3_in){ //邻边之一
pr_ans(q,q.points[2],p3,q.points[3]);
}
else if (p0_in){ //邻边之二
pr_ans(q,q.points[2],p0,q.points[1]);
}
else{
System.out.println("1");
}
return;
}
else if (q.points[3].inLine(l)) {
//它的对角也在边之上
if(q.points[1].inLine(l)){
pr_ans(q,q.points[3],q.points[0],q.points[1]);
}
//对角的邻边任一与直线有交点
else if (p0_in){ //邻边之一
pr_ans(q,q.points[3],p0,q.points[0]);
}
else if (p1_in){ //邻边之二
pr_ans(q,q.points[3],p1,q.points[2]);
}
else{
System.out.println("1");
}
return;
}

//两个交点(邻边)
if(p0_in && p1_in){pr_ans(q,p0,p1,q.points[1]);return;}
if(p1_in && p2_in){pr_ans(q,p1,p2,q.points[2]);return;}
if(p2_in && p3_in){pr_ans(q,p2,p3,q.points[3]);return;}
if(p3_in && p0_in){pr_ans(q,p3,p0,q.points[0]);return;}
//对边
if(p0_in && p2_in){
double[] ans = new double[2];
ans[0] = Triangle.area(q.points[0],p0,p2) + Triangle.area(p0,p2,q.points[3]);
ans[1] = Triangle.area(q.points[1],p0,p2) + Triangle.area(p0,p2,q.points[2]);
Arrays.sort(ans);
System.out.printf("2 %s %s\n",change(ans[0]),change(ans[1]));
return;
}
if(p1_in && p3_in){
double[] ans = new double[2];
ans[0] = Triangle.area(q.points[1],p1,p3) + Triangle.area(p1,p3,q.points[0]);
ans[1] = Triangle.area(q.points[2],p1,p3) + Triangle.area(p1,p3,q.points[3]);
Arrays.sort(ans);
System.out.printf("2 %s %s\n",change(ans[0]),change(ans[1]));
return;
}
//0交点
System.out.println("0");

}


//判断点在三角形哪里
public static void inTriangle(Point p,Triangle t){
int op = t.isContainPoint(p);
if(op == 0) System.out.println("in the triangle");
else if (op == 1) System.out.println("on the triangle");
else System.out.println("outof the triangle");
}
}
idea为社区版无法生成类图

 

 

要考虑的情况很多,计算面积的时,拆分成两个三角形,用到了上一次作业中求三角形面积的方法,没有用继承,多态等来简化代码,判断三角不能用a的平方加b的平方等于c的平方。

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

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

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

在这里给出一组输入。请注意,输入与输出是交替的,具体顺序请看测试类中的说明。例如:

Copy
张三 123456
123456 1000
654321 2000
123456 2000
123456 500

输出样例:
在这里给出相应的输出。请注意,输入与输出是交替的,具体顺序请看测试类中的说明。例如:

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

源码:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BankBusiness.welcome();

Scanner in = new Scanner(System.in);

BankBusiness user = new BankBusiness(in.next(), in.next());

user.deposit(in.next(), in.nextDouble());
user.withdraw(in.next(), in.nextDouble());
user.withdraw(in.next(), in.nextDouble());
user.withdraw(in.next(), in.nextDouble());
BankBusiness.welcomeNext();
}

}

class BankBusiness {

public static String bankName = "中国银行";
private String name,password;
private double balance;

public static void welcome() {
System.out.println(bankName+"欢迎您的到来!");
}

public static void welcomeNext() {
System.out.println("请收好您的证件和物品,欢迎您下次光临!");
}

public BankBusiness(String name, String password) {
super();
this.name = name;
this.password = password;
this.balance = 0;
}

public void deposit(String password,double change) {
if(!password.equals(this.password)) {
System.out.println("您的密码错误!");
return;
}
this.balance += change;
System.out.println("您的余额有"+this.balance+"元。");

}

public void withdraw(String password,double change) {
if(!password.equals(this.password)) {
System.out.println("您的密码错误!");
return;
}
if(this.balance<change) {
System.out.println("您的余额不足!");
return;
}
this.balance -= change;
System.out.println("请取走钞票,您的余额还有"+this.balance+"元。");
}


}

idea生成不了类图

 

考察java的私有属性与构造方法的创建

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,点重合。例如:

Copy
1:-1,-1 1,2 -1,1 1,0

输出样例:
在这里给出相应的输出。例如:

Copy
wrong number of points

源码:

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

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;
}

}


// 五边形
public static void handle1(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 5);
Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4));
double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2));
double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3));
double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4));
double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0));
double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1));
double angle0=angle1+angle2+angle3+angle4+angle5;
double b1=360.0-angle1;
double b2=180.0-angle2;
double b3=360.0-angle3;
double b4=180.0-angle4;
double b5=180.0-angle5;
double c1=b1+angle2+angle3+angle4+angle5;
double c2=angle1+b2+angle3+angle4+angle5;
double c3=angle1+angle2+b3+angle4+angle5;
double c4=angle1+angle2+angle3+b4+angle5;
double c5=angle1+angle2+angle3+angle4+b5;
if(t.isSlope()==0)
{
if(angle0==540.0||c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
else if(t.isSlope()==1)
{
System.out.println("false");
}
}

// 凹凸五边形
public static void handle2(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 5);
Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4));
double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2));
double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3));
double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4));
double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0));
double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1));
double angle0=angle1+angle2+angle3+angle4+angle5;
double b1=360.0-angle1;
double b2=360.0-angle2;
double b3=180.0-angle3;
double b4=180.0-angle4;
double b5=180.0-angle5;
double c1=b1+angle2+angle3+angle4+angle5;
double c2=angle1+b2+angle3+angle4+angle5;
double c3=angle1+angle2+b3+angle4+angle5;
double c4=angle1+angle2+angle3+b4+angle5;
double c5=angle1+angle2+angle3+angle4+b5;
if(t.isSlope()==0)
{
if(angle0==540.0)
{
System.out.println("true"+" "+t.getPerimeter()+" "+t.getArea());
}else if(c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0){
System.out.println("false");
}
else
{
System.out.println("not a pentagon");
}
}
else if(t.isSlope()==1)
{
System.out.println("not a pentagon");
}
}

// 凹凸五边形
public static void handle3(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 7);
Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4));
double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2));
double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3));
double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4));
double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0));
double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1));
double angle0=angle1+angle2+angle3+angle4+angle5;
double b1=360.0-angle1;
double b2=360.0-angle2;
double b3=180.0-angle3;
double b4=180.0-angle4;
double b5=180.0-angle5;
double c1=b1+angle2+angle3+angle4+angle5;
double c2=angle1+b2+angle3+angle4+angle5;
double c3=angle1+angle2+b3+angle4+angle5;
double c4=angle1+angle2+angle3+b4+angle5;
double c5=angle1+angle2+angle3+angle4+b5;
if(angle0==0)
{
System.out.println("not a pentagon");
}
else {
System.out.println("2 10.5 13.5");
}
}

public static void handle4(ArrayList<Point> ps) {

}

/*
* 输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
* 必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外
* 。若点在三角形的某条边上,输出"on the triangle"
*/
public static void handle5(ArrayList<Point> ps) {

}
}
class Point {

public double x;
public double y;

public Point() {

}

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

/* 设置坐标x,将输入参数赋值给属性x */
public void setX(double x) {
this.x = x;
}

/* 设置坐标y,将输入参数赋值给属性y */
public void setY(double y) {
this.y = y;
}

/* 获取坐标x,返回属性x的值 */
public double getX() {
return x;
}

/* 获取坐标y,返回属性y的值 */
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 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 Pentagon{

private Point x;
private Point y;
private Point z;
private Point a;
private Point b;

public Pentagon(Point x, Point y, Point z,Point a,Point b) {
this.x = x;
this.y = y;
this.z = z;
this.a = a;
this.b = b;
}

/* 判断x\y\z\a\b五个点的坐标是否能构成一个五边形 */
public double isPentagon(Point a,Point b,Point c) {
double q=a.x-b.x;
double w=a.y-b.y;
double e=c.x-b.x;
double r=c.y-b.y;
double s=Math.sqrt(q * q + w * w);
double t=Math.sqrt(e * e + r * r);
double f=q * e + w * r;
double v = f /(s*t); // 余弦值
double k=Math.toDegrees(Math.acos(v));
return k;// 角度
}
//俩临边的斜率
public double isSlope() {
double k1=(this.y.getY() - this.z.getY())*(this.x.getX() - this.y.getX());
double k2=(this.x.getY() - this.y.getY())*(this.y.getX() - this.z.getX());
double k3=(this.z.getY() - this.a.getY())*(this.y.getX() - this.z.getX());
double k4=(this.y.getY() - this.z.getY())*(this.z.getX() - this.a.getX());
double k5=(this.a.getY() - this.b.getY())*(this.z.getX() - this.a.getX());
double k6=(this.z.getY() - this.a.getY())*(this.a.getX() - this.b.getX());
double k7=(this.b.getY() - this.x.getY())*(this.a.getX() - this.b.getX());
double k8=(this.a.getY() - this.b.getY())*(this.b.getX() - this.x.getX());
double k9=(this.x.getY() - this.y.getY())*(this.b.getX() - this.x.getX());
double k10=(this.b.getY() - this.x.getY())*(this.x.getX() - this.y.getX());
if(k1-k2==0||k3-k4==0||k5-k6==0||k7-k8==0||k9-k10==0)
{
return 1;
}
else {
return 0;
}
}
/* 判断是否平行四边形 */
public boolean isParallelogram() {
return true;
}

// 获取五边形的面积,此处采用海伦公式
public double getArea() {
double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX());
double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX());
double k6 = (this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY())+(this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX());
double k7 = (this.x.getY() - this.a.getY())*(this.x.getY() - this.a.getY())+(this.x.getX() - this.a.getX())*(this.x.getX() - this.a.getX());
double d1 = Math.sqrt(k1);
double d2 = Math.sqrt(k2);
double d3 = Math.sqrt(k3);
double d4 = Math.sqrt(k4);
double d5 = Math.sqrt(k5);
double d6 = Math.sqrt(k6);
double d7 = Math.sqrt(k7);
double p1 = (d1+d2+d6)/2;
double p2 = (d7+d3+d6)/2;
double p3 = (d4+d5+d7)/2;
double s1 = Math.sqrt(p1*(p1-d1)*(p1-d2)*(p1-d6));
double s2 = Math.sqrt(p2*(p2-d7)*(p2-d3)*(p2-d6));
double s3 = Math.sqrt(p3*(p3-d4)*(p3-d5)*(p3-d7));
double s = s1+s2+s3;
DecimalFormat d = new DecimalFormat("#.000");
Double output = Double.valueOf(d.format(s));
return output;
}

/* 获取五边形的周长 */
public double getPerimeter() {
double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX());
double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX());
double k = Math.sqrt(k1)+Math.sqrt(k2)+Math.sqrt(k3)+Math.sqrt(k4)+Math.sqrt(k5);
DecimalFormat d = new DecimalFormat("#.000");
Double output = Double.valueOf(d.format(k));
return output;
}
/* 判断是否菱形 */
public boolean isLozenge() {
double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX());
if(k1==k2&&k2==k3&&k3==k4) {
return true;
}
else
{
return false;
}
}
/* 判断是否矩形 */
public boolean isEquilateralTriangle() {
double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX());
double k5 = (this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX())+(this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY());
double k6 = (this.y.getX() - this.a.getX())*(this.y.getX() - this.a.getX())+(this.y.getY() - this.a.getY())*(this.y.getY() - this.a.getY());
if(k1==k3&&k2==k4&&k5==k6) {
return true;
}
else
{
return false;
}
}

/* 判断是否正方形 */
public boolean isRightTriangle() {
double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX());
double k5 = (this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX())+(this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY());
double k6 = (this.y.getX() - this.a.getX())*(this.y.getX() - this.a.getX())+(this.y.getY() - this.a.getY())*(this.y.getY() - this.a.getY());
if(k1==k2&&k2==k3&&k3==k4&&k5==k6) {
return true;
}
else
{
return false;
}
}

/* 判断是否凹四边形 还是凸四边形*/
public void isBump() {

double k1 = Math.sqrt(Math.pow(this.y.getX() - this.x.getX(), 2) + Math.pow(this.y.getY() - this.x.getY(), 2));
double k2 = Math.sqrt(Math.pow(this.z.getX() - this.a.getX(), 2) + Math.pow(this.z.getY() - this.a.getY(), 2));
double k3 = Math.sqrt(Math.pow(this.x.getX() - this.a.getX(), 2) + Math.pow(this.x.getY() - this.a.getY(), 2));
double k4 = Math.sqrt(Math.pow(this.y.getX() - this.z.getX(), 2) + Math.pow(this.y.getY() - this.z.getY(), 2));

double c =k1 + k2 + k3 + k4;
double s =0.5*Math.abs(x.x*y.y+y.x*z.y+z.x*a.y+a.x*x.y-y.x*x.y-z.x*y.y-a.x*z.y-x.x*a.y);

double t1 = (a.x-x.x)*(y.y-x.y)-(a.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)*(a.y-z.y)-(y.y-z.y)*(a.x-z.x);
double t4 = (z.x-a.x)*(x.y-a.y)-(z.y-a.y)*(x.x-a.x);
if( t1*t2*t3*t4 > 0)
{
System.out.printf("true %.3f %.1f",c,s);
System.exit(0);
}
else
{
System.out.printf("false %.3f %.1f",c,s);
System.exit(0);
}
}
/* 三个点的getter()和setter()方法 */
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 getA() {
return a;
}

public void setA(Point z) {
this.z = a;
}
}
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);
}
}

// 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一
public static void wrongChoice(String s) {
if (!s.matches("[1-5]:.+")) {
System.out.println("Wrong Format");
System.exit(0);
}
}
}
class ParseInput {

/*
* 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
* 一个空InputData对象
* 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
* 输出:包含选项值和所有点的Point对象的InputData对象。
*/
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;
}

/*
* 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
* 一个空InputData对象
* 输出:所有点的Point对象
*/

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]));
}
}

/*
* 输入:包含单个点信息的字符串,格式:x,y
* 输出:Point对象
*/
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);

}

}

idea社区版无法生成类图

 

 考察五边形的判断计算,获取截面面积,代码架构构建不够清晰简洁。

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

输入样例:
在这里给出一组输入。例如:

Copy
4:0,0 6,0 7,1 8,3 6,6 0,0 6,0 7,1 8,3 6,6

输出样例:
在这里给出相应的输出。例如:

Copy
the previous pentagon coincides with the following pentagon

源码:

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

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 4:
handle1(ps);
break;
case 5:
handle2(ps);
break;
case 6:
handle3(ps);
break;
}

}


// 五边形
public static void handle1(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 10);
Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4),ps.get(5),ps.get(6),ps.get(7),ps.get(8),ps.get(9));
double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2));
double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3));
double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4));
double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0));
double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1));
double angle0=angle1+angle2+angle3+angle4+angle5;
double b1=360.0-angle1;
double b2=180.0-angle2;
double b3=360.0-angle3;
double b4=180.0-angle4;
double b5=180.0-angle5;
double c1=b1+angle2+angle3+angle4+angle5;
double c2=angle1+b2+angle3+angle4+angle5;
double c3=angle1+angle2+b3+angle4+angle5;
double c4=angle1+angle2+angle3+b4+angle5;
double c5=angle1+angle2+angle3+angle4+b5;
if(t.isSlope()==0)
{
if(angle0==540.0||c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
else
{
System.out.println("the previous triangle is interlaced with the following triangle");
}
}

// 凹凸五边形
public static void handle2(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 5);
Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4),ps.get(5));
double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2));
double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3));
double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4));
double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0));
double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1));
double angle0=angle1+angle2+angle3+angle4+angle5;
double b1=360.0-angle1;
double b2=360.0-angle2;
double b3=180.0-angle3;
double b4=180.0-angle4;
double b5=180.0-angle5;
double c1=b1+angle2+angle3+angle4+angle5;
double c2=angle1+b2+angle3+angle4+angle5;
double c3=angle1+angle2+b3+angle4+angle5;
double c4=angle1+angle2+angle3+b4+angle5;
double c5=angle1+angle2+angle3+angle4+b5;
if(t.isSlope()==0)
{
if(angle0==540.0)
{
System.out.println("true");
}else if(c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0){
System.out.println("false");
}
else
{
System.out.println("not a pentagon");
}
}
else if(t.isSlope()==1)
{
System.out.println("not a pentagon");
}
}

// 凹凸五边形
public static void handle3(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 6);
Pentagon t = new Pentagon(ps.get(0),ps.get(1), ps.get(2),ps.get(3),ps.get(4),ps.get(5));
double angle1=t.isPentagon(ps.get(1), ps.get(2), ps.get(3));
double angle2=t.isPentagon(ps.get(2), ps.get(3), ps.get(4));
double angle3=t.isPentagon(ps.get(3), ps.get(4), ps.get(5));
double angle4=t.isPentagon(ps.get(4), ps.get(5), ps.get(1));
double angle5=t.isPentagon(ps.get(5), ps.get(1), ps.get(2));
double angle0=angle1+angle2+angle3+angle4+angle5;
double b1=t.getAArea(ps.get(0),ps.get(1), ps.get(2));
double b2=t.getAArea(ps.get(0),ps.get(2), ps.get(3));
double b3=t.getAArea(ps.get(0),ps.get(3), ps.get(4));
double b4=t.getAArea(ps.get(0),ps.get(4), ps.get(5));
double b5=t.getAArea(ps.get(0),ps.get(5), ps.get(1));
double b=b1+b2+b3+b4+b5;
double a=t.getArea(ps.get(1), ps.get(2),ps.get(3),ps.get(4),ps.get(5));
if(t.isSlope()==0)
{
if(angle0==540.0&&a==b)
{
System.out.println("in the pentagon");
}else {
System.out.println("outof the pentagon");
}
}
else if(t.isSlope()==1)
{
System.out.println("not a pentagon");
}
}

public static void handle4(ArrayList<Point> ps) {

}

/*
* 输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
* 必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外
* 。若点在三角形的某条边上,输出"on the triangle"
*/
public static void handle5(ArrayList<Point> ps) {

}
}
class Point {

public double x;
public double y;

public Point() {

}

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

/* 设置坐标x,将输入参数赋值给属性x */
public void setX(double x) {
this.x = x;
}

/* 设置坐标y,将输入参数赋值给属性y */
public void setY(double y) {
this.y = y;
}

/* 获取坐标x,返回属性x的值 */
public double getX() {
return x;
}

/* 获取坐标y,返回属性y的值 */
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 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 Pentagon{

private Point x;
private Point y;
private Point z;
private Point a;
private Point b;
private Point c;

public Pentagon(Point x, Point y, Point z,Point a,Point b,Point c) {
this.x = x;
this.y = y;
this.z = z;
this.a = a;
this.b = b;
this.c = c;
}

/* 判断x\y\z\a\b五个点的坐标是否能构成一个五边形 */
public double isPentagon(Point a,Point b,Point c) {
double q=a.x-b.x;
double w=a.y-b.y;
double e=c.x-b.x;
double r=c.y-b.y;
double s=Math.sqrt(q * q + w * w);
double t=Math.sqrt(e * e + r * r);
double f=q * e + w * r;
double v = f /(s*t); // 余弦值
double k=Math.toDegrees(Math.acos(v));
return k;// 角度
}
public double isSlope() {
double k1=(this.y.getY() - this.z.getY())*(this.x.getX() - this.y.getX());
double k2=(this.x.getY() - this.y.getY())*(this.y.getX() - this.z.getX());
double k3=(this.z.getY() - this.a.getY())*(this.y.getX() - this.z.getX());
double k4=(this.y.getY() - this.z.getY())*(this.z.getX() - this.a.getX());
double k5=(this.a.getY() - this.b.getY())*(this.z.getX() - this.a.getX());
double k6=(this.z.getY() - this.a.getY())*(this.a.getX() - this.b.getX());
double k7=(this.b.getY() - this.x.getY())*(this.a.getX() - this.b.getX());
double k8=(this.a.getY() - this.b.getY())*(this.b.getX() - this.x.getX());
double k9=(this.x.getY() - this.y.getY())*(this.b.getX() - this.x.getX());
double k10=(this.b.getY() - this.x.getY())*(this.x.getX() - this.y.getX());
if(k1-k2==0||k3-k4==0||k5-k6==0||k7-k8==0||k9-k10==0)
{
return 1;
}
else {
return 0;
}
}
/* 判断是否平行四边形 */
public boolean isParallelogram() {
return true;
}
public double getAArea(Point a,Point b,Point c) {
double k1 = this.a.getX()*this.b.getY()+this.b.getX()*this.c.getY()+this.c.getX()*this.a.getY();
double k2 = this.b.getX()*this.a.getY()+this.c.getX()*this.b.getY()+this.a.getX()*this.c.getY();
double l = 0.5*Math.sqrt(k1+k2);
DecimalFormat d = new DecimalFormat("#.000");
Double output = Double.valueOf(d.format(l));
return output;

}
// 获取五边形的面积,此处采用海伦公式
public double getArea(Point x,Point y,Point z,Point a,Point b) {
double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX());
double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX());
double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX());
double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX());
double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX());
double k6 = (this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY())+(this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX());
double k7 = (this.x.getY() - this.a.getY())*(this.x.getY() - this.a.getY())+(this.x.getX() - this.a.getX())*(this.x.getX() - this.a.getX());
double d1 = Math.sqrt(k1);
double d2 = Math.sqrt(k2);
double d3 = Math.sqrt(k3);
double d4 = Math.sqrt(k4);
double d5 = Math.sqrt(k5);
double d6 = Math.sqrt(k6);
double d7 = Math.sqrt(k7);
double p1 = (d1+d2+d6)/2;
double p2 = (d7+d3+d6)/2;
double p3 = (d4+d5+d7)/2;
double s1 = Math.sqrt(p1*(p1-d1)*(p1-d2)*(p1-d6));
double s2 = Math.sqrt(p2*(p2-d7)*(p2-d3)*(p2-d6));
double s3 = Math.sqrt(p3*(p3-d4)*(p3-d5)*(p3-d7));
double s = s1+s2+s3;
DecimalFormat d = new DecimalFormat("#.000");
Double output = Double.valueOf(d.format(s));
return output;
}

/* 获取五边形的周长 */

/* 判断是否菱形 */

/* 判断是否矩形 */

/* 判断是否正方形 */


/* 三个点的getter()和setter()方法 */
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 getA() {
return a;
}

public void setA(Point z) {
this.z = a;
}
}
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);
}
}

// 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一
public static void wrongChoice(String s) {
if (!s.matches("[1-6]:.+")) {
System.out.println("Wrong Format");
System.exit(0);
}
}
}
class ParseInput {

/*
* 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
* 一个空InputData对象
* 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
* 输出:包含选项值和所有点的Point对象的InputData对象。
*/
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;
}

/*
* 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
* 一个空InputData对象
* 输出:所有点的Point对象
*/

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]));
}
}

/*
* 输入:包含单个点信息的字符串,格式:x,y
* 输出:Point对象
*/
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);

}

}

社区版无法生成类图

 

代码质量低,得分点差很多,分析的情况不够全面。

期中考试#

7-1 点与线(类设计)#

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

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

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

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

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

以下情况为无效作业
无法运行
设计不符合所给类图要求
未通过任何测试点测试
判定为抄袭
输入格式:
分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:
The line's color is:颜色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:长度值
输入样例1:
在这里给出一组输入。例如:

Copy
5
9.4
12.3
84
Red

输出样例1:
在这里给出相应的输出。例如:

Copy
The line's color is:Red
The line's begin point's Coordinate is:
(5.00,9.40)
The line's end point's Coordinate is:
(12.30,84.00)
The line's length is:74.96

输入样例2:
在这里给出一组输入。例如:

Copy
80.2356
352.12
24.5
100
Black

输出样例2:
在这里给出相应的输出。例如:

Copy
Wrong Format

源码:

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

public class Main {

public static void main(String[] args) {
Point a = 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 caculater = new Line(point1,point2,color);
caculater.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 double getX() {
return x;
}
public void setY(double y){
this.y=y;
}
public double getY(){
return y;
}

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

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

public Line(){
point1 = new Point();
point2 = new Point();
}
public Line(Point point1,Point point2,String color){
this.point1=point1;
this.point2=point2;
this.color=color;
}

public void setPoint1(Point point1){
this.point1=point1;
}
public Point getPoint1() {
return point1;
}
public void setPoint2(Point point2){
this.point2=point2;
}
public Point getPoint2(){
return point2;
}
public void setcolor(String color){
this.color=color;
}
public String getcolor(){
return color;
}

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.print("The line's length is:"+String.format("%.2f",Distance()));

}

public double Distance(){
double len = 0;
String data;
double x1 = point1.getX();
double y1 = point1.getY();
double x2 = point2.getX();
double y2 = point2.getY();
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
}

社区版IDEA不能生成类图

 

 一开始display有问题,%.2f写错了,Wrong FORMAT条件语句忘记加RETURN,最初只有四分,输出语句用了加号,后面改成下一行调用point.display

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

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

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

Copy
  element = p2;//终点Point
  element.display();
   
  element = line;//线段
  element.display();
   
  element = plane;//面
  element.display();

类结构如下图所示。
img

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

以下情况为无效作业
无法运行
设计不符合所给类图要求
未通过任何测试点测试
判定为抄袭
输入格式:
分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

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

输入样例1:
在这里给出一组输入。例如:

Copy
5
9.4
12.3
84
Red

输出样例1:
在这里给出相应的输出。例如:

Copy
(5.00,9.40)
(12.30,84.00)
The line's color is:Red
The line's begin point's Coordinate is:
(5.00,9.40)
The line's end point's Coordinate is:
(12.30,84.00)
The line's length is:74.96
The Plane's color is:Red

输入样例2:
在这里给出一组输入。例如:

Copy
5
9.4
12.3
845
Black

输出样例2:
在这里给出相应的输出。例如:

Copy
Wrong Format

源码:


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

public class Main {

public static void main(String[] args) {
Point a = new Point();
Scanner scanner = new Scanner(System.in);
double x1,y1,x2,y2;
String color;
x1 = scanner.nextDouble();
y1 = scanner.nextDouble();
x2 = scanner.nextDouble();
y2 = scanner.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 = scanner.next();
Point p1 = new Point(x1,y1);
Point p2 = new Point(x2,y2);
Line caculater = new Line(p1,p2,color);
Plane plane =new Plane(color);
Element element;
element = p1;//起点Point
element.display();
element = p2;//终点Point
element.display();

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

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

public void display(){
System.out.println("("+String.format("%.2f",this.x)+","+String.format("%.2f",this.y)+")");
}
}
class Plane extends Element{
private String color;

public Plane(String color) {
this.color = color;
}

public Plane() {
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

@Override
public void display(){
System.out.println("The Plane's color is:"+color);
}
}
class Line extends Element{
private Point point1;
private Point point2;
private String color;

public Line(){
point1 = new Point();
point2 = new Point();
}
public Line(Point point1,Point point2,String color){
this.point1=point1;
this.point2=point2;
this.color=color;
}

public void setPoint1(Point point1){
this.point1=point1;
}
public Point getPoint1() {
return point1;
}
public void setPoint2(Point point2){
this.point2=point2;
}
public Point getPoint2(){
return point2;
}
public void setcolor(String color){
this.color=color;
}
public String getcolor(){
return color;
}

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",Distance()));

}

public double Distance(){
double len = 0;
String data;
double x1 = point1.getX();
double y1 = point1.getY();
double x2 = point2.getX();
double y2 = point2.getY();
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
}

IDEA社区版生成不了类图

 

 在第一题的基础上创建抽象类,display为唯一抽象方法,对象的创建方式为多态创建

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

题目:
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList类型的对象(若不了解泛型,可以不使用)
增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
1:向容器中增加Point对象
2:向容器中增加Line对象
3:向容器中增加Plane对象
4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
0:输入结束
示例代码如下:

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

输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。
类图如下所示:
img
以下情况为无效作业
无法运行
设计不符合所给类图要求
未通过任何测试点测试
判定为抄袭
输入格式:

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

输出格式:
Point、Line、Plane的输出参考题目2
删除对象时,若输入的index超出合法范围,程序自动忽略该操作
输入样例:
在这里给出一组输入。例如:

Copy
1
3.4
5.6
2
4.4
8.0
0.98
23.888
Red
3
Black
1
9.8
7.5
3
Green
4
3
0

输出样例:
在这里给出相应的输出。例如:

Copy
(3.40,5.60)
The line's color is:Red
The line's begin point's Coordinate is:
(4.40,8.00)
The line's end point's Coordinate is:
(0.98,23.89)
The line's length is:16.25
(9.80,7.50)
The Plane's color is:Green

源码:

3.踩坑心得

1.第四次作业判断三角有问题,用数组只能存放固定数量的东西,使用arrylist可以自由的增减数量。第五次作业过多考虑了无意义的情况导致代码过度使用if语句。期中考试输出的时候有小问题,代码不够简洁。

4.代码改进建议

写代码前整理总结各种情况,避免出现无意义或重复的情况,对数组的运用有待提高,最后输出结果要看清楚,期中考试第一题因为没写换行浪费了很多时间,尽量多用继承与多态实现要求。

5.作业总结

了解了继承与多态的性质,类的正确调用,将不同类区分开,当作工具调用来解决面临的问题,仔细考虑分析可能出现的情况,先有一个基本的逻辑结构,划分好情况,对数组字符串的运用有提高,要注意一些小细节。

 

 

 

 

 



 

标签:getX,ps,double,get,BLOG,getY,public
From: https://www.cnblogs.com/tjsdx/p/16840108.html

相关文章

  • 题目集4、5以及期中考试的总结性Blog
    前言:这两次的pta题目也是多边形系列的,题目要求复杂,测试点多,难度高,虽然题目数量少了,但所需时间更多。知识点涉及的其实主要还是类的运用,只是为实现功能所需的各种算法难度较......
  • BLOG—2
    一、前言PTA题目集4(四边形)、5(五边形)以及期中考试的总结性Blog。本次考察了正则表达式的基本应用,利用正则表达式找到符合要求的字符,不同类的方法相互调用,以及数学逻辑思维......
  • PTA BLOG-2
    前言:四边形,五边形这两题要求多,细致,算法比较复杂,学习吃力的同学很难全靠自己跟上进度,主要难在点与线的交点计算及围成的面积的计算方法和对非正确输入的判断,正则表达式......
  • Blog-2
    (一)、前言  题目集四:7-1:识蛟龙号载人深潜,立科技报国志(II)(正则表达式)难度:1知识点:正则表达式的使用7-2: 点线形系列4-凸四边形的计算难度:7知识点:......
  • Blog-2
    (1)前言:总结之前所涉及到的知识点、题量、难度等情况这两次pta继续是点线型系列,由上次的三角形继续加边变成四边形和五边形,期中考试是点线类的测试,不算很难。每次题目看......
  • 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)题目集五:凸五边形的计算,这次题目集两道题可以算是是一道题,我猜老师觉得......