java练习总结
1.前言:
本次主要分析Java中图形类的设计的题目,Java实现单向以及双向链表类练习,以及Java中容器类的设计;
考查难度由易到难,层层递进,前面的题目即为后面的代码做了很大的设计铺垫。
2.题目分析:
 
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

源代码如下:

 

  1 import java.util.Scanner;
  2 public class Main {
  3     public static void main(String[] args) {
  4      double result = 0;
  5      String x1;
  6      String x2;
  7      String y1;
  8      String y2;
  9      int e=0,f=0,g=0,h=0,k=0;
 10      String firstCoordinate;
 11      String secondCoordinate;
 12      Scanner in = new Scanner(System.in);
 13      String input = in.nextLine();
 14      for(int i=0;i<input.length();i++)
 15      {
 16          if(input.charAt(i)==' ')
 17          f++;
 18      }
 19      if(f==0)
 20      System.out.println("Wrong Format");
 21      String[] inpute = input.split(" ");
 22      firstCoordinate = inpute[0];
 23      secondCoordinate = inpute[1];
 24     if(inpute.length<=2)
 25      {
 26         for(int i=0;i<firstCoordinate.length();i++)
 27      {
 28          if(firstCoordinate.charAt(i)==',')
 29          g++;
 30      }
 31         for(int i=0;i<secondCoordinate.length();i++)
 32      {
 33          if(secondCoordinate.charAt(i)==',')
 34          h++;
 35      }
 36         if(g==0||h==0)
 37         System.out.println("Wrong Format");
 38         else
 39         {
 40          String[] first = firstCoordinate.split(",");
 41          String[] second = secondCoordinate.split(",");
 42          if(first.length==2&&second.length==2)
 43          {
 44              x1 = first[0];
 45              y1 = first[1];
 46              x2 = second[0];
 47              y2 = second[1];
 48              for(int i=0;i<x1.length();i++)
 49              {
 50                  if(x1.charAt(i)=='+'||x1.charAt(i)=='-')
 51                  e++;
 52                  if(x1.charAt(i)=='.'&&i==x1.length()-1)
 53                  k++;
 54              }
 55              if(e>=2||(e==1&&x1.charAt(0)!='+'&&x1.charAt(0)!='-')||k==1)
 56              System.out.println("Wrong Format");
 57              else
 58              {
 59                  e=0;
 60                  k=0;
 61                  for(int i=0;i<y1.length();i++)
 62                  {
 63                      if(y1.charAt(i)=='+'||y1.charAt(i)=='-')
 64                      e++;
 65                      if(y1.charAt(i)=='.'&&i==y1.length()-1)
 66                      k++;
 67                  }
 68              if(e>=2||(e==1&&y1.charAt(0)!='+'&&y1.charAt(0)!='-')||k==1)
 69              System.out.println("Wrong Format");
 70              else
 71              {
 72                  e=0;
 73                  k=0;
 74                  for(int i=0;i<x2.length();i++)
 75                  {
 76                      if(x2.charAt(i)=='+'||x2.charAt(i)=='-')
 77                      e++;
 78                      if(x2.charAt(i)=='.'&&i==x2.length()-1)
 79                      k++;
 80                  }
 81              if(e>=2||(e==1&&x2.charAt(0)!='+'&&x2.charAt(0)!='-')||k==1)
 82              System.out.println("Wrong Format");
 83              else
 84              {
 85                  e=0;
 86                  k=0;
 87                  for(int i=0;i<y2.length();i++)
 88                  {
 89                      if(y2.charAt(i)=='+'||y2.charAt(i)=='-')
 90                      e++;
 91                      if(y2.charAt(i)=='.'&&i==y2.length()-1)
 92                      k++;
 93                  }
 94              if(e>=2||(e==1&&y2.charAt(0)!='+'&&y2.charAt(0)!='-')||k==1)
 95              System.out.println("Wrong Format");
 96              else
 97              {
 98                  if((x1.charAt(0)=='0'&&x1.length()>=2&&x1.charAt(1)!='.')||(x2.charAt(0)=='0'&&x2.length()>=2&&x2.charAt(1)!='.')||(y1.charAt(0)=='0'&&y1.length()>=2&&y1.charAt(1)!='.')||(y2.charAt(0)=='0'&&y2.length()>=2&&y2.charAt(1)!='.'))
 99                  System.out.println("Wrong Format");
100                  else
101                  {
102                      double a = Double.parseDouble(x1);
103                      double b = Double.parseDouble(y1);
104                      double c = Double.parseDouble(x2);
105                      double d = Double.parseDouble(y2);
106                      result = Math.sqrt(Math.pow(a-c,2)+Math.pow(b-d,2));
107                      System.out.println(result);
108                  }
109              }
110                  }
111                      }
112                          }
113          }
114          else
115          System.out.println("Wrong Format");
116      }
117      }
118      else
119      System.out.println("wrong number of points");
120 }
121 }

 

代码分析:

本题重点在于对非法输入的判断;运用已知条件进行边界的测试,保证代码的严谨合理性。

采坑心得:

对于一些日常不会考虑的问题也应该进行判断,如”0.”,”1.”,”1++”,”–1″,”+1+”,”0.0″等情况,是可能会缺乏考虑的.运用合理的方法进行数值的筛选,以达到合法的输入。

 

2.点线形系列-三角形的计算

用户输入一组选项和数据,进行与三角形有关的计算。选项包括:
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”,

 

