BLOG-2

BLOG-2

题目集总结:

(1)前言

题目集4:

知识点:类与对象、字符串方法调用、边界值判断

题量:中

难度:难

期中考试:

知识点:类设计、继承与多态、容器类

题量:中

难度:中

题目集6:

知识点:类与对象、字符串方法调用、边界值判断、正则表达式

题量:中

难度:难

单向链表与双向链表:

知识点:类与对象、接口、泛型、基本链表结构

题量:少

难度:中

(2)设计与分析

题目集4:

7-2 点线形系列2-线的计算

用户输入一组选项和数据,进行与直线有关的计算。选项包括:

1:输入两点坐标,计算斜率,若线条垂直于X轴,输出"Slope does not exist"。

2:输入三个点坐标,输出第一个点与另外两点连线的垂直距离。

3:输入三个点坐标,判断三个点是否在一条线上,输出true或者false。

4:输入四个点坐标,判断前两个点所构成的直线与后两点构成的直线是否平行,输出true或者false.

5:输入四个点坐标,计算输出前两个点所构成的直线与后两点构成的直线的交点坐标,x、y坐标之间以英文分隔",",并输出交叉点是否在两条线段之内(不含四个端点)的判断结果(true/false),判断结果与坐标之间以一个英文空格分隔。若两条线平行,没有交叉点,则输出"is parallel lines,have no intersection point"。

输入格式:

基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。

例如:1:0,0 1,1

如果不符合基本格式,输出"Wrong Format"。

如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。

不论哪个选项,如果格式、点数量都符合要求,但构成任一条线的两个点坐标重合,输出"points coincide",

 

分析:本题麻烦之处首先在于如何将输入的数据提取运用,我使用的是字符串方法,并利用subString的方法进行数据提取,由于存在错误输入的问题,需对输入是可能的情况进行分析,如:1:0,0  1,11:0 0, 1 1,1……得到Wrong Format的结果,同时也因出现点输入数量不符的情况,需与错误输入区分后单独讨论,我这里是先通过逗号和空格判断错误输入后,再根据空格数量来判断是否为点输入数量不符的情况,但讨论不够全面,有几个测试点没有通过。接下来是对前四个选项功能的实现,这四个功能较为简单,简单的数学计算即可拿下;最后一个选项相对复杂许多,首先需要考虑两直线平行的情况(注意斜率不存在),点重合的情况依旧需要考虑,对于判断交点的计算我是列方程得出通解,再利用Math类中的maxmin方法得到极大和极小点,再判断是否在两条线段之内。

7-3 点线形系列3-三角形的计算

 

用户输入一组选项和数据,进行与三角形有关的计算。选项包括:

1:输入三个点坐标,判断是否是等腰三角形、等边三角形,判断结果输出true/false,两个结果之间以一个英文空格符分隔。

2:输入三个点坐标,输出周长、面积、重心坐标,三个参数之间以一个英文空格分隔,坐标之间以英文","分隔。

3:输入三个点坐标,输出是钝角、直角还是锐角三角形,依次输出三个判断结果(true/false),以一个英文空格分隔,

4:输入五个点坐标,输出前两个点所在的直线与三个点所构成的三角形相交的交点数量,如果交点有两个,则按面积大小依次输出三角形被直线分割成两部分的面积。若直线与三角形一条线重合,输出"The point is on the edge of the triangle"

5:输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。

必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外。若点在三角形的某条边上,输出"on the triangle"

输入格式:

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

输出格式:

基本输出格式见每种选项的描述。

异常情况输出:

如果不符合基本格式,输出"Wrong Format"。

如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。

如果输入的三个点无法构成三角形,输出"data error"。

注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333333,1.0按格式输出为1.0

选项4中所输入线的两个点坐标重合,输出"points coincide",

 

分析:本题与7-2结果类似,从线的计算上升到三角形的计算,异常情况的测试点与7-2基本一致,输入方式也相符合,因此继续使用了7-2的数据提取方式。对于功能实现方面前三个功能依旧很轻松可以实现,三角形任意两边之和大于第三边,周长公式,重心公式简单利用。面积我选择了海伦公式来计算,判断三角形类型则是运用了余弦定理。接下来的四五选项为此题的难点,先说第五选项,题目要求用射影法实现判断,我不会射影法转而利用了面积法来判断,具体思路为:将此点分别与三角形中任意两点构成三个小三角形,并计算三个小三角形面积的总和,若等与大三角形面积相等则证明此点在三角形内部。第四选项没能拿下。

期中考试:

7-1 点与线(类设计)

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

 

 

分析:此题为简单题,根据类图即可轻松写出。

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

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

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

      element = p1;//起点Point

      element.display();

      

      element = p2;//终点Point

      element.display();

      

      element = line;//线段

      element.display();

      

      element = plane;//面

      element.display();

 

 

 

分析:此题在上一题的基础上新增了抽象父类Element,原有的LinePointPlane类通过继承Element类实现多态,难度不高。

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

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

  • 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>)
  • 增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1ArrayList中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()方法进行输出。

 

 

分析:本题在上一题的前提上要求使用容器类,由于对容器类的不熟悉,我花费了很长时间来研究,在考试结束后10分钟才彻底写对...现在重新来分析此题。其实本题难度并不高依旧是根据类图的指引写代码。首先在容器类中加入ArrayList<E>对象,并利用ArrayList创建add和remove方法,在主函数中进行调用。输出则用for循环来实现。

题目集6:

用户输入一组选项和数据,进行与四边形有关的计算。

以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。

选项包括:

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"。

 

