Daifff-

第二次博客作业

一、前言:

1).这几个星期接触了点线形系列,其中有大量的数学知识运用,这些题目要求的是缜密的数学思想用代码表达出来。

2).还有就是自学了正则表达式, 进行了一些正则表达式的训练,简单的利用了正则表达式判断输出如格式是否正确以及提取所需要的字符。

3).在题目集六中接触了点线形系列-凸四边形的计算,并且还有设计一个银行业务类。

4).这几个星期还进行了链表练习,实现了单向链表以及双向链表的一些功能。

5).在中国大学MOOC上进行了继承与多态的练习,学会了减少代码重复。

6).进行了期中考试,三道题目,联系十分紧密,迭代关系,第一道题的源码第二题第三题都能够直接用上,再按照要求修改。

在这几次的题目集中,明显感受到题目难度上升了不少,以及题量的加大,所以在写pta花费的时间增加了不少。

 

二、设计与分析:

题目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:长度值
  ```

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        String color = input.next();
        if (x1 <= 0 || x1 > 200 || y1 <= 0 || y1 > 200 || x2 <= 0 || x2 > 200 || y2 <= 0 || y2 > 200) {
            System.out.println("Wrong Format");
        } else {
            Point point1 = new Point(x1, y1);
            Point point2 = new Point(x2, y2);
            point1.setX(x1);
            point2.setX(x2);
            point1.setY(y1);
            point2.setY(y2);
            Line line = new Line(point1, point2, color);
            line.setColor(color);
            line.display(point1, point2, color);
        }
    }
}
class Point {
    private double x;
    private double y;

    public Point() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Point(double x, double 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("(");
        System.out.printf("%.2f", x);
        System.out.print(",");
        System.out.printf("%.2f", y);
        System.out.print(")");
        System.out.println("");
    }
}
class Line {
    private Point point1;
    private Point point2;
    private String color;

    public Line() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Line(Point point1, Point point2, String 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(Point point1, Point point2) {
        double distance = 0;
        distance = Math.sqrt((point1.getX() - point2.getX()) * (point1.getX() - point2.getX())
                + (point1.getY() - point2.getY()) * (point1.getY() - point2.getY()));
        return distance;
    }

    public void display(Point point1, Point point2, String color) {
        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.print("The line's length is:");
        System.out.printf("%.2f", getDistance(point1, point2));
    }
}

 

分析:

  • 这道题主要考查了由于类中属性是私有的,所以无法直接调用,要通过getter setter调用类中的私有属性。

  • 其次考察了控制输出格式,保留两位小数输出。

     

     

题目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();

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        String color = input.next();
        if (x1 <= 0 || x1 > 200 || y1 <= 0 || y1 > 200 || x2 <= 0 || x2 > 200 || y2 <= 0 || y2 > 200) {
            System.out.println("Wrong Format");
        } else {
            Point point1 = new Point();
            Point point2 = new Point();
            Line line = new Line();
            Plane plane = new Plane();
            point1.setX(x1);
            point2.setX(x2);
            point1.setY(y1);
            point2.setY(y2);
            line.setColor(color);
            line.setPoint1(point1);
            line.setPoint2(point2);
            plane.setColor(color);
            Element element = new Element();
            element = point1;// 起点Point
            element.display();

            element = point2;// 终点Point
            element.display();

            element = line;// 线段
            element.display();

            element = plane;//
            element.display();
        }
    }
}
class Point extends Element {
    private double x;
    private double y;

    public Point() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Point(double x, double y) {
    super();
    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("(");
        System.out.printf("%.2f", x);
        System.out.print(",");
        System.out.printf("%.2f", y);
        System.out.print(")");
        System.out.println("");
    }
}
class Line extends Element{
    private Point point1;
    private Point point2;
    private String color;

    public Line() {
        super();
        // TODO 自动生成的构造函数存根
    }

    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(Point point1, Point point2) {
        double distance = 0;
        distance = Math.sqrt((point1.getX() - point2.getX()) * (point1.getX() - point2.getX())
                + (point1.getY() - point2.getY()) * (point1.getY() - point2.getY()));
        return distance;
    }

        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.print("The line's length is:");
        System.out.printf("%.2f", getDistance(point1, point2));
        System.out.println("");
    }
}
class Element {
    
public Element() {
        super();
        // TODO 自动生成的构造函数存根
    }


    public void display() {
        
    }
    
}
class Plane extends Element {
    private String color;
    public Plane() {
        super();
        // TODO 自动生成的构造函数存根
    }
    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);
    }
}

 

分析:

  • 本题主要是在上一题的基础上增加一个父类Element,在主方法内只需要通过new一个Element类型的对象,再将element赋值为自己所需要的Point类对象,Line类对象,Plane类对象,在赋值之后只需要使用Element类的方法,他会调用对应类型中的方法,达到减少代码重复。

  • 其次就是表子类时要在子类后加上extends Element。

 

 

题目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();
    }

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

			choice = input.nextInt();

		}
		for (Element element : geometryobject.getList()) {
			element.display();
		}
	}
}

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

	public GeometryObject() {
		super();
		// TODO 自动生成的构造函数存根
	}
	
	public void add(Element element) {
		list.add(element);
	}
	
	public void remove(int index) {
        if(index >= 0 && index < list.size())
		list.remove(index);
	}
	
	public ArrayList<Element> getList(){
		return list;
	}
}
class Plane extends Element {
	private String color;
	public Plane() {
		super();
		// TODO 自动生成的构造函数存根
	}
	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 Element {

	public void display() {
		
	}
	
}
class Line extends Element{
	private Point point1;
	private Point point2;
	private String color;

	public Line() {
		super();
		// TODO 自动生成的构造函数存根
	}

	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(Point point1, Point point2) {
		double distance = 0;
		distance = Math.sqrt((point1.getX() - point2.getX()) * (point1.getX() - point2.getX())
				+ (point1.getY() - point2.getY()) * (point1.getY() - point2.getY()));
		return distance;
	}

		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.print("The line's length is:");
		System.out.printf("%.2f", getDistance(point1, point2));
	}
}
class Point extends Element {
	private double x;
	private double y;

	public Point() {
		super();
		// TODO 自动生成的构造函数存根
	}

	public Point(double x, double y) {
	super();
	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("(");
		System.out.printf("%.2f", x);
		System.out.print(",");
		System.out.printf("%.2f", y);
		System.out.print(")");
		System.out.println("");
	}
}

 

分析:

  • 定义一个GeometryObject类,定义一个ArrayList<Element>容器,储存所有的元素。
  • 增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index >= 0)个对象。

 

题目4).

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

}

 

代码如下:

package List;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LList<Integer> list = new LList<>();

		list.add(2);
		list.add(4);
		list.add(6);
		list.add(8);
		list.headPrintList();
		;
		list.remove(3);
		list.tailPrintList();
		;
		list.add(2, 100);
		list.headPrintList();
		;
		System.out.println(list.get(4));
	}

}

package List;

public class Node<E> {
	private E data;
	private Node<E> next;
	private Node<E> prior;

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

	public Node() {
		super();
		// TODO 自动生成的构造函数存根
	}

	public E getData() {
		return data;
	}

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

	public Node<E> getPrior() {
		return prior;
	}

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

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

	public void setPrior(Node<E> prior) {
		this.prior = prior;
	}

}

package List;

public class LList<E> implements LinearListlnterface<E> {
	private Node<E> head;
	private Node<E> curr;
	private Node<E> tail;

	private int size = 0;

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

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

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

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

		curr = head;
		if (index > this.size >> 1) {
			curr = tail;
			for (int i = size; i > index; i--) {
				curr = curr.getPrior();
			}
		} else {
			for (int i = 0; i < index; i++) {
				curr = curr.getNext();
			}
		}
		return curr.getData();
	}

	@Override
	public void remove(int index) {
		// TODO Auto-generated method stub
		if (index < 1 || index > this.size) {
			return;
		}
		if (head.getNext() == null) {
			System.out.println("链表为空");
			return;
		}

		curr = head;

		if (index == 1) {
			curr = head.getNext();
			head.setNext(curr.getNext());
			curr.getNext().setPrior(head);
		}
		if (index == this.size) {
			tail = curr;
		} else {
			for (int i = 1; i < index; i++) {
				curr = curr.getNext();
			}

			curr.setNext(curr.getNext().getNext());
			curr.getNext().setPrior(curr);
		}

		this.size--;

	}

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

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

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

		this.size++;

	}

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

	}

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

		System.out.println("");

	}

	public void tailPrintList() {
		// TODO Auto-generated method stub
		curr = tail;
		for (int i = this.size; i > 0; i--) {
			System.out.print(curr.getData() + " ");
			curr = curr.getPrior();
		}

		System.out.println("");

	}

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

		}

		return null;
	}

}

package List;

public interface LinearListlnterface<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 headPrintList();

	public void tailPrintList();

}

 

分析:

  • 在将链表每个节点连接起来时没有考虑此节点是否为空,直接使用此节点的setPrior或setNext将其连接起来,导致报错,当此节点为空则不能使用此节点的setPrior与setNext,要使用此节点的前一节点将的setNext将其连接起来。
  • 在进行双向链表的连接时,在节点为空时进行双向链接时,要使用此节点前一节点的setNext的setNext连接,与后一节点的setPrior的setPrior将其连接起来。

 

题目5).

点线形系列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"

 

Code
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {		
		Scanner input = new Scanner(System.in);
		String a = input.nextLine();
		 if(a.charAt(0) < '1' || a.charAt(0) > '5')
	        {
	            System.out.println("Wrong Format");
	            System.exit(0);
	        }
		String num = a.substring(0, 1);
		int choice = Integer.parseInt(num);
		if(choice ==  1) {						//选项1
			int count1 = 0;int count2 = 0;
			for(int i = 0;i < a.length();i ++) {
				if(a.charAt(i) == ' ')
					count1 ++;
			}
             for(int i = 0;i < a.length();i ++) {
				if(a.charAt(i) == ',')
					count2 ++;
			}
//			if(!checkFormat3(a) || count1 == 0 || a.charAt(1) != ':')
//				System.out.println("Wrong Format");
//			else if(count1 != 2 && count2 != 3)
//				System.out.println("wrong number of points");
//			else 
				if(count1 == 2) {
				int flag3 = 0;int flag4 = 0;
				for(int j = 0;j < a.length();j ++) {
					if(a.charAt(j) == ' ') {
						flag3 = j;
						break;
					}
				}
				
				for(int j = flag3 + 1;j < a.length();j ++) {
					if(a.charAt(j) == ' ') {
						flag4 = j;
						break;
					}
				}
				
				String b = a.substring(2,flag3);//第一个坐标
				String c = a.substring(flag3 +1 , flag4);//第二个坐标
				String d = a.substring(flag4 + 1, a.length());//第三个坐标
				int m1 = findComma(b);
				int m2 = findComma(c);
				int m3 = findComma(d);
				String x10 = b.substring(0, m1); String y10 = b.substring(m1 + 1, b.length());
				String x20 = c.substring(0, m2); String y20 = c.substring(m2 + 1, c.length());
				String x30 = d.substring(0, m3); String y30 = d.substring(m3 + 1, d.length());
				double x1 = Double.parseDouble(x10);
				double y1 = Double.parseDouble(y10);
				double x2 = Double.parseDouble(x20);
				double y2 = Double.parseDouble(y20);
				double x3 = Double.parseDouble(x30);
				double y3 = Double.parseDouble(y30);
				if(checkTriangle(x1,y1,x2,y2,x3,y3)) {
					double A = result(x1,y1,x2,y2);//第一条边
					double B = result(x1,y1,x3,y3);//第二条边
					double C = result(x2,y2,x3,y3);//第三条边
				if(A == B  || A == C  || B == C)//判断是否为等腰三角形
					System.out.print("true");
				else
					System.out.print("false");
				if(A == B && B == C) //判断是否为等边三角形
					System.out.print(" true");
				else
					System.out.print(" false");
				}
				else
					System.out.println("data error");
		}
			else
				System.out.println("Wrong Format");
	  }
		
		else if(choice == 2) {		//选项2
			int count1 = 0;int count2 = 0;
			for(int i = 0;i < a.length();i ++) {
				if(a.charAt(i) == ' ')
					count1 ++;
			}
             for(int i = 0;i < a.length();i ++) {
				if(a.charAt(i) == ',')
					count2 ++;
			}
//			if(!checkFormat3(a) || count1 == 0 || a.charAt(1) != ':')
//				System.out.println("Wrong Format");
//			else if(count1 != 2 && count2 != 3)
//				System.out.println("wrong number of points");
//			else 
				if(count1 == 2) {
				int flag3 = 0;int flag4 = 0;
				for(int j = 0;j < a.length();j ++) {
					if(a.charAt(j) == ' ') {
						flag3 = j;
						break;
					}
				}
				
				for(int j = flag3 + 1;j < a.length();j ++) {
					if(a.charAt(j) == ' ') {
						flag4 = j;
						break;
					}
				}
				
				String b = a.substring(2,flag3);//第一个坐标
				String c = a.substring(flag3 +1 , flag4);//第二个坐标
				String d = a.substring(flag4 + 1, a.length());//第三个坐标
				int m1 = findComma(b);
				int m2 = findComma(c);
				int m3 = findComma(d);
				String x10 = b.substring(0, m1); String y10 = b.substring(m1 + 1, b.length());
				String x20 = c.substring(0, m2); String y20 = c.substring(m2 + 1, c.length());
				String x30 = d.substring(0, m3); String y30 = d.substring(m3 + 1, d.length());
				double x1 = Double.parseDouble(x10);
				double y1 = Double.parseDouble(y10);
				double x2 = Double.parseDouble(x20);
				double y2 = Double.parseDouble(y20);
				double x3 = Double.parseDouble(x30);
				double y3 = Double.parseDouble(y30);
				if(checkTriangle(x1,y1,x2,y2,x3,y3)) {
				double perimeter = 0;
				perimeter = result(x1,y1,x2,y2) + result(x1,y1,x3,y3) + result(x2,y2,x3,y3);
				perimeter = Math.round(perimeter * 1000000) / 1000000.0;
				double area = 0;
				area = (x1 * y2 + x2 * y3 + x3 * y1 - x1 * y3 - x2 * y1 - x3 * y2) / 2;
				area = Math.round(area * 1000000) / 1000000.0;
				double x0 = 0;double y0 = 0;
				x0 = (x1 + x2 + x3) / 3;y0 = (y1 + y2 + y3) / 3;
				x0 = Math.round(x0 * 1000000) / 1000000.0;y0 = Math.round(y0 * 1000000) / 1000000.0;
				System.out.print(perimeter);
				System.out.print(" ");
				System.out.print(area);
				System.out.print(" ");
				System.out.print(x0);
				System.out.print(",");
				System.out.print(y0);
				}
				else
					System.out.println("data error");
			}
			else System.out.println("Wrong Format");
		}
		
				else if(choice == 3) {				//选项3
				int count1 = 0;
				int count2 = 0;
				for(int i = 0;i < a.length();i ++) {
					if(a.charAt(i) == ' ')
						count1 ++;
				}
	             for(int i = 0;i < a.length();i ++) {
					if(a.charAt(i) == ',')
						count2 ++;
				}
				 if(count1 == 0) {
                         System.out.println("Wrong Format");
                         System.exit(0);
                     }
                    else if(!checkFormat3(a)){
				System.out.println("Wrong Format");
				System.exit(0);
			}
					else if(count1 != 2 && count2 != 3) {
				System.out.println("wrong number of points");
				System.exit(0);
            }
			else if (count1 == 2)
                    {
					int flag3 = 0;int flag4 = 0;
					for(int j = 0;j < a.length();j ++) {
						if(a.charAt(j) == ' ') {
							flag3 = j;
							break;
						}
					}
					
					for(int j = flag3 + 1;j < a.length();j ++) {
						if(a.charAt(j) == ' ') {
							flag4 = j;
							break;
						}
					}
					
					String b = a.substring(2,flag3);//第一个坐标
					String c = a.substring(flag3 +1 , flag4);//第二个坐标
					String d = a.substring(flag4 + 1, a.length());//第三个坐标
					int m1 = findComma(b);
					int m2 = findComma(c);
					int m3 = findComma(d);
					String x10 = b.substring(0, m1); String y10 = b.substring(m1 + 1, b.length());
					String x20 = c.substring(0, m2); String y20 = c.substring(m2 + 1, c.length());
					String x30 = d.substring(0, m3); String y30 = d.substring(m3 + 1, d.length());
					double x1 = Double.parseDouble(x10);
					double y1 = Double.parseDouble(y10);
					double x2 = Double.parseDouble(x20);
					double y2 = Double.parseDouble(y20);
					double x3 = Double.parseDouble(x30);
					double y3 = Double.parseDouble(y30);
					if(checkTriangle(x1,y1,x2,y2,x3,y3)) {
						double A = result(x1,y1,x2,y2);//第一条边
						double B = result(x1,y1,x3,y3);//第二条边
						double C = result(x2,y2,x3,y3);//第三条边
						double max = findMax(A,B,C);
						if(A * A + B * B + C * C - max * max - max * max > 0)
							System.out.println("false false true");
						else if(Math.abs(A * A + B * B + C * C - max * max - max * max) < 0.000001)
							System.out.println("false true false");
						else if(A * A + B * B + C * C - max * max - max * max < -0.00001)
							System.out.println("true false false");
						
					}
					else
						System.out.println("data error");
				
				}
				else
					System.out.println("Wrong Format");
			
			
		}
		
				else if(choice == 4) {
					int count1 = 0;int count2 = 0;
					for(int i = 0;i < a.length();i ++) {
						if(a.charAt(i) == ' ')
							count1 ++;
					}
		             for(int i = 0;i < a.length();i ++) {
						if(a.charAt(i) == ',')
							count2 ++;
					}
		             if(count1 == 0) {
                         System.out.println("Wrong Format");
                         System.exit(0);
                     }
                    else if(!checkFormat3(a)){
				System.out.println("Wrong Format");
				System.exit(0);
			} 
		            	 
//					if(!checkFormat3(a) || count1 == 0 || a.charAt(1) != ':')
//						System.out.println("Wrong Format");
					else if(count1 != 4 && count2 != 5)
						System.out.println("wrong number of points");
					else 
						if(count1 == 4) {
						int flag3 = 0;int flag4 = 0;int flag5 = 0;int flag6 = 0;
						for(int j = 0;j < a.length();j ++) {
							if(a.charAt(j) == ' ') {
								flag3 = j;
								break;
							}
						}
						
						for(int j = flag3 + 1;j < a.length();j ++) {
							if(a.charAt(j) == ' ') {
								flag4 = j;
								break;
							}
						}
						
						for(int j = flag4 + 1;j < a.length();j ++) {
							if(a.charAt(j) == ' ') {
								flag5 = j;
								break;
							}
						}
						
						for(int j = flag5 + 1;j < a.length();j ++) {
							if(a.charAt(j) == ' ') {
								flag6 = j;
								break;
							}
						}
						
						String b = a.substring(2,flag3);//第一个坐标
						String c = a.substring(flag3 +1 , flag4);//第二个坐标
						String d = a.substring(flag4 + 1, flag5);//第三个坐标
						String e = a.substring(flag5 + 1, flag6);//第四个坐标
						String f = a.substring(flag6 + 1, a.length());//第五个坐标
						int m1 = findComma(b);
						int m2 = findComma(c);
						int m3 = findComma(d);
						int m4 = findComma(e);
						int m5 = findComma(f);
						String x10 = b.substring(0, m1); String y10 = b.substring(m1 + 1, b.length());
						String x20 = c.substring(0, m2); String y20 = c.substring(m2 + 1, c.length());
						String x30 = d.substring(0, m3); String y30 = d.substring(m3 + 1, d.length());
						String x40 = e.substring(0, m4); String y40 = e.substring(m4 + 1, e.length());
						String x50 = f.substring(0, m5); String y50 = f.substring(m5 + 1, f.length());
						double x1 = Double.parseDouble(x10); double y1 = Double.parseDouble(y10);
						double x2 = Double.parseDouble(x20); double y2 = Double.parseDouble(y20);
						double x3 = Double.parseDouble(x30); double y3 = Double.parseDouble(y30);
						double x4 = Double.parseDouble(x40); double y4 = Double.parseDouble(y40);
						double x5 = Double.parseDouble(x50); double y5 = Double.parseDouble(y50);
						if(x1 == x2 && y1 == y2)
							System.out.println("points coincide");
						else
						if(checkTriangle(x3,y3,x4,y4,x5,y5)) {
							double k0 = 0;double k1 = 0;double k2 = 0;double k3 = 0;
							double n0 = 0;double n1 = 0;double n2 = 0;double n3 = 0;
						//	if(x3 != x4 && x3 != x5 && x4 != x5 && x1 != x2) {							//计算斜率存在时的斜率
							k0 = (y2 - y1) / (x2 - x1); n0 = y2 - k0 * x2;
							k1 = (y3 - y4) / (x3 - x4); n1 = y3 - k1 * x3;
							k2 = (y3 - y5) / (x3 - x5); n2 = y3 - k2 * x3;
							k3 = (y4 - y5) / (x4 - x5); n3 = y4 - k3 * x4;
							if((k0 == k1 && n0 == n1) || (k0 == k2 && n0 == n2) || (k0 == k3 && n0 == n3))
								System.out.println("The point is on the edge of the triangle");
							  else {															//不重合情况
								  int sum = 0;
								  int counta = 0;												//有效交点个数
								  int countb = 0;
								  int countc = 0;
								  int flag = 0;
								  double temp = 0;
								  double focusX1 = 0;double focusY1 = 0;
								  double focusX2 = 0;double focusY2 = 0;
								  double focusX3 = 0;double focusY3 = 0;
                                  focusX1 = calculateFoucsX(x1,y1,x2,y2,x3,y3,x4,y4);
                                  focusY1 = calculateFoucsY(x1,y1,x2,y2,x3,y3,x4,y4);
                                  focusX2 = calculateFoucsX(x1,y1,x2,y2,x3,y3,x5,y5);
                                  focusY2 = calculateFoucsY(x1,y1,x2,y2,x3,y3,x5,y5);
                                  focusX3 = calculateFoucsX(x1,y1,x2,y2,x4,y4,x5,y5);
                                  focusY3 = calculateFoucsY(x1,y1,x2,y2,x4,y4,x5,y5);
                                  if(!calculateDis(x3,y3,focusX1,focusY1,x4,y4) && !calculateDis(x3,y3,focusX2,focusY2,x5,y5) && !calculateDis(x4,y4,focusX3,focusY3,x5,y5)) {
                                	  System.out.println("0");
                                	  System.exit(0);
                                  }
                                  if((focusX1 == focusX2 && focusY1 == focusY2 && !calculateDis(x4,y4,focusX3,focusY3,x5,y5)) || 
                                		  (focusX1 == focusX3 && focusY1 == focusY3 && !calculateDis(x3,y3,focusX2,focusY2,x5,y5)) ||  
                                		  (focusX2 == focusX3 && focusY2 == focusY3 && !calculateDis(x3,y3,focusX1,focusY1,x4,y4))) {
                                	  System.out.println("1");
      								System.exit(0);
                                  }
                                  else {
                                	  if(calculateDis(x3,y3,focusX1,focusY1,x4,y4) && calculateDis(x4,y4,focusX3,focusY3,x5,y5)) {
                                		  double areaSum = Math.abs(x3 * y4 + x4 * y5 + x5 * y3 - x3 * y5 - x4 * y3 - x5 * y4) / 2;
                                		  double areaMin = Math.abs(x4 * focusY1 + focusX1 * focusY3 + focusX3 * y4 - x4 * focusY3 - focusX1 * y4 - focusX3 * focusY1) / 2;
    									  double areaMax = areaSum - areaMin;
    									  if(areaMin > areaMax) {
    										  temp = areaMin;
    										  areaMin = areaMax;
    										  areaMax = temp;
    									  }
    									  areaMin = Math.round(areaMin * 1000000) / 1000000.0;
    									  areaMax = Math.round(areaMax * 1000000) / 1000000.0;
    									  System.out.println("2"+" "+areaMin+" "+areaMax);
    									  System.exit(0);
                                	  }
                                	  if(calculateDis(x3,y3,focusX1,focusY1,x4,y4) && calculateDis(x3,y3,focusX2,focusY2,x5,y5)) {
                                		  double areaSum = Math.abs(x3 * y4 + x4 * y5 + x5 * y3 - x3 * y5 - x4 * y3 - x5 * y4) / 2;
                                		  double areaMin = Math.abs(x3 * focusY1 + focusX1 * focusY2 + focusX2 * y3 - x3 * focusY2 - focusX1 * y3 - focusX2 * focusY1) / 2;
    									  double areaMax = areaSum - areaMin;
    									  if(areaMin > areaMax) {
    										  temp = areaMin;
    										  areaMin = areaMax;
    										  areaMax = temp;
    									  }
    									  areaMin = Math.round(areaMin * 1000000) / 1000000.0;
    									  areaMax = Math.round(areaMax * 1000000) / 1000000.0;
    									  System.out.println("2"+" "+areaMin+" "+areaMax);
    									  System.exit(0);
                                	  }
                                	  
                                	  if(calculateDis(x3,y3,focusX2,focusY2,x5,y5) && calculateDis(x4,y4,focusX3,focusY3,x5,y5)) {
                                		  double areaSum = Math.abs(x3 * y4 + x4 * y5 + x5 * y3 - x3 * y5 - x4 * y3 - x5 * y4) / 2;
                                		  double areaMin = Math.abs(x5 * focusY2 + focusX2 * focusY3 + focusX3 * y5 - x5 * focusY3 - focusX2 * y5 - focusX3 * focusY2) / 2;
    									  double areaMax = areaSum - areaMin;
    									  if(areaMin > areaMax) {
    										  temp = areaMin;
    										  areaMin = areaMax;
    										  areaMax = temp;
    									  }
    									  areaMin = Math.round(areaMin * 1000000) / 1000000.0;
    									  areaMax = Math.round(areaMax * 1000000) / 1000000.0;
    									  System.out.println("2"+" "+areaMin+" "+areaMax);
    									  System.exit(0);
                                	  }
                                  }
                                    }
                                 }
							else {
								System.out.println("data error");
								System.exit(0);
							}	  
						}
				}
				else if(choice == 5) {
                     
					int count1 = 0;int count2 = 0;
					for(int i = 0;i < a.length();i ++) {
						if(a.charAt(i) == ' ')
							count1 ++;
					}
		             for(int i = 0;i < a.length();i ++) {
						if(a.charAt(i) == ',')
							count2 ++;
					}
            if (count1 == 0) {
				System.out.println("Wrong Format");
				System.exit(0);
			} 
			else if (count1 != 3 && count2 != 4) {
				System.out.println("wrong number of points");
				System.exit(0);
			} else if (count1 == 3){
						int flag3 = 0;int flag4 = 0;int flag5 = 0;int flag6 = 0;
						for(int j = 0;j < a.length();j ++) {
							if(a.charAt(j) == ' ') {
								flag3 = j;
								break;
							}
						}
						
						for(int j = flag3 + 1;j < a.length();j ++) {
							if(a.charAt(j) == ' ') {
								flag4 = j;
								break;
							}
						}
						
						for(int j = flag4 + 1;j < a.length();j ++) {
							if(a.charAt(j) == ' ') {
								flag5 = j;
								break;
							}
						}
						
						String b = a.substring(2,flag3);//第一个坐标
						String c = a.substring(flag3 +1 , flag4);//第二个坐标
						String d = a.substring(flag4 + 1, flag5);//第三个坐标
						String e = a.substring(flag5 + 1, a.length());//第四个坐标
						int m1 = findComma(b);
						int m2 = findComma(c);
						int m3 = findComma(d);
						int m4 = findComma(e);
						String x10 = b.substring(0, m1); String y10 = b.substring(m1 + 1, b.length());
						String x20 = c.substring(0, m2); String y20 = c.substring(m2 + 1, c.length());
						String x30 = d.substring(0, m3); String y30 = d.substring(m3 + 1, d.length());
						String x40 = e.substring(0, m4); String y40 = e.substring(m4 + 1, e.length());
						double x1 = Double.parseDouble(x10); double y1 = Double.parseDouble(y10);
						double x2 = Double.parseDouble(x20); double y2 = Double.parseDouble(y20);
						double x3 = Double.parseDouble(x30); double y3 = Double.parseDouble(y30);
						double x4 = Double.parseDouble(x40); double y4 = Double.parseDouble(y40);
						if(checkTriangle(x2,y2,x3,y3,x4,y4)) {
                            int num5 = 0;
							double focusX1 = 0;double focusY1 = 0;
						    double focusX2 = 0;double focusY2 = 0;
						    double focusX3 = 0;double focusY3 = 0;
                            double k1 = 0;double k2 = 0;
                            focusX1 = calculateFoucsX(x1,y1,x1 + 1000,y1 + 30,x2,y2,x3,y3);
                            focusY1 = calculateFoucsY(x1,y1,x1 + 1000,y1 + 30,x2,y2,x3,y3);
                            focusX2 = calculateFoucsX(x1,y1,x1 + 1000,y1 + 30,x2,y2,x4,y4);
                            focusY2 = calculateFoucsY(x1,y1,x1 + 1000,y1 + 30,x2,y2,x4,y4);
                            focusX3 = calculateFoucsX(x1,y1,x1 + 1000,y1 + 30,x3,y3,x4,y4);
                            focusY3 = calculateFoucsY(x1,y1,x1 + 1000,y1 + 30,x3,y4,x4,y4);
                            k1 = 30 / 1000;k2 = (y4 - y2) / (x4 - x2);
							if((x1 == x2 && y1 == y2) || (x1 == x3 && y1 == y3) || (x1 == x4 && y1 == y4)) {     //在三角形三个顶点上
                                System.out.println("on the triangle");
					            System.exit(0);
                            }
                            
							if(calculateDis(x2,y2,x1,y1,x3,y3) || calculateDis(x2,y2,x1,y1,x4,y4) || calculateDis(x3,y3,x1,y1,x4,y4)) {
                                System.out.println("on the triangle");
					            System.exit(0);
                            }//在三角形三条边上
							if(Math.abs(k1 - k2) < 0.000001 ) {
                            focusX2 = x1;
                            focusY2 = y1;
                            }
                            if(focusX1 > x1)
                                num5 ++;
                            if(focusX2 > x1)
                                num5 ++;
                            if(focusX3 > x1)
                                num5 ++;
                            if(num5 == 1) {
					System.out.println("in the triangle");
					System.exit(0);
				}
				if(num5 == 0 || num5 == 2) {
					System.out.println("outof the triangle");
					System.exit(0);
				}
			}
						else
							System.out.println("data error");
			  }
		}
	}
	public static boolean checkFormat3(String S) {
		int count = 0;
//		int count1 = 0;
//		for(int i = 0 ; i < S.length() ; i++) {
//			if(S.charAt(i) == ',')
//				count1 ++;
//		}
             for(int i = 0 ; i < S.length() ; i++) {
                if(S.charAt(i) == ':')
                    count ++;
            }
//          if(count != 1 || count1 != 3)
//             return false;
		for(int i = 0 ; i < S.length() - 1 ; i++)
		if ((S.charAt(i) == '+' && S.charAt(i + 1) == '+') || (S.charAt(i) == '-' && S.charAt(i + 1) == '-')
					|| (S.charAt(i) == '+' && S.charAt(i + 1) == '-') || (S.charAt(i) == '-' && S.charAt(i + 1) == '+')
					|| (S.charAt(i) == '.' && S.charAt(i + 1) == ',') || (S.charAt(i) == ',' && S.charAt(i + 1) == '.')
					|| (S.charAt(i) == ' ' && S.charAt(i + 1) == '.') || (S.charAt(i) == '.' && S.charAt(i + 1) == '.')
					|| (S.charAt(i) == ' ' && S.charAt(i + 1) == ',') || (S.charAt(i) == ',' && S.charAt(i + 1) == ' ')
					|| (S.charAt(i) == ' ' && S.charAt(i + 1) == ' ') || (S.charAt(i) == ':' && S.charAt(i + 1) == ':')
					|| (S.charAt(i) == ',' && S.charAt(i + 1) == ',') || (S.charAt(S.length() - 1) == ',')
					|| (S.charAt(i) == ':' && S.charAt(i + 1) == ',')
 					|| (S.charAt(i) == '0' && (S.charAt(i + 1) > '0' && S.charAt(i + 1) < '9'))
					|| (S.charAt(i) == ':' && S.charAt(i + 1) == '.')) {
			return false;
		}
		
		return true;
	}
	
	public static int findComma (String str) {
		int location = 0;
		for(int i = 0;i < str.length();i ++) {
			if(str.charAt(i) == ',') 
				location =  i;
		}
		return location;
	}
	
	public static double result (double a,double b,double c,double d) {
		double distance = 0;
		distance = Math.sqrt((c - a) * (c - a) + (d - b) * (d - b));
		return distance;
	}
	
	public static boolean checkTriangle(double x1,double y1,double x2,double y2,double x3,double y3) {
		double a = result(x1,y1,x2,y2);//第一条边
		double b = result(x1,y1,x3,y3);//第二条边
		double c = result(x2,y2,x3,y3);//第三条边
		if(a + b > c && a + c > b && b + c > a)
			return true;
		else
			return false;
	}
	
//	public static void Print(double a) {
//		double temp = a;
//		temp = temp * 10000000;
//		temp = temp % 1000000;
//		if(temp == 0)
//			System.out.print(a);
//		else
//			System.out.printf("%f", a);
//	}
	
	public static double findMax(double a,double b,double c) {
		if(a > b && a > c)
			return a;
		else if(b > c && b > a)
			return b;
		else if(c > a && c > b)
			return c;
			return a;
	}
	
// 	public static boolean checkCross(double k1,double b1,double k2,double b2) {
// 		if(k1 != k2 && b1 != b2)
// 			return true;
// 		else
// 			return false;
// 	}
	
	public static double calculateX (double k1,double b1,double k2,double b2) {
		double x = (b2 - b1) / (k1 - k2);
		return x;
	}
	
	public static double calculateY (double k1,double b1,double k2,double b2) {
		double y = k1 * calculateX(k1,b1,k2,b2) + b1;
		return y;
	}
	
	public static boolean calculateDis(double xa,double ya,double xb,double yb,double xc,double yc) {
		double dis1 = Math.sqrt((xb - xa) * (xb - xa) + (yb - ya) * (yb - ya));
		double dis2 = Math.sqrt((xb - xc) * (xb - xc) + (yb - yc) * (yb - yc));
		double disSum  = Math.sqrt((xa - xc) * (xa - xc) + (ya - yc) * (ya - yc));
		if(Math.abs(dis1 + dis2 - disSum)<0.000001)
			return true;
		else
			return false;
	}
	public static double calculateFoucsX(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
		double x0 = (y3 * x4 * x2 - y4 * x3 * x2 - y3 * x4 * x1 + y4 * x3 * x1 - y1 * x2 * x4 + y2 * x1 * x4 + y1 * x2 * x3 - y2 * x1 * x3) / (x4 * y2 - x4 * y1- x3 * y2 + x3 * y1 - x2 * y4 + x2 * y3 + x1 * y4 - x1 * y3);
        return x0;
	}
	public static double calculateFoucsY(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
		double y0 = (-y3 * x4 * y2 + y4 * x3 * y2 + y3 * x4 * y1 - y4 * x3 * y1 + y1 * x2 * y4 - y1 * x2 * y3 - y2 * x1 * y4 + y2 * x1 * y3) / (y4 * x2-y4 * x1 - y3 * x2 + x1 * y3 - y2 * x4 + y2 * x3 + y1 * x4 - y1 * x3);
		return y0;
	}
}

类图:

分析:

  • 我认为这种题目要求就是你的逻辑要十分清晰,对每种情况进行归纳与分类,在每种大情况下进行讨论分析其下的小情况,如果稍微不注意就会导致情况归纳错误,导致逻辑混乱,所以我认为在编写程序之前需要画流程图,在这种情况多而且其中关系复杂的情况下,防止出现讨论情况分类错误。
  • 在进行双精度型的数字计算时要考虑其误差。

 

题目6).

设计一个银行业务类

编写一个银行业务类BankBusiness,具有以下属性和方法:
(1)公有、静态的属性:银行名称bankName,初始值为“中国银行”。
(2)私有属性:账户名name、密码password、账户余额balance。
(3)银行对用户到来的欢迎(welcome)动作(静态、公有方法),显示“中国银行欢迎您的到来!”,其中“中国银行”自动使用bankName的值。
(4)银行对用户离开的提醒(welcomeNext)动作(静态、公有方法),显示“请收好您的证件和物品,欢迎您下次光临!”
(5)带参数的构造方法,完成开户操作。需要账户名name、密码password信息,同时让账户余额为0。
(6)用户的存款(deposit)操作(公有方法,需要密码和交易额信息),密码不对时无法存款且提示“您的密码错误!”;密码正确、完成用户存款操作后,要提示用户的账户余额,例如“您的余额有1000.0元。”。
(7)用户的取款(withdraw)操作(公有方法,需要密码和交易额信息)。密码不对时无法取款且提示“您的密码错误!”;密码正确但余额不足时提示“您的余额不足!”;密码正确且余额充足时扣除交易额并提示用户的账户余额,例如“请取走钞票,您的余额还有500.0元。”。

编写一个测试类Main,在main方法中,先后执行以下操作:
(1)调用BankBusiness类的welcome()方法。
(2)接收键盘输入的用户名、密码信息作为参数,调用BankBusiness类带参数的构造方法,从而创建一个BankBusiness类的对象account。
(3)调用account的存款方法,输入正确的密码,存入若干元。密码及存款金额从键盘输入。
(4)调用account的取款方法,输入错误的密码,试图取款若干元。密码及取款金额从键盘输入。
(5)调用account的取款方法,输入正确的密码,试图取款若干元(取款金额大于余额)。密码及取款金额从键盘输入。
(6)调用account的取款方法,输入正确的密码,试图取款若干元(取款金额小于余额)。密码及取款金额从键盘输入。
(7)调用BankBusiness类的welcomeNext()方法。

输入格式:

输入开户需要的姓名、密码
输入正确密码、存款金额
输入错误密码、取款金额
输入正确密码、大于余额的取款金额
输入正确密码、小于余额的取款金额

输出格式:

中国银行(银行名称)欢迎您的到来!
您的余额有多少元。
您的密码错误!
您的余额不足!
请取走钞票,您的余额还有多少元。
请收好您的证件和物品,欢迎您下次光临!

 

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String name = input.next();//用户姓名
		int password = input.nextInt();//正确密码
		
		int rightPassword = input.nextInt();//正确密码
		double saveMoney = input.nextDouble();//存款金额
		
		int wrongPassword = input.nextInt();//错误密码
		double withdrawMoney = input.nextDouble();//取款金额
		
		int inputPassword2 = input.nextInt();//输入正确密码
		double withdrawMoneymore = input.nextDouble();//取款金额(大于)
		
		int inputPassword3 = input.nextInt();//输入正确密码
		double withdrawMoneyless = input.nextDouble();//取款金额(小于)
		BankBusiness account = new BankBusiness(name,password);
		account.welcome();
		account.deposit(rightPassword,saveMoney);
		account.withdraw(wrongPassword,withdrawMoney);
		account.withdraw(inputPassword2,withdrawMoneymore);
		account.withdraw(inputPassword3,withdrawMoneyless);
		account.welcomeNext();
	}
}

class BankBusiness {
	public String bankName = "中国银行";
	private String name;
	private int password;
	private double balance;

	public BankBusiness(String name,int password) {
		super();
		// TODO 自动生成的构造函数存根
		this.name = name;
		this.password = password;
	}

	public void welcome() {
		System.out.println(bankName + "欢迎您的到来!");
	}
	
	public void welcomeNext() {
		System.out.println("请收好您的证件和物品,欢迎您下次光临!");
	}
	
//	public void openAccount(String name,String password,double balance) {
//		Scanner input = new Scanner(System.in);
//		name = input.nextLine();
//		password = input.nextLine();
//		balance = 0;
//	}
	
	public void deposit(int inputPassword,double saveMoney) {
		if(inputPassword != password) {
			System.out.println("您的密码错误!");
		}
		else {
			balance = balance + saveMoney;
			System.out.println("您的余额有" + balance + "元。");
		}
	}
	
	public void withdraw(int inputPassword,double withdrawMoney) {
		if(inputPassword != password) {
			System.out.println("您的密码错误!");
		}
		else {
			if(withdrawMoney > balance) {
				System.out.println("您的余额不足!");
			}
			else {
				balance = balance - withdrawMoney;
				System.out.println("请取走钞票,您的余额还有" + balance + "元。");
			}
		}
	}
	
}

类图:

分析:

  • 这道题目难度较低,只需要根据题目的要求一步一步实现其功能。
  • 但是需要注意输出格式,容易遗漏交易金额后需要输出”元“。
  • 以及观察输入格式。

 

题目7).

点线形系列4-凸四边形的计算

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

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner input = new Scanner(System.in);
		String a = input.nextLine();
		if (a.charAt(0) < '1' || a.charAt(0) > '5') {
			System.out.println("Wrong Format");
			System.exit(0);
		}
		String num = a.substring(0, 1);
		String A = a.substring(2, a.length());
		int choice = Integer.parseInt(num);
		if (choice == 1) {
			String format = "([\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s?)+";
			boolean flag0 = A.matches(format);
			String str = "[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?";
			boolean flag = A.matches(str);
			if(flag0 == false) {
				System.out.println("Wrong Format");
				System.exit(0);
			}
			else if (flag == false) {
				System.out.println("wrong number of points");
			} else {
				int flag3 = 0;
				int flag4 = 0;
				int flag5 = 0;
				for (int j = 0; j < a.length(); j++) {
					if (a.charAt(j) == ' ') {
						flag3 = j;
						break;
					}
				}

				for (int j = flag3 + 1; j < a.length(); j++) {
					if (a.charAt(j) == ' ') {
						flag4 = j;
						break;
					}
				}

				for (int j = flag4 + 1; j < a.length(); j++) {
					if (a.charAt(j) == ' ') {
						flag5 = j;
						break;
					}
				}

				String b = a.substring(2, flag3);// 第一个坐标
				String c = a.substring(flag3 + 1, flag4);// 第二个坐标
				String d = a.substring(flag4 + 1, flag5);// 第三个坐标
				String e = a.substring(flag5 + 1, a.length());// 第四个坐标
				int m1 = findComma(b);
				int m2 = findComma(c);
				int m3 = findComma(d);
				int m4 = findComma(e);
				String x10 = b.substring(0, m1);
				String y10 = b.substring(m1 + 1, b.length());
				String x20 = c.substring(0, m2);
				String y20 = c.substring(m2 + 1, c.length());
				String x30 = d.substring(0, m3);
				String y30 = d.substring(m3 + 1, d.length());
				String x40 = e.substring(0, m4);
				String y40 = e.substring(m4 + 1, e.length());
				if((x10.length() > 1 && x10.charAt(0) == 0 && x10.charAt(1) != '.') || (x20.length() > 1 && x20.charAt(0) == 0 && x20.charAt(1) != '.') || (x30.length() > 1 && x30.charAt(0) == 0 && x30.charAt(1) != '.') || (x40.length() > 1 && x40.charAt(0) == 0 && x40.charAt(1) != '.'))
				System.out.println("Wrong Format");
				double x1 = Double.parseDouble(x10);
				double y1 = Double.parseDouble(y10);
				double x2 = Double.parseDouble(x20);
				double y2 = Double.parseDouble(y20);
				double x3 = Double.parseDouble(x30);
				double y3 = Double.parseDouble(y30);
				double x4 = Double.parseDouble(x40);
				double y4 = Double.parseDouble(y40);
				if ((x1 == x2 && y1 == y2) || (x1 == x3 && y1 == y3) || (x1 == x4 && y1 == y4) || (x2 == x3 && y2 == y3)
						|| (x2 == x4 && y2 == y4) || (x3 == x4 && y3 == y4)) {
					System.out.println("points coincide");
					System.exit(0);
				}
				if (!checkFour(x1, y1, x2, y2, x3, y3, x4, y4))
					System.out.println("false" + " " + "false");
				else {
					System.out.print("true" + " ");
					if (parallelFourline(x1, y1, x2, y2, x3, y3, x4, y4))
						System.out.print("true");
					else
						System.out.print("false");
				}
			}
		}

		else if (choice == 2) {
			String format = "([\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s?)+";
			boolean flag0 = A.matches(format);
			String str = "[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?";
			boolean flag = A.matches(str);
			if(flag0 == false) {
				System.out.println("Wrong Format");
				System.exit(0);
			}
			else if (flag == false) {
				System.out.println("wrong number of points");
			}  else {
				int flag3 = 0;
				int flag4 = 0;
				int flag5 = 0;
				for (int j = 0; j < a.length(); j++) {
					if (a.charAt(j) == ' ') {
						flag3 = j;
						break;
					}
				}

				for (int j = flag3 + 1; j < a.length(); j++) {
					if (a.charAt(j) == ' ') {
						flag4 = j;
						break;
					}
				}

				for (int j = flag4 + 1; j < a.length(); j++) {
					if (a.charAt(j) == ' ') {
						flag5 = j;
						break;
					}
				}

				String b = a.substring(2, flag3);// 第一个坐标
				String c = a.substring(flag3 + 1, flag4);// 第二个坐标
				String d = a.substring(flag4 + 1, flag5);// 第三个坐标
				String e = a.substring(flag5 + 1, a.length());// 第四个坐标
				int m1 = findComma(b);
				int m2 = findComma(c);
				int m3 = findComma(d);
				int m4 = findComma(e);
				String x10 = b.substring(0, m1);
				String y10 = b.substring(m1 + 1, b.length());
				String x20 = c.substring(0, m2);
				String y20 = c.substring(m2 + 1, c.length());
				String x30 = d.substring(0, m3);
				String y30 = d.substring(m3 + 1, d.length());
				String x40 = e.substring(0, m4);
				String y40 = e.substring(m4 + 1, e.length());
				double x1 = Double.parseDouble(x10);
				double y1 = Double.parseDouble(y10);
				double x2 = Double.parseDouble(x20);
				double y2 = Double.parseDouble(y20);
				double x3 = Double.parseDouble(x30);
				double y3 = Double.parseDouble(y30);
				double x4 = Double.parseDouble(x40);
				double y4 = Double.parseDouble(y40);
//                 if((x1 == x2 && y1 == y2) || (x1 == x3 && y1 == y3) || (x1 == x4 && y1 == y4) || (x2 == x3 && y2 == y3) || (x2 == x4 && y2 == y4) || (x3 == x4 && y3 == y4)){
//                     System.out.println("points coincide");
//                     System.exit(0);
//                 }
				if (!checkFour(x1, y1, x2, y2, x3, y3, x4, y4)) {
					System.out.println("not a quadrilateral");
				} else {
					if (!parallelFourline(x1, y1, x2, y2, x3, y3, x4, y4)) {
						System.out.println("false false false");
					} else {
						if (Math.abs(distance(x1, y1, x2, y2) - distance(x3, y3, x2, y2)) < 0.000001) {
							System.out.print("true");
							if ((x1 - x2) * (x3 - x2) + (y1 - y2) * (y3 - y2) == 0)
								System.out.print(" true true");
							else
								System.out.print(" false false");
						} else if ((x1 - x2) * (x3 - x2) + (y1 - y2) * (y3 - y2) == 0) {
							System.out.println("false true false");
						} else
							System.out.println("false false false");
					}
				}
			}
		}

		else if (choice == 3) {
// 			String format = "([\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s?)+";
// 			boolean flag0 = A.matches(format);
// 			String str = "[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?";
// 			boolean flag = A.matches(str);
// 			if(flag0 == false) {
// 				System.out.println("Wrong Format");
// 				System.exit(0);
// 			}
// 			else if (flag == false) {
// 				System.out.println("wrong number of points");
// 			} else 
            {
				int flag3 = 0;
				int flag4 = 0;
				int flag5 = 0;
				for (int j = 0; j < a.length(); j++) {
					if (a.charAt(j) == ' ') {
						flag3 = j;
						break;
					}
				}

				for (int j = flag3 + 1; j < a.length(); j++) {
					if (a.charAt(j) == ' ') {
						flag4 = j;
						break;
					}
				}

				for (int j = flag4 + 1; j < a.length(); j++) {
					if (a.charAt(j) == ' ') {
						flag5 = j;
						break;
					}
				}

				String b = a.substring(2, flag3);// 第一个坐标
				String c = a.substring(flag3 + 1, flag4);// 第二个坐标
				String d = a.substring(flag4 + 1, flag5);// 第三个坐标
				String e = a.substring(flag5 + 1, a.length());// 第四个坐标
				int m1 = findComma(b);
				int m2 = findComma(c);
				int m3 = findComma(d);
				int m4 = findComma(e);
				String x10 = b.substring(0, m1);
				String y10 = b.substring(m1 + 1, b.length());
				String x20 = c.substring(0, m2);
				String y20 = c.substring(m2 + 1, c.length());
				String x30 = d.substring(0, m3);
				String y30 = d.substring(m3 + 1, d.length());
				String x40 = e.substring(0, m4);
				String y40 = e.substring(m4 + 1, e.length());
				double x1 = Double.parseDouble(x10);
				double y1 = Double.parseDouble(y10);
				double x2 = Double.parseDouble(x20);
				double y2 = Double.parseDouble(y20);
				double x3 = Double.parseDouble(x30);
				double y3 = Double.parseDouble(y30);
				double x4 = Double.parseDouble(x40);
				double y4 = Double.parseDouble(y40);
				if ((x1 == x2 && y1 == y2) || (x1 == x3 && y1 == y3) || (x1 == x4 && y1 == y4) || (x2 == x3 && y2 == y3)
						|| (x2 == x4 && y2 == y4) || (x3 == x4 && y3 == y4)) {
					System.out.println("points coincide");
					System.exit(0);
				}
				if (!checkFour(x1, y1, x2, y2, x3, y3, x4, y4)) {
					System.out.println("not a quadrilateral");
				} else {
					if (Concavequadrilateral(x1, y1, x2, y2, x3, y3, x4, y4))
						System.out.print("false");
					else
						System.out.print("true");
					double perimeter = distance(x1, y1, x2, y2) + distance(x2, y2, x3, y3) + distance(x3, y3, x4, y4)
							+ distance(x4, y4, x1, y1);
					double area = Math.abs((x1 - x3) * (y2 - y4) - (y1 - y3) * (x2 - x4)) * 0.5;
					perimeter = Math.round(perimeter * 1000) / 1000.0;
					area = Math.round(area * 1000) / 1000.0;
					System.out.print(" " + perimeter + " " + area);
				}
			}
		}
		
		
	}

	public static boolean checkFour(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
			double y4) {
		if ((y4 - y3) * (x4 - x2) == (y4 - y2) * (x4 - x3))
			return false;
		else if ((y4 - y3) * (x4 - x1) == (y4 - y1) * (x4 - x3))
			return false;
		else if ((y4 - y2) * (x4 - x1) == (y4 - y1) * (x4 - x2))
			return false;
		else if ((y3 - y2) * (x3 - x1) == (y3 - y1) * (x3 - x2))
			return false;                                       // 任意三个顶点成直线,非四边形
		else
			return true;
	}

	public static int findComma(String str) {
		int location = 0;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == ',')
				location = i;
		}
		return location;
	}

	public static boolean parallelFourline(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
			double y4) {
		if ((y2 - y1) / (x2 - x1) == (y3 - y4) / (x3 - x4) && (y4 - y1) / (x4 - x1) == (y3 - y2) / (x3 - x2)) {
			return true;
		} else
			return false;
	}

	public static double distance(double x1, double y1, double x2, double y2) {
		return Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
	}

	public static boolean Concavequadrilateral(double x1, double y1, double x2, double y2, double x3, double y3,
			double x4, double y4) {
		double t1 = (x4 - x1) * (y2 - y1) - (y4 - y1) * (x2 - x1);
		double t2 = (x1 - x2) * (y3 - y2) - (y1 - y2) * (x3 - x2);
		double t3 = (x2 - x3) * (y4 - y3) - (y2 - y3) * (x4 - x3);
		double t4 = (x3 - x4) * (y1 - y4) - (y3 - y4) * (x1 - x4);
		if (t1 * t2 * t3 * t4 < 0)
			return true;
		else
			return false;

	}
}

 

分析:

这道题我认为难度很大,其中不仅包括了许多数学知识,而且还对你的逻辑思维考察十分严格,在找出了判断凹凸四边形方法后要用代码实现它,在判断四个点是否能构成四边形时,要考虑多多种情况,

这几次图形类的题目都在告诉我们以后的问题情况只会比更难,我们需要用更加严谨的态度,更加严密的逻辑去对待,在每次编写程序之前养成画流程图的习惯,这是最好的锻炼自己的方式。

三、踩坑心得:

题目1

出现问题:

  • 在设计程序时,输入所需数据时未用setter将值传进其中,导致输出没有数据。

 

心得:首先,我个人认为出现这样的低级错误是十分不应该的,就算是在考试中都没有做到完全避免这种粗心的错误,说明平常的编程习惯还需要改进,在编程时要做到百分百的投入,防止这些不严谨的习惯导致的错误结果,还要花费时间重新浏览一遍代码,甚至可能需要debug才能发现自己所犯的错误,这是十分浪费时间的。

 

  • 在第三题中,我直接利用了第二题的源码,导致输入数据与其所需要的数据不匹配,如下图:

 

心得:在完成迭代关系的题目时,并不是对前几道题目的源码一味的复制并且使用,还是需要结合当前问题,观察题目的变化,以此做出相应的修改,就像在这道题中,在switch语句中,每个不同的case所需要的变量,所需要传进的参数都不一样,如果只是盲目的复制粘贴之前的源码就会导致下面的程序无法运行,多输入许多用不到的数据,最后导致报错,但是自己表面上一看并发现不了有什么错误,这也造成我在最后一题停留时间过长,一直在寻找代码逻辑为什么错误,但是实际上逻辑上没有错误,所以找了很久也没有找到,所以我认为在审题时一定要观察细致!!!

 

  • 在解决完上一个问题后,发现提交到pta上之后还有一个测试点错误

 

这说明我所移除的下标可能超出容器内原本有的下标,所以我在remove功能中添加了控制数据是否合理的语句,如下:

public void remove(int index) {
        if(index >= 0 && index < list.size())
		list.remove(index);
	}

 

心得:在每次设计程序时都应该控制分析数据是否合理,在以后所有的设计程序时都要考虑输入数据是否合乎逻辑,要多加控制这一问题。

 

题目设计一个银行业务类)

出现问题:
  • 在最开始我将密码定义为String类型导致在输入时密码被赋值为一串字符串里包含回车空格,导致判断密码是否正确时将不包含回车空格的字符串与输入的密码比较,造成一直显示密码错误。
 
心得:在一开始我并没有找到我的代码错误,于是进行了debug调试,发现了读取字符串时多读取了空格与回车,于是我将密码类型改为int型,我认为这个解决方法还不够完善,并没有说密码只能是int类型,所以我认为还是得继续修改改进,用别的方法解决字符串多读取空格回车。
 
 

四、改进建议:

我认为这三次题目集的难度是逐级递增的,在遇到简单问时不能轻视,也需要严格按照写代码步骤:

1)其次就是看清题目,清晰题目所有的要求,在此基础上开始想如何解决,如何编写代码。

2)先画流程图以便于整理自己的思路,这样就不会导致简单的问题代码反复来回修改,养成一个良好习惯。

3)我认为在编写代码时应该更加专心,如果打断很难沿着的思路继续编写代码,这会导致逻辑混乱,写出很多无用且错误的代码。

4)在使用双精度浮点型时注意其误差控制,例如在判断是否为直角三角形是,直角边平方和等于第三边的平方时就需要控制。

5)在读取字符串时,要注意读取的是否是自己所需要的那段字符,防止多读取空格与火车,导致结果错误。

6)在找不到代码错误之后立马使用debug调试,不能一直在那里只看代码,因为光看代码只能看出逻辑错误,无法找到自己运用的方法是否正确,以及数据是否是自己所需要的,debug可以帮助你找到自己的错误并且清晰的发现错在哪里。

 

五 、总结:

1)这几次的作业让我感受到了编写代码需要严谨,要考虑每种不同的情况,这样才能保证程序没有漏洞,在不严谨的思绪下写出来的代码,可能你用数据测试最后得到的结果却是正确的,最后找不到漏洞在哪,需要花更大量的时间去找到漏洞并且修复。

2)从图形类设计的题目深深的体会的到了流程图的重要性,在类的数量很多,且类间关系复杂时就更体现了流程图的重要性,如果不画流程图就会陷入无从下手的处境。

3)尽管这几周题量以及题目难度都上升了不少,虽然学业比较繁重,但是我们要学会合理利用时间,利用起碎片化的时间,学习,写作业,完成练习。

4)Java这门课程难度本身就很大,从这几周的作业也能看出,所以希望自己能够更加认真的对待。

 

posted on 2022-05-01 17:02  21206104-戴奕为  阅读(29)  评论(0编辑  收藏  举报

导航