1.源代码如下:

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 str = in.nextLine();
        if(str.charAt(0)=='1')
        {
             int a=0;
             String x1;
             String x2;
             String y1;
             String y2;
             String z1;
             String z2;
             int e=0,f=0,g=0,h=0,k=0;
             double result=0;
             String input0 = str.substring(2);
             for(int i=0;i<input0.length();i++)
             {
                 if(input0.charAt(i)==' ')
                 a++;
             }
             if(a!=2)
             System.out.println("Wrong Format");
             else
             {
                 String firstCoordinate;
                 String secondCoordinate;
                 String thirdCoordinate;
                 String[] input1 = input0.split(" ");
                 firstCoordinate = input1[0];
                 secondCoordinate = input1[1];
                 thirdCoordinate = input1[2];
                 if(input1.length<=3)
                 {
                     for(int i=0;i<firstCoordinate.length();i++)
                     {
                         if(firstCoordinate.charAt(i)==',')
                         g++;
                     }
                        for(int i=0;i<secondCoordinate.length();i++)
                     {
                         if(secondCoordinate.charAt(i)==',')
                         h++;
                     }
                     for(int i=0;i<thirdCoordinate.length();i++)
                     {
                         if(thirdCoordinate.charAt(i)==',')
                         f++;
                     }
                     if(g==0||h==0||f==0)
                     System.out.println("Wrong Format");
                     else
                     {
                         String[] first = firstCoordinate.split(",");
                         String[] second = secondCoordinate.split(",");
                         String[] third = thirdCoordinate.split(",");
                         if(first.length==2&&second.length==2&&third.length==2)
                         {
                             x1 = first[0];
                             y1 = first[1];
                             x2 = second[0];
                             y2 = second[1];
                             z1 = third[0];
                             z2 = third[1];
                             for(int i=0;i<x1.length();i++)
                             {
                                 if(x1.charAt(i)=='+'||x1.charAt(i)=='-')
                                 e++;
                                 if(x1.charAt(i)=='.'&&i==x1.length()-1)
                                 k++;
                             }
                             if(e>=2||(e==1&&x1.charAt(0)!='+'&&x1.charAt(0)!='-')||k==1)
                             System.out.println("Wrong Format");
                             else
                             {
                                 e=0;
                                 k=0;
                                 for(int i=0;i<y1.length();i++)
                                 {
                                     if(y1.charAt(i)=='+'||y1.charAt(i)=='-')
                                     e++;
                                     if(y1.charAt(i)=='.'&&i==y1.length()-1)
                                     k++;
                                 }
                                 if(e>=2||(e==1&&y1.charAt(0)!='+'&&y1.charAt(0)!='-')||k==1)
                                 System.out.println("Wrong Format");
                                 else
                                 {
                                     e=0;
                                     k=0;
                                     for(int i=0;i<x2.length();i++)
                                     {
                                         if(x2.charAt(i)=='+'||x2.charAt(i)=='-')
                                         e++;
                                         if(x2.charAt(i)=='.'&&i==x2.length()-1)
                                         k++;
                                     }
                                     if(e>=2||(e==1&&x2.charAt(0)!='+'&&x2.charAt(0)!='-')||k==1)
                                     System.out.println("Wrong Format");
                                     else
                                     {
                                         e=0;
                                         k=0;
                                         for(int i=0;i<y2.length();i++)
                                         {
                                             if(y2.charAt(i)=='+'||y2.charAt(i)=='-')
                                             e++;
                                             if(y2.charAt(i)=='.'&&i==y2.length()-1)
                                             k++;
                                         }
                                         if(e>=2||(e==1&&y2.charAt(0)!='+'&&y2.charAt(0)!='-')||k==1)
                                         System.out.println("Wrong Format");
                                         else
                                         {
                                             e=0;
                                             k=0;
                                             for(int i=0;i<z1.length();i++)
                                             {
                                                 if(z1.charAt(i)=='+'||z1.charAt(i)=='-')
                                                 e++;
                                                 if(z1.charAt(i)=='.'&&i==z1.length()-1)
                                                 k++;
                                             }
                                             if(e>=2||(e==1&&z1.charAt(0)!='+'&&z1.charAt(0)!='-')||k==1)
                                             System.out.println("Wrong Format");
                                             else
                                             {
                                                 e=0;
                                                 k=0;
                                                 for(int i=0;i<z2.length();i++)
                                                 {
                                                     if(z2.charAt(i)=='+'||z2.charAt(i)=='-')
                                                     e++;
                                                     if(z2.charAt(i)=='.'&&i==z2.length()-1)
                                                     k++;
                                                 }
                                                 if(e>=2||(e==1&&z2.charAt(0)!='+'&&z2.charAt(0)!='-')||k==1)
                                                 System.out.println("Wrong Format");
                                                 else
                                                 {
                                                     if((x1.charAt(0)=='0'&&x1.length()>=2&&x1.charAt(1)!='.')||(x2.charAt(0)=='0'&&x2.length()>=2&&x2.charAt(1)!='.')||(y1.charAt(0)=='0'&&y1.length()>=2&&y1.charAt(1)!='.')||(y2.charAt(0)=='0'&&y2.length()>=2&&y2.charAt(1)!='.')||(z1.charAt(0)=='0'&&z1.length()>=2&&z1.charAt(1)!='.')||(z2.charAt(0)=='0'&&z2.length()>=2&&z2.charAt(1)!='.'))
                                                     System.out.println("Wrong Format");
                                                     else
                                                     {
                                                         double a1 = Double.parseDouble(x1);
                                                         double b1 = Double.parseDouble(y1);
                                                         double a2 = Double.parseDouble(x2);
                                                         double b2 = Double.parseDouble(y2);
                                                         double a3 = Double.parseDouble(z1);
                                                         double b3 = Double.parseDouble(z2);
                                                         double a01 = Math.sqrt(Math.pow(b2-b1, 2)+Math.pow(a2-a1, 2));
                                                         double b01 = Math.sqrt(Math.pow(b3-b2, 2)+Math.pow(a3-a2, 2));
                                                         double c01 = Math.sqrt(Math.pow(b3-b1, 2)+Math.pow(a3-a1, 2));
                                                         result =Math.abs((b2-b3)*a1+(a3-a2)*b1+a2*b3-b2*a3)/Math.sqrt((b2-b3)*(b2-b3) +(a2-a3)*(a2-a3));
                                                         if(result==0)
                                                         {
                                                             System.out.print("data error");
                                                         }
                                                         else
                                                         {
                                                             if(a01==b01||a01==c01||b01==c01)
                                                             {
                                                                 if(a01==b01&&a01==c01&&b01==c01)
                                                                 {
                                                                     System.out.print("true true");
                                                                 }
                                                                 else
                                                                     System.out.print("true false");
                                                             }
                                                             else
                                                             {
                                                                 System.out.print("false false");
                                                             }        
                                                         }
                                                     }
                                                    
                                                 }
                            
                                             }
                                    
                                         }
                            
                                     }
                                
                                 }
                        
                             }
                    
                         }
                         else
                         System.out.println("Wrong Format");
                     }
                    
                 }
                 else
                 System.out.println("wrong number of points");
                
             }
            
        }
        if(str.charAt(0)=='2')
        {
            String x1;
             String x2;
             String y1;
             String y2;
             String z1;
             String z2;
             double result0 = 0;
             double result1 = 0;
             double resultx = 0;
             double resulty = 0;
             String input0 = str.substring(2);
             String firstCoordinate;
             String secondCoordinate;
             String thirdCoordinate;
             String[] input1 = input0.split(" ");
             firstCoordinate = input1[0];
             secondCoordinate = input1[1];
             thirdCoordinate = input1[2];
             String[] first = firstCoordinate.split(",");
             String[] second = secondCoordinate.split(",");
             String[] third = thirdCoordinate.split(",");
             x1 = first[0];
             y1 = first[1];
             x2 = second[0];
             y2 = second[1];
             z1 = third[0];
             z2 = third[1];
             double a1 = Double.parseDouble(x1);
             double b1 = Double.parseDouble(y1);
             double a2 = Double.parseDouble(x2);
             double b2 = Double.parseDouble(y2);
             double a3 = Double.parseDouble(z1);
             double b3 = Double.parseDouble(z2);
             double a01 = Math.sqrt(Math.pow(b2-b1, 2)+Math.pow(a2-a1, 2));
             double b01 = Math.sqrt(Math.pow(b3-b2, 2)+Math.pow(a3-a2, 2));
             double c01 = Math.sqrt(Math.pow(b3-b1, 2)+Math.pow(a3-a1, 2));
             result0 = a01+b01+c01;
             result1 = Math.sqrt(0.5*result0*(0.5*result0-a01)*(0.5*result0-b01)*(0.5*result0-c01));
             resultx = (a1+a2+a3)/3;
             resulty = (b1+b2+b3)/3;
            System.out.println((double)(Math.round(result0*1000000)/1000000.0)+" "+(double)(Math.round(result1*1000000)/1000000.0)+" "+
                                (double)(Math.round(resultx*1000000)/1000000.0)+","+(double)(Math.round(resulty*1000000)/1000000.0));

        }
        if(str.charAt(0)=='3')
        {
             String x1;
             String x2;
             String y1;
             String y2;
             String z1;
             String z2;
             double result = 0;
             String input0 = str.substring(2);
             String firstCoordinate;
             String secondCoordinate;
             String thirdCoordinate;
             String[] input1 = input0.split(" ");
             firstCoordinate = input1[0];
             secondCoordinate = input1[1];
             thirdCoordinate = input1[2];
             String[] first = firstCoordinate.split(",");
             String[] second = secondCoordinate.split(",");
             String[] third = thirdCoordinate.split(",");
             x1 = first[0];
             y1 = first[1];
             x2 = second[0];
             y2 = second[1];
             z1 = third[0];
             z2 = third[1];
             double a1 = Double.parseDouble(x1);
             double b1 = Double.parseDouble(y1);
             double a2 = Double.parseDouble(x2);
             double b2 = Double.parseDouble(y2);
             double a3 = Double.parseDouble(z1);
             double b3 = Double.parseDouble(z2);
             double a01 = Math.sqrt(Math.pow(b2-b1, 2)+Math.pow(a2-a1, 2));
             double b01 = Math.sqrt(Math.pow(b3-b2, 2)+Math.pow(a3-a2, 2));
             double c01 = Math.sqrt(Math.pow(b3-b1, 2)+Math.pow(a3-a1, 2));
             
             if(a01+b01<c01||b01+c01<a01||a01+c01<b01)
             {
                 System.out.print("data error");
             }
             else {
                 if(Math.pow(a01, 2)+Math.pow(b01, 2)<Math.pow(c01, 2)||Math.pow(a01, 2)+Math.pow(c01, 2)<Math.pow(b01, 2)||Math.pow(b01, 2)+Math.pow(c01, 2)<Math.pow(a01, 2))
                 {
                     System.out.print("true false false");
                 }
                 else
                     if(Math.abs(Math.pow(a01, 2)+Math.pow(b01, 2)-Math.pow(c01, 2))<0.000001||Math.abs(Math.pow(a01, 2)+Math.pow(c01, 2)-Math.pow(b01, 2))<0.000001||Math.abs(Math.pow(b01, 2)+Math.pow(c01, 2)-Math.pow(a01, 2))<0.000001)
                     {
                         System.out.print("false true false");
                     }
                     else
                         System.out.print("false false true");     
             }
             
        }
        if(str.charAt(0)=='4')
        {
             String x1;
             String x2;
             String y1;
             String y2;
             String z1;
             String z2;
             String p1;
             String p2;
             String q1;
             String q2;
             double result0 = 0;
             double result1 = 0;
             String input0 = str.substring(2);
             String firstCoordinate;
             String secondCoordinate;
             String thirdCoordinate;
             String forthCoordinate;
             String fifthCoordinate;
             String[] input1 = input0.split(" ");
             firstCoordinate = input1[0];
             secondCoordinate = input1[1];
             thirdCoordinate = input1[2];
             forthCoordinate = input1[3];
             fifthCoordinate = input1[4];
             String[] first = firstCoordinate.split(",");
             String[] second = secondCoordinate.split(",");
             String[] third = thirdCoordinate.split(",");
             String[] forth = forthCoordinate.split(",");
             String[] fifth = fifthCoordinate.split(",");
             x1 = first[0];
             y1 = first[1];
             x2 = second[0];
             y2 = second[1];
             z1 = third[0];
             z2 = third[1];
             p1 = forth[0];
             p2 = forth[1];
             q1 = fifth[0];
             q2 = fifth[1];
             double a1 = Double.parseDouble(x1);
             double b1 = Double.parseDouble(y1);
             double a2 = Double.parseDouble(x2);
             double b2 = Double.parseDouble(y2);
             double a3 = Double.parseDouble(z1);
             double b3 = Double.parseDouble(z2);
             double a4 = Double.parseDouble(p1);
             double b4 = Double.parseDouble(p2);
             double a5 = Double.parseDouble(q1);
             double b5 = Double.parseDouble(q2);
             result0 = (b1-b2)/(a1-a2);
             result1 = (b3-b4)/(a3-a4);
             if(result0 == result1)
             System.out.println("true");
             else
             System.out.println("false");    
        }
        if(str.charAt(0)=='5')
        {
            String x1;
             String x2;
             String y1;
             String y2;
             String z1;
             String z2;
             String p1;
             String p2;
             double result0 = 0;
             double result1 = 0;
             double result2 = 0;
             String input0 = str.substring(2);
             String firstCoordinate;
             String secondCoordinate;
             String thirdCoordinate;
             String forthCoordinate;
             String[] input1 = input0.split(" ");
             firstCoordinate = input1[0];
             secondCoordinate = input1[1];
             thirdCoordinate = input1[2];
             forthCoordinate = input1[3];
             String[] first = firstCoordinate.split(",");
             String[] second = secondCoordinate.split(",");
             String[] third = thirdCoordinate.split(",");
             String[] forth = forthCoordinate.split(",");
             x1 = first[0];
             y1 = first[1];
             x2 = second[0];
             y2 = second[1];
             z1 = third[0];
             z2 = third[1];
             p1 = forth[0];
             p2 = forth[1];
             double a1 = Double.parseDouble(x1);
             double b1 = Double.parseDouble(y1);
             double a2 = Double.parseDouble(x2);
             double b2 = Double.parseDouble(y2);
             double a3 = Double.parseDouble(z1);
             double b3 = Double.parseDouble(z2);
             double a4 = Double.parseDouble(p1);
             double b4 = Double.parseDouble(p2);
             result0 = Math.abs((b2-b3)*a1+(a3-a2)*b1+a2*b3-b2*a3)/Math.sqrt((b2-b3)*(b2-b3) +(a2-a3)*(a2-a3));
             result1 = Math.abs((b3-b4)*a1+(a4-a3)*b1+a3*b4-b3*a4)/Math.sqrt((b3-b4)*(b3-b4) +(a3-a4)*(a3-a4));
             result2 = Math.abs((b2-b4)*a1+(a4-a2)*b1+a2*b4-b2*a4)/Math.sqrt((b2-b4)*(b2-b4) +(a2-a4)*(a2-a4));
             double a01 = Math.sqrt(Math.pow(b2-b3, 2)+Math.pow(a2-a3, 2));
             double b01 = Math.sqrt(Math.pow(b3-b4, 2)+Math.pow(a3-a4, 2));
             double c01 = Math.sqrt(Math.pow(b2-b4, 2)+Math.pow(a2-a4, 2));
             if(a01+b01<c01||b01+c01<a01||a01+c01<b01)
             {
                 System.out.print("data error");
             }
             else
             {
                 if(result0==0||result1==0||result2==0)
             System.out.print("on the triangle");
             else
             {
                 if(result0*a01+result1*b01+result2*c01==Math.sqrt(0.5*result0*(0.5*result0-a01)*(0.5*result0-b01)*(0.5*result0-c01)))
                 System.out.print("in the triangle");
                 else
                 System.out.print("outof the triangle");
             }
             }
             
        }

    }

}