分析:此题也是先前三角形类的拓展,但难度有了进一步提升。提取数据依然跟先前一致但错误输入运用了新学会的正则表达式。在第一个功能的实现上就有一定难度。对四边形的判定我通过任选一个点计算这个点到另外三个点的斜率,若这三个斜率不相等则可证得四边形,平行四边形在此基础上可轻松判断;第二个功能判断是否是菱形、矩形、正方形,菱形在四边形的基础证明四条边相等即可,矩形的判断通过证明四边形两条对角线相等;第三个功能要求判断凹凸四边形,我判断的方法为将四边形每个顶点依次命为1234,计算三角形123、三角形134、三角形234和三角形124的面积,判断前两个三角形的面积和是否与后两个三角形的面积和相等即可得出是否为凸四边形,在面积的计算上若为凸四边形则任意计算123134234124的面积和即为凸四边形的面积,而凹四边形的面积为两者面积和中较小的一个。第四五个功能未能成功实现。

单向链表练习:

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

}

 

分析:此次练习要求利用接口和泛型创建一个单向链表,实现增删改查功能,增加功能分为在中间插入和在尾部插入。首先找到index前一个节点,令此节点指向新创建的节点,新创建的节点再指向index前一个节点的原后一个节点即可;删除功能需将index前一个节点的指向它的下下个节点。

双向链表练习:

在单链表基础上改写代码,实现双向链表数据结构:

public class Node<E> {

    private E data;//数据域,类型为泛型E

    private Node<E> next;//后继引用(指针)

    private Node<E> previous;//前驱引用(指针)

}

 

public interface DoubleLinkedListImpl<E> {

/**

 * 判断链表是否为空

 * @return

 */

public boolean isEmpty();

/**

 * 获取当前链表节点数量

 * @return 节点数

 */

public int getSize();

/**

 * 获取链表中第index个位置的节点的data值

 * @param index:节点在链表中的位置

 * @return:返回该节点的data值

 */

public E getData(int index);

/**

 * 删除链表最后一个节点

 */

public void remove();

/**

 *删除链表中第index位置的节点 

 * @param index:节点在链表中的位置

 */

public void remove(int index);

/**

 * 在链表的第index个位置之前插入一个节点,值为theElement,index∈[1,size]

 * @param index:插入节点在链表中的位置

 * @param theElement:新插入节点的data值

 */

public void add(int index, E theElement);

/**

 * 在链表尾插入节点,插入节点data值为element

 * @param element

 */

public void add(E element);

/**

 * 输出链表

 */

public void printList();

/**

 * 获取第一个节点的data值

 * @return

 */

public E getFirst();

/**

 * 获取链表最后一个节点的data值

 * @return

 */

public E getLast();

}

 

public class DoubleLinkedList<E> implements DoubleLinkedListImpl<E> {

private Node<E> head;//头结点,非第一个节点

private Node<E> curr;//当前节点

private Node<E> tail;//最后一个节点

private int size;//当前链表节点数

        ......

}

 

分析:双向链表是再单向链表上的拓展,增加一个previous指针思路与上题相似。

 

(3)踩坑心得

题目集4:

7-2:主要问题在于错误输入的判定

如:1:0,,1 1,1、1: 0,1 0,2、1:0,1   0,1等

