linyangyu  

(1)前言

  •      知识点 

  • 类和对象:

  类(class)和对象(object)是两种以计算机为载体的计算机语言的合称。对象是对客观事物的抽象,类是对对象的抽象。类是一种抽象的数据类型。

  它们的关系是,对象是类的实例,类是对象的模板。对象是通过new classname产生的,用来调用类的方法;类的构造方法 。

  • 对象交互:

        当一个对象里有多个对象的时候,那些对象之间是如何交互的,对象和对象之间的联系是如何建立的,对象如何和其他对象交流。

  • 继承与多态:

继承:就是保持已有类的特性而构造新类的过程。继承后,子类能够利用父类中定义的变量和方法,就像它们属于子类本身一样。

        多态:(1)一个类继承自某个父类时,对父类中的方法进行改写。

                    (2)重载是两个方法的名称相同,但参数不同,重载与多态没有关系。

                    (3)抽象类。

                    (4)接口(interface)

  • 对象容器:

Java具有丰富的容器,Java的容器具有丰富的功能和良好的性能。熟悉并能充分有效地利用好容器,是现代程序设计的基本能力。

  • 设计原则:

1、单一职责原则

2、里氏替换原则

3、依赖倒置原则

4、接口隔离原则

5、迪米特法则(最少知道原则)

6、开闭原则

  • 抽象 

       Java语言中,用abstract 关键字来修饰一个类时,这个类叫作抽象类。抽象类是它的所有子类的公共属性的集合,是包含一个或多

      个抽象方法的类。 抽象类可以看作是对类的进一步抽象。在面向对象领域,抽象类主要用来进行类型隐藏。

 

 

  • 题目数量
  • PTA系列
  1. 题目集4:7-1--7-3
  2. 题目集5:7-1--7-5
  3. 题目集6:7-1--7-3
  4. 期中测试:7-1--7-3
  • 超星系列
  1. List(链表1)
  2. List2(链表2)

 

  • 题目难度
  1. Easy题目集5(7-1--7-4)
  1. Medium题目集4(7-1) 题目集6(7-1,7-3)
  1. Hard题目集4(7-2,7-3)   题目集5(7-5)  题目集6(7-2)  期中测试(7-1--7-3)List  List2

 

 **********************************************************************************************************************************************************************************************************************************************************

**********************************************************************************************************************************************************************************************************************************************************

(2)设计与分析

 

  • 题目集5(7-1--7-4)
  1.  分析:这四道题目主要是对于正则表达式的基本考察还有对字符串考察,总体来说并没有难度,可能在没学正则表达式的同学有一点困难。在这里我就简单分析一下,代码就不放上来了,相信大家也不会对这四道题目有什么疑义。
    • 7-1:主要就是这一句 String check = "[1-9]\\d{4,14}";会写正则表达式那么这道题目就没有难度了。
    • 7-2:字符串可以直接比较大小,这里我就直接运用了一个冒泡排序解决问题。
    • 7-3:String check = "[A-Za-z0-9]{4}"; 这句依旧是重点。
    • 7-4:这道题我用了一种比较笨的方法进行判断,一个一个判断,虽然笨,但是并不容易出错, String check = "[2][0][2][0](([1][1-7])|([7][1-3])|([8][1-2])|([6][1]))(([1-3][0-9])|([0][1-9])|([4][0]))";