题目分析:

此题应当分方法进行合理的规划,以避免代码的大量重复,类的设计以及方法的设计都十分考验Java水准,要仔细思考再下手码代码。

采坑心得:

本题我的代码不建议借鉴,可以当作一个反例,写编程题一点不要性急,重在分析题目,整理思路,可以设计一个通用的父类以减少代码的重复性,提高代码的可扩展性和复用性。

 

3.Java实现单链表功能:

使用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;

}

源代码如下:

  1 interface LinearListInterface<E> {
  2 
  3 public boolean isEmpty();
  4 
  5 public int size();
  6 
  7 public E get(int index);
  8 
  9 public void remove(int index);
 10 
 11 public void add(int index, E theElement);
 12 
 13 public void add(E element);
 14 
 15 public void printList();    
 16 
 17 }
 18 
 19 class LList<E>implements LinearListInterface<E>{
 20 
 21      private Node<E> head,curr,tail;
 22      private int size; 
 23      public boolean isEmpty() {
 24             if(this.tail==null) {
 25                 return false;
 26             }
 27             return true;
 28         }
 29      public int size() {
 30             return this.size;
 31         }
 32      public E get(int index) {
 33             int count = 0;
 34             if(index>=0&&index<=size)
 35             {
 36                 this.curr = head;
 37                 while(count<index) 
 38                 {
 39                 this.curr = this.curr.next;
 40                 count++;
 41                 }
 42                 return this.curr.o;
 43             }
 44             return null;
 45         }
 46      public void remove(int index) {
 47             int count = 0;
 48             if(index>=0&&index<=size) {
 49                 if(index==0) {
 50                     this.head = this.head.next;
 51                     this.size--;
 52                 }else {
 53                     this.curr = this.head;
 54                     while(count<index-1) {
 55                         this.curr = this.curr.next;
 56                         count++;
 57                     }
 58                     this.curr.next = this.curr.next.next;
 59                 }
 60                 this.size--;
 61             }
 62         }
 63      public void add(int index, E theElement) {
 64             int count = 0;
 65             if(index>=0&&index<=this.size) {
 66                 if(index==0) {
 67                     this.head = new Node<E>(theElement,this.head);
 68                 }else {
 69                     this.curr = head;
 70                     while(count<index-1) {
 71                         this.curr = this.curr.next;
 72                         count++;
 73                     }
 74                     this.curr.next = new Node<E>(theElement,this.curr.next);
 75                 }
 76                 this.size++;
 77             }
 78         }
 79      public void add(E element) {
 80             Node<E> node =new Node<E>(element,null);
 81             this.curr = this.tail;//
 82             this.tail = node;
 83             if(curr==null) {
 84                 head = node;
 85             }else{
 86                 curr.next = node;
 87             }
 88             this.size++;
 89         }
 90      public void printList() {
 91             for(int i = 0;i<this.size;i++) {
 92                 System.out.print(get(i).toString()+" ");
 93             }
 94         }
 95     }
 96 
 97 
 98 class Node<E>{
 99 
100      private E o;
101      
102      private Node<E> next;
103      
104     }