代码如下:

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        int length = str.length();
        char choice = str.charAt(0);
        int flag = 0;
        Distence distence = new Distence(str,length);
        if (choice < '1' || choice > '5'){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        if (choice == '1') {
            flag = distence.select();
            if (flag == 0)
                System.out.println("Wrong Format");
            if(flag == 1) {
                distence.output();
                if(distence.getX1() == distence.getX2()&&distence.getY1() != distence.getY2())
                    System.out.println("Slope does not exist");
                else if(distence.getX1() == distence.getX2()&&distence.getY1() == distence.getY2())
                    System.out.println("points coincide");
                else {
                    System.out.println((distence.getY1()-distence.getY2())/(distence.getX1()-distence.getX2()));
                }
            }
            if(flag == 2)
                System.out.println("wrong number of points");
        }
        else if (choice == '2') {
            flag = distence.select();
            if (flag == 0)
                System.out.println("Wrong Format");
            if(flag == 1) {
                distence.output();
                if (distence.getX2() == distence.getX3() && distence.getY2() == distence.getY3())
                    System.out.println("points coincide");
                else if (distence.getX2() == distence.getX3() && distence.getY2() != distence.getY3()){
                    if (distence.getX1() == distence.getX2())
                        System.out.println("0.0");
                    else
                        System.out.println(Math.abs(distence.getX1())- distence.getX2());
                }
                else if(distence.getX2() != distence.getX3() && distence.getY2() == distence.getY3()){
                    if (distence.getY1() == distence.getY2())
                        System.out.println("0.0");
                    else
                        System.out.println(Math.abs(distence.getY1())- distence.getY2());
                }
                else {
                    double x = (distence.getY2()- distence.getY3())* (distence.getY2()- distence.getY3())+(distence.getX2() - distence.getX3())*(distence.getX2() - distence.getX3());
                    double distance = Math.abs((distence.getY2()- distence.getY3()) * distence.getX1() + (distence.getX3() - distence.getX2()) * distence.getY1() + distence.getX2() * distence.getY3() - distence.getY2() * distence.getX3()) / Math.sqrt(x);
                    System.out.println(distance);
                }
            }
            if(flag == 2)
                System.out.println("wrong number of points");
        }
        else if (choice == '3') {
            flag = distence.select();
            if (flag == 0)
                System.out.println("Wrong Format");
            if (flag == 1){
                distence.output();
                if (distence.getX2() == distence.getX3() && distence.getY2() == distence.getY3())
                    System.out.println("points coincide");
                else if (distence.getX2() == distence.getX3() && distence.getX1() == distence.getX3())
                    System.out.println("true");
                else if (distence.getX2() == distence.getX3() && distence.getX1() != distence.getX3())
                    System.out.println("false");
                else {
                    double k1 = (distence.getY1() - distence.getY2()) / (distence.getX1() - distence.getX2());
                    double k2 = (distence.getY1() - distence.getY3()) / (distence.getX1() - distence.getX3());
                    if (k1 == k2)
                        System.out.println("true");
                    else
                        System.out.println("false");
                }
            }
            if(flag == 2)
                System.out.println("wrong number of points");
        }
        else if (choice == '4') {
            flag = distence.select();
            if (flag == 0)
                System.out.println("Wrong Format");
            if (flag == 1){
                distence.output();
                if (distence.getX1() == distence.getX2() && distence.getY1() == distence.getY2())
                    System.out.println("points coincide");
                else if (distence.getX3() == distence.getX4() && distence.getY3() == distence.getY4())
                    System.out.println("points coincide");
                else if (distence.getX1() == distence.getX2() && distence.getY1() != distence.getY2() && distence.getX3() == distence.getX4() && distence.getY3() != distence.getY4())
                    System.out.println("true");
                else if (distence.getX1() == distence.getX2() && distence.getY1() != distence.getY2() && distence.getX3() != distence.getX4())
                    System.out.println("false");
                else if(distence.getX1() != distence.getX2() && distence.getX3() == distence.getX4() && distence.getY3() != distence.getY4())
                    System.out.println("false");
                else {
                    double k1 = (distence.getY1() - distence.getY2()) / (distence.getX1() - distence.getX2());
                    double k2 = (distence.getY3() - distence.getY4()) / (distence.getX3() - distence.getX4());
                    if (k1 == k2)
                        System.out.println("true");
                    else
                        System.out.println("false");
                }
            }
            if(flag == 2)
                System.out.println("wrong number of points");
        }
        else if (choice == '5') {
            flag = distence.select();
            if (flag == 0)
                System.out.println("Wrong Format");
            if (flag == 1){
                distence.output();
                double k1 = (distence.getY1() - distence.getY2()) / (distence.getX1() - distence.getX2());
                double k2 = (distence.getY3() - distence.getY4()) / (distence.getX3() - distence.getX4());
                if (distence.getX1() == distence.getX2() && distence.getY1() == distence.getY2())
                    System.out.println("points coincide");
                else if (distence.getX3() == distence.getX4() && distence.getY3() == distence.getY4())
                    System.out.println("points coincide");
                else if ((distence.getX1() == distence.getX2() && distence.getY1() != distence.getY2() && distence.getX3() == distence.getX4() && distence.getY3() != distence.getY4()) || k1 == k2)
                    System.out.println("is parallel lines,have no intersection point");
                else {
                    double x = (distence.getY3() * distence.getX4() * distence.getX2() - distence.getY4() * distence.getX3() * distence.getX2() - distence.getX1() * distence.getY3() * distence.getX4() + distence.getX1() * distence.getX3() * distence.getY4() - distence.getY1() * distence.getX2() * distence.getX4() + distence.getY2() * distence.getX4() * distence.getX1() + distence.getY1() * distence.getX3() * distence.getX2() - distence.getY2() * distence.getX1() * distence.getX3()) / (distence.getX4() * distence.getY2() - distence.getX4() * distence.getY1() - distence.getX3() * distence.getY2() + distence.getX3() * distence.getY1() - distence.getX2() * distence.getY4() + distence.getX2() * distence.getY3() + distence.getX1() * distence.getY4() - distence.getX1() * distence.getY3());
                    double y = (-distence.getY3() * distence.getX4() * distence.getX2() + distence.getY4() * distence.getX3() * distence.getX2() + distence.getY1() * distence.getY3() * distence.getX4() - distence.getY1() * distence.getX3() * distence.getY4() + distence.getY1() * distence.getX2() * distence.getY4() - distence.getY3() * distence.getX2() * distence.getY1() - distence.getY2() * distence.getX1() * distence.getY4() + distence.getY2() * distence.getX1() * distence.getY3()) / (distence.getX2() * distence.getY4() - distence.getX1() * distence.getY4() - distence.getX2() * distence.getY3() + distence.getX1() * distence.getY3() - distence.getX4() * distence.getY2() + distence.getX3() * distence.getY2() + distence.getX4() * distence.getY1() - distence.getX3() * distence.getY1());
                    System.out.print(x+","+y);
                    double max1 = Math.max(distence.getX1(), distence.getX2());
                    double max2 = Math.max(distence.getX3(), distence.getX4());
                    double min1 = Math.min(distence.getX1(), distence.getX2());
                    double min2 = Math.min(distence.getX3(), distence.getX4());
                    if ((x > min1 || x > min2) && (x < max2 || x < max1)) {
                        System.out.println(" " + true);
                    } else {
                        System.out.println(" " + false);
                    }
                }
            }
            if(flag == 2)
                System.out.println("wrong number of points");
        }
    }
}

class Distence {
    private int len;
    private String string;
    private int[] arr = new int[100];
    private double x1;
    private double y1;
    private double x2;
    private double y2;
    private double x3;
    private double y3;
    private double x4;
    private double y4;

    public Distence() {
    }

    public Distence(String string, int len) {
        this.string = string;
        this.len = len;
    }

    public double getX1() {
        return x1;
    }

    public void setX1(double x1) {
        this.x1 = x1;
    }

    public double getY1() {
        return y1;
    }

    public void setY1(double y1) {
        this.y1 = y1;
    }

    public double getX2() {
        return x2;
    }

    public void setX2(double x2) {
        this.x2 = x2;
    }

    public double getY2() {
        return y2;
    }

    public void setY2(double y2) {
        this.y2 = y2;
    }

    public double getX3() {
        return x3;
    }

    public void setX3(double x3) {
        this.x3 = x3;
    }

    public double getY3() {
        return y3;
    }

    public void setY3(double y3) {
        this.y3 = y3;
    }

    public double getX4() {
        return x4;
    }

    public void setX4(double x4) {
        this.x4 = x4;
    }

    public double getY4() {
        return y4;
    }

    public void setY4(double y4) {
        this.y4 = y4;
    }