2.   我的心得:对于这四道题目,我给的定位在Easy的层次,基本上只要掌握了基本的正则表达式的运用,就不会有问题。

 

  • 题目集4(7-1) 题目集6(7-1,7-3)
  1. 分析:对于这三道题目,总结下来难度不大,题目集4三道题目有点迭代的意味,所以作为题目集4的第一题更多的是引导你完成一些基础的东西,例如:分割字符串,转换double等,题目集6的这两道题我觉得相对与他们的大哥大7-2来说,基本上就没有难度,题目集6的7-2后面我会重点分析,这道题目我也确实花费了非常多时间,7-1和7-3主要是对我们的基础知识的考查,7-1是复习正则表达式的运用,7-3是对类与对象的复习
    • 题目集4(7-1):点线的基本运用,掌握分割字符串就可以快速的完成该题,并且为后续的非常多的题目打好良好的基础知识储备。
    • 题目集6(7-1):正则表达式的基本运用, String[] s1 = s.split("\\D+");
    • 题目集6(7-3):设计银行账户,相对于之前还有一个银行取款来说相对没有那么难,是属于比较容易上手的题目啦!注意把类分好,方法一个个填进去就好了。

 

       2. 我的心得:对于这三道题目,难度不大,更多的有点像为后面的boss们做一些准备工作,像:分割字符串,转换类型,分类,点线关系等。

 

  • 题目集4(7-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",

    输出格式:

    见题目描述。

    输入样例1:

    选项1,两点重合。例如:

    1:-2,+5 -2,+5

    输出样例:

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

    points coincide
  • 分析:这道题目还是比较典型的题目,是值得一做的,将题目读完并且仔细分析题意,认真思考结构

 

  • 类图

                  

 

 

        注意:这里我的类图,后面我会对这类进行批评。。。

 

 

 

  • SourceMonitor

        

 

 

 

 

  •      源码:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        try {
            int t1=0,t2=0;
            String s = in.nextLine();
            String[] s1 = s.split(":|,| ");
            for(int i = 0;i<s.length();i++){
                if(s.charAt(i)==' ') t1++;
                else if(s.charAt(i)==',') t2++;
            }
            if(t2!=t1+1) System.out.println("Wrong Format");
            else {
                if (s1[0].charAt(0) == '1') {
                    if (s.charAt(1) == ':')
                        one(s1);
                    else
                        System.out.println("Wrong Format");
                } else if (s1[0].charAt(0) == '2') {
                    if (s.charAt(1) == ':')
                        two(s1);
                    else
                        System.out.println("Wrong Format");
                } else if (s1[0].charAt(0) == '3') {
                    if (s.charAt(1) == ':')
                        three(s1);
                    else
                        System.out.println("Wrong Format");
                } else if (s1[0].charAt(0) == '4') {
                    if (s.charAt(1) == ':')
                        four(s1);
                    else
                        System.out.println("Wrong Format");
                } else if (s1[0].charAt(0) == '5') {
                    if (s.charAt(1) == ':')
                        five(s1);
                    else
                        System.out.println("Wrong Format");
                } else {
                    System.out.println("Wrong Format");
                }
            }

        }catch (Exception e){
            System.out.println("Wrong Format");
        }
    }
    public static void one(String[] s1) {
        if(s1[1].charAt(s1[1].length()-1)=='.') {
            System.out.println("Wrong Format");return;
        }
        if(s1[2].charAt(s1[2].length()-1)=='.') {
            System.out.println("Wrong Format");return;
        }
       if(s1[1].length()>=2&&s1[1].charAt(0)=='0'){
           System.out.println("Wrong Format");return;
       }
        if(s1[2].length()>=2&&s1[2].charAt(0)=='0'){
            System.out.println("Wrong Format");return;
        }
        if(s1[3].length()>=2&&s1[3].charAt(0)=='0'){
            System.out.println("Wrong Format");return;
        }
        if(s1[4].length()>=2&&s1[4].charAt(0)=='0'){
            System.out.println("Wrong Format");return;
        }
        if(s1.length!=5){
            System.out.println("wrong number of points");return;
        }
        double x1 = Double.parseDouble(s1[1]);
        double y1 = Double.parseDouble(s1[2]);
        double x2 = Double.parseDouble(s1[3]);
        double y2 = Double.parseDouble(s1[4]);
        if (x1 == x2) {
            if (y1 == y2)
                System.out.println("points coincide");
            else
                System.out.println("Slope does not exist");
        } else {
           /* if(y1==y2){
                System.out.println("0");return;
            }*/
            double k = (y2 - y1) / (x2 - x1);
            System.out.println(k);
        }

    }

    public static void two(String[] s1){
        if(s1[1].charAt(s1[1].length()-1)=='.') {
            System.out.println("Wrong Format");return;
        }
        if(s1[2].charAt(s1[2].length()-1)=='.') {
            System.out.println("Wrong Format");return;
        }
        if(s1.length!=7){
            System.out.println("wrong number of points");return;
        }
        double x1 = Double.parseDouble(s1[1]);
        double y1 = Double.parseDouble(s1[2]);
        double x2 = Double.parseDouble(s1[3]);
        double y2 = Double.parseDouble(s1[4]);
        double x3 = Double.parseDouble(s1[5]);
        double y3 = Double.parseDouble(s1[6]);
  
        if(x2!=x3){
            double k = (y2-y3)/(x2-x3);
            double d = Math.abs(k*x1-y1+y2-k*x2) / Math.sqrt(1+ k*k);
            System.out.println(d);
        }
        else
            System.out.println(Math.abs(x1-x2));
    }

    public static void three(String[] s1){
        if(s1[1].charAt(s1[1].length()-1)=='.') {
            System.out.println("Wrong Format");return;
        }
        if(s1[2].charAt(s1[2].length()-1)=='.') {
            System.out.println("Wrong Format");return;
        }
        if(s1[1].equals("000")||s1[1].equals("00")||s1[2].equals("000")||s1[2].equals("00")||
                s1[3].equals("000")||s1[3].equals("00")||s1[4].equals("000")||s1[4].equals("00")||
                s1[5].equals("000")||s1[5].equals("00")||s1[6].equals("000")||s1[6].equals("00")) {
            System.out.println("Wrong Format");return;
        }
        if(s1.length>7&&s1.length<10){
            System.out.println("Wrong Format");return;
        }
        if(s1[1].length()>=2&&s1[1].charAt(0)=='0'){
            System.out.println("Wrong Format");return;
        }
        if(s1[2].length()>=2&&s1[2].charAt(0)=='0'){
            System.out.println("Wrong Format");return;
        }
        if(s1[3].length()>=2&&s1[3].charAt(0)=='0'){
            System.out.println("Wrong Format");return;
        }
        if(s1[4].length()>=2&&s1[4].charAt(0)=='0'){
            System.out.println("Wrong Format");return;
        }
        if(s1[5].length()>=2&&s1[5].charAt(0)=='0'){
            System.out.println("Wrong Format");return;
        }
        if(s1[6].length()>=2&&s1[6].charAt(0)=='0'){
            System.out.println("Wrong Format");return;
        }
        if(s1.length!=7){
            System.out.println("wrong number of points");return;
        }
        double x1 = Double.parseDouble(s1[1]);
        double y1 = Double.parseDouble(s1[2]);
        double x2 = Double.parseDouble(s1[3]);
        double y2 = Double.parseDouble(s1[4]);
        double x3 = Double.parseDouble(s1[5]);
        double y3 = Double.parseDouble(s1[6]);
        if ((y3 - y2) / (x3 - x2)==(y3 - y1) / (x3 - x1)) System.out.println("true");
        else System.out.println("false");

    }
    public static void four(String[] s1){
        if(s1[1].charAt(s1[1].length()-1)=='.') {
            System.out.println("Wrong Format");return;
        }
        if(s1[2].charAt(s1[2].length()-1)=='.') {
            System.out.println("Wrong Format");return;
        }
        if(s1[1].equals("000")||s1[1].equals("00")||s1[2].equals("000")||s1[2].equals("00")||
                s1[3].equals("000")||s1[3].equals("00")||s1[4].equals("000")||s1[4].equals("00")||
                s1[5].equals("000")||s1[5].equals("00")||s1[6].equals("000")||s1[6].equals("00")||
                s1[7].equals("000")||s1[7].equals("00")||s1[8].equals("000")||s1[8].equals("00")) {
            System.out.println("Wrong Format");return;
        }
        /*if(s1.length>9&&s1.length<12){
            System.out.println("Wrong Format");return;
        }*/
        if(s1.length!=9){
            System.out.println("wrong number of points");return;
        }
        double x1 = Double.parseDouble(s1[1]);
        double y1 = Double.parseDouble(s1[2]);
        double x2 = Double.parseDouble(s1[3]);
        double y2 = Double.parseDouble(s1[4]);
        double x3 = Double.parseDouble(s1[5]);
        double y3 = Double.parseDouble(s1[6]);
        double x4 = Double.parseDouble(s1[7]);
        double y4 = Double.parseDouble(s1[8]);
        double k1 = (y3 - y4) / (x3 - x4);
        double k2 = (y2 - y1) / (x2 - x1);
        if ((x1 == x2&&y1 == y2)||(x4==x3&&y4==y3)) {
            System.out.println("points coincide");return;
        }
        else if(x1==x2&&x3==x4)  {System.out.println("true");return;}
        if (k1 == k2) System.out.println("true");
        else System.out.println("false");

    }
    public static void five(String[] s1) {
        if(s1[1].charAt(s1[1].length()-1)=='.') {
            System.out.println("Wrong Format");return;
        }
        if(s1[2].charAt(s1[2].length()-1)=='.') {
            System.out.println("Wrong Format");return;
        }
        if(s1[1].equals("000")||s1[1].equals("00")||s1[2].equals("000")||s1[2].equals("00")||
                s1[3].equals("000")||s1[3].equals("00")||s1[4].equals("000")||s1[4].equals("00")||
                s1[5].equals("000")||s1[5].equals("00")||s1[6].equals("000")||s1[6].equals("00")||
                s1[7].equals("000")||s1[7].equals("00")||s1[8].equals("000")||s1[8].equals("00")) {
            System.out.println("Wrong Format");return;
        }
        /*if(s1.length>9&&s1.length<12){
            System.out.println("Wrong Format");return;
        }*/
        if(s1.length!=9){
            System.out.println("wrong number of points");return;
        }
        double x1 = Double.parseDouble(s1[1]);
        double y1 = Double.parseDouble(s1[2]);
        double x2 = Double.parseDouble(s1[3]);
        double y2 = Double.parseDouble(s1[4]);
        double x3 = Double.parseDouble(s1[5]);
        double y3 = Double.parseDouble(s1[6]);
        double x4 = Double.parseDouble(s1[7]);
        double y4 = Double.parseDouble(s1[8]);
        double k2 = (y3 - y4) / (x3 - x4);
        double k1 = (y2 - y1) / (x2 - x1);
        double b1 = y2 - k1 * x2;
        double b2 = y4 - k2 * x4;
        float xx,yy;
        if ((x1 == x2&&y1 == y2)||(x4==x3&&y4==y3)) {
            System.out.println("points coincide");return;
        }
        if((x1==x2&&x3==x4)){
            System.out.println("is parallel lines,have no intersection point");return;
        }
        else if(x1!=x2&&x3==x4){
            xx=(float)x3;yy=(float)(k1*xx+b1);
            System.out.print(xx + "," + yy);
            System.out.println(" false");return;
        }
        else if(x1==x2&&x3!=x4){
            xx=(float)x1;yy=(float)(k2*xx+b2);
            System.out.print(xx + "," + yy);
            System.out.println(" false");return;
        }
        if (k1 == k2) System.out.println("is parallel lines,have no intersection point");
        else {
            xx = (float)((b2 - b1) / (k1 - k2));
            yy = (float)(k1 * xx + b1);
            System.out.print(xx + "," + yy);
            if ((xx >= x1 && xx >= x2&&xx >= x3&&xx >= x4) || (y1<= yy && yy>= y2&&yy>= y3&&yy>= y4) || (xx <= x1 && xx <= x2&&xx <= x3&&xx <= x4)|| (y1>= yy && yy>= y2&&yy<= y3&&yy<= y4))
                System.out.println(" false");
            else
                System.out.println(" true");
        }
    }
}

 

  • 我的心得:对于这道题目,我觉得难度还是有点的,我给出难度等级是Hard♥,我觉得我的类图是存在极大的问题的,这也是因为最开始犯了上手直接写代码的错误,导致了代码结构并没有分析清楚,应该将点类和线类分开。这里的一部分难点也在于错误格式的输入,例如:01或者1.等格式型错误,这些是需要认真揣摩和考虑的(非常难想),还有就是精度问题:double和float,斜率存不存在等等细节问题都是需要考虑的。

 

  • 题目集4(7-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",

    输入样例1:

    选项4,定义线的两点重合。例如:

    4:1,0 1,0 0,0 2,0 4,0
    
     

    输出样例:

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

    points coincide
  • 分析:对于这道题,我们可以看到依旧要仔细分析我们的程序设计,设计是非常重要滴,如果像我一样直接上手代码,非常容易混乱,并且有很多东西很难保证全部考虑清楚。

 

  • 类图
  •  

     (ps:再次批评这种类图)

 

  • SourceMonitor
  •  

 

  • 源码
    import java.text.DecimalFormat;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            try {
                int t1=0,t2=0;
                String s = in.nextLine();
                String[] s1 = s.split(":|,| ");
                for(int i = 0;i<s.length();i++){
                    if(s.charAt(i)==' ') t1++;
                    else if(s.charAt(i)==',') t2++;
                }
                if(t2!=t1+1) System.out.println("Wrong Format");
                else {
                    if (s1[0].charAt(0) == '1') {
                        if (s.charAt(1) == ':')
                            one(s1);
                        else
                            System.out.println("Wrong Format");
                    } else if (s1[0].charAt(0) == '2') {
                        if (s.charAt(1) == ':')
                            two(s1);
                        else
                            System.out.println("Wrong Format");
                    } else if (s1[0].charAt(0) == '3') {
                        if (s.charAt(1) == ':')
                            three(s1);
                        else
                            System.out.println("Wrong Format");
                    } else if (s1[0].charAt(0) == '4') {
                        if (s.charAt(1) == ':')
                            four(s1);
                        else
                            System.out.println("Wrong Format");
                    } else if (s1[0].charAt(0) == '5') {
                        if (s.charAt(1) == ':')
                            five(s1);
                        else
                            System.out.println("Wrong Format");
                    } else {
                        System.out.println("Wrong Format");
                    }
                }
    
            }catch (Exception e){
                System.out.println("Wrong Format");
            }
        }
        public static void one(String[] s1) {
            if(s1[1].charAt(s1[1].length()-1)=='.') {
                System.out.println("Wrong Format");return;
            }
            if(s1[2].charAt(s1[2].length()-1)=='.') {
                System.out.println("Wrong Format");return;
            }
            if(s1[1].equals("000")||s1[1].equals("00")||s1[2].equals("000")||s1[2].equals("00")||
                    s1[3].equals("000")||s1[3].equals("00")||s1[4].equals("000")||s1[4].equals("00")||
                    s1[5].equals("000")||s1[5].equals("00")||s1[6].equals("000")||s1[6].equals("00")) {
                System.out.println("Wrong Format");return;
            }
            if(s1.length>7&&s1.length<10){
                System.out.println("Wrong Format");return;
            }
            if(s1[1].length()>=2&&s1[1].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[2].length()>=2&&s1[2].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[3].length()>=2&&s1[3].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[4].length()>=2&&s1[4].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[5].length()>=2&&s1[5].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[6].length()>=2&&s1[6].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1.length!=7){
                System.out.println("wrong number of points");return;
            }
    
            double x1 = Double.parseDouble(s1[1]);
            double y1 = Double.parseDouble(s1[2]);
            double x2 = Double.parseDouble(s1[3]);
            double y2 = Double.parseDouble(s1[4]);
            double x3 = Double.parseDouble(s1[5]);
            double y3 = Double.parseDouble(s1[6]);
            double d1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
            double d2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
            double d3 = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
            if(d1+d2>d3&&d1+d3>d2&&d2+d3>d1&&d1-d2<d3&&d1-d3<d2&&d2-d3<d1){
                if(d1==d2||d1==d3||d2==d3){
                    System.out.print("true ");
                    if((d1==d2&&d2==d3)){
                        System.out.println("true");
                    }
                    else{
                        System.out.println("false");
                    }
                }
                else{
                    System.out.println("false false");
                }
            }
            else{
                System.out.println("data error");
            }
        }
    
        public static void two(String[] s1){
            if(s1[1].charAt(s1[1].length()-1)=='.') {
                System.out.println("Wrong Format");return;
            }
            if(s1[2].charAt(s1[2].length()-1)=='.') {
                System.out.println("Wrong Format");return;
            }
            if(s1[1].equals("000")||s1[1].equals("00")||s1[2].equals("000")||s1[2].equals("00")||
                    s1[3].equals("000")||s1[3].equals("00")||s1[4].equals("000")||s1[4].equals("00")||
                    s1[5].equals("000")||s1[5].equals("00")||s1[6].equals("000")||s1[6].equals("00")) {
                System.out.println("Wrong Format");return;
            }
            if(s1.length>7&&s1.length<10){
                System.out.println("Wrong Format");return;
            }
            if(s1[1].length()>=2&&s1[1].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[2].length()>=2&&s1[2].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[3].length()>=2&&s1[3].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[4].length()>=2&&s1[4].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[5].length()>=2&&s1[5].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[6].length()>=2&&s1[6].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1.length!=7){
                System.out.println("wrong number of points");return;
            }
            double x1 = Double.parseDouble(s1[1]);
            double y1 = Double.parseDouble(s1[2]);
            double x2 = Double.parseDouble(s1[3]);
            double y2 = Double.parseDouble(s1[4]);
            double x3 = Double.parseDouble(s1[5]);
            double y3 = Double.parseDouble(s1[6]);
            double d1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
            double d2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
            double d3 = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
            double s = Math.sqrt((d1+d2+d3)*(d1+d2-d3)*(d1+d3-d2)*(d2+d3-d1))/4;
    
            if(d1+d2>d3&&d1+d3>d2&&d2+d3>d1&&d1-d2<d3&&d1-d3<d2&&d2-d3<d1){
                System.out.println(new DecimalFormat("0.0#####").format(d1+d2+d3)+" "+new DecimalFormat("0.0#####").format(s)+
                        " "+new DecimalFormat("0.0#####").format((x1+x2+x3)/3)+","+new DecimalFormat("0.0#####").format((y1+y2+y3)/3));
            }
    
            else{
                System.out.println("data error");
            }
        }
    
        public static void three(String[] s1){
            if(s1[1].charAt(s1[1].length()-1)=='.') {
                System.out.println("Wrong Format");return;
            }
            if(s1[2].charAt(s1[2].length()-1)=='.') {
                System.out.println("Wrong Format");return;
            }
            if(s1[1].equals("000")||s1[1].equals("00")||s1[2].equals("000")||s1[2].equals("00")||
                    s1[3].equals("000")||s1[3].equals("00")||s1[4].equals("000")||s1[4].equals("00")||
                    s1[5].equals("000")||s1[5].equals("00")||s1[6].equals("000")||s1[6].equals("00")) {
                System.out.println("Wrong Format");return;
            }
            if(s1.length>7&&s1.length<10){
                System.out.println("Wrong Format");return;
            }
            if(s1[1].length()>=2&&s1[1].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[2].length()>=2&&s1[2].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[3].length()>=2&&s1[3].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[4].length()>=2&&s1[4].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[5].length()>=2&&s1[5].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1[6].length()>=2&&s1[6].charAt(0)=='0'){
                System.out.println("Wrong Format");return;
            }
            if(s1.length!=7){
                System.out.println("wrong number of points");return;
            }
            double x1 = Double.parseDouble(s1[1]);
            double y1 = Double.parseDouble(s1[2]);
            double x2 = Double.parseDouble(s1[3]);
            double y2 = Double.parseDouble(s1[4]);
            double x3 = Double.parseDouble(s1[5]);
            double y3 = Double.parseDouble(s1[6]);
            double d1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
            double d2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
            double d3 = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
            if(d1+d2>d3&&d1+d3>d2&&d2+d3>d1&&d1-d2<d3&&d1-d3<d2&&d2-d3<d1){
                if(Math.abs(d1*d1-d2*d2-d3*d3)<=0.001||Math.abs(d2*d2-d1*d1-d3*d3)<=0.001||Math.abs(d3*d3-d2*d2-d1*d1)<=0.001){
                    System.out.println("false true false");
                }
                else if(d1*d1>d2*d2+d3*d3||d2*d2>d1*d1+d3*d3||d3*d3>d2*d2+d1*d1){
                    System.out.println("true false false");
                }
                else{
                    System.out.println("false false true");
                }
            }
    
            else{
                System.out.println("data error");
            }
    
        }
        public static void four(String[] s1){
            if(s1[1].charAt(s1[1].length()-1)=='.') {
                System.out.println("Wrong Format");return;
            }
            if(s1[2].charAt(s1[2].length()-1)=='.') {
                System.out.println("Wrong Format");return;
            }
            if(s1[1].equals("000")||s1[1].equals("00")||s1[2].equals("000")||s1[2].equals("00")||
                    s1[3].equals("000")||s1[3].equals("00")||s1[4].equals("000")||s1[4].equals("00")||
                    s1[5].equals("000")||s1[5].equals("00")||s1[6].equals("000")||s1[6].equals("00")||
                    s1[7].equals("000")||s1[7].equals("00")||s1[8].equals("000")||s1[8].equals("00")) {
                System.out.println("Wrong Format");return;
            }
            if(s1.length!=11){
                System.out.println("wrong number of points");return;
            }
            double x1 = Double.parseDouble(s1[1]);
            double y1 = Double.parseDouble(s1[2]);
            double x2 = Double.parseDouble(s1[3]);
            double y2 = Double.parseDouble(s1[4]);
            double x3 = Double.parseDouble(s1[5]);
            double y3 = Double.parseDouble(s1[6]);
            double x4 = Double.parseDouble(s1[7]);
            double y4 = Double.parseDouble(s1[8]);
            double x5 = Double.parseDouble(s1[9]);
            double y5 = Double.parseDouble(s1[10]);
            double d1 = Math.sqrt((x4-x3)*(x4-x3)+(y4-y3)*(y4-y3));
            double d2 = Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5));
            double d3 = Math.sqrt((x5-x3)*(x5-x3)+(y5-y3)*(y5-y3));
            double k1 = (y2 - y1) / (x2 - x1);
            double k2 = (y4 - y3) / (x4 - x3);
            double k3 = (y5 - y4) / (x5 - x4);
            double k4 = (y5 - y3) / (x5 - x3);
            double b1 = y2 - k1 * x2;
            double b2 = y4 - k2 * x4;
            double b3 = y4 - k3 * x4;
            double b4 = y5 - k4 * x5;
            if(x1==x2&&y1==y2){
                System.out.println("points coincide");return;
            }
            if(d1+d2>d3&&d1+d3>d2&&d2+d3>d1&&d1-d2<d3&&d1-d3<d2&&d2-d3<d1) {
                int count=0;
                if((k1*x3+b1>y3&&k1*x4+b1>y4&&k1*x5+b1>y5)||(k1*x3+b1<y3&&k1*x4+b1<y4&&k1*x5+b1<y5)||(x1==x2&&x1<x3&&x1<x4&&x1<x5)||(x1==x2&&x1>x3&&x1>x4&&x1>x5))
                    count=0;
                else if((y3==k1*x3+b1&&y4>k1*x4+b1&&y5>k1*x5+b1)||(y3==k1*x3+b1&&y4<k1*x4+b1&&y5<k1*x5+b1)||
                        (y4==k1*x4+b1&&y3>k1*x3+b1&&y5>k1*x5+b1)||(y4==k1*x4+b1&&y3<k1*x3+b1&&y5<k1*x5+b1)||
                        (y5==k1*x5+b1&&y4>k1*x4+b1&&y3>k1*x3+b1)||(y5==k1*x5+b1&&y4<k1*x4+b1&&y3<k1*x3+b1)||
                        (x1==x2&&((x3==x1&&x4>x1&&x5>x1)||(x3==x1&&x4<x1&&x5<x1)||(x4==x1&&x3>x1&&x5>x1)||(x4==x1&&x3<x1&&x5<x1)||(x5==x1&&x4>x1&&x3>x1)||(x5==x1&&x4<x1&&x3<x1))))
                    count=1;
                else count=2;
                if(count!=2){
                    System.out.println(count);
                }
                else {
                    double area =Math.abs(0.5*(x3*y4-x3*y5+x4*y5-x4*y3+x5*y3-x5*y4));
                    if(x1==x2){
                        if((x3>x1&&x4<=x1&&x5<=x1)||(x3<x1&&x4>=x1&&x5>=x1)){
                            double xx1 = x1;
                            double yy1 = (k2 * xx1 + b2);
                            double xx3 = x1;
                            double yy3 = (k4 * xx3 + b4);
                            double area1 = Math.abs(0.5 * (xx3 * y3 - xx3 * yy1 + x3 * yy1 - x3 * yy3 + xx1 * yy3 - xx1 * y3));
                            double area2 = area - area1;
                            if(area1>area2)
                                System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                            else
                                System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                        }
                        else if((x4>x1&&x3<=x1&&x5<=x1)||(x4<x1&&x3>=x1&&x5>=x1)){
                            double xx1 = x1;
                            double yy1 = (k2 * xx1 + b2);
                            double xx2 = x1;
                            double yy2 = (k3 * xx2 + b3);
                            double area1 = Math.abs(0.5 * (xx2 * y4 - xx2 * yy1 + x4 * yy1 - x4 * yy2 + xx1 * yy2 - xx1 * y4));
                            double area2 = area - area1;
                            if(area1>area2)
                                System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                            else
                                System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                        }
                        else if((x5>x1&&x3<=x1&&x4<=x1)||(x5<x1&&x3>=x1&&x4>=x1)){
                            double xx3 = x1;
                            double yy3 = (k4 * xx3 + b4);
                            double xx2 = x1;
                            double yy2 = (k3 * xx2 + b3);
                            double area1 = Math.abs(0.5 * (xx2 * y5 - xx2 * yy3 + x5 * yy3 - x5 * yy2 + xx3 * yy2 - xx3 * y5));
                            double area2 = area - area1;
                            if(area1>area2)
                                System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                            else
                                System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                        }
                    }
                    else {
                        if(x3==x4){
                            if((k1*x3+b1<y3&&k1*x4+b1>y4&&k1*x5+b1>y5)||(k1*x3+b1>y3&&k1*x4+b1<y4&&k1*x5+b1<y5)){
                                double xx1 = x3;
                                double yy1 = (k1 * xx1 + b1);
                                double xx2 = ((b4 - b1) / (k4 - k1));
                                double yy2 = (k1 * xx1 + b1);
                                double area1 = Math.abs(0.5 * (xx2 * y3 - xx2 * yy1 + x3 * yy1 - x3 * yy2 + xx1 * yy2 - xx1 * y3));
                                double area2 = area - area1;
                                if(area1>area2)
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                                else
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                            }
                            else if((k1*x4+b1<y4&&k1*x3+b1>y3&&k1*x5+b1>y5)||(k1*x4+b1>y4&&k1*x3+b1<y3&&k1*x5+b1<y5)){
                                double xx1 = x4;
                                double yy1 = (k1 * xx1 + b1);
                                double xx2 = ((b3 - b1) / (k3 - k1));
                                double yy2 = (k1 * xx1 + b1);
                                double area1 = Math.abs(0.5 * (xx2 * y3 - xx2 * yy1 + x3 * yy1 - x3 * yy2 + xx1 * yy2 - xx1 * y3));
                                double area2 = area - area1;
                                if(area1>area2)
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                                else
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                            }
                            else{
                                double xx1 = ((b3 - b1) / (k3 - k1));
                                double yy1 = (k1 * xx1 + b1);
                                double xx2 = ((b4 - b1) / (k4 - k1));
                                double yy2 = (k1 * xx2 + b1);
                                double area1 = Math.abs(0.5 * (xx2 * y5 - xx2 * yy1 + x5 * yy1 - x5 * yy2 + xx1 * yy2 - xx1 * y5));
                                double area2 = area - area1;
                                if(area1>area2)
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                                else
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                            }
                        }
                        else if(x4==x5){
                            if((k1*x5+b1<y5&&k1*x4+b1>y4&&k1*x3+b1>y3)||(k1*x5+b1>y5&&k1*x4+b1<y4&&k1*x3+b1<y3)){
                                double xx1 = x5;
                                double yy1 = (k1 * xx1 + b1);
                                double xx2 = ((b4 - b1) / (k4 - k1));
                                double yy2 = (k1 * xx1 + b1);
                                double area1 = Math.abs(0.5 * (xx2 * y5 - xx2 * yy1 + x5 * yy1 - x5 * yy2 + xx1 * yy2 - xx1 * y5));
                                double area2 = area - area1;
                                if(area1>area2)
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                                else
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                            }
                            else if((k1*x4+b1<y4&&k1*x3+b1>y3&&k1*x5+b1>y5)||(k1*x4+b1>y4&&k1*x3+b1<y3&&k1*x5+b1<y5)){
                                double xx1 = x4;
                                double yy1 = (k1 * xx1 + b1);
                                double xx2 = ((b2 - b1) / (k2 - k1));
                                double yy2 = (k1 * xx1 + b1);
                                double area1 = Math.abs(0.5 * (xx2 * y5 - xx2 * yy1 + x5 * yy1 - x5 * yy2 + xx1 * yy2 - xx1 * y5));
                                double area2 = area - area1;
                                if(area1>area2)
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                                else
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                            }
                            else{
                                double xx1 = ((b3 - b1) / (k3 - k1));
                                double yy1 = (k1 * xx1 + b1);
                                double xx2 = ((b2 - b1) / (k2 - k1));
                                double yy2 = (k1 * xx2 + b1);
                                double area1 = Math.abs(0.5 * (xx2 * y3 - xx2 * yy1 + x3 * yy1 - x3 * yy2 + xx1 * yy2 - xx1 * y3));
                                double area2 = area - area1;
                                if(area1>area2)
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                                else
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                            }
    
                        }
                        else if(x3==x5){
                            if((k1*x3+b1<y3&&k1*x4+b1>y4&&k1*x5+b1>y5)||(k1*x3+b1>y3&&k1*x4+b1<y4&&k1*x5+b1<y5)){
                                double xx1 = x3;
                                double yy1 = (k1 * xx1 + b1);
                                double xx2 = ((b2 - b1) / (k2 - k1));
                                double yy2 = (k1 * xx1 + b1);
                                double area1 = Math.abs(0.5 * (xx2 * y3 - xx2 * yy1 + x3 * yy1 - x3 * yy2 + xx1 * yy2 - xx1 * y3));
                                double area2 = area - area1;
                                if(area1>area2)
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                                else
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                            }
                            else if((k1*x5+b1<y5&&k1*x3+b1>y3&&k1*x4+b1>y4)||(k1*x5+b1>y5&&k1*x3+b1<y3&&k1*x4+b1<y4)){
                                double xx1 = x5;
                                double yy1 = (k1 * xx1 + b1);
                                double xx2 = ((b2 - b1) / (k2 - k1));
                                double yy2 = (k1 * xx1 + b1);
                                double area1 = Math.abs(0.5 * (xx2 * y3 - xx2 * yy1 + x3 * yy1 - x3 * yy2 + xx1 * yy2 - xx1 * y3));
                                double area2 = area - area1;
                                if(area1>area2)
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                                else
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                            }
                            else{
                                double xx1 = ((b3 - b1) / (k3 - k1));
                                double yy1 = (k1 * xx1 + b1);
                                double xx2 = ((b2 - b1) / (k2 - k1));
                                double yy2 = (k1 * xx2 + b1);
                                double area1 = Math.abs(0.5 * (xx2 * y4 - xx2 * yy1 + x4 * yy1 - x4 * yy2 + xx1 * yy2 - xx1 * y4));
                                double area2 = area - area1;
                                if(area1>area2)
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                                else
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                            }
                        }
                        else {
                            double xx1 = ((b2 - b1) / (k1 - k2));
                            double yy1 = (k2 * xx1 + b2);
                            double xx2 = ((b3 - b1) / (k1 - k3));
                            double yy2 = (k3 * xx2 + b3);
                            double xx3 = ((b4 - b1) / (k1 - k4));
                            double yy3 = (k4 * xx3 + b4);
                            if ((k1 * x3 + b1 < y3 && k1 * x4 + b1 > y4 && k1 * x5 + b1 > y5) || (k1 * x3 + b1 > y3 && k1 * x4 + b1 < y4 && k1 * x5 + b1 < y5)) {
                                double area1 = Math.abs(0.5 * (xx3 * y3 - xx3 * yy1 + x3 * yy1 - x3 * yy3 + xx1 * yy3 - xx1 * y3));
                                double area2 = area - area1;
                                if(area1>area2)
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                                else
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                            } else if ((k1 * x4 + b1 < y4 && k1 * x3 + b1 > y3 && k1 * x5 + b1 > y5) || (k1 * x4 + b1 > y4 && k1 * x3 + b1 < y3 && k1 * x5 + b1 < y5)) {
                                double area1 = Math.abs(0.5 * (xx2 * y4 - xx2 * yy1 + x4 * yy1 - x4 * yy2 + xx1 * yy2 - xx1 * y4));
                                double area2 = area - area1;
                                if(area1>area2)
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                                else
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                            } else if ((k1 * x5 + b1 < y5 && k1 * x3 + b1 > y3 && k1 * x4 + b1 > y4) || (k1 * x5 + b1 > y5 && k1 * x3 + b1 < y3 && k1 * x4 + b1 < y4)) {
                                double area1 = Math.abs(0.5 * (xx2 * y5 - xx2 * yy3 + x5 * yy3 - x5 * yy2 + xx3 * yy2 - xx3 * y5));
                                double area2 = area - area1;
                                if(area1>area2)
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
                                else
                                    System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
                            }
                        }
                    }
    
                }
                //}
            }
    
            else{
                System.out.println("data error");
            }
    
        }
        public static void five(String[] s1) {
            if(s1[1].charAt(s1[1].length()-1)=='.') {
                System.out.println("Wrong Format");return;
            }
            if(s1[2].charAt(s1[2].length()-1)=='.') {
                System.out.println("Wrong Format");return;
            }
            if(s1.length!=9){
                System.out.println("wrong number of points");return;
            }
            double x1 = Double.parseDouble(s1[1]);
            double y1 = Double.parseDouble(s1[2]);
            double x2 = Double.parseDouble(s1[3]);
            double y2 = Double.parseDouble(s1[4]);
            double x3 = Double.parseDouble(s1[5]);
            double y3 = Double.parseDouble(s1[6]);
            double x4 = Double.parseDouble(s1[7]);
            double y4 = Double.parseDouble(s1[8]);
            double k1 = (1-y1)/(1-x1);
            double k2 = (y3 - y2) / (x3 - x2);
            double k3 = (y4 - y3) / (x4 - x3);
            double k4 = (y4 - y2) / (x4 - x2);
            double b1 = y1-k1*x1;
            double b2 = y3 - k2 * x3;
            double b3 = y3 - k3 * x3;
            double b4 = y4 - k4 * x4;
            double area=0,area1=0,area2=0,area3=0;
            area = Math.abs(0.5*(x2*y3-x2*y4+x3*y4-x3*y2+x4*y2-x4*y3));
            area1=Math.abs(0.5*(x1*y3-x1*y4+x3*y4-x3*y1+x4*y1-x4*y3));
            area2=Math.abs(0.5*(x1*y2-x1*y4+x2*y4-x2*y1+x4*y1-x4*y2));
            area3=Math.abs(0.5*(x1*y3-x1*y2+x3*y2-x3*y1+x2*y1-x2*y3));
            if(area==area1+area2+area3&&y1!=k2*x1+b2&&y1!=k3*x1+b3&&y1!=k4*x1+b4) {
                System.out.println("in the triangle");
            }
            else if(area!=area1+area2+area3){
                System.out.println("outof the triangle");
            }
            else{
                System.out.println("on the triangle");
            }
    
        }
        private static String strs(String str) {
            if (str.indexOf(".") > 0) {
                str = str.replaceAll("0+?$", "");
            }
            if(str.charAt(0)=='.'){
                return '0'+str;
            }
            if(str.charAt(str.length()-1)=='.'){
                return str+'0';
            }
            return str;
        }
    }

     

  • 我的心得:对于这道题目,我们来分析一下,真正可以把这道题目拿到满分的其实很少的,我给这道题目难度等级是Hard♥♥♥,那么我们就可以发现到我们同学的一些共有的小毛病(当然包括我自己),这道题目的细节以及结构考虑还有细节考虑都是存在或多或少的问题,例如这里格式错误的测试点相当***钻,如果你不仔细考虑,是绝对不可能把这道题目拿到满分的,还有一些斜率不存在的情况等等都是我们需要仔细考虑的。

 

  • 题目集5(7-5)
  • 设计ATM仿真系统,具体要求参见作业说明。
    OO作业8-1题目说明.pdf

    输入格式:

    每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:

    • 存款、取款功能输入数据格式:
      卡号 密码 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编号]上存款¥[金额]

    当前余额为¥[金额]

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

    ④查询余额业务输出

    ¥[金额]

    金额保留两位小数。

    输入样例1:

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

    6222081502001312390 88888888 06 -500.00
    #
    
     

    输出样例1:

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

    张无忌在中国工商银行的06号ATM机上存款¥500.00当前余额为¥10500.00
  • 分析:这道题目考验了大家的类和对象的基本要求,为后面的一个究极大boss做准备,整体难度还是有的,做好分析和结构构建。

 

  • 类图
  •  

     

  • SourceMonitor
  •  

     

  • 源码

  • import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Account account = new Account();
            User user = new User();
            Card card = new Card();
            ATM atm = new ATM();
            Bank bank = new Bank();
            String[] accNum = {"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007","6222081502001312389","6222081502001312390","6222081502001312399","6222081502001312400","6222081502051320785","6222081502051320786"};
            Scanner in = new Scanner(System.in);
            String check = "#";
            StringBuffer s1 = new StringBuffer();
            while(true) {
                String s = in.nextLine();
                if (s.matches(check)) {
                    break;
                }
                else{
                    s1.append(s);
                    s1.append("\n");
                }
            }
            String data = String.valueOf(s1);
            String[] data1 = data.split("\n");
    
           for(int i = 0; i < data1.length; i++){
                String[] data2 = data1[i].split("\\s+");
                if(data2.length==1){
                    if(card.check(data2[0])) {
                        for (int j = 0; j < accNum.length; j++) {
                            if (accNum[j].equals(data2[0])) {
                                if (j == 0 || j == 1 || j == 2)
                                    System.out.printf("¥%.2f\n", account.getyMoney());
                                else if (j == 3) {
                                    System.out.printf("¥%.2f\n", account.getgMoney());
                                } else if (j <= 7) {
                                    System.out.printf("¥%.2f\n", account.getzMoney());
                                } else {
                                    System.out.printf("¥%.2f\n", account.getwMoney());
                                }
                            }
                        }
                    }
                    else{
                        System.out.println("Sorry,this card does not exist.");
                    }
                }
                else{
                    if(card.check(data2[0])) {
                        if(user.code(data2[1])){
                            if(atm.check(data2[2])){
                                if(bank.check(data2[0],data2[2])){
                                    double money = Double.parseDouble(data2[3]);
                                    for (int j = 0; j < accNum.length; j++) {
                                        if (accNum[j].equals(data2[0])) {
                                            if (j == 0 || j == 1 || j == 2) {
                                                if (money > 0) {
                                                    if (money > account.getyMoney()) {
                                                        System.out.println("Sorry,your account balance is insufficient.");
                                                        System.exit(0);
                                                    }
                                                    account.setyMoney(account.getyMoney() - money);
                                                    System.out.printf("杨过在中国建设银行的%s号ATM机上取款¥%s\n当前余额为¥%.2f\n", data2[2],data2[3],account.getyMoney());
                                                }
                                                else{
                                                    account.setyMoney(account.getyMoney() - money);
                                                    System.out.printf("杨过在中国建设银行的%s号ATM机上存款¥%.2f\n当前余额为¥%.2f\n", data2[2],(-1)*money, account.getyMoney());
                                                }
    
                                            }
                                            else if (j == 3) {
                                                if (money > 0) {
                                                    if (money > account.getgMoney()) {
                                                        System.out.println("Sorry,your account balance is insufficient.");
                                                    }
                                                    account.setgMoney(account.getgMoney() - money);
                                                    System.out.printf("郭靖在中国建设银行的%s号ATM机上取款¥%s\n当前余额为¥%.2f\n", data2[2], data2[3], account.getgMoney());
                                                }
                                                else{
                                                    account.setgMoney(account.getgMoney() - money);
                                                    System.out.printf("郭靖在中国建设银行的%s号ATM机上存款¥%.2f\n当前余额为¥%.2f\n", data2[2],(-1)*money, account.getgMoney());
                                                }
                                            }
                                            else if (j <= 7) {
                                                if (money > 0) {
                                                    if (money > account.getzMoney()) {
                                                        System.out.println("Sorry,your account balance is insufficient.");
                                                    }
                                                    account.setzMoney(account.getzMoney() - money);
                                                    System.out.printf("张无忌在中国工商银行的%s号ATM机上取款¥%s\n当前余额为¥%.2f\n", data2[2], data2[3], account.getzMoney());
                                                }
                                                else{
                                                    account.setzMoney(account.getzMoney() - money);
                                                    System.out.printf("张无忌在中国工商银行的%s号ATM机上存款¥%.2f\n当前余额为¥%.2f\n", data2[2],(-1)*money, account.getzMoney());
                                                }
                                            } else {
                                                if (money > 0) {
                                                    if (money > account.getwMoney()) {
                                                        System.out.println("Sorry,your account balance is insufficient.");
                                                    }
                                                    account.setwMoney(account.getwMoney() - money);
                                                    System.out.printf("韦小宝在中国工商银行的%s号ATM机上取款¥%s\n当前余额为¥%.2f\n", data2[2], data2[3], account.getwMoney());
                                                }
                                                else{
                                                    account.setwMoney(account.getwMoney() - money);
                                                    System.out.printf("韦小宝在中国工商银行的%s号ATM机上存款¥%.2f\n当前余额为¥%.2f\n", data2[2],(-1)*money, account.getwMoney());
                                                }
                                            }
                                        }
    
    
                                    }
    
                                }
                                else{
                                    System.out.println("Sorry,cross-bank withdrawal is not supported.");
                                }
                            }
                            else{
                                System.out.println("Sorry,the ATM's id is wrong.");
                            }
                        }
                        else{
                            System.out.println("Sorry,your password is wrong.");
                        }
                    }
                    else{
                        System.out.println("Sorry,this card does not exist.");
                    }
                }
            }
        }
    }
    
    class Bank {
        String[] accNum = {"6217000010041315709", "6217000010041315715", "6217000010041315718", "6217000010051320007", "6222081502001312389", "6222081502001312390", "6222081502001312399", "6222081502001312400", "6222081502051320785", "6222081502051320786"};
    
        public boolean check(String accnum, String atm) {
            for (int j = 0; j < accNum.length; j++) {
                int a = Integer.parseInt(atm);
                if (accNum[j].equals(accnum)) {
                    if(j<4&&a>=5&&a<=6){
                        return false;
                    }
                    else if(j>=4&&j<=9&&a>=1&&a<=4){
                        return false;
                    }
                }
            }
            return true;
        }
    
    }
    class User {
        String[] accNum = {"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007","6222081502001312389","6222081502001312390","6222081502001312399","6222081502001312400","6222081502051320785","6222081502051320786"};
        public String name(String s){
            for(int i = 0; i < accNum.length; i++){
                if(accNum[i].equals(s)){
                    if(i<=2){
                        return "杨过";
                    }
                    else if(i==3){
                        return "郭靖";
                    }
                    else if(i<=6){
                        return "张无忌";
                    }
                    else{
                        return "韦小宝";
                    }
                }
            }
            return "韦小宝";
        }
    
        public boolean code(String codes){
            if(codes.equals("88888888")){
                return true;
            }
            return false;
        }
    }
    class Account {
        private double yMoney = 10000;
        private double gMoney = 10000;
        private double zMoney = 10000;
        private double wMoney = 10000;
    
        public double getyMoney() {
            return yMoney;
        }
    
        public void setyMoney(double yMoney) {
            this.yMoney = yMoney;
        }
    
        public double getgMoney() {
            return gMoney;
        }
    
        public void setgMoney(double gMoney) {
            this.gMoney = gMoney;
        }
    
        public double getzMoney() {
            return zMoney;
        }
    
        public void setzMoney(double zMoney) {
            this.zMoney = zMoney;
        }
    
        public double getwMoney() {
            return wMoney;
        }
    
        public void setwMoney(double wMoney) {
            this.wMoney = wMoney;
        }
    }
    class Card {
        String[] accNum = {"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007","6222081502001312389","6222081502001312390","6222081502001312399","6222081502001312400","6222081502051320785","6222081502051320786"};
        public boolean check(String s){
            for(int i = 0; i < accNum.length; i ++){
                if(accNum[i].equals(s)){
                    return true;
                }
            }
            return false;
        }
    
    }
    class ATM {
      public boolean check(String num){
        int a = Integer.parseInt(num);
        if(a>0&&a<7){
            return true;
        }
        return false;
    }
    }

     

  • 我的心得:这道题目主要难点体现在对类与对象的整合思考,该题目难度还是有点的,我给的难度系数在Hard♥♥,我的这道题目的可扩展性其实并不好,后续若有该题目的迭代,还是需要进行一定的修改的,其次就是这道题我在做的时候有很多代码多余,这些本来是都可以进行整合省略的,所以还是有比较大的提升空间。

 

  • List和Liste2(单向和双向)

