(1)前言:
通过不断深入学习java,最近的题目难度提升了很多,题量减少了但写作业的时间却增加了很多。在这段时间里,学习了很多如对象和类、对象交互、继承和多态,对象容器,抽象与接口以及用java实现链表功能和双向链表和正值表达式。
(2)设计与分析:
题目集06-1
背景简介:
“蛟龙号”载人深潜器是我国首台自主设计、自主集成研制的作业型深海载人潜水器,设计最大下潜深度为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:
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:
9165
6692
6990
6991
7050
7095
7099
类图:无 就一个main
大概思路:
方法一:用正值表达式String[] d = str.split("\\D")切割非数字存入字符串数组d里,再将字符串转化成数字并向加;
方法二:一个一个判断再存入数组(下方有代码);
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner in = new Scanner(System.in); double[] a = new double[20]; String str,str1=""; int i = 0; while(true) { str = in.nextLine(); if(str.equals("end")) break; for(int j = 0;j<str.length();j++) { if(str.charAt(j)>=48&&str.charAt(j)<=57) { str1=str1+str.charAt(j); } else { if(str1!="") a[i]=a[i]+Double.parseDouble(str1); str1=""; } if(j==str.length()-1&&!str1.isEmpty()) { a[i]=a[i]+Double.parseDouble(str1); str1=""; } } i++; } for(int j = 0;j<i;j++) System.out.println((int)a[j]); } }
题目集06-2
7-2 点线形系列4-凸四边形的计算
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,输出"not a quadrilateral or triangle"。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出"on the triangle或者on the quadrilateral"。若后四个点不符合四边形或三角形,输出"not a quadrilateral or triangle"。
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。
选项4中,若前两个输入线的点重合,输出"points coincide"。
输入样例1:
选项1,点重合。例如:
1:-1,-1 -1,-1 1,2 1,-2
输出样例:
在这里给出相应的输出。例如:
points coincide
类图:
![]()
大概思路:
先设计两个类,写关于判断的方法和计算面积的方法...... 再用这些方法去完成题目;
1.四边形的判断:任意两点不能重合,任意三点不能共线,对边(线段)不能相交。
2.平行四边形的判断:首先是一个四边形,对边平行。
3.菱形的判断:首先是一个平行四边形,对角线斜率之积为-1。
4.矩形的判断:首先是一个平行四边形,有一个角为直角。
5.正方形的判断:既是菱形又是矩形。
6.凹凸四边形的判断:如果对角线相交则是凸四边形,反之为凹四边形。
7.选项5可以用射线发和面积法判断点的位置。
import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner in = new Scanner(System.in); String str; str = in.nextLine(); int i = (int)str.charAt(0)-48; if(i<1||i>5||str.charAt(1)!=':') { System.out.println("Wrong Format"); System.exit(0); } str=str.substring(2); if((!str.matches("((-|[+])?((([1-9]\\d*)|[0])(\\.\\d+)?),(-|[+])?((([1-9]\\d*)|[0])(\\.\\d+)?))((\\s)(-|[+])?((([1-9]\\d*)|[0])(\\.\\d+)?),(-|[+])?((([1-9]\\d*)|[0])(\\.\\d+)?))*\\s?"))) { System.out.println("Wrong Format"); System.exit(0); } switch(i) { case 1:{ if(str.matches("((-|[+])?(([1-9]\\d*|[0])(\\.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(\\.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3]),Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7])); if(q.isCoincide()) { System.out.println("points coincide"); System.exit(0); } else System.out.println(q.isQuadrilateral()+" "+q.isParallelogram()); } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); System.exit(0); } break; } case 2:{ if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3]),Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7])); if(q.isCoincide()) System.out.println("points coincide"); else if(!q.isQuadrilateral()) System.out.println("not a quadrilateral"); else System.out.println(q.isDiamond()+" "+q.isRectangle()+" "+q.isSquare()); } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); System.exit(0); } break; } case 3:{ if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3]),Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7])); if(q.isCoincide()) { System.out.println("points coincide"); System.exit(0); }else if(!q.isQuadrilateral()) System.out.println("not a quadrilateral"); else { if(q.isBump()) System.out.println(q.isBump()+" "+q.format(q.circumference())+" "+q.format(q.Area1())); else System.out.println(q.isBump()+" "+q.format(q.circumference())+" "+q.format(q.Area2())); } } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); System.exit(0); } break; } case 4:{ if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){5}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7]),Float.parseFloat(a[8]),Float.parseFloat(a[9]),Float.parseFloat(a[10]),Float.parseFloat(a[11])); if(a[0].equals(a[2])&&a[1].equals(a[3])) { System.out.println("points coincide"); System.exit(0); }else if(!q.isComposition()) { System.out.println("not a quadrilateral or triangle"); System.exit(0); }else if(q.isECoincide(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3]))) { System.out.println("The line is coincide with one of the lines"); System.exit(0); } else if(q.isQuadrilateral()) { q.four(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3])); }else { q.tree(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3])); } } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){5}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); } break; } case 5:{//面积法 if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){4}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[2]),Float.parseFloat(a[3]),Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7]),Float.parseFloat(a[8]),Float.parseFloat(a[9])); if(!q.isComposition()) System.out.println("not a quadrilateral or triangle"); else if(q.isQuadrilateral()&&q.isDCoincide(Float.parseFloat(a[0]),Float.parseFloat(a[1]))) //四边形点在边上 System.out.println("on the quadrilateral"); else if(!q.isQuadrilateral()&&q.isDCoincide(Float.parseFloat(a[0]),Float.parseFloat(a[1])))//三角形点在边上 System.out.println("on the triangle"); else if(q.isQuadrilateral()) { if(q.Area3(Float.parseFloat(a[0]),Float.parseFloat(a[1]))) System.out.println("in the quadrilateral"); else System.out.println("outof the quadrilateral"); }else { if(q.Area3(Float.parseFloat(a[0]),Float.parseFloat(a[1]))) System.out.println("in the triangle"); else System.out.println("outof the triangle"); } } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){4}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); System.exit(0); } } break; } } } class Quadrilateral{ private float x1,y1,x2,y2,x3,y3,x4,y4; public Quadrilateral() { super(); // TODO 自动生成的构造函数存根 } public Quadrilateral(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { super(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.x3 = x3; this.y3 = y3; this.x4 = x4; this.y4 = y4; } public boolean isCoincide(){//是否重合 if((x1==x2&&y1==y2)||(x2==x3&&y2==y3)||(x3==x4&&y3==y4)||(x4==x1&&y4==y1)) return true; return false; } public boolean isQuadrilateral() {//是否是四边形 Line a1 = new Line(x2,y2,x3,y3,x1,y1,x4,y4); Line a2 = new Line(x4,y4,x3,y3,x1,y1,x2,y2); if((x1==x3&&y1==y3)||(x2==x4&&y2==y4)||(x1==x2&&y1==y2)||(x4==x3&&y4==y3)||a1.isDot3()||a2.isDot3()) return false; else if((x1==x2&&x1==x3)||(x1==x2&&x1==x4)||(x1==x3&&x1==x4)||(x3==x2&&x3==x4)) return false; else if(Math.abs((y1-y2)/(x1-x2)-(y1-y3)/(x1-x3))<0.01||Math.abs((y1-y2)/(x1-x2)-(y1-y4)/(x1-x4))<0.01||Math.abs((y1-y4)/(x1-x4)-(y1-y3)/(x1-x3))<0.01||Math.abs((y3-y2)/(x3-x2)-(y2-y4)/(x2-x4))<0.01) return false; else return true; } public boolean isParallelogram() {//是否是平行四边形 if(isQuadrilateral()) { if((x1==x2&&x3==x4||(y1-y2)/(x1-x2)==(y4-y3)/(x4-x3))&&(x3==x2&&x1==x4||(y3-y2)/(x3-x2)==(y4-y1)/(x4-x1))) return true; else return false; } else return false; } public boolean isDiamond() {//是否是菱形 if(isParallelogram()) { if(((y1==y3&&x2==x4)||(y2==y4&&x1==x3))||(((y1-y3)/(x1-x3))*((y4-y2)/(x4-x2))==-1)) return true; else return false; } else return false; } public boolean isRectangle() {//是否是矩形 if(isParallelogram()) { if((x1==x2&&y2==y3)||(y1==y2&&x2==x3)||((y1-y2)/(x1-x2))*((y3-y2)/(x3-x2))==-1) return true; else return false; } else return false; } public boolean isSquare() {//是否是正方形 if(isRectangle()&&isDiamond()) return true; else return false; } public boolean isBump() {//凹为false 凸为true double x = -((x2-x4)*(x3*y1-x1*y3)-(x1-x3)*(x4*y2-x2*y4))/((y2-y4)*(x1-x3)-(y1-y3)*(x2-x4)); double y = -((y2-y4)*(y3*x1-y1*x3)-(y1-y3)*(y4*x2-y2*x4))/((x2-x4)*(y1-y3)-(x1-x3)*(y2-y4)); if ((((x>=x3&&x<=x1)||(x<=x3&&x>=x1))&&((y>=y3&&y<=y1)||(y<=y3&&y>=y1)))&&(((x>=x2&&x<=x4)||(x<=x2&&x>=x4))&&((y>=y2&&y<=y4)||(y<=y2&&y>=y4)))) return true; else return false; } public boolean isComposition() {//是否为三角形和四边形 if(isQuadrilateral()) return true; if((x1==x2&&y1==y2&&x3==x2&&y3==y2)||(x1==x2&&y1==y2&&x4==x2&&y4==y2)||(x1==x4&&y1==y4&&x3==x4&&y3==y4)||(x4==x2&&y4==y2&&x3==x4&&y3==y4)) return false; if((x1==x2&&x2==x3&&x3==x4)||(y1-y3)/(x1-x3)==(y2-y4)/(x2-x4)) return false; if((x1==x2&&y1==y2)||(x4==x2&&y4==y2)||(x3==x4&&y3==y4)||(x4==x1&&y4==y1)) return true; if((x1==x2&&x2==x3||Math.abs((y1-y2)/(x1-x2)-(y2-y3)/(x2-x3))<0.01)&&((x1>=x2&&x2>=x3)||x3>=x2&&x2>=x1)&&((y1>=y2&&y2>=y3)||y3>=y2&&y2>=y1)) return true; if((x1==x2&&x2==x4||Math.abs((y1-y2)/(x1-x2)-(y2-y4)/(x2-x4))<0.01)&&((x4>=x1&&x1>=x2)||x1>=x2&&x2>=x4)&&((y4>=y1&&y1>=y2)||y2>=y1&&y1>=y4)) return true; if((x4==x2&&x2==x3||Math.abs((y4-y2)/(x4-x2)-(y2-y3)/(x2-x3))<0.01)&&((x4>=x3&&x3>=x2)||x2>=x3&&x3>=x4)&&((y4>=y3&&y3>=y2)||y2>=y3&&y3>=y4)) return true; if((x1==x4&&x4==x3||Math.abs((y1-y4)/(x1-x4)-(y4-y3)/(x4-x3))<0.01)&&((x1>=x4&&x4>=x3)||x3>=x4&&x4>=x1)&&((y1>=y4&&y4>=y3)||y3>=y4&&y4>=y1)) return true; return false; } public boolean isECoincide(float x5,float y5,float x6,float y6) {//是否边重合 if((x1==x2&&x5==x6&&x2==x5&&y1!=y2)||(x3==x2&&x5==x6&&x2==x5&&y3!=y2)||(x3==x4&&x5==x6&&x4==x5&&y3!=y4)||(x1==x4&&x5==x6&&x4==x5&&y1!=y4)) return true; Line a1 = new Line(x5,y5,x6,y6,x1,y1,x2,y2); Line a2 = new Line(x5,y5,x6,y6,x2,y2,x3,y3); Line a3 = new Line(x5,y5,x6,y6,x3,y3,x4,y4); Line a4 = new Line(x5,y5,x6,y6,x4,y4,x1,y1); if( a1.isCoincide()||a2.isCoincide()||a3.isCoincide()||a4.isCoincide() ) return true; return false; } public boolean isDCoincide(float x5,float y5) {//是否三点共线 if((x1==x2&&x5==x1&&((x5>=x1&&x5<=x2)||(x5<=x1&&x5>=x2))&&((y5>=y1&&y5<=y2)||(y5<=y1&&y5>=y2)))||(x3==x2&&x5==x2&&((x5>=x3&&x5<=x2)||(x5<=x3&&x5>=x2))&&((y5>=y3&&y5<=y2)||(y5<=y3&&y5>=y2)))||(x3==x4&&x5==x3&&((x5>=x3&&x5<=x4)||(x5<=x3&&x5>=x4))&&((y5>=y3&&y5<=y4)||(y5<=y3&&y5>=y4)))||(x1==x4&&x5==x4&&((x5>=x1&&x5<=x4)||(x5<=x1&&x5>=x4))&&((y5>=y1&&y5<=y4)||(y5<=y1&&y5>=y4)))) return true; if((x1!=x2&&y1!=y2&&(y1-y2)/(x1-x2)==(y5-y1)/(x5-x1)&&((x5>=x1&&x5<=x2)||(x5<=x1&&x5>=x2))&&((y5>=y1&&y5<=y2)||(y5<=y1&&y5>=y2)))||(x3!=x2&&y3!=y2&&(y3-y2)/(x3-x2)==(y5-y2)/(x5-x2)&&((x5>=x3&&x5<=x2)||(x5<=x3&&x5>=x2))&&((y5>=y3&&y5<=y2)||(y5<=y3&&y5>=y2)))||(x3!=x4&&y3!=y4&&(y3-y4)/(x3-x4)==(y5-y3)/(x5-x3)&&((x5>=x3&&x5<=x4)||(x5<=x3&&x5>=x4))&&((y5>=y3&&y5<=y4)||(y5<=y3&&y5>=y4)))||(x1!=x4&&y1!=y4&&(y1-y4)/(x1-x4)==(y5-y4)/(x5-x4)&&((x5>=x1&&x5<=x4)||(x5<=x1&&x5>=x4))&&((y5>=y1&&y5<=y4)||(y5<=y1&&y5>=y4)))) return true; return false; } public void tree(float x5,float y5,float x6,float y6) { if((x1==x2&&y1==y2)) { x2=x4; y2=y4; } if((x1==x3&&y1==y3)||(x2==x3&&y2==y3)) { x3=x4; y3=y4; } if((x1==x2&&x2==x3||(y1-y2)/(x1-x2)==(y2-y3)/(x2-x3))&&((x1>=x2&&x2>=x3)||x3>=x2&&x2>=x1)&&((y1>=y2&&y2>=y3)||y3>=y2&&y2>=y1)) { x2=x4; y2=y4; } if((x1==x2&&x2==x4||(y1-y2)/(x1-x2)==(y2-y4)/(x2-x4))&&((x4>=x1&&x1>=x2)||x1>=x2&&x2>=x4)&&((y4>=y1&&y1>=y2)||y2>=y1&&y1>=y4)) { x1=x4; y1=y4; } if((x4==x2&&x2==x3||(y4-y2)/(x4-x2)==(y2-y3)/(x2-x3))&&((x4>=x3&&x3>=x2)||x2>=x3&&x3>=x4)&&((y4>=y3&&y3>=y2)||y2>=y3&&y3>=y4)) { x3=x4; y3=y4; } Line a1 = new Line(x5,y5,x6,y6,x1,y1,x2,y2); Line a2 = new Line(x5,y5,x6,y6,x2,y2,x3,y3); Line a3 = new Line(x5,y5,x6,y6,x3,y3,x1,y1); double s1,s2; if(a1.isCoincide()||a2.isCoincide()||a3.isCoincide()) System.out.println("The point is on the edge of the triangle"); else if((a1.getxValue()==a2.getxValue()&&a1.getyValue()==a2.getyValue()&&!a3.isDot())||(a1.getxValue()==a3.getxValue()&&a1.getyValue()==a3.getyValue()&&!a2.isDot())||(a3.getxValue()==a2.getxValue()&&a3.getyValue()==a2.getyValue()&&!a1.isDot())) System.out.println(1); else if(!a1.isDot()&&!a2.isDot()&&!a3.isDot()) System.out.println(0); else { if(a1.isDot()&&a2.isDot()&&!a3.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x2,k3 = y2; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; }else if(a1.isDot()&&a3.isDot()&&!a2.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x1,k3 = y1; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; }else if(a2.isDot()&&a3.isDot()&&!a1.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x3,k3 = y3; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; } else { if(a1.getxValue()==a2.getxValue()&&a1.getyValue()==a2.getyValue()&&a3.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a3.getxValue(); float k2 = a3.getyValue(); float g3 = x1,k3 = y1; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; } else if(a3.getxValue()==a2.getxValue()&&a3.getyValue()==a2.getyValue()&&a1.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x2,k3 = y2; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; }else { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x3,k3 = y3; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; } if(s1<=s2) System.out.println("2 " + new DecimalFormat("0.0##").format(s1) + " " + new DecimalFormat("0.0##").format(s2)); else System.out.println("2 " + new DecimalFormat("0.0##").format(s2) + " " + new DecimalFormat("0.0##").format(s1)); } } } public void four(float x5,float y5,float x6,float y6) {// 直线与四边形 Line a1 = new Line(x5,y5,x6,y6,x1,y1,x2,y2); Line a2 = new Line(x5,y5,x6,y6,x2,y2,x3,y3); Line a3 = new Line(x5,y5,x6,y6,x3,y3,x4,y4); Line a4 = new Line(x5,y5,x6,y6,x4,y4,x1,y1); double s1=0,u=0; if(!a1.isDot()&&!a2.isDot()&&!a3.isDot()&&!a4.isDot()) System.out.println(0); else if((a1.getxValue()==a2.getxValue()&&a1.getyValue()==a2.getyValue()&&!a3.isDot()&&!a4.isDot())||(a2.getxValue()==a3.getxValue()&&a2.getyValue()==a3.getyValue()&&!a1.isDot()&&!a4.isDot()) ||(a3.getxValue()==a4.getxValue()&&a3.getyValue()==a4.getyValue()&&!a1.isDot()&&!a4.isDot())||(a1.getxValue()==a4.getxValue()&&a1.getyValue()==a4.getyValue()&&!a2.isDot()&&!a3.isDot())) { System.out.println(1); }else { if(a3.isDot()&&a1.isDot()) { s1 =0.5 * Math.abs(x1 * a1.getyValue() + x4 * y1 + a1.getxValue() * y4 - x1 * y4 - x4 * a1.getyValue() -a1.getxValue() * y1) + 0.5 * Math.abs(x4 * a1.getyValue() + a3.getxValue() * y4 + a1.getxValue() * a3.getyValue() - x4 * a3.getyValue() - a3.getxValue() * a1.getyValue() -a1.getxValue() * y4); u=1; } if(u==1) { if(s1<Area1()-s1) System.out.println("2 "+format(s1)+" "+format(Area1()-s1)); else System.out.println("2 "+format(Area1()-s1)+" "+format(s1)); } } } public String format(double a) {//格式化 return new DecimalFormat("0.0##").format(a); } public double Area(float x1, float y1, float x2, float y2, float x3, float y3) {//三角形面积 return 0.5 * Math.abs(x1 * y3 + x2 * y1 + x3 * y2 - x1 * y2 - x2 * y3 -x3 * y1); } public double Area1() {//凸形面积 return 0.5*Math.abs((x3-x1)*(y4-y2)-(y3-y1)*(x4-x2)); } public double Area2() {//凹形面积 return 0.5*Math.abs((x3-x1)*(y4-y2)-(y3-y1)*(x4-x2)); } public boolean Area3(float x5,float y5) {//面积是否相等 面积法 double a = 0.5 * Math.abs(x1 * y3 + x2 * y1 + x3 * y2 - x1 * y2 - x2 * y3 -x3 * y1) + 0.5 * Math.abs(x1 * y3 + x4 * y1 + x3 * y4 - x1 * y4 - x4 * y3 -x3 * y1); double b = 0.5 * Math.abs(x1 * y5 + x2 * y1 + x5 * y2 - x1 * y2 - x2 * y5 -x5 * y1) + 0.5 * Math.abs(x2 * y3 + x5 * y2 + x3 * y5 - x2 * y5 - x5 * y3 -x3 * y2) + 0.5 * Math.abs(x4 * y3 + x5 * y4 + x3 * y5 - x4 * y5 - x5 * y3 -x3 * y4) + 0.5 * Math.abs(x1 * y4 + x5 * y1 + x4 * y5 - x1 * y5 - x5 * y4 -x4 * y1); if(Math.abs(a-b)<0.01) return true; return false; } public double circumference() {//四边形周长 return Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2),0.5) + Math.pow((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2),0.5) + Math.pow((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4),0.5) +Math.pow((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4),0.5); } } class Line{ private float x1,x2,x3,x4,y1,y2,y3,y4; public Line(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4) { super(); this.x1 = x1; this.x2 = x2; this.x3 = x3; this.x4 = x4; this.y1 = y1; this.y2 = y2; this.y3 = y3; this.y4 = y4; } public Line() { super(); // TODO 自动生成的构造函数存根 } boolean isDot() { double x = -((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4)); double y = -((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4)); if(Math.abs((y1-y2)/(x1-x2)-(y3-y4)/(x3-x4))<0.01) return false; else if (((x>=x3&&x<=x4)||(x<=x3&&x>=x4))&&((y>=y3&&y<=y4)||(y<=y3&&y>=y4))) return true; else return false; } boolean isDot3() { double x = -((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4)); double y = -((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4)); if(Math.abs((y1-y2)/(x1-x2)-(y3-y4)/(x3-x4))<0.01) return false; else if (((x>=x3&&x<=x4)||(x<=x3&&x>=x4))&&((y>=y3&&y<=y4)||(y<=y3&&y>=y4))&&((x>=x1&&x<=x2)||(x<=x1&&x>=x2))&&((y>=y1&&y<=y2)||(y<=y1&&y>=y2))) return true; else return false; } boolean isCoincide(){ if(Math.abs((y4-y3)*x1+(x3-x4)*y1+x4*y3-y4*x3)/Math.sqrt((y4-y3)*(y4-y3)+(x4-x3)*(x4-x3))<0.01&&(Math.abs((y1-y2)/(x1-x2)-(y3-y4)/(x3-x4))<0.01||((x1==x2)&&(x3==x4)))) return true; else return false; } float getxValue(){ return -((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4)); } float getyValue(){ return -((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4)); } }
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()方法。
输入格式:
输入开户需要的姓名、密码
输入正确密码、存款金额
输入错误密码、取款金额
输入正确密码、大于余额的取款金额
输入正确密码、小于余额的取款金额
输出格式:
中国银行(银行名称)欢迎您的到来!
您的余额有多少元。
您的密码错误!
您的余额不足!
请取走钞票,您的余额还有多少元。
请收好您的证件和物品,欢迎您下次光临!
类图:

思路:按照题目写出相关类和方法 用String[] d = str.split("\\s+")切割输入。该题简单。
import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner in = new Scanner(System.in); String str; str = in.nextLine(); String[] c = str.split("\\s+"); Bank1 a = new Bank1(c[0], c[1]); a.welcome(); str = in.nextLine(); String[] d = str.split("\\s+"); a.deposit(d[0],Double.parseDouble(d[1])); str = in.nextLine(); String[]e = str.split("\\s+"); a.withdraw(e[0],Double.parseDouble(e[1])); str = in.nextLine(); String[]e1 = str.split("\\s+"); a.withdraw(e1[0],Double.parseDouble(e1[1])); str = in.nextLine(); String[]e2 = str.split("\\s+"); a.withdraw(e2[0],Double.parseDouble(e2[1])); a.welcomeNext(); } } class Bank1 { private String cardNumber, cardNumber1; private String bankName; private String name; private String password; private double balance; public Bank1(String name, String password) { super(); this.bankName = "中国银行"; this.name = name; this.password = password; this.balance = 0; } public Bank1() { super(); // TODO 自动生成的构造函数存根 } public String getCardNumber1() { return cardNumber1; } public void setCardNumber1(String cardNumber1) { this.cardNumber1 = cardNumber1; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } public String getCardNumber() { return cardNumber; } public void setCardNumber(String cardNumber) { this.cardNumber = cardNumber; } public String getKey() { return password; } public void setKey(String password) { this.password = password; } public double getMoney() { return balance; } public void setMoney(double balance) { this.balance = balance; } public boolean isEquil(String str) { if (str.equals(cardNumber) || str.equals(cardNumber1)) return true; return false; } public boolean isKey(String str) { if (str.equals(password)) return true; return false; } public boolean isatm(String str) { if (bankName.equals("中国建设银行")) { if (str.equals("01") || str.equals("02") || str.equals("03") || str.equals("04")) return true; else return false; } else if (str.equals("05") || str.equals("06")) return true; else return false; } public boolean isatm1(String str) { if (str.equals("01") || str.equals("02") || str.equals("03") || str.equals("04") || str.equals("05") || str.equals("06")) return true; else return false; } public void welcome() { System.out.println("中国银行欢迎您的到来!"); } public void welcomeNext() { System.out.println("请收好您的证件和物品,欢迎您下次光临!"); } public void deposit(String key, double i) { if (key.equals(password)) { balance = balance + i; System.out.println("您的余额有" + balance + "元。"); } else { System.out.println("您的密码错误!"); } } public void withdraw(String key, double i) { if (Double.parseDouble(key)==Double.parseDouble(password) ){ if(balance>i) { balance = balance - i; System.out.println("请取走钞票,您的余额还有" + balance + "元。"); } else System.out.println("您的余额不足!"); } else { System.out.println("您的密码错误!"); } } }
期中考试(OOP)
期中考试(OOP)
-
设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,
-
除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:
(x,y),数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]。若输入有误,系统则直接输出Wrong Format -
设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,
-
同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:
``` The line's color is:颜色值 The line's begin point's Coordinate is: (x1,y1) The line's end point's Coordinate is: (x2,y2) The line's length is:长度值 ```其中,所有数值均保留两位小数,建议可用
String.format("%.2f", data)方法。设计类图如下图所示。

** 题目要求:在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的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:长度值
根据所给类图写出相关类并完成题目要求
其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner in = new Scanner(System.in); String[] str = new String[5]; for(int i = 0;i<5;i++) { str[i] = in.next(); } for(int i = 0;i<4;i++) { if(Double.parseDouble(str[i])<=0||Double.parseDouble(str[i])>200) { System.out.println("Wrong Format"); System.exit(0); } } Point point1 = new Point(Double.parseDouble(str[0]),Double.parseDouble(str[1])); Point point2 = new Point(Double.parseDouble(str[2]),Double.parseDouble(str[3])); Line line = new Line(point1,point2,str[4]); line.display(); } } class Point{ private double x, y; public Point() { super(); // TODO 自动生成的构造函数存根 } public Point(double x, double y) { super(); this.x = x; this.y = y; } public void display() { System.out.println("("+String.format("%.2f", x)+","+String.format("%.2f", y)+")"); } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } } class Line{ private Point point1,point2; private String color; public Line() { super(); // TODO 自动生成的构造函数存根 } public Line(Point point1, Point point2, String color) { super(); this.point1 = point1; this.point2 = point2; this.color = 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.pow( Math.pow((point1.getX()-point2.getX()), 2) + Math.pow((point1.getY()-point2.getY()), 2), 0.5); } public void display() { System.out.println("The line's color is:"+color); System.out.println("The line's begin point's Coordinate is:"); point1.display(); System.out.println("The line's end point's Coordinate is:"); point2.display(); System.out.println("The line's length is:"+String.format("%.2f", getDistance())); } }
在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。
- 对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
- 再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:
The Plane's color is:颜色 - 在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),
- 然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:
element = p1;//起点Point element.display(); element = p2;//终点Point element.display(); element = line;//线段 element.display(); element = plane;//面 element.display();类结构如下图所示。

其中,所有数值均保留两位小数,建议可用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:颜色值
根据所给类图写出相关类并完成题目要求在题目一上迭代。
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner in = new Scanner(System.in); String[] str = new String[5]; for(int i = 0;i<5;i++) { str[i] = in.next(); } for(int i = 0;i<4;i++) { if(Double.parseDouble(str[i])<=0||Double.parseDouble(str[i])>200) { System.out.println("Wrong Format"); System.exit(0); } } Point point1 = new Point(Double.parseDouble(str[0]),Double.parseDouble(str[1])); Point point2 = new Point(Double.parseDouble(str[2]),Double.parseDouble(str[3])); Line line = new Line(point1,point2,str[4]); Plane plane = new Plane(str[4]); Element element; element = point1;//起点Point element.display(); element = point2;//终点Point element.display(); element = line;//线段 element.display(); element = plane;//面 element.display(); } } class Point extends Element{ private double x, y; public Point() { super(); // TODO 自动生成的构造函数存根 } public Point(double x, double y) { super(); this.x = x; this.y = y; } @Override public void display() { System.out.println("("+String.format("%.2f", x)+","+String.format("%.2f", y)+")"); } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } } class Line extends Element{ private Point point1,point2; private String color; public Line() { super(); // TODO 自动生成的构造函数存根 } public Line(Point point1, Point point2, String color) { super(); this.point1 = point1; this.point2 = point2; this.color = 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.pow( (point1.getX()-point2.getX())*(point1.getX()-point2.getX()) +(point1.getY()-point2.getY())*(point1.getY()-point2.getY()), 0.5); } @Override public void display() { System.out.println("The line's color is:"+color); System.out.println("The line's begin point's Coordinate is:"); point1.display(); System.out.println("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{ private String color; public Plane() { super(); // TODO 自动生成的构造函数存根 } public Plane(String color) { super(); this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public void display() { // TODO 自动生成的方法存根 System.out.println("The Plane's color is:" + color); } } abstract class Element{ public abstract void display(); }
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
- 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为
ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>) - 增加该类的
add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象 - 在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
- 1:向容器中增加Point对象
- 2:向容器中增加Line对象
- 3:向容器中增加Plane对象
- 4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
- 0:输入结束
choice = input.nextInt(); while(choice != 0) { switch(choice) { case 1://insert Point object into list ... break; case 2://insert Line object into list ... break; case 3://insert Plane object into list ... break; case 4://delete index - 1 object from list int index = input.nextInt(); ... } choice = input.nextInt(); }输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。
类图如下所示:

- 以下情况为无效作业
- 无法运行
- 设计不符合所给类图要求
- 未通过任何测试点测试
- 判定为抄袭
输入格式:
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超出合法范围,程序自动忽略该操作
增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象,
注意remove(int index)方法传入的index是否合法以及删除的是第index - 1
输出时这样输出
for(Element e : obj.getList() )
{
e.display();
}
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner in = new Scanner(System.in); String str1; double x1,y1,x2,y2; GeometryObject obj = new GeometryObject(); int choice = in.nextInt(); while(choice!=0) { switch(choice) { case 1://insert Point object into list x1 = Double.parseDouble(in.next()); y1 = Double.parseDouble(in.next()); if(x1<=0||x1>200||y1<=0||y1>200) { System.out.println("Wrong Format"); System.exit(0); } Point point1 = new Point(x1,y1); obj.add(point1); break; case 2://insert Line object into list x1 = Double.parseDouble(in.next()); y1 = Double.parseDouble(in.next()); x2 = Double.parseDouble(in.next()); y2 = Double.parseDouble(in.next()); str1 = in.next(); Point point2 = new Point(x1,y1); Point point3 = new Point(x2,y2); Line line = new Line(point2,point3,str1); obj.add(line); break; case 3://insert Plane object into list str1 = in.next(); Plane plane = new Plane(str1); obj.add(plane); break; case 4://delete index - 1 object from list int i = in.nextInt(); obj.remove(i); break; } choice = in.nextInt(); } for(Element e : obj.getList() ) { e.display(); } } } class Point extends Element{ private double x, y; public Point() { super(); // TODO 自动生成的构造函数存根 } public Point(double x, double y) { super(); this.x = x; this.y = y; } @Override public void display() { System.out.println("("+String.format("%.2f", x)+","+String.format("%.2f", y)+")"); } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } } class Line extends Element{ private Point point1,point2; private String color; public Line() { super(); // TODO 自动生成的构造函数存根 } public Line(Point point1, Point point2, String color) { super(); this.point1 = point1; this.point2 = point2; this.color = 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.pow( (point1.getX()-point2.getX())*(point1.getX()-point2.getX()) +(point1.getY()-point2.getY())*(point1.getY()-point2.getY()), 0.5); } @Override public void display() { System.out.println("The line's color is:"+color); System.out.println("The line's begin point's Coordinate is:"); point1.display(); System.out.println("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{ private String color; public Plane() { super(); // TODO 自动生成的构造函数存根 } public Plane(String color) { super(); this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public void display() { // TODO 自动生成的方法存根 System.out.println("The Plane's color is:" + color); } } class GeometryObject{ private ArrayList<Element> list = new ArrayList<Element>(); public GeometryObject() { super(); // TODO 自动生成的构造函数存根 } public void add(Element element) { list.add(element); } public void remove(int index) { if(index>0&&index<=list.size()) list.remove(index-1); } public ArrayList<Element> getList() { return list; } } abstract class Element{ public abstract void display(); }
双向链表
在单链表基础上改写代码,实现双向链表数据结构:
public class Node<E> {
private E data;//数据域,类型为泛型E
private Node<E> next;//后继引用(指针)
private Node<E> previous;//前驱引用(指针)
}
public interface DoubleLinkedListImpl<E> {
/**
* 判断链表是否为空
* @return
*/
public boolean isEmpty();
/**
* 获取当前链表节点数量
* @return 节点数
*/
public int getSize();
/**
* 获取链表中第index个位置的节点的data值
* @param index:节点在链表中的位置
* @return:返回该节点的data值
*/
public E getData(int index);
/**
* 删除链表最后一个节点
*/
public void remove();
/**
*删除链表中第index位置的节点
* @param index:节点在链表中的位置
*/
public void remove(int index);
/*
* 在链表的第index个位置之前插入一个节点,值为theElement,index∈[1,size]
* @param index:插入节点在链表中的位置
* @param theElement:新插入节点的data值
*/
public void add(int index, E theElement);
/*
* 在链表尾插入节点,插入节点data值为element
* @param element
*/
public void add(E element);
/**
* 输出链表
*/
public void printList();
/**
* 获取第一个节点的data值
* @return
*/
public E getFirst();
/**
* 获取链表最后一个节点的data值
* @return
*/
public E getLast();
}
public class DoubleLinkedList<E> implements DoubleLinkedListImpl<E> {
private Node<E> head;//头结点,非第一个节点
private Node<E> curr;//当前节点
private Node<E> tail;//最后一个节点
private int size;//当前链表节点
}

我的代码
public class Main {
public static void main(String[] args) {
// TODO �Զ����ɵķ������
LList<String> list = new LList<String>();
list.add("aewrgh");
list.add("brtjh");
list.add("cghdf");
list.add("ddf");
list.add(2,"edgfhngm");
list.remove(3);
list.printList();
}
}
public interface LinearListInterface<E> {
public boolean isEmpty();
public int size();
public E get(int index);
public void remove(int index);
public void add(int index, E theElement);
public void add(E element);
public void printList();
public E getFirst();
public E getList();
}
public class Node<E> {
private E o;
private Node<E> front;
private Node<E> next;
public E getO() {
return o;
}
public void setO(E o) {
this.o = o;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
public Node() {
super();
// TODO �Զ����ɵĹ��캯�����
}
public Node<E> getFront() {
return front;
}
public void setFront(Node<E> front) {
this.front = front;
}
public E getFirst() {
return o;
}
public E getList() {
return o;
}
}
public class LList<E> implements LinearListInterface<E>{
private Node<E> head = new Node<E>(),curr = new Node<E>(),tail = new Node<E>();
private int size;
public LList() {
super();
}
@Override
public boolean isEmpty() {
// TODO �Զ����ɵķ������
if(curr!=null) return true;
return false;
}
@Override
public int size() {
// TODO �Զ����ɵķ������
return size;
}
@Override
public E get(int index) {
// TODO �Զ����ɵķ������
int i = 1;
for(curr=head;i<=size;curr=curr.getNext()) {
if(i==index) return curr.getO();
i++;
}
return null;
}
@Override
public void remove(int index) {
// TODO �Զ����ɵķ������
int i = 1;
Node<E> p = new Node<E>();
for(curr=head;isEmpty();curr=curr.getNext()) {
if(index==1) {
head=head.getNext();
head.setFront(null);
size--;
break;
}else if(i==size) {
p.setNext(null);
size--;
break;
}else if(i==index) {
p.setNext(curr.getNext());
curr.getNext().setFront(p);
size--;
break;
}
p=curr;
i++;
}
}
@Override
public void add(int index, E theElement) {
// TODO �Զ����ɵķ������
int i = 1;
Node<E> p = new Node<E>();
Node<E> q = new Node<E>();
q.setO(theElement);
for(curr=head;isEmpty();curr=curr.getNext()) {
if(index==1) {
q.setNext(head);
head.setFront(q);
head=q;
size++;
break;
}else if(i==index) {
p.setNext(q);
q.setFront(p);
q.setNext(curr);
curr.setFront(q);
size++;
break;
}
p=curr;
i++;
}
}
@Override
public void add(E element) {
// TODO �Զ����ɵķ������
if(size==0) {
head.setO(element);
head.setNext(tail);
size++;
}
else if(size==1) {
tail.setO(element);
tail.setNext(null);
tail.setFront(head);
size++;
}else {
Node<E> q = new Node<E>();
q.setO(element);
q.setNext(null);
tail.setNext(q);
q.setFront(tail);
tail=q;
size++;
}
}
@Override
public void printList() {
for(curr=head;isEmpty();curr=curr.getNext()) {
System.out.println(curr.getO());
}
}
@Override
public E getFirst() {
return head.getO();
}
@Override
public E getList() {
return tail.getO();
}
}
(三)总结
本阶段(7-10周)学习了学习了很多如对象和类、对象交互、继承和多态,对象容器,抽象与接口,用java实现链表功能和双向链表和正值表达式以及图(Map)、流类(Stream)的相关知识,
掌握了基本的用法,学会了Java语言中继承的基本概念及使用方法和Java语言中super关键字的使用方法、
了解继承与组合的区别和程序设计中代码复用的重要性,学会了Java语言中多态的基本概念及使用方法,明白了上转型和下转型的基本概念及使用方法,
了解了Object类中的toString()、equals()的使用方法和instanceOf运算符的使用方法, 掌握Java语言中成员变量隐藏的基本概念及使用方法;

浙公网安备 33010602011771号