    public int select() {
        int a = 0;
        int ret = 0;
        int n = 0;
        int k = 0;
        int num = 0;

        if (string.charAt(1) != ':')
            a = 0;
        for (int i = 2; i < len; i++) {
            if (string.charAt(i) == '+' || string.charAt(i) == '-') {
                if (string.charAt(i + 1) == '+' || string.charAt(i + 1) == '-') {
                    ret = 1;
                }
            }
            if (string.charAt(i) == '.' && string.charAt(i + 1) == ' ') {
                ret = 1;
                break;
            }
        }
        if (ret == 1)
        a = 0;
        else {
            if (string.charAt(0) == '1') {
                for (int j = 2; j < len; j++) {
                    if (string.charAt(j) == ' ')
                        n++;
                    if (string.charAt(j) == ',' || string.charAt(j) == ' ') {
                        k++;
                    }
                    if (string.charAt(j) != '.' && (string.charAt(j) != '+' && string.charAt(j) != '-') && !Character.isDigit(string.charAt(j))) {
                        arr[num] = j;
                        num++;
                    }
                }
                if (n != 1 && k > 3)
                    a = 2;
                if (n != 1 && k <= 3)
                    a = 0;
                if (n == 1 && k == 3) {
                    a = 1;
                }
            }
            if (string.charAt(0) == '2' || string.charAt(0) == '3') {
                            for (int j = 2; j < len; j++) {
                                if (string.charAt(j) == ' ')
                                    n++;
                                if (string.charAt(j) == ',' || string.charAt(j) == ' ') {
                                    k++;
                    }
                    if (string.charAt(j) != '.' && (string.charAt(j) != '+' && string.charAt(j) != '-') && !Character.isDigit(string.charAt(j))) {
                        arr[num] = j;
                        num++;
                    }
                }
                if (n != 2 && k > 5)
                    a = 2;
                if (n != 2 && k <= 5)
                    a = 0;
                if (n == 2 && k == 5) {
                    a = 1;
                }
            }
            if (string.charAt(0) == '4' || string.charAt(0) == '5') {
                for (int j = 2; j < len; j++) {
                    if (string.charAt(j) == ' ')
                        n++;
                    if (string.charAt(j) == ',' || string.charAt(j) == ' ') {
                        k++;
                    }
                    if (string.charAt(j) != '.' && (string.charAt(j) != '+' && string.charAt(j) != '-') && !Character.isDigit(string.charAt(j))) {
                        arr[num] = j;
                        num++;
                    }
                }
                if (n != 3 && k > 7)
                    a = 2;
                if (n != 3 && k <= 7)
                    a = 0;
                if (n == 3 && k == 7) {
                    a = 1;
                }
            }
        }
        return a;
    }

    public void output() {
        String stringx1;
        String stringy1;
        String stringx2;
        String stringy2;
        String stringx3;
        String stringy3;
        String stringx4;
        String stringy4;
        if (string.charAt(0) == '1') {
            stringx1 = string.substring(2,arr[0]);
            stringy1 = string.substring(arr[0]+1,arr[1]);
            stringx2 = string.substring(arr[1]+1,arr[2]);
            stringy2 = string.substring(arr[2]+1,len);
            x1 = Double.parseDouble(stringx1);
            y1 = Double.parseDouble(stringy1);
            x2 = Double.parseDouble(stringx2);
            y2 = Double.parseDouble(stringy2);
        }
        else if (string.charAt(0) == '2' || string.charAt(0) == '3'){
            stringx1 = string.substring(2,arr[0]);
            stringy1 = string.substring(arr[0]+1,arr[1]);
            stringx2 = string.substring(arr[1]+1,arr[2]);
            stringy2 = string.substring(arr[2]+1,arr[3]);
            stringx3 = string.substring(arr[3]+1,arr[4]);
            stringy3 = string.substring(arr[4]+1,len);
            x1 = Double.parseDouble(stringx1);
            y1 = Double.parseDouble(stringy1);
            x2 = Double.parseDouble(stringx2);
            y2 = Double.parseDouble(stringy2);
            x3 = Double.parseDouble(stringx3);
            y3 = Double.parseDouble(stringy3);
        }
        else {
            stringx1 = string.substring(2,arr[0]);
            stringy1 = string.substring(arr[0]+1,arr[1]);
            stringx2 = string.substring(arr[1]+1,arr[2]);
            stringy2 = string.substring(arr[2]+1,arr[3]);
            stringx3 = string.substring(arr[3]+1,arr[4]);
            stringy3 = string.substring(arr[4]+1,arr[5]);
            stringx4 = string.substring(arr[5]+1,arr[6]);
            stringy4 = string.substring(arr[6]+1,len);
            x1 = Double.parseDouble(stringx1);
            y1 = Double.parseDouble(stringy1);
            x2 = Double.parseDouble(stringx2);
            y2 = Double.parseDouble(stringy2);
            x3 = Double.parseDouble(stringx3);
            y3 = Double.parseDouble(stringy3);
            x4 = Double.parseDouble(stringx4);
            y4 = Double.parseDouble(stringy4);
        }
    }
}
View Code

 

 

 

 

7-3:数据保留问题(注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333333,1.0按格式输出为1.0),通过引入DecimalFormat类解决

代码如下:

 

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