分析:在写链表的时候,虽然在c语言中已经有了一定的基础,但是在刚开始携带时候还是有一定的难度的,如果没有java知识的良好基础,是比较难写出要求的链表的,第二次的链表是上一次链表的迭代,这里设置了双向,多了前置指针,这里可以便于去寻找前一位元素。

 

类图

  •  

 

  • SourceMonitor

 

 

 

 

 

 

         

 

 

  • 源码

(1)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Llist<Integer> list1 = new Llist();
        System.out.println("该链表是否为空   "+list1.isEmpty());
        System.out.println("请输入你想储存数字个数:");
        int n = in.nextInt();
        System.out.println("请输入你想输入的数字");
        int[]a = new int[n];
        for(int i = 0; i < n; i ++){
            a[i] = in.nextInt();
            list1.add(a[i]);
        }
        System.out.println();
        System.out.println("该链表是否为空   "+list1.isEmpty());
        System.out.println("该链表大小为:"+list1.size());
        list1.printList();
        System.out.println();
        while(true) {
            System.out.println("1,在尾部添加\n2,在指定位置添加元素\n3,删除元素\n");
            int choice = in.nextInt();
            if (choice == 1) {
                int s;
                System.out.println("请输入你要添加的数字:");
                s = in.nextInt();
                list1.add(s);
                list1.printList();
            } else if (choice == 2) {
                int s, s1;
                System.out.println("请在___位置插入___\n");
                s = in.nextInt();
                s1 = in.nextInt();
                list1.add(s - 1, s1);
                list1.printList();
            } else if (choice == 3) {
                int s;
                System.out.println("将___位置的数字删除\n");
                s = in.nextInt();
                list1.remove(s - 1);
                list1.printList();
            }  else {
                System.out.println("输入错误,请重新输入\n");
            }
            System.out.println();
        }
    }
}
public class Llist<E> implements LinearListInterface<E> {
    private Node<E> head,curr,tail;
    private int size = 0;