分析:

链表是java数据结构中一种很基础很常见却也很重要的数据结构,它的结构如下:

链表的基本操作

包括链表的增删查改,以及判别某结点是否存在链表中,链表的结构很简单,就是一个个节点连接在一起,形成一个完整的链条,每个节点包含2部分,数据域,和一个指向下一个节点引用的指针next,,再说说删除操作,同样需要找到数据所在的位置,然后进行删除,不同的是,删除的时候,链表只需要改变一下前后节点的引用关系即可,就完成了节点的删除,而没有像数组那样触发一次全部数据的移动。

 

 

 

 

4. 点与线(类设计)

  • 设计一个类表示平面直角坐标系上的点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)方法。

  • 类图如下:
  • 输入格式:

    分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

    输出格式:

    The line's color is:颜色值
    The line's begin point's Coordinate is:
    (x1,y1)
    The line's end point's Coordinate is:
    (x2,y2)
    The line's length is:长度值
  • 源代码如下:

  •   1 import java.util.Scanner;
      2 
      3 public class Main {
      4 
      5     public static void main(String[] args) {
      6         // TODO Auto-generated method stub
      7         Scanner in = new Scanner(System.in);
      8         Point p1 = new Point();
      9         String color = null;
     10         Point p2 = new Point();
     11         p1.setX(in.nextDouble());
     12         p1.setY(in.nextDouble());
     13         p2.setX(in.nextDouble());
     14         p2.setY(in.nextDouble());
     15         color = in.next();
     16         Line line =new Line( p1,  p2,  color);
     17         line.display();
     18     }
     19     
     20 }
     21 class Point{
     22     private double x;
     23     private double y;
     24     public Point() {
     25         super();
     26         // TODO Auto-generated constructor stub
     27     }
     28     public Point(double x, double y) {
     29         super();
     30         this.x = x;
     31         this.y = y;
     32     }
     33     public double getX() {
     34         return x;
     35     }
     36     public void setX(double x) {
     37         this.x = x;
     38     }
     39     public double getY() {
     40         return y;
     41     }
     42     public void setY(double y) {
     43         this.y = y;
     44     }
     45     public void display() {
     46         if(x>0&&x<=200&&y>0&&y<=200) {
     47              System.out.printf("(%.2f,%.2f)\n",x,y);
     48         }
     49         else
     50             System.out.print("Wrong Format");
     51     }
     52 }
     53 class Line{
     54     private Point point1;
     55     private Point point2;
     56     private String color;
     57     public Line() {
     58         super();
     59         // TODO Auto-generated constructor stub
     60     }
     61     public Line(Point p1, Point p2, String color) {
     62         super();
     63         this.point1 = p1;
     64         this.point2 = p2;
     65         this.color = color;
     66     }
     67     public Point getPoint1() {
     68         return point1;
     69     }
     70     public void setPoint1(Point point1) {
     71         this.point1 = point1;
     72     }
     73     public Point getPoint2() {
     74         return point2;
     75     }
     76     public void setPoint2(Point point2) {
     77         this.point2 = point2;
     78     }
     79     public String getColor() {
     80         return color;
     81     }
     82     public void setColor(String color) {
     83         this.color = color;
     84     }
     85     public double getDistence() {
     86         double result;
     87         result = Math.abs(Math.sqrt((Math.pow(point1.getY()-point2.getY(), 2))+(Math.pow(point1.getX()-point2.getX(), 2))));
     88         return result;
     89     }
     90     public void display() {
     91         if(point1.getX()<=0||point1.getX()>200||point1.getY()<=0||point1.getY()>200||point2.getX()<=0||point2.getX()>200||point2.getY()<=0||point2.getY()>200)
     92         {
     93             System.out.printf("Wrong Format");
     94         }
     95         else {
     96             System.out.printf("The line's color is:%s\n",color);
     97             System.out.printf("The line's begin point's Coordinate is:\n");
     98             point1.display();
     99             System.out.printf("The line's end point's Coordinate is:\n");
    100             point2.display();
    101             System.out.printf("The line's length is:%.2f",getDistence());
    102         }
    103         
    104     }
    105 }

    分析及采坑心得:

  • 本题主要考察对类图的运用,要知道怎么看类图,做到自己写出的代码结构的类图与题目给出的类图契合是本题的重中之重,学会按类图的要求来设计类是编程的基本功。本题较易,后面两题是这道题的层层递进,由易到难。

 

 