public class Main {
    public static void main(String[] args) {
        DecimalFormat format = new DecimalFormat("0.######");
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        int length = str.length();
        char choice = str.charAt(0);
        int flag;
        double circumference;
        double area;
        double c1;
        double c2;
        Distence distence = new Distence(str,length);
        if (choice < '1' || choice > '5'){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        if (choice == '1') {
            flag = distence.select();
            /*if (flag == 0)
                System.out.println("Wrong Format");*/
            if(flag == 1) {
                distence.output();
                if(!distence.isTriangle()){
                    System.out.println("data error");
                    System.exit(0);
                }
                distence.Isosceles();
            }
            /*if(flag == 2)
                System.out.println("wrong number of points");*/
        }
        else if (choice == '2') {
            flag = distence.select();
            /*if (flag == 0)
                System.out.println("Wrong Format");*/
            if(flag == 1) {
                distence.output();
                if(!distence.isTriangle()){
                    System.out.println("data error");
                    System.exit(0);
                }
                circumference = distence.getA() + distence.getB() + distence.getC();
                area = Math.sqrt(circumference / 2 * (circumference / 2 - distence.getA()) * (circumference / 2 - distence.getB()) * (circumference / 2 - distence.getC()));
                c1 = (distence.getX1() + distence.getX2() + distence.getX3()) / 3;
                c2 = (distence.getY1() + distence.getY2() + distence.getY3()) / 3;
                System.out.println(new DecimalFormat("0.######").format(circumference)+ " " + new DecimalFormat("0.######").format(area) + " " + new DecimalFormat("0.0#####").format(c1) + "," + new DecimalFormat("0.######").format(c2));
            }
            /*if(flag == 2)
                System.out.println("wrong number of points");*/
        }
        else if (choice == '3') {
            flag = distence.select();
            if (flag == 0)
                System.out.println("Wrong Format");
            if (flag == 1){
                distence.output();
                if(!distence.isTriangle()){
                    System.out.println("data error");
                    System.exit(0);
                }
                distence.kinds();
            }
            if(flag == 2)
                System.out.println("wrong number of points");
        }
        else if (choice == '4') {
            flag = distence.select();
            if (flag == 0)
                System.out.println("Wrong Format");
            if (flag == 1){
                distence.output();
                if (distence.getX1() == distence.getX2() && distence.getY1() == distence.getY2())
                    System.out.println("points coincide");
                else if (distence.getX3() == distence.getX4() && distence.getX4() == distence.getX5() || distence.getY3() == distence.getY4() && distence.getY4() == distence.getY5())
                    System.out.println("data error");
                else {

                }
            }
            if(flag == 2)
                System.out.println("wrong number of points");
        }
        else if (choice == '5') {
            flag = distence.select();
            if (flag == 0)
                System.out.println("Wrong Format");
            if (flag == 1){
                distence.output();
                if(!distence.isTriangle()){
                    System.out.println("data error");
                    System.exit(0);
                }
                distence.inside();
            }
            if(flag == 2)
                System.out.println("wrong number of points");
        }
    }
}

class Distence {
    private int len;
    private String string;
    private int[] arr = new int[100];
    private double x1;
    private double y1;
    private double x2;
    private double y2;
    private double x3;
    private double y3;
    private double x4;
    private double y4;
    private double x5;
    private double y5;
    private double a;
    private double b;
    private double c;

    public Distence() {
    }

    public Distence(String string, int len) {
        this.string = string;
        this.len = len;
    }

    public double getX1() {
        return x1;
    }

    public void setX1(double x1) {
        this.x1 = x1;
    }

    public double getY1() {
        return y1;
    }

    public void setY1(double y1) {
        this.y1 = y1;
    }

    public double getX2() {
        return x2;
    }

    public void setX2(double x2) {
        this.x2 = x2;
    }

    public double getY2() {
        return y2;
    }

    public void setY2(double y2) {
        this.y2 = y2;
    }

    public double getX3() {
        return x3;
    }

    public void setX3(double x3) {
        this.x3 = x3;
    }

    public double getY3() {
        return y3;
    }

    public void setY3(double y3) {
        this.y3 = y3;
    }

    public double getX4() {
        return x4;
    }

    public void setX4(double x4) {
        this.x4 = x4;
    }

    public double getY4() {
        return y4;
    }

    public void setY4(double y4) {
        this.y4 = y4;
    }

    public double getX5() {
        return x5;
    }

    public void setX5(double x5) {
        this.x5 = x5;
    }

    public double getY5() {
        return y5;
    }

    public void setY5(double y5) {
        this.y5 = y5;
    }

    public double getA() {
        return a;
    }

    public void setA(double a) {
        this.a = a;
    }

    public double getB() {
        return b;
    }

    public void setB(double b) {
        this.b = b;
    }

    public double getC() {
        return c;
    }

    public void setC(double c) {
        this.c = c;
    }

    public int select() {
        int a = 0;
        int ret = 0;
        int n = 0;
        int k = 0;
        int num = 0;

        if (string.charAt(1) != ':')
            a = 0;
        for (int i = 2; i < len; i++) {
            if (string.charAt(i) == '+' || string.charAt(i) == '-') {
                if (string.charAt(i + 1) == '+' || string.charAt(i + 1) == '-') {
                    ret = 1;
                }
            }
            if (string.charAt(i) == '.' && string.charAt(i + 1) == ' ') {
                ret = 1;
                break;
            }
        }
        if (ret == 1)
            a = 0;
        else {
            if (string.charAt(0) == '1' || string.charAt(0) == '2' || string.charAt(0) == '3') {
                for (int j = 2; j < len; j++) {
                    if (string.charAt(j) == ' ')
                        n++;
                    if (string.charAt(j) == ',' || string.charAt(j) == ' ') {
                        k++;
                    }
                    if (string.charAt(j) != '.' && (string.charAt(j) != '+' && string.charAt(j) != '-') && !Character.isDigit(string.charAt(j))) {
                        arr[num] = j;
                        num++;
                    }
                }
                if (n != 2 && k > 5)
                    a = 2;
                if (n != 2 && k <= 5)
                    a = 0;
                if (n == 2 && k == 5) {
                    a = 1;
                }
            }
            if (string.charAt(0) == '4') {
                for (int j = 2; j < len; j++) {
                    if (string.charAt(j) == ' ')
                        n++;
                    if (string.charAt(j) == ',' || string.charAt(j) == ' ') {
                        k++;
                    }
                    if (string.charAt(j) != '.' && (string.charAt(j) != '+' && string.charAt(j) != '-') && !Character.isDigit(string.charAt(j))) {
                        arr[num] = j;
                        num++;
                    }
                }
                if (n != 4 && k > 9)
                    a = 2;
                if (n != 4 && k <= 9)
                    a = 0;
                if (n == 4 && k == 9) {
                    a = 1;
                }
            }
            if (string.charAt(0) == '5') {
                for (int j = 2; j < len; j++) {
                    if (string.charAt(j) == ' ')
                        n++;
                    if (string.charAt(j) == ',' || string.charAt(j) == ' ') {
                        k++;
                    }
                    if (string.charAt(j) != '.' && (string.charAt(j) != '+' && string.charAt(j) != '-') && !Character.isDigit(string.charAt(j))) {
                        arr[num] = j;
                        num++;
                    }
                }
                if (n != 3 && k > 7)
                    a = 2;
                if (n != 3 && k <= 7)
                    a = 0;
                if (n == 3 && k == 7) {
                    a = 1;
                }
            }
        }
        return a;
    }