    public Llist(){
        head = new Node<>();
        tail = head;
    }

    @Override
    public boolean isEmpty() {
        if(size != 0)
        return false;
        else return true;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public E get(int index) {
        curr = head;
        for (int i = 0; i < index; i++);{
            curr = curr.getNext();
            return curr.getO();
        }
    }

    @Override
    public void remove(int index) {
      if(index == 0){
          curr = head.getNext();
          head = curr;
          size --;
      }
      else if(index>=0&&index<size){
          curr = head;
          for (int i = 0; i < index - 1; i++);
          Node<E> temp = curr.getNext().getNext();
          curr.setNext(temp);
          size --;
          curr = curr.getNext();
      }
      else {
          System.out.println("Sorry,the index is out of index");
      }
    }

    @Override
    public void add(int index, E theElement) {
        if (index == 0) {
            curr = head;
            head = new Node<>(theElement, curr);
            size ++;
        } else if(index>=0&&index<size){
            curr = head;
            for (int i = 0; i < index - 1; i++)
                curr = curr.getNext();
            Node<E> temp = curr.getNext();
            curr.setNext(new Node<>(theElement, temp));
            size ++;
        }
        else {
            System.out.println("Sorry,the index is out of index");
        }
    }


    @Override
    public void add(E element) {
        if (head.getO() == null) {
            head.setO(element);
            size ++;
        } else {
            tail = head;
            while (tail.getNext() != null) {
                tail = tail.getNext();
            }
            tail.setNext(new Node<E>(element,null));
            size ++;
        }
    }

    @Override
    public void printList() {
        curr = head;
        for (int i = 0; i < size; i++, curr = curr.getNext()) {
            System.out.print( curr.getO() + " ");
        }
        System.out.println();
    }
}
public class Node<E> {
    private E o;

