Boke-liu的BLOG-2
作者:互联网
一、前言
最近这段时间在PTA做了Java的两次题目集和期中考试,还有超星学习通上的两次链表练习,先做个总结:
1、题目集四的三道题目主要考察的是类与对象,设计类,在类中写对应的方法,创建对象调用方法完成相应的输出。包括点线形系列的三个题目,第一个是计算两点之间的距离,
计算简单,主要是要满足输入与输出的格式,第二个是线的计算,第三个是三角形的计算,难度都比较大,都需要用代码解决数学问题。
2、题目集五包含五道题目,前面四道题目考正则表达式最后一道题目是ATM机类结构设计,前面四道题目通过网上查资料了解正则表达式能比较快的完成,最后一道题目ATM机类结
构设计比较难。
3、期中考试的三道题目考察了类设计、继承与多态、还有容器类,第一道题目是点与线,第二道题目是点线面问题重构,第三道题目是点线面问题再重构,三道题目是后面一题在
前面一题代码的基础之上改进,对于我来说挺难的。
4、链表的两道题目一个是单向链表,一个是双向链表,C语言链表学的好,Java链表应该就不会很难吧。
二、设计与分析
1、点线形系列1-计算两点之间的距离,题目要求如下:
输入连个点的坐标,计算两点之间的距离
输入格式:
4个double类型的实数,两个点的x,y坐标,依次是x1、y1、x2、y2,两个点的坐标之间以空格分隔,每个点的x,y坐标以英文“,”分隔。例如:0,0 1,1或0.1,-0.3 +3.5,15.6。
若输入格式非法,输出"Wrong Format"。
若输入格式合法但坐标点的数量超过两个,输出“wrong number of points”。
输出格式:
计算所得的两点之间的距离。例如:1.4142135623730951
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String s=in.nextLine(); double d; boolean judge = true; String regEx= "[\\+-]?([0]|([1-9](\\d?)+))(\\.\\d+)?\\,[\\+-]?([0]|([1-9](\\d?)+))(\\.\\d+)?"; String[] a = s.split("\\s+"); for(int i=0;i<2;i++) { judge=a[i].matches(regEx); if(judge==false) break; } if(a.length>2) { System.out.println("wrong number of points"); System.exit(0); } if(a.length<2) { System.out.println("Wrong Format"); System.exit(0); } if(judge) { String[] s1 = a[0].split(","); String[] s2 = a[1].split(","); double x1 = Double.parseDouble(s1[0]); double y1 = Double.parseDouble(s1[1]); double x2 = Double.parseDouble(s2[0]); double y2 = Double.parseDouble(s2[1]); d = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); System.out.println(d); } else System.out.println("Wrong Format"); } }
用正则表达式String regEx= "[\\+-]?([0]|([1-9](\\d?)+))(\\.\\d+)?\\,[\\+-]?([0]|([1-9](\\d?)+))(\\.\\d+)?";控制输入,使两个点之间用空格分开,点的横纵坐标之间用逗号间隔的输入为正确输入,对于
正确的输入,用String[] a = s.split("\\s+");转换成字符数组,一个点为一个元素,空格为分隔条件,再用逗号为分隔条件将一个坐标值转换为一个元素,将字符型转换成double型进行计算。
对于点数多于两个或少于两个用if-else进行分类输出。
2、点线形系列2-线的计算,题目要求如下:
用户输入一组选项和数据,进行与直线有关的计算。选项包括:
1:输入两点坐标,计算斜率,若线条垂直于X轴,输出"Slope does not exist"。
2:输入三个点坐标,输出第一个点与另外两点连线的垂直距离。
3:输入三个点坐标,判断三个点是否在一条线上,输出true或者false。
4:输入四个点坐标,判断前两个点所构成的直线与后两点构成的直线是否平行,输出true或者false.
5:输入四个点坐标,计算输出前两个点所构成的直线与后两点构成的直线的交点坐标,x、y坐标之间以英文分隔",",并输出交叉点是否在两条线段之内(不含四个端点)的判断结果(true/false),判断结果与坐标之间以一个英文空格分隔。若两条线平行,没有交叉点,则输出"is parallel lines,have no intersection point"。
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。
例如:1:0,0 1,1
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
不论哪个选项,如果格式、点数量都符合要求,但构成任一条线的两个点坐标重合,输出"points coincide",
输出格式:
见题目描述。
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String s=in.nextLine(); char []b=s.toCharArray(); String c=s.substring(2); boolean judge = true; String regEx= "[\\+-]?([0]|([1-9](\\d?)+))(\\.\\d+)?\\,[\\+-]?([0]|([1-9](\\d?)+))(\\.\\d+)?"; String[] a = c.split("\\s+"); if(b[0]==49) { Dian dian1=new Dian(); if(a.length>2) { System.out.println("wrong number of points"); System.exit(0); } if(a.length<2) { System.out.println("wrong number of points"); System.exit(0); } if(a.length==2) { for(int i=0;i<2;i++) { judge=a[i].matches(regEx); if(judge==false) break; } } if(judge) { String[] s1 = a[0].split(","); String[] s2 = a[1].split(","); double x1 = Double.parseDouble(s1[0]); double y1 = Double.parseDouble(s1[1]); double x2 = Double.parseDouble(s2[0]); double y2 = Double.parseDouble(s2[1]); Dian dian2=new Dian(x1,y1); Dian dian3=new Dian(x2,y2); if(x1==x2&&y1==y2) { System.out.println("points coincide"); } else if(x1==x2&&y1!=y2) { System.out.println("Slope does not exist"); } else System.out.println(dian1.Slope(dian2, dian3)); } else System.out.println("Wrong Format"); } if(b[0]==50) { Dian dian1=new Dian(); if(a.length>3) { System.out.println("wrong number of points"); System.exit(0); } if(a.length<3) { System.out.println("wrong number of points"); System.exit(0); } if(a.length==3) { for(int i=0;i<3;i++) { judge=a[i].matches(regEx); if(judge==false) break; } } if(judge) { String[] s1 = a[0].split(","); String[] s2 = a[1].split(","); String[] s3 = a[2].split(","); double x1 = Double.parseDouble(s1[0]); double y1 = Double.parseDouble(s1[1]); double x2 = Double.parseDouble(s2[0]); double y2 = Double.parseDouble(s2[1]); double x3 = Double.parseDouble(s3[0]); double y3 = Double.parseDouble(s3[1]); Dian dian2=new Dian(x1,y1); Dian dian3=new Dian(x2,y2); Dian dian4=new Dian(x3,y3); if(x1==x2&&y1==y2||x2==x3&&y2==y3||x1==x3&&y1==y3) { System.out.println("points coincide"); } else { if(x2==x3) { System.out.println(Math.abs(x2-x1)); } else System.out.println(dian1.juLi(dian2, dian3, dian4)); } } else System.out.println("Wrong Format"); } if(b[0]==51) { Dian dian1=new Dian(); if(a.length>3) { System.out.println("wrong number of points"); System.exit(0); } if(a.length<3) { System.out.println("wrong number of points"); System.exit(0); } if(a.length==3) { for(int i=0;i<3;i++) { judge=a[i].matches(regEx); if(judge==false) break; } } if(judge) { String[] s1 = a[0].split(","); String[] s2 = a[1].split(","); String[] s3 = a[2].split(","); double x1 = Double.parseDouble(s1[0]); double y1 = Double.parseDouble(s1[1]); double x2 = Double.parseDouble(s2[0]); double y2 = Double.parseDouble(s2[1]); double x3 = Double.parseDouble(s3[0]); double y3 = Double.parseDouble(s3[1]); Dian dian2=new Dian(x1,y1); Dian dian3=new Dian(x2,y2); Dian dian4=new Dian(x3,y3); if(x1==x2&&y1==y2||x2==x3&&y2==y3||x1==x3&&y1==y3) { System.out.println("points coincide"); } else System.out.println(dian1.gongXian(dian2, dian3, dian4)); } else System.out.println("Wrong Format"); } if(b[0]==52) { Dian dian1=new Dian(); if(a.length>4) { System.out.println("wrong number of points"); System.exit(0); } if(a.length<4) { System.out.println("wrong number of points"); System.exit(0); } if(a.length==4) { for(int i=0;i<4;i++) { judge=a[i].matches(regEx); if(judge==false) break; } } if(judge) { String[] s1 = a[0].split(","); String[] s2 = a[1].split(","); String[] s3 = a[2].split(","); String[] s4 = a[3].split(","); double x1 = Double.parseDouble(s1[0]); double y1 = Double.parseDouble(s1[1]); double x2 = Double.parseDouble(s2[0]); double y2 = Double.parseDouble(s2[1]); double x3 = Double.parseDouble(s3[0]); double y3 = Double.parseDouble(s3[1]); double x4 = Double.parseDouble(s4[0]); double y4 = Double.parseDouble(s4[1]); Dian dian2=new Dian(x1,y1); Dian dian3=new Dian(x2,y2); Dian dian4=new Dian(x3,y3); Dian dian5=new Dian(x4,y4); if(x1==x2&&y1==y2||x2==x3&&y2==y3||x1==x3&&y1==y3||x1==x4&&y1==y4||x2==x4&&y2==y4||x3==x4&&y3==y4) { System.out.println("points coincide"); } else { if(dian1.pingXing(dian2, dian3, dian4, dian5)) { System.out.println("true"); } else System.out.println("false"); } } else System.out.println("Wrong Format"); } if(b[0]==53) { Dian dian1=new Dian(); if(a.length>4) { System.out.println("wrong number of points"); System.exit(0); } if(a.length<4) { System.out.println("wrong number of points"); System.exit(0); } if(a.length==4) { for(int i=0;i<4;i++) { judge=a[i].matches(regEx); if(judge==false) break; } } if(judge) { String[] s1 = a[0].split(","); String[] s2 = a[1].split(","); String[] s3 = a[2].split(","); String[] s4 = a[3].split(","); double x1 = Double.parseDouble(s1[0]); double y1 = Double.parseDouble(s1[1]); double x2 = Double.parseDouble(s2[0]); double y2 = Double.parseDouble(s2[1]); double x3 = Double.parseDouble(s3[0]); double y3 = Double.parseDouble(s3[1]); double x4 = Double.parseDouble(s4[0]); double y4 = Double.parseDouble(s4[1]); Dian dian2=new Dian(x1,y1); Dian dian3=new Dian(x2,y2); Dian dian4=new Dian(x3,y3); Dian dian5=new Dian(x4,y4); if(x1==x2&&y1==y2||x2==x3&&y2==y3||x1==x3&&y1==y3||x1==x4&&y1==y4||x2==x4&&y2==y4||x3==x4&&y3==y4) { System.out.println("points coincide"); } else if(dian1.Slope(dian2, dian3)==dian1.Slope(dian4, dian5)||x1==x2&&x3==x4) { System.out.println("is parallel lines,have no intersection point"); } else { if(dian1.jiaoDian(dian2, dian3, dian4, dian5).getY()>dian2.getY()&&dian1.jiaoDian(dian2, dian3, dian4, dian5).getY() <dian3.getY()||dian1.jiaoDian(dian2, dian3, dian4, dian5).getY()>dian4.getY()&&dian1.jiaoDian(dian2, dian3, dian4, dian5).getY() <dian5.getY()||dian1.jiaoDian(dian2, dian3, dian4, dian5).getY()>dian3.getY()&&dian1.jiaoDian(dian2, dian3, dian4, dian5).getY() <dian2.getY()||dian1.jiaoDian(dian2, dian3, dian4, dian5).getY()>dian5.getY()&&dian1.jiaoDian(dian2, dian3, dian4, dian5).getY() <dian4.getY()) { System.out.println(dian1.jiaoDian(dian2, dian3, dian4, dian5).showJiaodian()+" true"); } else System.out.println(dian1.jiaoDian(dian2, dian3, dian4, dian5).showJiaodian()+" false"); } } else System.out.println("Wrong Format"); } } } class Dian { private double x; private double y; public Dian() { } public Dian(double x,double y) { this.x = x; this.y = 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; } public double Slope(Dian dian1,Dian dian2) { double k; k=(dian1.y-dian2.y)/(dian1.x-dian2.x); return k; } public double juLi(Dian dian1,Dian dian2,Dian dian3) { double k=(dian2.y-dian3.y)/(dian2.x-dian3.x); double d=(Math.abs(dian1.y-k*dian1.x+k*dian3.x-dian3.y)/Math.sqrt(1+k*k)); return d; } public boolean gongXian(Dian dian1,Dian dian2,Dian dian3) { if(dian1.x==dian2.x&&dian2.x==dian3.x){ return true; } else if(dian1.x!=dian2.x&&dian2.x!=dian3.x&&dian1.x!=dian3.x) { double k1=(dian2.y-dian1.y)/(dian2.x-dian1.x); double k2=(dian2.y-dian3.y)/(dian2.x-dian3.x); if(k1==k2) { return true; } else return false; } else return false; } public boolean pingXing(Dian dian1,Dian dian2,Dian dian3,Dian dian4) { if((dian1.y-dian2.y)/(dian1.x-dian2.x)==(dian3.y-dian4.y)/(dian3.x-dian4.x)) { return true; } else return false; } public Dian jiaoDian(Dian dian1,Dian dian2,Dian dian3,Dian dian4) { double a,b; a = ((dian2.x - dian1.x) * (dian3.x - dian4.x) * (dian3.y - dian1.y) - dian3.x * (dian2.x - dian1.x) * (dian3.y - dian4.y) + dian1.x * (dian2.y - dian1.y) * (dian3.x - dian4.x)) / ((dian2.y - dian1.y) * (dian3.x - dian4.x) - (dian2.x - dian1.x) * (dian3.y - dian4.y)); b = ((dian2.y - dian1.y) * (dian3.y - dian4.y) * (dian3.x - dian1.x) - dian3.y * (dian2.y - dian1.y) * (dian3.x - dian4.x) + dian1.y * (dian2.x - dian1.x) * (dian3.y - dian4.y)) / ((dian2.y - dian1.y) * (dian3.y - dian4.y) - (dian2.y - dian1.y) * (dian3.x - dian4.x)); return new Dian(a,b); } public String showJiaodian(){ return x+","+y; } }
同样用第一题的正则表达式控制输入格式,还需要设计一个类,我设计了一个Dian类,属性是横纵坐标,除了构造方法和getter和setter外就是一些方法,我当时写的时候还是写
的带参的方法,计算斜率、点到直线的距离、两直线之间的交点、判断是否平行还是共线等等都是数学问题,还是有很多需要考虑的地方,比如说斜率不存在的情况。
3、点线形系列3-三角形的计算,题目要求如下:
用户输入一组选项和数据,进行与三角形有关的计算。选项包括:
1:输入三个点坐标,判断是否是等腰三角形、等边三角形,判断结果输出true/false,两个结果之间以一个英文空格符分隔。
2:输入三个点坐标,输出周长、面积、重心坐标,三个参数之间以一个英文空格分隔,坐标之间以英文","分隔。
3:输入三个点坐标,输出是钝角、直角还是锐角三角形,依次输出三个判断结果(true/false),以一个英文空格分隔,
4:输入五个点坐标,输出前两个点所在的直线与三个点所构成的三角形相交的交点数量,如果交点有两个,则按面积大小依次输出三角形被直线分割成两部分的面积。若直线与三角形一条线重合,输出"The point is on the edge of the triangle"
5:输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外。若点在三角形的某条边上,输出"on the triangle"
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
如果输入的三个点无法构成三角形,输出"data error"。
注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333333,1.0按格式输出为1.0
选项4中所输入线的两个点坐标重合,输出"points coincide",
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String s=in.nextLine(); char []b=s.toCharArray(); String c=s.substring(2); boolean judge = true; String regEx= "[\\+-]?([0]|([1-9](\\d?)+))(\\.\\d+)?\\,[\\+-]?([0]|([1-9](\\d?)+))(\\.\\d+)?"; String[] a = c.split("\\s+"); if(b[0]==49) { Dian dian1=new Dian(); if(a.length>3) { System.out.println("wrong number of points"); System.exit(0); } if(a.length<3) { System.out.println("wrong number of points"); System.exit(0); } if(a.length==3) { for(int i=0;i<3;i++) { judge=a[i].matches(regEx); if(judge==false) break; } } if(judge) { String[] s1 = a[0].split(","); String[] s2 = a[1].split(","); String[] s3 = a[2].split(","); double x1 = Double.parseDouble(s1[0]); double y1 = Double.parseDouble(s1[1]); double x2 = Double.parseDouble(s2[0]); double y2 = Double.parseDouble(s2[1]); double x3 = Double.parseDouble(s3[0]); double y3 = Double.parseDouble(s3[1]); Dian dian2=new Dian(x1,y1); Dian dian3=new Dian(x2,y2); Dian dian4=new Dian(x3,y3); if(dian1.triangle(dian2, dian3, dian4)) { if(dian1.dengYao(dian2, dian3, dian4)) { if(dian1.dengBian(dian2, dian3, dian4)) System.out.println("true true"); else System.out.println("true false"); } else { if(dian1.dengBian(dian2, dian3, dian4)) System.out.println("false true"); else System.out.println("false false"); } } else System.out.println("data error"); } else System.out.println("Wrong Format"); } if(b[0]==50) { Dian dian1=new Dian(); if(a.length>3) { System.out.println("wrong number of points"); System.exit(0); } if(a.length<3) { System.out.println("wrong number of points"); System.exit(0); } if(a.length==3) { for(int i=0;i<3;i++) { judge=a[i].matches(regEx); if(judge==false) break; } } if(judge) { String[] s1 = a[0].split(","); String[] s2 = a[1].split(","); String[] s3 = a[2].split(","); double x1 = Double.parseDouble(s1[0]); double y1 = Double.parseDouble(s1[1]); double x2 = Double.parseDouble(s2[0]); double y2 = Double.parseDouble(s2[1]); double x3 = Double.parseDouble(s3[0]); double y3 = Double.parseDouble(s3[1]); Dian dian2=new Dian(x1,y1); Dian dian3=new Dian(x2,y2); Dian dian4=new Dian(x3,y3); if(dian1.triangle(dian2, dian3, dian4)) { if(x1==x2&&y1==y2||x2==x3&&y2==y3||x1==x3&&y1==y3) { System.out.println("points coincide"); } else System.out.println(dian1.zhouChang(dian2, dian3, dian4)+" "+dian1.area(dian2, dian3, dian4) +" "+dian1.zhongXin(dian2, dian3, dian4).showZuobiao()); } else System.out.println("data error"); } else System.out.println("Wrong Format"); } if(b[0]==51) { Dian dian1=new Dian(); if(a.length>3) { System.out.println("wrong number of points"); System.exit(0); } if(a.length<3) { System.out.println("wrong number of points"); System.exit(0); } if(a.length==3) { for(int i=0;i<3;i++) { judge=a[i].matches(regEx); if(judge==false) break; } } if(judge) { String[] s1 = a[0].split(","); String[] s2 = a[1].split(","); String[] s3 = a[2].split(","); double x1 = Double.parseDouble(s1[0]); double y1 = Double.parseDouble(s1[1]); double x2 = Double.parseDouble(s2[0]); double y2 = Double.parseDouble(s2[1]); double x3 = Double.parseDouble(s3[0]); double y3 = Double.parseDouble(s3[1]); Dian dian2=new Dian(x1,y1); Dian dian3=new Dian(x2,y2); Dian dian4=new Dian(x3,y3); if(dian1.triangle(dian2, dian3, dian4)) { if(dian1.dunJiao(dian2, dian3, dian4)) { System.out.println("true false false"); } else { if(dian1.zhiJiao(dian2, dian3, dian4)) { System.out.println("false true false"); } else { System.out.println("false false true"); } } } else System.out.println("data error"); } else System.out.println("Wrong Format"); } if(b[0]==52) { Dian dian1=new Dian(); if(a.length>5) { System.out.println("wrong number of points"); System.exit(0); } if(a.length<5) { System.out.println("wrong number of points"); System.exit(0); } if(a.length==5) { for(int i=0;i<5;i++) { judge=a[i].matches(regEx); if(judge==false) break; } } if(judge) { String[] s1 = a[0].split(","); String[] s2 = a[1].split(","); String[] s3 = a[2].split(","); String[] s4 = a[3].split(","); String[] s5 = a[4].split(","); double x1 = Double.parseDouble(s1[0]); double y1 = Double.parseDouble(s1[1]); double x2 = Double.parseDouble(s2[0]); double y2 = Double.parseDouble(s2[1]); double x3 = Double.parseDouble(s3[0]); double y3 = Double.parseDouble(s3[1]); double x4 = Double.parseDouble(s4[0]); double y4 = Double.parseDouble(s4[1]); double x5 = Double.parseDouble(s5[0]); double y5 = Double.parseDouble(s5[1]); Dian dian2=new Dian(x1,y1); Dian dian3=new Dian(x2,y2); Dian dian4=new Dian(x3,y3); Dian dian5=new Dian(x4,y4); Dian dian6=new Dian(x5,y5); if(x1==x2&&y1==y2) { System.out.println("points coincide"); } else { if(dian1.triangle(dian4, dian5, dian6)) { System.out.println(1); } else System.out.println("data error"); } } else System.out.println("Wrong Format"); } if(b[0]==53) { Dian dian1=new Dian(); if(a.length>4) { System.out.println("wrong number of points"); System.exit(0); } if(a.length<4) { System.out.println("wrong number of points"); System.exit(0); } if(a.length==4) { for(int i=0;i<4;i++) { judge=a[i].matches(regEx); if(judge==false) break; } } if(judge) { String[] s1 = a[0].split(","); String[] s2 = a[1].split(","); String[] s3 = a[2].split(","); String[] s4 = a[3].split(","); double x1 = Double.parseDouble(s1[0]); double y1 = Double.parseDouble(s1[1]); double x2 = Double.parseDouble(s2[0]); double y2 = Double.parseDouble(s2[1]); double x3 = Double.parseDouble(s3[0]); double y3 = Double.parseDouble(s3[1]); double x4 = Double.parseDouble(s4[0]); double y4 = Double.parseDouble(s4[1]); Dian dian2=new Dian(x1,y1); Dian dian3=new Dian(x2,y2); Dian dian4=new Dian(x3,y3); Dian dian5=new Dian(x4,y4); if(dian1.triangle(dian3, dian4, dian5)) { if(dian1.inTriangle(dian2, dian3, dian4, dian5)) { System.out.println("in the triangle"); } else if(dian1.onTriangle(dian2, dian3, dian4, dian5)) { System.out.println("on the triangle"); } else System.out.println("outof triangle"); } else System.out.println("data error"); } else System.out.println("Wrong Format"); } } } class Dian { private double x; private double y; public Dian() { } public Dian(double x,double y) { this.x = x; this.y = 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; } public boolean triangle(Dian dian1,Dian dian2,Dian dian3) { if((dian3.y-dian2.y)*(dian3.x-dian1.x) != (dian3.y-dian1.y)*(dian3.x-dian2.x)) return true; else return false; } public boolean dengYao(Dian dian1,Dian dian2,Dian dian3) { if((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y) ==(dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)|| (dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y) ==(dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)|| (dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y) ==(dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) { return true; } else return false; } public boolean dengBian(Dian dian1,Dian dian2,Dian dian3) { if((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y) ==(dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)&& (dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y) ==(dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) { return true; } else return false; } public double zhouChang(Dian dian1,Dian dian2,Dian dian3) { double c; c=Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) +Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) +Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)); String result = String.format("%.6f", c); double n = Double.parseDouble(result); return n; } public double area(Dian dian1,Dian dian2,Dian dian3) { double k=(dian2.y-dian3.y)/(dian2.x-dian3.x); double d=(Math.abs(dian1.y-k*dian1.x+k*dian3.x-dian3.y)/Math.sqrt(1+k*k)); double area=d/2*Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)); String result = String.format("%.6f", area); double m = Double.parseDouble(result); return m; } public Dian zhongXin(Dian dian1,Dian dian2,Dian dian3) { double a,b; a=(dian1.x+dian2.x+dian3.x)/3; b=(dian1.y+dian2.y+dian3.y)/3; String result1 = String.format("%.6f", a); String result2 = String.format("%.6f", b); double m = Double.parseDouble(result1); double n = Double.parseDouble(result2); return new Dian(m,n); } public String showZuobiao(){ return x+","+y; } public boolean dunJiao(Dian dian1,Dian dian2,Dian dian3) { if(Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) >Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) &&Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) >Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y))) { if((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y) >(dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)+ (dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) { return true; } else return false; } else if(Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) >Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) &&Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) >Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y))) { if((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y) >(dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)+ (dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) { return true; } else return false; } else if(Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) >Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) &&Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) >Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y))) { if((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y) >(dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)+ (dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) { return true; } else return false; } else return false; } public boolean ruiJiao(Dian dian1,Dian dian2,Dian dian3) { if(Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) <Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) &&Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) <Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y))) { if((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y) <(dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)+ (dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) { return true; } else return false; } else if(Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) <Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) &&Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) <Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y))) { if((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y) <(dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)+ (dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) { return true; } else return false; } else if(Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) <Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) &&Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) <Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y))) { if((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y) <(dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)+ (dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) { return true; } else return false; } else return false; } public boolean zhiJiao(Dian dian1,Dian dian2,Dian dian3) { if(Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) >Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) &&Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) >Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y))) { if((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y) ==(dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)+ (dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) { return true; } else return false; } else if(Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) >Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) &&Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) >Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y))) { if((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y) ==(dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)+ (dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) { return true; } else return false; } else if(Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) >Math.sqrt((dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)) &&Math.sqrt((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y)) >Math.sqrt((dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y))) { if((dian2.x-dian3.x)*(dian2.x-dian3.x) +(dian2.y-dian3.y)*(dian2.y-dian3.y) ==(dian1.x-dian2.x)*(dian1.x-dian2.x) +(dian1.y-dian2.y)*(dian1.y-dian2.y)+ (dian1.x-dian3.x)*(dian1.x-dian3.x) +(dian1.y-dian3.y)*(dian1.y-dian3.y)) { return true; } else return false; } else return false; } public boolean inTriangle(Dian dian1,Dian dian2,Dian dian3,Dian dian4) { if((dian1.x-dian2.x)*(dian3.y-dian2.y)-(dian1.y-dian2.y)*(dian3.x-dian2.x)>0&& (dian1.x-dian3.x)*(dian4.y-dian3.y)-(dian1.y-dian3.y)*(dian4.x-dian3.x)>0&& (dian1.x-dian4.x)*(dian2.y-dian4.y)-(dian1.y-dian4.y)*(dian2.x-dian4.x)>0|| (dian1.x-dian2.x)*(dian3.y-dian2.y)-(dian1.y-dian2.y)*(dian3.x-dian2.x)<0&& (dian1.x-dian3.x)*(dian4.y-dian3.y)-(dian1.y-dian3.y)*(dian4.x-dian3.x)<0&& (dian1.x-dian4.x)*(dian2.y-dian4.y)-(dian1.y-dian4.y)*(dian2.x-dian4.x)<0) { return true; } else return false; } public boolean onTriangle(Dian dian1,Dian dian2,Dian dian3,Dian dian4) { if((dian1.x-dian2.x)*(dian3.y-dian2.y)-(dian1.y-dian2.y)*(dian3.x-dian2.x)==0&& ((dian1.x-dian3.x)*(dian4.y-dian3.y)-(dian1.y-dian3.y)*(dian4.x-dian3.x)>0&& (dian1.x-dian4.x)*(dian2.y-dian4.y)-(dian1.y-dian4.y)*(dian2.x-dian4.x)>0|| (dian1.x-dian3.x)*(dian4.y-dian3.y)-(dian1.y-dian3.y)*(dian4.x-dian3.x)<0&& (dian1.x-dian4.x)*(dian2.y-dian4.y)-(dian1.y-dian4.y)*(dian2.x-dian4.x)<0)|| (dian1.x-dian3.x)*(dian4.y-dian3.y)-(dian1.y-dian3.y)*(dian4.x-dian3.x)==0&& ((dian1.x-dian2.x)*(dian3.y-dian2.y)-(dian1.y-dian2.y)*(dian3.x-dian2.x)>0&& (dian1.x-dian4.x)*(dian2.y-dian4.y)-(dian1.y-dian4.y)*(dian2.x-dian4.x)>0|| (dian1.x-dian2.x)*(dian3.y-dian2.y)-(dian1.y-dian2.y)*(dian3.x-dian2.x)<0&& (dian1.x-dian4.x)*(dian2.y-dian4.y)-(dian1.y-dian4.y)*(dian2.x-dian4.x)<0)|| (dian1.x-dian4.x)*(dian2.y-dian4.y)-(dian1.y-dian4.y)*(dian2.x-dian4.x)==0&& ((dian1.x-dian2.x)*(dian3.y-dian2.y)-(dian1.y-dian2.y)*(dian3.x-dian2.x)>0&& (dian1.x-dian3.x)*(dian4.y-dian3.y)-(dian1.y-dian3.y)*(dian4.x-dian3.x)>0|| (dian1.x-dian2.x)*(dian3.y-dian2.y)-(dian1.y-dian2.y)*(dian3.x-dian2.x)<0&& (dian1.x-dian3.x)*(dian4.y-dian3.y)-(dian1.y-dian3.y)*(dian4.x-dian3.x)<0)) { return true; } else return false; } }
同样用第一题的正则表达式控制输入格式,因为还要用坐标进行计算,所以还是Dian类,属性是横纵坐标,除了构造方法和getter和setter外就是一些方法,也是数学问题,会用
数学方法解决用代码就不难写,但是还是要考虑很多种情况。
4、ATM机类结构设计,题目要求如下:
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
- 存款、取款功能输入数据格式:
卡号 密码 ATM机编号 金额
(由一个或多个空格分隔),
其中,当金额大于0时,代表取款,否则代表存款。 - 查询余额功能输入数据格式:
卡号
输出格式:
①输入错误处理
- 如果输入卡号不存在,则输出
Sorry,this card does not exist.
。 - 如果输入ATM机编号不存在,则输出
Sorry,the ATM's id is wrong.
。 - 如果输入银行卡密码错误,则输出
Sorry,your password is wrong.
。 - 如果输入取款金额大于账户余额,则输出
Sorry,your account balance is insufficient.
。 - 如果检测为跨行存取款,则输出
Sorry,cross-bank withdrawal is not supported.
。
②取款业务输出
输出共两行,格式分别为:
[用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
③存款业务输出
输出共两行,格式分别为:
[用户姓名]在[银行名称]的[ATM编号]上存款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
④查询余额业务输出
¥[金额]
金额保留两位小数。
import java.util.Scanner; import java.util.ArrayList; //Bank类 class Bank { String bankname; ArrayList<String> ATMList; //创建无参构造方法 public Bank(){ } //创建带参构造方法 public Bank(String bankname,ArrayList<String> ATMList) { this.bankname=bankname; this.ATMList=ATMList; } //getter public String getBankname() { return bankname; } public ArrayList<String> getATMList() { return ATMList; } //setter public void setBankname(String bankname){ this.bankname=bankname; } public void setATMList(ArrayList<String> tATMList){ this.ATMList=ATMList; } } //Account类 class Account { private String name; private String account; private String password; private double balance; ArrayList<Bank> banklist; Bank bank; private ArrayList<String> cardList; //创建无参构造方法 public Account(){ } //创建带参构造方法 public Account(String name,String account,String password,double balance,ArrayList<Bank> banklist,Bank bank,ArrayList<String> cardList){ this.name=name; this.account=account; this.password=password; this.balance=balance; this.bank=bank; this.banklist=banklist; this.cardList=cardList; } //getter public String getName() { return name; } public String getAccount() { return account; } public String getPassword() { return password; } public double getBalance() { return balance; } public ArrayList<String> getCardList() { return cardList; } //setter public void setName(String name) { this.name = name; } public void setAccount(String account) { this.account = account; } public void setPassword(String password) { this.password = password; } public void setBalance(double balance) { this.balance = balance; } public void setCardList(ArrayList<String> cardList) { this.cardList = cardList; } } //存取款的检查类 class Check { ArrayList<Account> accountList; String card; String password; String number; double money; public Check(ArrayList<Account> accountList,String card,String password,String number,double money){ this.accountList=accountList; this.card=card; this.password=password; this.number=number; this.money=money; } public boolean check(){ int flag=0; int i,j; int k=0; //检查账号是否正确 for(i=0;i<accountList.size();i++){ for(j=0;j<accountList.get(i).getCardList().size();j++){ if (card.equals(accountList.get(i).getCardList().get(j))){ flag=1; k=i; break; } } if(flag==1){ break; } } //检查密码是否正确 if(flag==1){ if(password.equals(accountList.get(k).getPassword())){ flag=2; } else{ System.out.println("Sorry,your password is wrong.");//银行卡密码错误 return false; } } else{ System.out.println("Sorry,this card does not exist.");//卡号不存在 return false; } //检查ATM机编号是否正确 if(flag==2){ for(i=0;i<accountList.get(k).banklist.size();i++){ for(j=0;j<accountList.get(k).banklist.get(i).ATMList.size();j++){ if(number.equals(accountList.get(k).banklist.get(i).ATMList.get(j))){ flag=3; break; } } } } //检查金额是否正确 if(flag==3){ if(money<=accountList.get(k).getBalance()){ flag=4; } else{ System.out.println("Sorry,your account balance is insufficient.");//取款金额大于账户余额 return false; } } else{ System.out.println("Sorry,the ATM's id is wrong.");//ATM机编号不存在 return false; } //检查是否跨行 if(flag==4){ for(i=0;i<accountList.get(k).bank.ATMList.size();i++){ if(number.equals(accountList.get(k).bank.ATMList.get(i))){ flag=5; break; } } } if(flag!=5){ System.out.println("Sorry,cross-bank withdrawal is not supported.");//跨行存取款 return false; } else return true; } } //余额查询的检查类 class Check1 { ArrayList<Account> accountList; String card; public Check1(ArrayList<Account> accountList, String card){ this.accountList = accountList; this.card = card; } public boolean check(){ int i,j; int flag=0; //卡号校验 for(i=0;i<accountList.size();i++){ for(j=0;j<accountList.get(i).getCardList().size();j++){ if (card.equals(accountList.get(i).getCardList().get(j))){ flag=1; break; } } if(flag==1){ break; } } if(flag==1) return true; else{ System.out.println("Sorry,this card does not exist.");//卡号不存在 return false; } } } //存取款类 class Access{ ArrayList<Account> accountList; String card; String password; String number; double money; public Access(ArrayList<Account> accountList,String card,String password,String number,double money){ this.password=password; this.number=number; this.card=card; this.accountList=accountList; this.money=money; } public void access(){ int i,j; int t=0; //卡号校验 for(i=0;i<accountList.size();i++){ for(j=0;j<accountList.get(i).getCardList().size();j++){ if(card.equals(accountList.get(i).getCardList().get(j))){ t=i; break; } } } accountList.get(t).setBalance(accountList.get(t).getBalance()-money); } } //存取款的展示类 class Show{ ArrayList<Account> accountList; String card; String password; String number; double money; public Show(ArrayList<Account> accountList,String card,String password,String number,double money){ this.password=password; this.number=number; this.card=card; this.accountList=accountList; this.money=money; } public void show(){ int i,j; int t=0; //卡号校验 for(i=0;i<accountList.size();i++){ for(j=0;j<accountList.get(i).getCardList().size();j++){ if(card.equals(accountList.get(i).getCardList().get(j))){ t=i; break; } } } if(money>=0){//取款 System.out.printf(accountList.get(t).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上取款¥%.2f\n",money); } else{ money=-money; System.out.printf(accountList.get(t).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上存款¥%.2f\n",money); } System.out.printf("当前余额为¥%.2f\n",accountList.get(t).getBalance()); } } //余额查询的展示类 class Show1{ ArrayList<Account> accountList; String card; public Show1(ArrayList<Account> accountList,String card){ this.accountList=accountList; this.card=card; } public void show1(){ int i,j; int t=0; //卡号校验 for(i=0;i<accountList.size();i++){ for(j=0;j<accountList.get(i).getCardList().size();j++){ if(card.equals(accountList.get(i).getCardList().get(j))){ t=i; break; } } } System.out.printf("¥%.2f\n",accountList.get(t).getBalance()); } } //主类 public class Main { public static void main(String[] args) { //ATM机编号数组 ArrayList<String> ATMList1 = new ArrayList<>(); ATMList1.add("01"); ATMList1.add("02"); ATMList1.add("03"); ATMList1.add("04"); Bank jsyh = new Bank("中国建设银行", ATMList1); ArrayList<String> ATMList2 = new ArrayList<>(); ATMList2.add("05"); ATMList2.add("06"); Bank gsyh = new Bank("中国工商银行", ATMList2); //银行数组 ArrayList<Bank> bankList = new ArrayList<>(); bankList.add(jsyh); bankList.add(gsyh); //用户数组 ArrayList<String> cardList1 = new ArrayList<>(); cardList1.add("6217000010041315709"); cardList1.add("6217000010041315715"); Account account1 = new Account("杨过", "3217000010041315709", "88888888", 10000.00, bankList, jsyh,cardList1); ArrayList<String> cardList2 = new ArrayList<>(); cardList2.add("6217000010041315718"); Account account2 = new Account("杨过", "3217000010041315715", "88888888", 10000.00, bankList,jsyh,cardList2); ArrayList<String> cardList3 = new ArrayList<>(); cardList3.add("6217000010051320007"); Account account3 = new Account("郭靖", "3217000010051320007", "88888888", 10000.00, bankList,jsyh,cardList3); ArrayList<String> cardList4 = new ArrayList<>(); cardList4.add("6222081502001312389"); Account account4 = new Account("张无忌", "3222081502001312389", "88888888", 10000.00, bankList,gsyh,cardList4); ArrayList<String> cardList5 = new ArrayList<>(); cardList5.add("6222081502001312390"); Account account5 = new Account("张无忌", "3222081502001312390", "88888888", 10000.00, bankList,gsyh,cardList5); ArrayList<String> cardList6 = new ArrayList<>(); cardList6.add("6222081502001312399"); cardList6.add("6222081502001312400"); Account account6 = new Account("张无忌", "3222081502001312399", "88888888", 10000.00, bankList,gsyh,cardList6); ArrayList<String> cardList7 = new ArrayList<>(); cardList7.add("6222081502051320785"); Account account7 = new Account("韦小宝", "3222081502051320785", "88888888", 10000.00, bankList,gsyh,cardList7); ArrayList<String> cardList8 = new ArrayList<>(); cardList8.add("6222081502051320786"); Account account8 = new Account("韦小宝", "3222081502051320786", "88888888", 10000.00, bankList,gsyh,cardList8); //用户总数组 ArrayList<Account> accountList = new ArrayList<>(); accountList.add(account1); accountList.add(account2); accountList.add(account3); accountList.add(account4); accountList.add(account5); accountList.add(account6); accountList.add(account7); accountList.add(account8); Scanner x=new Scanner(System.in); String kp; Check check; Check1 check1; kp=x.nextLine(); while(!kp.equals("#")){ int i,j; String[] shuju=kp.split("\\s+");//输入的每行数据用空格隔开,存为数组 /* for(i=0;i<shuju.length;i++) { System.out.println(shuju[i]); } */ if(shuju.length!=1){ check = new Check(accountList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3])); if (check.check()){ Access access = new Access(accountList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3])); access.access(); Show show = new Show(accountList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3])); show.show(); } } else { check1=new Check1(accountList,shuju[0]); if(check1.check()){ Show1 show1= new Show1(accountList,shuju[0]); show1.show1(); } } kp=x.nextLine(); } } }
5、期中考试三道题目,三题迭代,主要是第三题,题目要求如下:
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
- 在原有类设计的基础上,增加一个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()
方法进行输出。
import java.util.ArrayList; import java.util.Scanner; abstract class Element { public abstract void display(); } class Plane extends Element { private String color; public Plane() { super(); // TODO Auto-generated constructor stub } 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 Auto-generated method stub System.out.println("The Plane's color is:" + this.color); } } class Point extends Element{ private double x,y; public Point(){ } public Point(double x,double y){ this.x = x; this.y = y; } public double getX(){ return this.x; } public void setX(double x){ this.x = x; } public double getY(){ return this.y; } public void setY(double y){ this.y = y; } @Override public void display(){ System.out.println("(" + String.format("%.2f", x) + "," + String.format("%.2f",y) + ")"); } } class Line extends Element{ private Point point1,point2; private String color; public Line(){ } public Line(Point p1,Point p2,String color){ this.point1 = p1; this.point2 = p2; 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.sqrt(Math.pow(this.point1.getX() - this.point2.getX(), 2) + Math.pow(this.getPoint1().getY() - this.getPoint2().getY(), 2)); } @Override public void display(){ System.out.println("The line's color is:" + this.getColor()); System.out.println("The line's begin point's Coordinate is:"); this.getPoint1().display(); System.out.println("The line's end point's Coordinate is:"); this.getPoint2().display(); System.out.println("The line's length is:" + String.format("%.2f", this.getDistance())); } } class GeometryObject{ private ArrayList<Element> list = new ArrayList<>(); public GeometryObject() { } public void add(Element element) { list.add(element); } public void remove(int index) { if(index < 1 || index > list.size()) { return; } list.remove(index - 1); } public ArrayList<Element> getList(){ return this.list; } } public class Main { public static void main(String[] args) { double x1,y1,x2,y2; String color; Scanner input = new Scanner(System.in); int choice = 0,index = 0; GeometryObject container = new GeometryObject(); choice = input.nextInt(); while(choice != 0) { switch(choice) { case 1: x1 = input.nextDouble(); y1 = input.nextDouble(); container.add(new Point(x1,y1)); break; case 2: x1 = input.nextDouble(); y1 = input.nextDouble(); x2 = input.nextDouble(); y2 = input.nextDouble(); color = input.next(); container.add(new Line(new Point(x1,y1),new Point(x2,y2),color)); break; case 3: color = input.next(); container.add(new Plane(color)); break; case 4: index = input.nextInt(); container.remove(index); break; } choice = input.nextInt(); } for(Element element:container.getList()) { element.display(); } } }
6、单向链表,题目要求如下:
使用Java实现链表功能,具体功能如下(参考):
public interface LinearListInterface <E>{
public boolean isEmpty();
public int getSize();
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();
}
结构如下:
//LinkedList class
public class LList<E>{
private Node<E> head,curr,tail;//头结点(非第一个节点),当前节点,尾节点
private int size = 0;
}
//Node
public class Node<E>{
private E data;
private Node<E> next;
}
public interface LinearListInterface<E> { public boolean isEmpty(); public int getSize(); 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 class LList<E> implements LinearListInterface<E> { private Node<E> head;// 头结点(非第一个节点),当前节点,尾节点 private Node<E> curr; private Node<E> tail; private int size = 0; public LList() { super(); // TODO Auto-generated constructor stub head = new Node<E>(); head.setNext(null); curr = tail = null; this.size = 0; } @Override public boolean isEmpty() { // TODO Auto-generated method stub return this.size == 0; } @Override public int getSize() { // TODO Auto-generated method stub return this.size; } @Override public E get(int index) { // TODO Auto-generated method stub if(index < 1 || index > this.size) { return null; } curr = head; for(int i = 0; i < index; i ++) { curr = curr.getNext(); } return curr.getData(); } @Override public void remove(int index) { // TODO Auto-generated method stub if(index < 1 || index > this.size) { return ; } curr = head; if(index == 1) { curr = head.getNext(); head.setNext(curr.getNext()); }else {//找到第index - 1个节点赋给curr for(int i = 1;i < index; i++) { curr = curr.getNext(); } curr.setNext(curr.getNext().getNext()); } if(index == this.size) {//如果删除的是最后一个节点 tail = curr; } this.size --;//整个链表的节点数量-1 } @Override public void add(int index, E theElement) { // TODO Auto-generated method stub if(index < 1 || index > this.size + 1) { return ; } Node<E> curr = new Node<>(); curr.setData(theElement); curr.setNext(null); if(this.size == 0) { head.setNext(curr); tail = curr; }else if(index == this.size + 1) { this.tail.setNext(curr); tail = curr; }else { Node<E> tempNode = head; for(int i = 1; i < index;i++) { tempNode = tempNode.getNext(); } curr.setNext(tempNode.getNext()); tempNode.setNext(curr); } this.size ++; } @Override public void add(E element) { // TODO Auto-generated method stub this.add(this.size + 1,element); } @Override public void printList() { // TODO Auto-generated method stub curr = head.getNext(); for(int i = 1; i <= this.size;i ++) { System.out.print(curr.getData() + " "); curr = curr.getNext(); } System.out.println(""); } public E getLast() { if(this.size != 0) { return tail.getData(); } return null; } } //Node public class Node<E> { private E data; private Node<E> next; public Node() { } public Node(E data, Node<E> next) { super(); this.data = data; this.next = next; } public E getData() { return data; } public void setData(E data) { this.data = data; } public Node<E> getNext() { return next; } public void setNext(Node<E> next) { this.next = next; } } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub LList<Integer> list = new LList<>(); list.add(2); list.add(4); list.add(6); list.add(8); list.printList(); list.remove(3); list.printList(); list.add(2,100); list.printList(); System.out.println(list.get(4)); } }
7、双向链表,题目要求如下:
在单链表基础上改写代码,实现双向链表数据结构:
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 DoubleLinkedList<E> implements DoubleLinkedListlmpl<E> { private Node<E> head;//头结点,非第一个节点 private Node<E> curr;//当前节点 private Node<E> tail;//最后一个节点 private int size = 0;//当前链表节点数 public DoubleLinkedList() { super(); // TODO Auto-generated constructor stub head = new Node<E>(); head.setNext(null); head.setPrevious(null); curr = tail = null; this.size = 0; } @Override public boolean isEmpty() { // TODO Auto-generated method stub return this.size == 0; } @Override public int getSize() { // TODO Auto-generated method stub return this.size; } @Override public E getData(int index) { // TODO Auto-generated method stub if(index < 1 || index > this.size) { return null; } curr = head; for(int i = 0; i < index; i ++) { curr = curr.getNext(); } return curr.getData(); } @Override public void remove() { // TODO Auto-generated method stub } @Override public void remove(int index) { // TODO Auto-generated method stub if(index < 1 || index > this.size) { return ; } curr = head; if(index == 1) { curr = head.getNext(); head.setNext(curr.getNext()); }else {//找到第index - 1个节点赋给curr for(int i = 1;i < index; i++) { curr = curr.getNext(); } curr.setNext(curr.getNext().getNext()); } if(index == this.size) {//如果删除的是最后一个节点 tail = curr; } this.size --;//整个链表的节点数量-1 } @Override public void add(int index, E theElement) { // TODO Auto-generated method stub if(index < 1 || index > this.size + 1) { return ; } Node<E> curr = new Node<>(); curr.setData(theElement); curr.setNext(null); if(this.size == 0) { head.setNext(curr); curr.setPrevious(head); tail = curr; }else if(index == this.size + 1) { this.tail.setNext(curr); curr.setPrevious(this.tail); tail = curr; }else { Node<E> tempNode = head; for(int i = 1; i < index;i++) { tempNode = tempNode.getNext(); } curr.setNext(tempNode.getNext()); tempNode.setNext(curr); curr.setPrevious(tempNode); } this.size ++; } @Override public void add(E element) { // TODO Auto-generated method stub this.add(this.size + 1,element); } @Override public void printList() { // TODO Auto-generated method stub curr = head.getNext(); for(int i = 1; i <= this.size;i ++) { System.out.print(curr.getData() + " "); curr = curr.getNext(); } System.out.println(""); } @Override public E getFirst() { // TODO Auto-generated method stub if(this.size != 0) { return head.getData(); } return null; } @Override public E getLast() { // TODO Auto-generated method stub if(this.size != 0) { return tail.getData(); } return null; } } public interface DoubleLinkedListlmpl<E> { public boolean isEmpty(); public int getSize(); public E getData(int index); public void remove(); 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 getLast(); } public class Node<E> { private E data; private Node<E> next; private Node<E> previous; public Node() { } public Node(E data, Node<E> next, Node<E> previous) { super(); this.data = data; this.next = next; this.previous = previous; } public E getData() { return data; } public void setData(E data) { this.data = data; } public Node<E> getNext() { return next; } public void setNext(Node<E> next) { this.next = next; } public Node<E> getPrevious() { return previous; } public void setPrevious(Node<E> previous) { this.previous = previous; } } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub DoubleLinkedList<Integer> list = new DoubleLinkedList<>(); list.add(2); list.add(4); list.add(7); list.add(8); list.printList(); list.remove(3); list.printList(); list.add(3,6); list.printList(); System.out.println(list.getLast()); System.out.println(list.getData(3)); } }
三、踩坑心得
①多做题更熟练。
②做题时需要先理清思路再写程序,避免逻辑上的错误。
③当发现自己当前的逻辑方法完成不了题目时,可以尝试重新换个思路,以免执行不下去。
④做题时查缺补漏
⑤对数据类型的使用不够准确,也不够了解数据的精度和必要时的数据强制转换。
四、改进建议
①Dian类中的方法有点复杂,应该不带参数,而用setter传参。
②对于多个数据需要转换,可以思考整体的一个转换关系,而不是一个一个的转换。
③对于一个问题可以思考灵活的方法,做到可扩展性,增加功能或减少功能时不必修改太多的代码,灵活的方法也不需要太多的代码解决一个问题。
五、总结
通过这三次题目集的练习,我掌握了类的相关设计,对象的创建,方法的调用,继承与多态,但还需要更加深入地研究类与对象之间的联系,类与类之间的联系,对象与对象之间的联系。其他方面我觉得课上及课下组织方式已经非常好了,教师的教学也非常好,对于作业以及实验我也没有可以挑剔的地方。
标签:index,String,liu,BLOG,Boke,dian1,dian3,dian2,public 来源: https://www.cnblogs.com/Boke-Liu/p/16214235.html