    public void output() {
        String stringx1;
        String stringy1;
        String stringx2;
        String stringy2;
        String stringx3;
        String stringy3;
        String stringx4;
        String stringy4;
        String stringx5;
        String stringy5;
        if (string.charAt(0) == '1' || string.charAt(0) == '2' || string.charAt(0) == '3') {
            stringx1 = string.substring(2, arr[0]);
            stringy1 = string.substring(arr[0] + 1, arr[1]);
            stringx2 = string.substring(arr[1] + 1, arr[2]);
            stringy2 = string.substring(arr[2] + 1, arr[3]);
            stringx3 = string.substring(arr[3] + 1, arr[4]);
            stringy3 = string.substring(arr[4] + 1, len);
            x1 = Double.parseDouble(stringx1);
            y1 = Double.parseDouble(stringy1);
            x2 = Double.parseDouble(stringx2);
            y2 = Double.parseDouble(stringy2);
            x3 = Double.parseDouble(stringx3);
            y3 = Double.parseDouble(stringy3);
        } else if (string.charAt(0) == '4') {
            stringx1 = string.substring(2, arr[0]);
            stringy1 = string.substring(arr[0] + 1, arr[1]);
            stringx2 = string.substring(arr[1] + 1, arr[2]);
            stringy2 = string.substring(arr[2] + 1, arr[3]);
            stringx3 = string.substring(arr[3] + 1, arr[4]);
            stringy3 = string.substring(arr[4] + 1, arr[5]);
            stringx4 = string.substring(arr[5] + 1, arr[6]);
            stringy4 = string.substring(arr[6] + 1, arr[7]);
            stringx5 = string.substring(arr[7] + 1, arr[8]);
            stringy5 = string.substring(arr[8] + 1, len);
            x1 = Double.parseDouble(stringx1);
            y1 = Double.parseDouble(stringy1);
            x2 = Double.parseDouble(stringx2);
            y2 = Double.parseDouble(stringy2);
            x3 = Double.parseDouble(stringx3);
            y3 = Double.parseDouble(stringy3);
            x4 = Double.parseDouble(stringx4);
            y4 = Double.parseDouble(stringy4);
            x5 = Double.parseDouble(stringx5);
            y5 = Double.parseDouble(stringy5);
        } else {
            stringx1 = string.substring(2, arr[0]);
            stringy1 = string.substring(arr[0] + 1, arr[1]);
            stringx2 = string.substring(arr[1] + 1, arr[2]);
            stringy2 = string.substring(arr[2] + 1, arr[3]);
            stringx3 = string.substring(arr[3] + 1, arr[4]);
            stringy3 = string.substring(arr[4] + 1, arr[5]);
            stringx4 = string.substring(arr[5] + 1, arr[6]);
            stringy4 = string.substring(arr[6] + 1, len);
            x1 = Double.parseDouble(stringx1);
            y1 = Double.parseDouble(stringy1);
            x2 = Double.parseDouble(stringx2);
            y2 = Double.parseDouble(stringy2);
            x3 = Double.parseDouble(stringx3);
            y3 = Double.parseDouble(stringy3);
            x4 = Double.parseDouble(stringx4);
            y4 = Double.parseDouble(stringy4);
        }
    }

    public boolean isTriangle() {
        if (string.charAt(0) == '1' || string.charAt(0) == '2' || string.charAt(0) == '3') {
            a = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
            b = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
            c = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
        } else if (string.charAt(0) == '4') {
            a = Math.sqrt((x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4));
            b = Math.sqrt((x3 - x5) * (x3 - x5) + (y3 - y5) * (y3 - y5));
            c = Math.sqrt((x4 - x5) * (x4 - x5) + (y4 - y5) * (y4 - y5));
        } else {
            a = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
            b = Math.sqrt((x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4));
            c = Math.sqrt((x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4));
        }
        return a + b > c && a + c > b && b + c > a;
    }

    public void Isosceles() {
        if (a == b || b == c || a == c)
            System.out.print("true");
        else
            System.out.println("false");
        if (a == b && b == c)
            System.out.println(" true");
        else
            System.out.println(" false");
    }

    public void kinds() {
        double cosC = (a * a + b * b - c * c) / 2 * a * b;
        double cosB = (a * a + c * c - b * b) / 2 * a * c;
        double cosA = (b * b + c * c - a * a) / 2 * b * c;
        if (cosA < 0 || cosB < 0 || cosC < 0)
            System.out.print("true");
        else
            System.out.print("false");
        if (cosA == 0 || cosB == 0 || cosC == 0)
            System.out.print(" true");
        else
            System.out.print(" false");
        if (cosA > 0 && cosB > 0 && cosC > 0)
            System.out.print(" true");
        else
            System.out.print(" false");
    }

    public void inside() {
        double ABC = Area(x2,y2,x3,y3,x4,y4);
        double ABp = Area(x2,y2,x3,y3,x1,y1);
        double BCp = Area(x3,y3,x4,y4,x1,y1);
        double ACp = Area(x2,y2,x4,y4,x1,y1);
        double sum =  ABp + BCp + ACp;
        if(ACp == 0 || ABp == 0 || BCp == 0 && sum == ABC){
            System.out.println("on the triangle");
        }
        else {
            if(sum - ABC <= 0.00000001)
                System.out.println("in the triangle");
            else
                System.out.println("outof the triangle");
        }
    }