    private Node<E> next;

    public Node(){

    }

    public Node(E o,Node<E> next) {
        this.o = o;
        this.next = next;
    }

    public E getO(){
        return o;
    }

    public void setO(E o){
        this.o = o;
    }

    public Node<E> getNext(){
        return next;
    }

    public void setNext(Node<E> next) {
        this.next = next;
    }


}
public interface LinearListInterface<E> {
    public boolean isEmpty();

    public int size();

    public E get(int index);

    public void remove(int index);

    public void add(int index, E theElement);

    public void add(E  element);

    public void printList();
}

 

源码(2)

import java.util.Scanner;

public class Main {
    public Main() {
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        DoubleLinkedList<Integer> list1 = new DoubleLinkedList();
        System.out.println("该链表是否为空   "+list1.isEmpty());
        System.out.println("请输入你想储存数字个数:");
        int n = in.nextInt();
        System.out.println("请输入你想输入的数字");
        int[]a = new int[n];
        for(int i = 0; i < n; i ++){
            a[i] = in.nextInt();
            list1.add(a[i]);
        }
        System.out.println();
        System.out.println("该链表是否为空   "+list1.isEmpty());
        System.out.println("该链表大小为:"+list1.size());
        list1.printList();
        System.out.println();
        while(true) {
            System.out.println("1,在尾部添加\n2,在指定位置添加元素\n3,删除尾部元素\n4,删除指定位置的元素\n5,获得对应位置的元素\n6,获取该链表大小\n7,获取头节点对应元素\n8,获取尾节点对应元素\n");
            int choice = in.nextInt();
            if (choice == 1) {
                int s;
                System.out.println("请输入你要添加的数字:");
                s = in.nextInt();
                list1.add(s);
                list1.printList();
            } else if (choice == 2) {
                int s, s1;
                System.out.println("请在___位置插入___\n");
                s = in.nextInt();
                s1 = in.nextInt();
                list1.add(s-1 , s1);
                list1.printList();
            } else if(choice == 3){
                System.out.println("已删除!");
                list1.remove();
                list1.printList();
            } else if (choice == 4) {
                int s;
                System.out.println("将___位置的数字删除\n");
                s = in.nextInt();
                list1.remove(s);
                list1.printList();
            }else if(choice == 5){
                int index;
                System.out.println("请输入你要查询的位置对应的元素:");
                index = in.nextInt();
                System.out.println(list1.getData(index));
            }else if(choice == 6){
                System.out.println(list1.getSize());
            }else if(choice == 7){
                System.out.println(list1.getFirst());
            } else if(choice == 8){
                System.out.println(list1.getLast());
            }else {
                System.out.println("输入错误,请重新输入\n");
            }
            System.out.println();
        }
    }
}
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) {
        this.data = data;
        this.next = next;
        this.previous = previous;
    }

    public E getData() {

        return this.data;
    }

    public void setData(E data) {
        this.data = data;
    }

    public Node<E> getNext() {
        return this.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 DoubleLinkedList<E> implements DoubleLinkedListImpl<E> {
    private Node<E> head = new Node();
    private Node<E> curr;
    private Node<E> tail;
    private int size = 0;

    public DoubleLinkedList() {
        this.head.setNext(null);
        this.head.setPrevious(null);
        this.curr = this.tail = null;
        this.size = 0;
    }

    public boolean isEmpty() {
        return this.size == 0;
    }

    public int getSize() {
        return this.size;
    }

    @Override
    public E getData(int index) {
        if (index >= 1 && index <= this.size) {
            this.curr = this.head;

            for(int i = 0; i < index; ++i) {
                this.curr = this.curr.getNext();
            }

            return this.curr.getData();
        } else {
            return null;
        }
    }

    @Override
    public void remove() {
       remove(this.size);
    }

    @Override

    public void remove(int index) {
        if (index >= 1 && index <= this.size) {
            this.curr = this.head;
            if (index == 1) {
                this.curr = this.head.getNext();
                this.head.setNext(this.curr.getNext());
                this.curr.setPrevious(this.head);
            }  else if(index != this.size) {
                for(int i = 1; i < index; ++i) {
                    this.curr.setPrevious(this.curr);
                    this.curr = this.curr.getNext();
                }
                this.curr.getNext().getNext().setPrevious(this.curr);
                this.curr.setNext(this.curr.getNext().getNext());
            }else{
                this.tail = this.curr;
            }

            this.size--;
        }
    }

    public void add(int index, E theElement) {
        if (index >= 0 && index <= this.size + 1) {
            Node<E> curr = new Node();
            curr.setData(theElement);
            curr.setNext(null);
            if (this.size == 0) {
                this.head.setNext(curr);
                this.tail = curr;
                curr.setPrevious(head);
            } else if (index == this.size + 1) {
                this.tail.setNext(curr);
                curr.setPrevious(this.tail);
                this.tail = curr;
            }else if(index == 0){
                curr.setNext(head.getNext());
                head.setNext(curr);
            }else {
                Node<E> tempNode = this.head;
                for(int i = 1; i <= index; ++i) {
                    tempNode.setPrevious(tempNode);
                    tempNode = tempNode.getNext();
                }
                curr.setNext(tempNode.getNext());
                tempNode.getNext().setPrevious(curr);
                tempNode.setNext(curr);
                curr.setPrevious(tempNode);
            }

            this.size++;
        }
    }

    public void add(E element) {
        this.add(this.size + 1, element);
    }

    public void printList() {
        this.curr = this.head.getNext();

        for(int i = 1; i <= this.size; ++i) {
            System.out.print(this.curr.getData() + " ");
            this.curr = this.curr.getNext();
        }

        System.out.println("");
    }

    @Override
    public E getFirst() {
        return getData(1);
    }

    public E getLast() {

        return getData(this.size);
    }

    public int size() {
        return this.size;
    }
}
public interface DoubleLinkedListImpl<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();

}

 

  • 我的心得:对于链表的创建,我定义的难度等级为Hard♥♥,如果java的基础不是很好,是很难把这两条链表完成的,链表考到的知识还是比较多的,例如:类与对象,继承,接口等都是java的重要基础知识都在这个小小的链表中考到了,对于第二次的链表迭代,是需要你在原来的基础上添加前置指针,将链表改为双向链表,整体难度并不大,主要难度还是在于怎么去将链表创建出来。

 

  • 题目集6(7-2)****