5.点线面问题重构(继承与多态)

 

 

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

 

  • 对题目中的点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();
  • 类结构如下图所示:
  •  

     

  • 源代码如下:
  •   1 import java.util.Scanner;
      2 
      3 public class Main {
      4 
      5     public static void main(String[] args) {
      6         // TODO Auto-generated method stub
      7         Scanner in = new Scanner(System.in);
      8         Point p1 = new Point();
      9         String color = null;
     10         Point p2 = new Point();
     11         p1.setX(in.nextDouble());
     12         p1.setY(in.nextDouble());
     13         p2.setX(in.nextDouble());
     14         p2.setY(in.nextDouble());
     15         color = in.next();
     16         Plane plane = new Plane(color);
     17         Line line =new Line( p1,  p2,  color);
     18         Element element;
     19         if(p1.getX()<=0||p1.getX()>200||p1.getY()<=0||p1.getY()>200||p2.getX()<=0||p2.getX()>200||p2.getY()<=0||p2.getY()>200)
     20         {
     21             System.out.print("Wrong Format");
     22         }else {
     23               element = p1;//起点Point
     24               element.display();
     25               
     26               element = p2;//终点Point
     27               element.display();
     28               
     29               element = line;//线段
     30               element.display();
     31               
     32               element = plane;//
     33               element.display();
     34         }
     35          
     36 
     37     }
     38     
     39 }
     40 class Point extends Element{
     41     private double x;
     42     private double y;
     43     public Point() {
     44         super();
     45         // TODO Auto-generated constructor stub
     46     }
     47     public Point(double x, double y) {
     48         super();
     49         this.x = x;
     50         this.y = y;
     51     }
     52     public double getX() {
     53         return x;
     54     }
     55     public void setX(double x) {
     56         this.x = x;
     57     }
     58     public double getY() {
     59         return y;
     60     }
     61     public void setY(double y) {
     62         this.y = y;
     63     }
     64     @Override
     65     public void display() {
     66         if(x<=0||x>200||y<=0||y>200) {
     67             System.out.print("Wrong Format");
     68             System.exit(0);
     69         }    
     70         else {
     71             System.out.printf("(%.2f,%.2f)\n",x,y);
     72         }
     73     }
     74 }
     75 class Line extends Element{
     76     private Point point1;
     77     private Point point2;
     78     private String color;
     79     public Line() {
     80         super();
     81         // TODO Auto-generated constructor stub
     82     }
     83     public Line(Point p1, Point p2, String color) {
     84         super();
     85         this.point1 = p1;
     86         this.point2 = p2;
     87         this.color = color;
     88     }
     89     public Point getPoint1() {
     90         return point1;
     91     }
     92     public void setPoint1(Point point1) {
     93         this.point1 = point1;
     94     }
     95     public Point getPoint2() {
     96         return point2;
     97     }
     98     public void setPoint2(Point point2) {
     99         this.point2 = point2;
    100     }
    101     public String getColor() {
    102         return color;
    103     }
    104     public void setColor(String color) {
    105         this.color = color;
    106     }
    107     public double getDistence() {
    108         double result;
    109         result = Math.abs(Math.sqrt((Math.pow(point1.getY()-point2.getY(), 2))+(Math.pow(point1.getX()-point2.getX(), 2))));
    110         return result;
    111     }
    112     @Override
    113     public void display() {
    114         if(point1.getX()<=0||point1.getX()>200||point1.getY()<=0||point1.getY()>200||point2.getX()<=0||point2.getX()>200||point2.getY()<=0||point2.getY()>200)
    115         {
    116             System.out.printf("Wrong Format");
    117         }
    118         else {
    119             System.out.printf("The line's color is:%s\n",color);
    120             System.out.printf("The line's begin point's Coordinate is:\n");
    121             point1.display();
    122             System.out.printf("The line's end point's Coordinate is:\n");
    123             point2.display();
    124             System.out.printf("The line's length is:%.2f\n",getDistence());
    125         }
    126         
    127     }
    128 }
    129 abstract class Element{
    130     abstract protected void display();
    131 }
    132 
    133 class Plane extends Element{
    134     String color;
    135 
    136     public Plane() {
    137         super();
    138         // TODO Auto-generated constructor stub
    139     }
    140 
    141     public Plane(String color) {
    142         super();
    143         this.color = color;
    144     }
    145 
    146     public String getColor() {
    147         return color;
    148     }
    149 
    150     public void setColor(String color) {
    151         this.color = color;
    152     }    
    153 
    154     @Override
    155     public void display() {
    156         System.out.printf("The Plane's color is:%s",color);
    157     }
    158     
    159 }

    本题分析:

  •  

    这题在第一题的基础上增加了类以及对应的方法;较第一题难度上升但同样考查对类的合理规划与设计。

 

 

 

6.ATM机类结构设计

编写一个银行 ATM 机的模拟程序,能够完成用户的存款、取款以及查询余额功能

(1)相关概念说明

⚫中国银联(China UnionPay):成立于 2002 年 3 月,是经国务院同意,中 国人民银行批准设立的中国银行卡联合组织,总部设于上海。主要负责建设和 运营全国统一的银行卡跨行信息交换网络、提供银行卡跨行信息交换相关的专 业化服务、管理和经营“银联”品牌、制定银行卡跨行交易业务规范和技术标 准。其包含多家银行机构。

⚫银行(Bank):是依法成立的经营货币信贷业务的金融机构,是商品货币经 济发展到一定阶段的产物。

⚫银行用户(User):能够在银行办理相应业务的用户。一个银行机构可以拥 有多个银行用户及多个银行账户,一个用户也可以在多个银行机构开立账户。 ⚫银行账户(Account):客户在银行开立的存款账户、贷款账户、往来账户 的总称。一个银行用户可以拥有多个银行账户。

⚫银行卡(Card):经批准由商业银行(含邮政金融机构) 向社会发行的具有 消费信用、转账结算、存取现金等全部或部分功能的信用支付工具。一个银行 账户可以发行多张银行卡。

⚫ ATM(Automated Teller Machine),自动柜员机,因大部分用于取款,又 称自动取款机。它是一种高度精密的机电一体化装置,利用磁性代码卡或智能 卡实现金融交易的自助服务,代替银行柜面人员的工作。可提取现金、查询存 款余额、进行账户之间资金划拨、余额查询等工作。一般 ATM 机隶属于某一银 行机构。

 

(2)设计要求 尝试使用面向对象技术对银行用户在 ATM 机上进行相关操作的模拟系统设 计,上述的相关概念均要设计为实体类,业务(控制)类请自行根据实际需要进 行扩充和完善。 注意:为降低难度,本次作业限定银行卡均为借记卡(不能透支),且不允 许跨行办理相关业务(例如在中国建设银行的 ATM 机上对其他银行的账户进行操 作)。 2.2 程序功能需求 (1)实现功能

 基础数据的初始化;

 用户存款、取款及查询余额功能。

 

 

 

3)输入输出规则 1)输入规则 每一行输入一次业务操作,可以输入多行,最终以字符#终止。

具体每种业 务操作输入格式如下:

➢ 存款、取款功能输入数据格式 卡号 密码 ATM 机编号 金额(由一个或多个空格分隔) 其中,当金额大于 0 时,代表取款,否则代表存款。

➢ 查询余额功能输入数据格式 卡号 2)输出规则

①输入错误处理 

如果输入卡号不存在,则输出“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 编号]上存款¥[金额] 当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

④查询余额业务输出 ¥[金额] 金额保留两位小数

 