    public double Area(double x1,double y1,double x2,double y2,double x3,double y3) {
        return Math.abs(((x2 - x1) * (y3 - y2) - (y2 - y1) * (x3 - x2)) / 2.0);
    }
}
View Code

 

 

 

 

期中考试:第一道题注意数据范围否则只有4分

代码如下:

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double []arr = new double[100];
        for(int i = 0; i < 4 ; i++){
            arr[i] = input.nextDouble();
        }
        String color = input.next();
        if(arr[0]>0&&arr[0]<=200&&arr[1]>0&&arr[1]<=200&&arr[2]>0&&arr[2]<=200&&arr[3]>0&&arr[3]<=200){
        Point point1 = new Point(arr[0],arr[1]);
        Point point2 = new Point(arr[2],arr[3]);
        Line line = new Line(point1, point2, color);
        line.display();
        }
        else
            System.out.println("Wrong Format");
    }
}

class Point{
    private double x;
    private double y;

    public Point() {
    }

    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.print("(" + String.format("%.2f",x) + "," + String.format("%.2f",y) + ")");
    }
}

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

    public Line() {
    }

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

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

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

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

    public void display() {
        System.out.println("The line's color is:" + color);
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.println();
        System.out.println("The line's length is:" + String.format("%.2f",getDistance()));
    }
}
View Code

 

 

 

第二道题无

代码如下:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double []arr = new double[100];
        for(int i = 0; i < 4 ; i++){
            arr[i] = input.nextDouble();
        }
        String color = input.next();
        if(arr[0]>0&&arr[0]<=200&&arr[1]>0&&arr[1]<=200&&arr[2]>0&&arr[2]<=200&&arr[3]>0&&arr[3]<=200){
        Point point1 = new Point(arr[0],arr[1]);
        Point point2 = new Point(arr[2],arr[3]);
        Line line = new Line(point1, point2,color);
        Plane plane = new Plane(color);
        point1.display();
        System.out.println();
        point2.display();
        System.out.println();
        line.display();
        plane.display();
        }
        else
            System.out.println("Wrong Format");
    }
}

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

    public Point() {
    }

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

    public 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.print("(" + String.format("%.2f",x) + "," + String.format("%.2f",y) + ")");
    }
}

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

    public Line() {
    }

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

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

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

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

    public void display() {
        System.out.println("The line's color is:" + color);
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.println();
        System.out.println("The line's length is:" + String.format("%.2f",getDistance()));
    }
}

abstract class Element{
    public abstract void display();
}

class Plane extends Element{
    private String color;

    public Plane() {
    }

    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);
    }
}
View Code

 

第三道题注意删除对象为index-1个

代码如下:

 

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        GeometryObject geometryObject = new GeometryObject();
        while (choice!=0) {
            switch (choice) {
                case 1://insert Point object into list
                    double x = input.nextDouble();
                    double y = input.nextDouble();
                    Point point = new Point(x,y);
                    geometryObject.add(point);
                    break;
                case 2://insert Line object into list
                    double x1 = input.nextDouble();
                    double y1 = input.nextDouble();
                    double x2 = input.nextDouble();
                    double y2 = input.nextDouble();
                    String color = input.next();
                    Point point1 = new Point(x1,y1);
                    Point point2 = new Point(x2,y2);
                    Line line = new Line(point1,point2,color);
                    geometryObject.add(line);
                    break;
                case 3://insert Plane object into list
                    String color1 = input.next();
                    Plane plane = new Plane(color1);
                    geometryObject.add(plane);
                    break;
                case 4://delete index - 1 object from list
                    int index = input.nextInt();
                    geometryObject.remove(index);
                    break;
            }
            choice = input.nextInt();
        }

        for(int i = 0; i < geometryObject.getList().size();i++){
            geometryObject.getList().get(i).display();
        }
    }
}

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

    public Point() {
    }

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

    public 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.println("(" + String.format("%.2f",x) + "," + String.format("%.2f",y) + ")");
    }
}

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

    public Line() {
    }

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

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

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

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

    public void display() {
        System.out.println("The line's color is:" + color);
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.println("The line's length is:" + String.format("%.2f",getDistance()));
    }
}

abstract class Element{
    public abstract void display();
}

class Plane extends Element{
    private String color;

    public Plane() {
    }

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

class GeometryObject{
    ArrayList<Element>  list= new ArrayList<>();

    public GeometryObject() {
    }

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

    public void remove(int index) {
        if(index>0&&index<list.size())
        list.remove(index-1);
        else
            return;
    }

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

 

 

 

题目集6:

7-2:注意使用斜率判断时出现斜率不存在的情况,计算凹四边形面积时面积一定为两者中较小的一个。

单向双向链表:注意最后增加和删除在头和尾的位置的讨论。

 代码如下:

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DoubleLinkedList<Integer> list = new DoubleLinkedList<>();
        
        list.add(2);
        list.add(4);
        list.add(6);
        list.add(8);
        list.printList();
        list.remove(4);
        
        list.printList();
        list.add(2,100);
        list.printList();
        System.out.println(list.getData(4));
    }

}
//Node
public class Node<E> {
    private E data;
    private Node<E> next;
    private  Node<E> previous;

    public Node() {

    }
    
    public Node(E data, Node<E> nextm, Node<E> previous) {
        super();
        this.data = data;
        this.next = next;
        this.previous = previous;
    }

    public E getData() {
        return data;
    }

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

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

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

    public Node<E> getPrevious() {
        return previous;
    }

    public void setPrevious(Node<E> previous) {
        this.previous = previous;
    }
}
public interface DoubleLinkedListImpl<E> {

    /**

     * 判断链表是否为空

     * @return

     */

    public boolean isEmpty();



    /**

     * 获取当前链表节点数量

     * @return 节点数

     */

    public int getSize();



    /**

     * 获取链表中第index个位置的节点的data值

     * @param index:节点在链表中的位置

     * @return:返回该节点的data值

     */

    public E getData(int index);



    /**

     * 删除链表最后一个节点

     */

    public void remove();