(ps:这道题目我真的是感悟颇深,因为我花费非常多的时间去完成它)。

  • 点线形系列4-四边形题目说明.pdf
    用户输入一组选项和数据,进行与四边形有关的计算。
    以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
    选项包括:
    1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
    2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
    3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
    4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,输出"not a quadrilateral or triangle"。
    后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
    1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
    2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
    5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出"on the triangle或者on the quadrilateral"。若后四个点不符合四边形或三角形,输出"not a quadrilateral or triangle"。

    输入格式:

    基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。

    输出格式:

    基本输出格式见每种选项的描述。
    异常情况输出:
    如果不符合基本格式,输出"Wrong Format"。
    如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
    注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

    选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。
    选项4中,若前两个输入线的点重合,输出"points coincide"。

    输入样例1:

    选项1,点重合。例如:

    1:-1,-1 -1,-1 1,2 1,-2
    

    输出样例:

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

    points coincide
  • 分析:这道题目非常难,基本上我可以说是我目前java里面做的最难的一道题目,并不是它有多么难理解,而是复杂,这道题目是非常复杂的,所以一定注意合理分类!合理分类!合理分类!重要的事情说三遍!如果这道题目你不分类,还是按照以前我的那种做法,是绝对拿不到满分的,其次就是要分析!要分析!要分析!要分析!重要的事情说三遍!这道题是需要合理的分析的,不分析后面就会彻底乱掉。

 

  • 类图

 

  • SourceMonitor 
  •  

     

  • 源码: 

  • import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            BankBusiness bankBusiness = new BankBusiness();
            bankBusiness.welcome();
            BankBusiness account = new BankBusiness();
            String name = in.next();
            String password = in.next();
            account.access(name,password);
            String s1 = in.next();
            Double money = in.nextDouble();
            account.deposit(s1,money);
            s1 = in.next();
            money = in.nextDouble();
            account.withdraw(s1,money);
            s1 = in.next();
            money = in.nextDouble();
            account.withdraw(s1,money);
            s1 = in.next();
            money = in.nextDouble();
            account.withdraw(s1,money);
            account.welcomeNext();
        }
    }
    class BankBusiness {
        public String bankName = "中国银行";
        private String name,password;
        private double balance;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public double getBalance() {
            return balance;
        }
    
        public void setBalance(double balance) {
            this.balance = balance;
        }
    
        public void welcome(){
            System.out.println(bankName+"欢迎您的到来!");
        }
    
        public void access(String name,String password){
            this.name = name;
            this.password = password;
            this.balance = 0;
        }
    
        public void deposit(String password,double money){
            if(password.equals(this.password)){
               this.balance = this.balance + money;
               System.out.println("您的余额有"+this.balance+"元。");
            }else {
                System.out.println("您的密码错误!");
            }
        }
    
          public void withdraw(String password,double money){
            if(password.equals(this.password)){
                if(this.balance >= money) {
                    this.balance = this.balance - money;
                    System.out.println("请取走钞票,您的余额还有" + this.balance + "元。");
                }
                else{
                    System.out.println("您的余额不足!");
                }
            }else {
                System.out.println("您的密码错误!");
            }
        }
    
        public void welcomeNext(){
            System.out.println("请收好您的证件和物品,欢迎您下次光临!");
        }
    }

     

  • 我的心得:这道题目我给的难度定义在Hard♥♥♥♥,我最开始批评了我自己的那种类图就是属于没有分类,这道题目我最开始就是按照之前那样写,代码长度就会超过限制,我不管怎么修改就是只有40分(满分70),所以我挣扎了两天,我决定全部重写,这次我认认真真分析分类写方法,写着写着我就发现了分类写这种复杂的题目可以帮助我们解决很多麻烦,思路也会变得格外清晰,是不会存在混乱的问题。还有如果有哪里出现了代码,在之前的方法中我需要翻来覆去的找,但是用了新方法之后基本上就是可以非常快找到我的问题,并且可以进行有效修改,代码的可扩展性大大增强。这道题还是继承了之前图形题目的特点:麻烦,细节,数学。这里运用了蛮多数学知识的,因为本人数学并不算好,所以也会去找方法,这也是没有办法,还有各种测试点的错误都是需要我们去揣测分析的,类似于之前的格式错误,其实运用一个正则就可以全部解决,是不是很简单,这就是老师为什么在之前让我们训练正则的原因所在了,包括类与对象,都是为了打好基础。

 

  • 期中测试(7-1--7-3)
  • 在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。

    • 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>
    • 增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
    • 在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
      • 1:向容器中增加Point对象
      • 2:向容器中增加Line对象
      • 3:向容器中增加Plane对象
      • 4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
      • 0:输入结束
      示例代码如下:
         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()方法进行输出。
      类图如下所示:

    classdiagram.jpg

    • 以下情况为无效作业
      • 无法运行
      • 设计不符合所给类图要求
      • 未通过任何测试点测试
      • 判定为抄袭

    输入格式:

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

    输出格式:

    • Point、Line、Plane的输出参考题目2
    • 删除对象时,若输入的index超出合法范围,程序自动忽略该操作

    输入样例:

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

    1
    3.4
    5.6
    2
    4.4
    8.0
    0.98
    23.888
    Red
    3
    Black
    1
    9.8
    7.5
    3
    Green
    4
    3
    0
  • 分析:这三道题是属于非常契合的迭代题目,所以我这里就直接展示第三题的代码,这三道题目,第一题主要是对类与对象的运用,第二题是继承与多态,第三题是容器。可以说是迭代的比较好的,类图上,老师已经给我们了我们就不需要再重复了。

 

  • 源码
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        GeometryObject geometryObject = new GeometryObject();
        double x1 = 0,y1=0,x2=0,y2=0;
        String color = null;
        int choice = in.nextInt();
        while(choice != 0) {
            switch(choice) {
                case 1://insert Point object into list
                    x1 = in.nextDouble();
                    y1 = in.nextDouble();
                   if(x1>200||x1<=0||y1>200||y1<=0){
                        System.out.println("Wrong Format");return;
                    }
                    geometryObject.add(new Point(x1,y1));
                    break;
                case 2://insert Line object into list
                    x1 = in.nextDouble();
                    y1 = in.nextDouble();
                    x2 = in.nextDouble();
                    y2 = in.nextDouble();
                    color = in.next();
                   if(x1>200||x1<=0||y1>200||y1<=0||x2>200||x2<=0||y2>200||y2<=0){
                        System.out.println("Wrong Format");return;
                    }
                    geometryObject.add(new Line(new Point(x1,y1),new Point(x2,y2),color));
                    break;
                case 3://insert Plane object into list
                    color = in.next();
                    geometryObject.add(new Plane(color));
                    break;
                case 4://delete index - 1 object from list
                    int index = in.nextInt();
                    geometryObject.remove(index);

            }
            choice = in.nextInt();
            if(choice==0){
                for (Element each:geometryObject.getList()) {
                    each.display();
                }
            }
        }



    }
}
 class Point extends Element {
    private double x;
    private double y;

    public Point(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 void display(){
        System.out.printf("(%.2f,%.2f)\n",x,y);
    }
}
class Line extends Element{
    private Point p1;
    private Point p2;
    private String color;

    public Line(Point p1, Point p2, String color) {
        this.p1 = p1;
        this.p2 = p2;
        this.color = color;
    }

    public Point getP1() {
        return p1;
    }

    public void setP1(Point p1) {
        this.p1 = p1;
    }

    public Point getP2() {
        return p2;
    }

    public void setP2(Point p2) {
        this.p2 = p2;
    }


    public String getColor() {
        return color;
    }

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

    public double getDistance(){
        return Math.sqrt((p2.getY()-p1.getY())*(p2.getY()-p1.getY())+(p2.getX()-p1.getX())*(p2.getX()-p1.getX()));
    }

    public void display(){
        System.out.println("The line's color is:"+color);
        System.out.println("The line's begin point's Coordinate is:");
        p1.display();
        System.out.println("The line's end point's Coordinate is:");
        p2.display();
        System.out.printf("The line's length is:%.2f\n",getDistance());
    }

}
class GeometryObject {

    private ArrayList<Element> list = new ArrayList<>();

    public GeometryObject() {
        // TODO Auto-generated constructor stub
    }

    public void add(Element e) {
        list.add(e);
    }

    public void remove(int index) {
        if (index <= list.size()) {
            list.remove(index - 1);
        }
    }

    public ArrayList<Element> getList(){
        return list;
    }

}
class Plane extends Element {
    private String color;

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

    public String getColor() {
        return color;
    }

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

    public void display(){
        System.out.println("The Plane's color is:"+color);
    }
}
 abstract class Element {
    private double x;
    private double y;
    private Point p1;
    private Point p2;
    private String color;

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

    }

    public Element(){

    }

  public abstract void display();
}
  • 我的心得:这次期中测试虽然并不算太难,但是考的是属于比较全面的,我在考试的时候在抽象类那里稍微有点遗忘了,以前没有接触过这个类,也没有运用过,所以带来了一点麻烦,这次基本上基础知识扎实的就可以拿到满分,我给的难度是Hard♥