源代码如下:

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4     
  5     public static void main(String[] args) {
  6         // TODO Auto-generated method stub
  7         Scanner in = new Scanner (System.in);
  8         CCBDate ccbdate = new CCBDate();
  9         ICBCDate icbcdate = new ICBCDate();
 10         int i = 0;
 11         String Password = null;
 12         while(true)
 13         {
 14             atmCCB atmccb = new atmCCB();
 15             atmICBC atmicbc = new atmICBC();
 16             String CardNumber;
 17             if(i!=0)
 18             {
 19                 CardNumber=Password;
 20             }
 21             else
 22             {
 23                 CardNumber = in.next();
 24             }
 25             Password = null;
 26             String AtmNumber;
 27             float Money;
 28             String Char;
 29             i=0;
 30             if(CardNumber.equals("#"))
 31             System.exit(0);
 32             else
 33             Password = in.next();
 34             if(Password.equals("#")||Password.equals(icbcdate.getUser0_CardNumber01())||Password.equals(icbcdate.getUser0_CardNumber02())||Password.equals(icbcdate.getUser0_CardNumber1())||Password.equals(icbcdate.getUser0_CardNumber2())||Password.equals(icbcdate.getUser1_CardNumber1())||Password.equals(icbcdate.getUser1_CardNumber2())||Password.equals(ccbdate.getUser0_CardNumber01())||Password.equals(ccbdate.getUser0_CardNumber02())||Password.equals(ccbdate.getUser0_CardNumber1())||Password.equals(ccbdate.getUser1_CardNumber1()))
 35             {
 36                 i++;
 37                 if(ccbdate.checkValidate(CardNumber, ccbdate))
 38                 {
 39                         if(CardNumber.equals(ccbdate.getUser0_CardNumber01())||CardNumber.equals(ccbdate.getUser0_CardNumber02()))
 40                         {
 41                             System.out.printf("¥%.2f\n",ccbdate.getUser0_Card1Money());
 42                         }
 43                         if(CardNumber.equals(ccbdate.getUser0_CardNumber1()))
 44                         {
 45                             System.out.printf("¥%.2f\n",ccbdate.getUser0_Card2Money());
 46                         }
 47                         if(CardNumber.equals(ccbdate.getUser1_CardNumber1()))
 48                         {
 49                             System.out.printf("¥%.2f\n",ccbdate.getUser1_CardMoney());
 50                         }
 51                         
 52                 }
 53                 else
 54                 if(icbcdate.checkValidate(CardNumber, icbcdate))
 55                 {
 56                     if(CardNumber.equals(icbcdate.getUser0_CardNumber1()))
 57                     {
 58                         System.out.printf("¥%.2f\n",icbcdate.getUser0_Card1Money());
 59                     }
 60                     if(CardNumber.equals(icbcdate.getUser0_CardNumber2()))
 61                     {
 62                         System.out.printf("¥%.2f\n",icbcdate.getUser0_Card2Money());
 63                     }
 64                     if(CardNumber.equals(icbcdate.getUser0_CardNumber01())||CardNumber.equals(icbcdate.getUser0_CardNumber02()))
 65                     {
 66                         System.out.printf("¥%.2f\n",icbcdate.getUser0_Card3Money());
 67                     }
 68                     if(CardNumber.equals(icbcdate.getUser1_CardNumber1()))
 69                     {
 70                         System.out.printf("¥%.2f\n",icbcdate.getUser1_Card1Money());
 71                     }
 72                     if(CardNumber.equals(icbcdate.getUser1_CardNumber2()))
 73                     {
 74                         System.out.printf("¥%.2f\n",icbcdate.getUser1_Card2Money());
 75                     }
 76                     
 77                 }
 78                 else {
 79                     System.out.println("Sorry,this card does not exist.");
 80                     System.exit(0);
 81                 }
 82                 
 83             }
 84         
 85             else
 86             {
 87                 AtmNumber = in.next();
 88                 Money = in.nextFloat();
 89                         if(ccbdate.checkValidate(CardNumber, ccbdate))
 90                         {
 91                             if(Password.equals("88888888"))
 92                             {
 93                                 atmccb.Chackvalidate(AtmNumber);
 94                                 
 95                                 if(CardNumber.equals(ccbdate.getUser0_CardNumber01())||CardNumber.equals(ccbdate.getUser0_CardNumber02()))
 96                                 {
 97                                     if(Money>0)
 98                                     {
 99                                         if(Money>ccbdate.getUser0_Card1Money())
100                                         System.out.print("Sorry,your account balance is insufficient.");
101                                         else
102                                         {
103                                             ccbdate.setUser0_Card1Money(ccbdate.getUser0_Card1Money()-Money);
104                                             System.out.print(ccbdate.getUser0_name()+"在中国建设银行的"+AtmNumber+"号ATM机上取款¥");
105                                             System.out.printf("%.2f\n",Money);
106                                             System.out.printf("当前余额为¥%.2f\n",ccbdate.getUser0_Card1Money());
107                                         }
108                                     }
109                                     else
110                                     if(Money<=0)
111                                     {
112                                         ccbdate.setUser0_Card1Money(ccbdate.getUser0_Card1Money()-Money);
113                                         System.out.print(ccbdate.getUser0_name()+"在中国建设银行的"+AtmNumber+"号ATM机上存款¥");
114                                         System.out.printf("%.2f\n",-Money);
115                                         System.out.printf("当前余额为¥%.2f\n",ccbdate.getUser0_Card1Money());
116                                         
117                                     }
118                                 }
119                                 if(CardNumber.equals(ccbdate.getUser0_CardNumber1()))
120                                 {
121                                     if(Money>0)
122                                     {
123                                         if(Money>ccbdate.getUser0_Card2Money())
124                                         System.out.print("Sorry,your account balance is insufficient.");
125                                         else
126                                         {
127                                             ccbdate.setUser0_Card2Money(ccbdate.getUser0_Card2Money()-Money);
128                                             System.out.print(ccbdate.getUser0_name()+"在中国建设银行的"+AtmNumber+"号ATM机上取款¥");
129                                             System.out.printf("%.2f\n",Money);
130                                             System.out.printf("当前余额为¥%.2f\n",ccbdate.getUser0_Card2Money());
131                                         }
132                                     }
133                                     else
134                                     if(Money<=0)
135                                     {
136                                         ccbdate.setUser0_Card2Money(ccbdate.getUser0_Card2Money()-Money);
137                                         System.out.print(ccbdate.getUser0_name()+"在中国建设银行的"+AtmNumber+"号ATM机上存款¥");
138                                         System.out.printf("%.2f\n",-Money);
139                                         System.out.printf("当前余额为¥%.2f\n",ccbdate.getUser0_Card2Money());
140                                         
141                                     }
142                                     
143                                             
144                                 }
145                                 if(CardNumber.equals(ccbdate.getUser1_CardNumber1()))
146                                 {
147                                     if(Money>0)
148                                     {
149                                         if(Money>ccbdate.getUser1_CardMoney())
150                                         System.out.print("Sorry,your account balance is insufficient.");
151                                         else
152                                         {
153                                             ccbdate.setUser1_CardMoney(ccbdate.getUser1_CardMoney()-Money);
154                                             System.out.print(ccbdate.getUser1_name()+"在中国建设银行的"+AtmNumber+"号ATM机上取款¥");
155                                             System.out.printf("%.2f\n",Money);
156                                             System.out.printf("当前余额为¥%.2f\n",ccbdate.getUser1_CardMoney());
157                                         }
158                                     }
159                                     else
160                                     if(Money<=0)
161                                     {
162                                         ccbdate.setUser1_CardMoney(ccbdate.getUser1_CardMoney()-Money);
163                                         System.out.print(ccbdate.getUser1_name()+"在中国建设银行的"+AtmNumber+"号ATM机上存款¥");
164                                         System.out.printf("%.2f\n",-Money);
165                                         System.out.printf("当前余额为¥%.2f\n",ccbdate.getUser1_CardMoney());
166                                         
167                                     }
168                                     
169                                 }
170                                 
171                                         
172                             }
173                             else {
174                                 System.out.println("Sorry,your password is wrong.");
175                                 System.exit(0);
176                             }
177                                 
178                         }
179                         else
180                         if(icbcdate.checkValidate(CardNumber, icbcdate))
181                         {
182                             
183                             if(Password.equals("88888888"))
184                             {
185                                 atmicbc.Chackvalidate(AtmNumber);
186                                 
187                                 if(CardNumber.equals(icbcdate.getUser0_CardNumber1()))
188                                 {
189                                     if(Money>0)
190                                     {
191                                         if(Money>icbcdate.getUser0_Card1Money())
192                                         System.out.print("Sorry,your account balance is insufficient.");
193                                         else
194                                         {
195                                             icbcdate.setUser0_Card1Money(icbcdate.getUser0_Card1Money()-Money);
196                                             System.out.print(icbcdate.getUser0_name()+"在中国工商银行的"+AtmNumber+"号ATM机上取款¥");
197                                             System.out.printf("%.2f\n",Money);
198                                             System.out.printf("当前余额为¥%.2f\n",icbcdate.getUser0_Card1Money());
199                                         }
200                                     }
201                                     else
202                                     if(Money<=0)
203                                     {
204                                         icbcdate.setUser0_Card1Money(icbcdate.getUser0_Card1Money()-Money);
205                                         System.out.print(icbcdate.getUser0_name()+"在中国工商银行的"+AtmNumber+"号ATM机上存款¥");
206                                         System.out.printf("%.2f\n",-Money);
207                                         System.out.printf("当前余额为¥%.2f\n",icbcdate.getUser0_Card1Money());
208                                         
209                                     }
210                                     
211                                     
212                                     
213                                 }
214                                 if(CardNumber.equals(icbcdate.getUser0_CardNumber2()))
215                                 {
216                                     if(Money>0)
217                                     {
218                                         if(Money>icbcdate.getUser0_Card2Money())
219                                         System.out.print("Sorry,your account balance is insufficient.");
220                                         else
221                                         {
222                                             icbcdate.setUser0_Card2Money(icbcdate.getUser0_Card2Money()-Money);
223                                             System.out.print(icbcdate.getUser0_name()+"在中国工商银行的"+AtmNumber+"号ATM机上取款¥");
224                                             System.out.printf("%.2f\n",Money);
225                                             System.out.printf("当前余额为¥%.2f\n",icbcdate.getUser0_Card2Money());
226                                         }
227                                     }
228                                     else
229                                     if(Money<=0)
230                                     {
231                                         icbcdate.setUser0_Card2Money(icbcdate.getUser0_Card2Money()-Money);
232                                         System.out.print(icbcdate.getUser0_name()+"在中国工商银行的"+AtmNumber+"号ATM机上存款¥");
233                                         System.out.printf("%.2f\n",-Money);
234                                         System.out.printf("当前余额为¥%.2f\n",icbcdate.getUser0_Card2Money());
235                                         
236                                     }
237                                     
238                                     
239                                     
240                                     
241                                     
242                                     
243                                 }
244                                 if(CardNumber.equals(icbcdate.getUser0_CardNumber01())||CardNumber.equals(icbcdate.getUser0_CardNumber02()))
245                                 {
246                                     if(Money>0)
247                                     {
248                                         if(Money>icbcdate.getUser0_Card3Money())
249                                         System.out.print("Sorry,your account balance is insufficient.");
250                                         else
251                                         {
252                                             icbcdate.setUser0_Card3Money(icbcdate.getUser0_Card3Money()-Money);
253                                             System.out.print(icbcdate.getUser0_name()+"在中国工商银行的"+AtmNumber+"号ATM机上取款¥");
254                                             System.out.printf("%.2f\n",Money);
255                                             System.out.printf("当前余额为¥%.2f\n",icbcdate.getUser0_Card3Money());
256                                         }
257                                     }
258                                     else
259                                     if(Money<=0)
260                                     {
261                                         icbcdate.setUser0_Card3Money(icbcdate.getUser0_Card3Money()-Money);
262                                         System.out.print(icbcdate.getUser0_name()+"在中国工商银行的"+AtmNumber+"号ATM机上存款¥");
263                                         System.out.printf("%.2f\n",-Money);
264                                         System.out.printf("当前余额为¥%.2f\n",icbcdate.getUser0_Card3Money());
265                                         
266                                     }
267                                     
268                                     
269                                     
270                                 }
271                                 if(CardNumber.equals(icbcdate.getUser1_CardNumber1()))
272                                 {
273                                     if(Money>0)
274                                     {
275                                         if(Money>icbcdate.getUser1_Card1Money())
276                                         System.out.print("Sorry,your account balance is insufficient.");
277                                         else
278                                         {
279                                             icbcdate.setUser1_Card1Money(icbcdate.getUser1_Card1Money()-Money);
280                                             System.out.print(icbcdate.getUser1_name()+"在中国工商银行的"+AtmNumber+"号ATM机上取款¥");
281                                             System.out.printf("%.2f\n",Money);
282                                             System.out.printf("当前余额为¥%.2f\n",icbcdate.getUser1_Card1Money());
283                                         }
284                                     }
285                                     else
286                                     if(Money<=0)
287                                     {
288                                         icbcdate.setUser1_Card1Money(icbcdate.getUser1_Card1Money()-Money);
289                                         System.out.print(icbcdate.getUser1_name()+"在中国工商银行的"+AtmNumber+"号ATM机上存款¥");
290                                         System.out.printf("%.2f\n",-Money);
291                                         System.out.printf("当前余额为¥%.2f\n",icbcdate.getUser1_Card1Money());
292                                         
293                                     }
294                                     
295                                     
296                                     
297                                     
298                                 }
299                                 if(CardNumber.equals(icbcdate.getUser1_CardNumber2()))
300                                 {
301                                     if(Money>0)
302                                     {
303                                         if(Money>icbcdate.getUser1_Card2Money())
304                                         System.out.print("Sorry,your account balance is insufficient.");
305                                         else
306                                         {
307                                             icbcdate.setUser1_Card2Money(icbcdate.getUser1_Card2Money()-Money);
308                                             System.out.print(icbcdate.getUser1_name()+"在中国工商银行的"+AtmNumber+"号ATM机上取款¥");
309                                             System.out.printf("%.2f\n",Money);
310                                             System.out.printf("当前余额为¥%.2f\n",icbcdate.getUser1_Card2Money());
311                                         }
312                                     }
313                                     else
314                                     if(Money<=0)
315                                     {
316                                         icbcdate.setUser1_Card2Money(icbcdate.getUser1_Card2Money()-Money);
317                                         System.out.print(icbcdate.getUser1_name()+"在中国工商银行的"+AtmNumber+"号ATM机上存款¥");
318                                         System.out.printf("%.2f\n",-Money);
319                                         System.out.printf("当前余额为¥%.2f\n",icbcdate.getUser1_Card2Money());
320                                         
321                                     }
322                                     
323                                 }
324                                 
325                                 
326                             }
327                             else {
328                                 System.out.println("Sorry,your password is wrong.");
329                                 System.exit(0);
330                             }
331                                         
332                         }
333                         else {
334                             System.out.println("Sorry,this card does not exist.");
335                             System.exit(0);
336                         }
337                         
338                 
339                 
340             }
341             
342             
343             
344         }
345     }
346 
347 }
348 
349 class CCBDate {
350     private String user0_name = "杨过";
351     private String user1_name = "郭靖";
352     private String user0_CardNumber01 = "6217000010041315709";
353     private String user0_CardNumber02 = "6217000010041315715";
354     private String user0_CardNumber1 = "6217000010041315718";
355     private String user1_CardNumber1 = "6217000010051320007";
356     private float user0_Card1Money = (float) 10000.00;
357     private float user0_Card2Money = (float) 10000.00;
358     private float user1_CardMoney = (float) 10000.00;
359     public String getUser0_name() {
360         return user0_name;
361     }
362     public void setUser0_name(String user0_name) {
363         this.user0_name = user0_name;
364     }
365     public String getUser1_name() {
366         return user1_name;
367     }
368     public void setUser1_name(String user1_name) {
369         this.user1_name = user1_name;
370     }
371     public String getUser0_CardNumber01() {
372         return user0_CardNumber01;
373     }
374     public void setUser0_CardNumber01(String user0_CardNumber01) {
375         this.user0_CardNumber01 = user0_CardNumber01;
376     }
377     public String getUser0_CardNumber02() {
378         return user0_CardNumber02;
379     }
380     public void setUser0_CardNumber02(String user0_CardNumber02) {
381         this.user0_CardNumber02 = user0_CardNumber02;
382     }
383     public String getUser0_CardNumber1() {
384         return user0_CardNumber1;
385     }
386     public void setUser0_CardNumber1(String user0_CardNumber1) {
387         this.user0_CardNumber1 = user0_CardNumber1;
388     }
389     public String getUser1_CardNumber1() {
390         return user1_CardNumber1;
391     }
392     public void setUser1_CardNumber1(String user1_CardNumber1) {
393         this.user1_CardNumber1 = user1_CardNumber1;
394     }
395     public float getUser0_Card1Money() {
396         return user0_Card1Money;
397     }
398     public void setUser0_Card1Money(float user0_Card1Money) {
399         this.user0_Card1Money = user0_Card1Money;
400     }
401     public float getUser0_Card2Money() {
402         return user0_Card2Money;
403     }
404     public void setUser0_Card2Money(float user0_Card2Money) {
405         this.user0_Card2Money = user0_Card2Money;
406     }
407     public float getUser1_CardMoney() {
408         return user1_CardMoney;
409     }
410     public void setUser1_CardMoney(float user1_CardMoney) {
411         this.user1_CardMoney = user1_CardMoney;
412     }
413     public boolean checkValidate(String CardNumber,CCBDate ccbdate)
414     {
415         if(CardNumber.equals(ccbdate.getUser0_CardNumber01())||CardNumber.equals(ccbdate.getUser0_CardNumber02())||CardNumber.equals(ccbdate.getUser0_CardNumber1())||CardNumber.equals(ccbdate.getUser1_CardNumber1()))
416         {
417             return true;
418         }
419         else
420             return false;
421     }
422     
423     
424 }
425 class ICBCDate {
426     private String user0_name = "张无忌";
427     private String user1_name = "韦小宝";
428     private String user0_CardNumber1 = "6222081502001312389";
429     private String user0_CardNumber2 = "6222081502001312390";
430     private String user0_CardNumber01 = "6222081502001312399";
431     private String user0_CardNumber02 = "6222081502001312400";
432     private String user1_CardNumber1 = "6222081502051320785";
433     private String user1_CardNumber2 = "6222081502051320786";
434     private float user0_Card1Money = (float) 10000.00;
435     private float user0_Card2Money = (float) 10000.00;
436     private float user0_Card3Money = (float) 10000.00;
437     private float user1_Card1Money = (float) 10000.00;
438     private float user1_Card2Money = (float) 10000.00;
439     public String getUser0_CardNumber02() {
440         return user0_CardNumber02;
441     }
442 
443     public void setUser0_CardNumber02(String user0_CardNumber02) {
444         this.user0_CardNumber02 = user0_CardNumber02;
445     }
446 
447     public String getUser1_CardNumber1() {
448         return user1_CardNumber1;
449     }
450 
451     public void setUser1_CardNumber1(String user1_CardNumber1) {
452         this.user1_CardNumber1 = user1_CardNumber1;
453     }
454 
455     public String getUser1_CardNumber2() {
456         return user1_CardNumber2;
457     }
458 
459     public void setUser1_CardNumber2(String user1_CardNumber2) {
460         this.user1_CardNumber2 = user1_CardNumber2;
461     }
462 
463     public String getUser0_name() {
464         return user0_name;
465     }
466 
467     public void setUser0_name(String user0_name) {
468         this.user0_name = user0_name;
469     }
470 
471     public String getUser1_name() {
472         return user1_name;
473     }
474 
475     public void setUser1_name(String user1_name) {
476         this.user1_name = user1_name;
477     }
478 
479     public String getUser0_CardNumber1() {
480         return user0_CardNumber1;
481     }
482 
483     public void setUser0_CardNumber1(String user0_CardNumber1) {
484         this.user0_CardNumber1 = user0_CardNumber1;
485     }
486 
487     public String getUser0_CardNumber2() {
488         return user0_CardNumber2;
489     }
490 
491     public void setUser0_CardNumber2(String user0_CardNumber2) {
492         this.user0_CardNumber2 = user0_CardNumber2;
493     }
494 
495     public String getUser0_CardNumber01() {
496         return user0_CardNumber01;
497     }
498 
499     public void setUser0_CardNumber01(String user0_CardNumber01) {
500         this.user0_CardNumber01 = user0_CardNumber01;
501     }
502     public float getUser0_Card1Money() {
503         return user0_Card1Money;
504     }
505 
506     public void setUser0_Card1Money(float user0_Card1Money) {
507         this.user0_Card1Money = user0_Card1Money;
508     }
509 
510     public float getUser0_Card2Money() {
511         return user0_Card2Money;
512     }
513 
514     public void setUser0_Card2Money(float user0_Card2Money) {
515         this.user0_Card2Money = user0_Card2Money;
516     }
517 
518     public float getUser0_Card3Money() {
519         return user0_Card3Money;
520     }
521 
522     public void setUser0_Card3Money(float user0_Card3Money) {
523         this.user0_Card3Money = user0_Card3Money;
524     }
525 
526     public float getUser1_Card1Money() {
527         return user1_Card1Money;
528     }
529 
530     public void setUser1_Card1Money(float user1_Card1Money) {
531         this.user1_Card1Money = user1_Card1Money;
532     }
533 
534     public float getUser1_Card2Money() {
535         return user1_Card2Money;
536     }
537 
538     public void setUser1_Card2Money(float user1_Card2Money) {
539         this.user1_Card2Money = user1_Card2Money;
540     }
541     public boolean checkValidate(String CardNumber,ICBCDate icbcdate) {
542         if(CardNumber.equals(icbcdate.getUser0_CardNumber01())||CardNumber.equals(icbcdate.getUser0_CardNumber02())||CardNumber.equals(icbcdate.getUser0_CardNumber1())||CardNumber.equals(icbcdate.getUser0_CardNumber2())||CardNumber.equals(icbcdate.getUser1_CardNumber1())||CardNumber.equals(icbcdate.getUser1_CardNumber2()))
543         {
544             return true;
545         }
546         else
547         return false;
548     }
549     
550 }
551 class atmCCB {
552     public void Chackvalidate(String AtmNumber) {
553         if(!AtmNumber.equals("01")&&!AtmNumber.equals("02")&&!AtmNumber.equals("03")&&!AtmNumber.equals("04"))
554         {
555             if(AtmNumber.equals("05")||AtmNumber.equals("06"))
556             {
557                 System.out.println("Sorry,cross-bank withdrawal is not supported.");
558                 System.exit(0);
559             }
560             else
561             {
562                 System.out.println("Sorry,the ATM's id is wrong.");
563                 System.exit(0);
564             }
565         }
566             
567     }
568 }
569 class atmICBC {
570     public void Chackvalidate(String AtmNumber) {
571         if(!AtmNumber.equals("05")&&!AtmNumber.equals("06"))
572         {
573             if(AtmNumber.equals("01")||AtmNumber.equals("02")||AtmNumber.equals("03")||AtmNumber.equals("04"))
574             {
575                 System.out.println("Sorry,cross-bank withdrawal is not supported.");
576                 System.exit(0);
577             }
578             else {
579                 System.out.println("Sorry,the ATM's id is wrong.");
580                 System.exit(0);
581             }
582         }
583         
584     }
585 }

分析:本题解答比较复杂,主要是理解题目意思,每个人对类的设计都有不同,可尝试换一种方式来解题,可以使用Java数据库来解题并运用Java的继承与多态来减少重复的代码

 

 

总结:

这次题目集的完成,是一个从易到难的过程,从开始的点线基础到面相关的运用,以点及线,以线到面的过程,是自我编程能力不断提升,从这题

目集的练习我熟悉了正确运用循环及条件来控制程序,如何正确判断输入的边界值,对数值进行判断与处理,以及如何运用Java的多态与继承来提

高代码的复用性以及代码的可拓展性来简化一些题目的流程,使其条理清晰,更通俗易懂;面对一些复杂的问题时如何将其程序化,数字化,用面

向对象的思维来思考问题,处理问题,解决问题;同时也发现了自己的写程序方面的一些不足之处,编写速度,代码的算法的优越性等等,在这些

方面,我还应该加强。同时对教师、课程、作业、实验、课上及课下组织方式等方面,我觉得十分合理,老师这么做一定有他的道理,为了帮助我

们更好的学习专业知识等等。

版权声明:本文为liusir-blog原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/liusir-blog/p/16214546.html