    /**

     *删除链表中第index位置的节点

     * @param index:节点在链表中的位置

     */

    public void remove(int index);



    /**

     * 在链表的第index个位置之前插入一个节点,值为theElement,index∈[1,size]

     * @param index:插入节点在链表中的位置

     * @param theElement:新插入节点的data值

     */

    public void add(int index, E theElement);



    /**

     * 在链表尾插入节点,插入节点data值为element

     * @param element

     */

    public void add(E element);



    /**

     * 输出链表

     */

    public void printList();



    /**

     * 获取第一个节点的data值

     * @return

     */

    public E getFirst();



    /**

     * 获取链表最后一个节点的data值

     * @return

     */

    public E getLast();

}
public class DoubleLinkedList<E> implements DoubleLinkedListImpl<E>{
    private Node<E> head;// 头结点(非第一个节点),当前节点,尾节点
    private Node<E> curr;
    private Node<E> tail;

    private int size = 0;

    public DoubleLinkedList() {
        super();
        // TODO Auto-generated constructor stub
        head = new Node<E>();
        head.setNext(null);
        curr = tail = null;
        this.size = 0;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return this.size == 0;
    }

    @Override
    public int getSize() {
        // TODO Auto-generated method stub
        return this.size;
    }

    @Override
    public E getData(int index) {
        // TODO Auto-generated method stub
        if(index < 1 || index > this.size) {
            return null;
        }

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

        return curr.getData();
    }

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

    @Override
    public void remove(int index) {
        // TODO Auto-generated method stub
        if(index < 1 || index > this.size) {
            return ;
        }

        curr = head;

        if(index == 1) {
            curr = head.getNext();
            head.setNext(curr.getNext());
        } else if(index == this.size) {//如果删除的是最后一个节点
            tail = curr;
        } else {//找到第index - 1个节点赋给curr
            for(int i = 1;i < index; i++) {
                curr = curr.getNext();
            }
            curr.setNext(curr.getNext().getNext());
            curr.getNext().setPrevious(curr);
        }

        this.size --;//整个链表的节点数量-1

    }


    @Override
    public void add(int index, E theElement) {
        // TODO Auto-generated method stub
        if(index < 1 || index > this.size + 1) {
            return ;
        }

        Node<E> curr = new Node<>();
        curr.setData(theElement);
        curr.setNext(null);

        if(this.size == 0) {
            head.setNext(curr);
            tail = curr;
        }else if(index == this.size + 1) {
            this.tail.setNext(curr);
            tail = curr;
            tail.setPrevious(curr);
        }else {
            Node<E> tempNode = head;
            for(int i  = 1; i < index;i++) {
                tempNode = tempNode.getNext();
            }

            curr.setNext(tempNode.getNext());
            tempNode.getNext().setPrevious(curr);
            tempNode.setNext(curr);
            curr.setPrevious(tempNode.getNext());
        }

        this.size ++;

    }

    @Override
    public void add(E element) {
        // TODO Auto-generated method stub
        this.add(this.size + 1,element);

    }

    @Override
    public void printList() {
        // TODO Auto-generated method stub
        curr = head.getNext();
        for(int i = 1; i <= this.size;i ++) {
            System.out.print(curr.getData() + " ");
            curr = curr.getNext();
        }

        System.out.println("");

    }

    @Override
    public E getFirst() {
        return null;
    }

    public E getLast() {
        if(this.size != 0) {
            return tail.getData();
        }

        return null;
    }

}
View Code

 

(4)改进建议

题目集4:

7-2:代码最大的问题在于没有将方法写于类中几乎全部写在了主函数中,耦合性很强。

7-3:在7-2的代码上有了进步,但依旧有方法写在了主函数中

期中考试:

代码均根据类图书写,无大问题

题目集6:

7-2:代码结构的问题有了很大的改进,题目集4的问题解决,但将方法中重复的内容新建方法来书写可优化代码。

单向双向链表:

根据老师结构书写。

 

5)总结

这几次习题我几乎都没拿到满分,很遗憾,但我仍明显感受到自己学习到了不少知识。图形类题目的多次迭代,让我不断优化了错误输入和边界值的判断,从最初无计可施勉强用空格和逗号数量来进行判断,拿到一些分数,到最后运用正则表达式简单明了的优化,虽然只是代码中很简短的几行,但这样的优化是很有毕要的,能让其他看代码的人轻松看懂,大大提升了可读性,图形类习题的研究不仅仅是对java的研究,也是对数学的研究,数学当中简简单单能够判断的如四边形,在java中却并不轻松,数学中多种方法能够实现的事情在java中也未必都可行,比如在计算三角形面积时,我起初利用的是海伦公式,但由于开了一次根号,在数据的保留阶段便可能出现误差,这是我起初完全没有想象到的。期中考试也给我狠狠上了一课,光是平时看一遍网课是远远不够的,网课中关于容器类的讲解我自认为已经了解,但当真正实战用到时,完全是手忙脚乱,摸不着头脑,因此在java学习中不仅仅是理论学习,更多的还得多动手多实践,方能真正掌握。在对于链表的练习中,我起初以为自己根本无法完成,毕竟上学期c语言学习链表便已经是一知半解了,这学期还得加上java与刚学习的接口和泛型,因此第一次作业几乎为照着网上代码半知半解的打,功能并未全部实现,但在老师上课对单向链表进行复盘分析后,基础的链表我已确认拿下,当我根据自己的理解再实现双向链表时,没有借助任何网上代码便得以实现,带给我的成就感着实不小,我对自己信心又高了几分!

存在的不足:首先是在学习中留给java的时间还是不足,导致题目集中许多功能还没有彻底实现便已经到了截至日期,其次在java的封装性方面仍需提升,代码耦合性并未到达理想情况,最后是对自己学习的信心不足,今后尽可能攻破更多难题增强信心,若全力以赴依旧无法完成也不过分气馁,争取拿下!

 

posted @ 2022-05-01 22:33  yyy123138  阅读(151)  评论(0)    收藏  举报