######################################################################################################################################################################################################################################################

 

(3)踩坑心得

 作为踩坑无数的老废物,可以说是经验丰富,下面我就浅谈一下我的废物行为。

##①##题目集6 7-2

检验输入的格式错误

用正则!用正则!用正则!重要的事情说三遍!

最开始我是硬判断输入错误,代码长的吓人,而且后面你会发现代码长度超过限制,所以用正则表达式就会非常简单。

 

 

 

  ## ②##题目集6 7-2

精度问题

             

 

 

 

               注意到第一种是我最开始写的,直接判断它是不是相等,其实是存在问题的,double类型是存在一定误差的,所以这里若采用第一种,那么你将会有一个测试点没办法通过。所以我们采用第二种方式。

 

##③##题目集6 7-2

              斜率问题

     

 

 

 对于图形类题目恐怕斜率类问题是我们比较棘手和讨厌的问题了,但是如果你会采用一般式方程解题,你会发现根本不需要考虑斜率的问题,不会存在分母等于0的情况。

 

********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************

(4)改进建议

##①##题目集6  7-2

  • 在main函数依然有很多可以提出分类的代码,可扩展性是有待提高的。

 

 

 

 类似于这类代码其实都是可以进行简单的收缩的。

##②##题目集4  (7-1--7-3)

 

  • 所有对输入格式进行判断的地方都可以用正则直接表达,节省大量代码。

 

 

 

############################################################################################################################################################################################

(5)总结

  • 收获:

  1. 对java的基础知识掌握的更加清楚。
  2. 学会用正则表达式去判断,节省大量代码。
  3. 对类与对象掌握的更加熟练。
  4. 学会用抽象类,容器等。
  • 不足:

  1. 对分析题意的能力有待提高。
  2. 不擅长开始敲代码之前进行画图构思
  • 通过本阶段三次题目集的训练对教师、课程、作业、实验、课上及课下组织方式等方面提出的改进建议及意见 
    1. 有部分题目在题意上有点模糊。
    2. 希望老师对pta的题目进行讲解。
    3. 测试点全覆盖需要花费大量时间去推敲。

 

 

 

 

 

 

 

 

      

 

 

 

posted on 2022-04-30 17:33  听风念……  阅读(39)  评论(0编辑  收藏  举报