# PTA第4、5次大作业及期中考试总结

1. 期中考试题目总结
（1）总结：期中考试主要考察的是点线面类的设计，之前没有接触过类，或者说对类没有什么很清晰的概念和认知。现在才发现“类”是一个非常强大的工具，可以大大方便我们平时解决问题的过程。期中考试这几道题目难度适中，很适合我们平时拿来练手，不过也需要抓住各种细节，以防出现一些不该出现的错误。
（2）
1、7-1 点与线（类设计）

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

** 题目要求：在主方法中定义一条线段对象，从键盘输入该线段的起点坐标与终点坐标以及颜色，然后调用该线段的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:长度值

5
9.4
12.3
84
Red

The line"s color is:Red
The line"s begin point"s Coordinate is:
(5.00,9.40)
The line"s end point"s Coordinate is:
(12.30,84.00)
The line"s length is:74.96

80.2356
352.12
24.5
100
Black

Wrong Format

``````package pta.mid_term_examination;

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

public class Main1 {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
Point point1=new Point();
double x1=in.nextDouble();
double y1=in.nextDouble();
point1.setX(x1);
point1.setY(y1);
Point point2=new Point();
double x2=in.nextDouble();
double y2=in.nextDouble();
point2.setX(x2);
point2.setY(y2);
//double a=x1*x1-x2*x2;
//double b=y1*y1-y2*y2;
//double dis=Math.sqrt(a+b);
String color=in.next();
Line line=new Line();
line.setPoint1(point1);
line.setPoint2(point2);
line.setColor(color);
if ((x1<=0||x1>200)||(y1<=0||y1>200)||(x2<=0||x2>200)||(y2<=0||y2>200))
System.out.println("Wrong Format");
else line.display();
}
}

class Point{
private double x;//横坐标
private double y;//纵坐标

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

public Point() {
}

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(){
DecimalFormat two=new DecimalFormat("0.00");
System.out.println("("+two.format(getX())+","+two.format(getY())+")");
}
}

class Line{
private Point point1 = new Point();
private Point point2 = new Point();
String color;

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

public Line() {
}

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 p1,Point p2){

double a=p1.getX()-p2.getX();
double b=p1.getY()-p2.getY();

return Math.sqrt(a*a+b*b);
}
public void display(){
Line line=new Line();
DecimalFormat two=new DecimalFormat("0.00");
System.out.println("The line"s color is:"+getColor());
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:" + two.format(line.getDistance(point1,point2)));
}
}
``````

public double getDistance(Point p1,Point p2){

``````    double a=p1.getX()-p2.getX();
double b=p1.getY()-p2.getY();

return Math.sqrt(a*a+b*b);
}
``````

2、7-2 点线面问题重构（继承与多态）

element = p1;//起点Point
element.display();

``````  element = p2;//终点Point
element.display();

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

element = plane;//面
element.display();
``````

(x1,y1)
(x2,y2)
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:长度值
The Plane"s color is:颜色值

5
9.4
12.3
84
Red

(5.00,9.40)
(12.30,84.00)
The line"s color is:Red
The line"s begin point"s Coordinate is:
(5.00,9.40)
The line"s end point"s Coordinate is:
(12.30,84.00)
The line"s length is:74.96
The Plane"s color is:Red

5
9.4
12.3
845
Black

Wrong Format

``````package pta.mid_term_examination;

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

public class Main2 {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
Point1 p1=new Point1();
double x1=in.nextDouble();
double y1=in.nextDouble();
p1.setX(x1);
p1.setY(y1);
Point1 p2=new Point1();
double x2=in.nextDouble();
double y2=in.nextDouble();
p2.setX(x2);
p2.setY(y2);
String color=in.next();
Line1 line=new Line1();
line.setPoint1(p1);
line.setPoint2(p2);
line.setColor(color);
Plane plane=new Plane();
plane.setColor(color);
Element element=new Element() {
@Override
public void display() {

}
};
if ((x1<=0||x1>200)||(y1<=0||y1>200)||(x2<=0||x2>200)||(y2<=0||y2>200))
System.out.println("Wrong Format");
else{
element = p1;//起点Point
element.display();

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

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

element = plane;//面
element.display();
}
}
}

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

class Point1 extends Element{
private double x;//横坐标
private double y;//纵坐标

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

public Point1() {
}

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(){
DecimalFormat two=new DecimalFormat("0.00");
System.out.println("("+two.format(this.x)+","+two.format(this.y)+")");
}
}

class Line1 extends Element{
private Point1 point1 = new Point1();
private Point1 point2 = new Point1();
String color;

public Line1(Point1 point1, Point1 point2, String color) {
this.point1 = point1;
this.point2 = point2;
this.color = color;
}

public Line1() {
}

public Point1 getPoint1() {
return point1;
}

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

public Point1 getPoint2() {
return point2;
}

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

public String getColor() {
return color;
}

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

public double getDistance(Point1 p1,Point1 p2){

double a=p1.getX()-p2.getX();
double b=p1.getY()-p2.getY();

return Math.sqrt(a*a+b*b);
}
public void display(){
Line1 line=new Line1();
DecimalFormat two=new DecimalFormat("0.00");
System.out.println("The line"s color is:"+getColor());
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:" + two.format(line.getDistance(point1,point2)));
}
}

class Plane extends Element{
private String color;

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

public Plane() {
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}
public void display(){
System.out.println("The Plane"s color is:"+this.color);
}
}
``````

3、7-3 点线面问题再重构（容器类）

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

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

...
}

Point、Line、Plane的输出参考题目2

1
3.4
5.6
2
4.4
8.0
0.98
23.888
Red
3
Black
1
9.8
7.5
3
Green
4
3
0

(3.40,5.60)
The line"s color is:Red
The line"s begin point"s Coordinate is:
(4.40,8.00)
The line"s end point"s Coordinate is:
(0.98,23.89)
The line"s length is:16.25
(9.80,7.50)
The Plane"s color is:Green

``````package pta.mid_term_examination;

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

abstract class Element3 {
public abstract void display();

}

class Plane3 extends Element3 {
private String color;

public Plane3() {
super();
// TODO Auto-generated constructor stub
}

public Plane3(String color) {
super();
this.color = color;
}

public String getColor() {
return color;
}

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

@Override
public void display() {
// TODO Auto-generated method stub
System.out.println("The Plane"s color is:" + this.color);

}

}

class Point3 extends Element3{
private double x,y;

public Point3(){

}

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

public double getX(){
return this.x;
}

public void setX(double x){
this.x = x;
}

public double getY(){
return this.y;
}

public void setY(double y){
this.y = y;
}

@Override
public void display(){
System.out.println("(" + String.format("%.2f", x) + "," + String.format("%.2f",y) + ")");
}

}

class Line3 extends Element3{
private Point3 point1,point2;
private String color;

public Line3(){

}

public Line3(Point3 p1,Point3 p2,String color){
this.point1 = p1;
this.point2 = p2;
this.color =  color;
}

public Point3 getPoint1() {
return point1;
}

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

public Point3 getPoint2() {
return point2;
}

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

public String getColor() {
return color;
}

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

public double getDistance(){
return Math.sqrt(Math.pow(this.point1.getX() - this.point2.getX(), 2) +
Math.pow(this.getPoint1().getY() - this.getPoint2().getY(), 2));
}

@Override
public void display(){
System.out.println("The line"s color is:" + this.getColor());
System.out.println("The line"s begin point"s Coordinate is:");
this.getPoint1().display();
System.out.println("The line"s end point"s Coordinate is:");
this.getPoint2().display();
System.out.println("The line"s length is:" + String.format("%.2f", this.getDistance()));
}

}

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

public GeometryObject() {

}

public void add(Element3 element) {
}

public void remove(int index) {
if(index < 1 || index > list.size()) {
return;
}

list.remove(index - 1);
}

public ArrayList<Element3> getList(){
return this.list;
}
}

public class Main3 {

public static void main(String[] args) {
double x1,y1,x2,y2;
String color;
Scanner input = new Scanner(System.in);
int choice = 0,index = 0;

GeometryObject container = new GeometryObject();

choice = input.nextInt();
while(choice != 0) {
switch(choice) {
case 1:
x1 = input.nextDouble();
y1 = input.nextDouble();
break;
case 2:
x1 = input.nextDouble();
y1 = input.nextDouble();
x2 = input.nextDouble();
y2 = input.nextDouble();
color = input.next();
container.add(new Line3(new Point3(x1,y1),new Point3(x2,y2),color));
break;
case 3:
color = input.next();
break;
case 4:
index = input.nextInt();
container.remove(index);
break;
}
choice = input.nextInt();
}

for(Element3 element:container.getList()) {
element.display();
}
}

}

``````

1. PTA第4次大作业总结
1、7-1 sdut-String-2 识蛟龙号载人深潜，立科技报国志（II）（正则表达式）
背景简介：

“蛟龙号”载人深潜器是我国首台自主设计、自主集成研制的作业型深海载人潜水器，设计最大下潜深度为7000米级，也是目前世界上下潜能力最强的作业型载人潜水器。“蛟龙号”可在占世界海洋面积99.8%的广阔海域中使用，对于我国开发利用深海的资源有着重要的意义。

2012年6月27日11时47分，中国“蛟龙”再次刷新“中国深度”——下潜7062米。6月3日，“蛟龙”出征以来，已经连续书写了5个“中国深度”新纪录：6月15日，6671米；6月19日，6965米；6月22日，6963米；6月24日，7020米；6月27日，7062米。下潜至7000米，标志着我国具备了载人到达全球99%以上海洋深处进行作业的能力，标志着“蛟龙”载人潜水器集成技术的成熟，标志着我国深海潜水器成为海洋科学考察的前沿与制高点之一，标志着中国海底载人科学研究和资源勘探能力达到国际领先水平。

‘蛟龙’号是我国载人深潜发展历程中的一个重要里程碑。它不只是一个深海装备，更代表了一种精神，一种不畏艰险、赶超世界的精神，它是中华民族进军深海的号角。

2012年6月27日11时47分，中国“蛟龙”再次刷新“中国深度”——下潜7062米
6月15日，6671米
6月19日，6965米
6月22日，6963米
6月24日，7020米
6月27日，7062米

end

9165
6692
6990
6991
7050
7095
7099

6000米以上深度载人潜水器的国家包括中国、美国、日本、法国和俄罗斯

end

1102
6000
13547
20021
7000

``````import java.util.Scanner;

public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while (true){
String str = in.nextLine();
String[] s = str.split("\D+");
// \d表示数字  \D表示所有非数字字符，加上+号表示一个或多个
int sum = 0;//用于计算总和
int count = 0;//用于统计每一行被强制转化的字符串
if (str.equals("end"))
break;
for (int i = 0;i < s.length;i++){
if (!s[i].equals("")){
count = Integer.parseInt(s[i]);
sum += count;
}

}
System.out.println(sum);
}
}
}

``````

String[] s = str.split("D+");
// d表示数字 D表示所有非数字字符，加上+号表示一个或多个
nextLine（）方法返回的是Enter键之前的所有字符，它是可以得到带空格的字符串的。

next（）会自动消去有效字符前的空格，只返回输入的字符，不能得到带空格的字符串。

（简单点说，next我只要字，nextLine我啥都要）

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

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

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

points coincide

1:-1,-1 1,2 -1,1 ++1,0

Wrong Format

1:-1,-1 -1,2

wrong number of points

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

true false

2:10,10 1,1 0,0 1,20

2:0,0 0,80 80,80 80,0

true true true

2:0,0 -10,80 0,160 -10,80

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

true 10.472 6.0

3:0,0 -10,100 0,99 10,100

false 221.097 990.0

``````import java.util.Scanner;

public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String str = in.nextLine();
boolean flag = true;
String regStr = "[\-\+]?\d+(\.\d+)?,[\-\+]?\d+(\.\d+)?(\s[\-\+]?\d+(\.\d+)?,[\-\+]?\d+(\.\d+)?\s?){0,}";
if (str.length() > 2){
String str1 = str.substring(0,1);
String str2 = str.substring(2,str.length());
if (str2.matches(regStr))
flag = true;
else flag = false;
if (!flag) {
System.out.println("Wrong Format");
return;
}else {
String[] s = str2.split(",|\s");
for (int i = 0;i < s.length;i++){
if (s[i].matches("0\d+"))
System.out.println("Wrong Format");
}
if (qua.isCoincide()){
System.out.println("points coincide");
return;
else System.out.println(qua.isDiamond()+" "+qua.isRectangle()+" "+qua.isSquare());

}
}
}
}

//定义四边形类
private double x1,y1,x2,y2,x3,y3,x4,y4;

public Quadrilateral(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
this.x4 = x4;
this.y4 = y4;
}

super();
}

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 boolean isCoincide(){
//判断四边形的点是否重合的函数
if((x1==x2&&y1==y2)||(x2==x3&&y2==y3)||(x3==x4&&y3==y4)||(x4==x1&&y4==y1))
return true;
return false;
}

//判断是否为四边形
Line a1 = new Line(x2,y2,x3,y3,x1,y1,x4,y4);
Line a2 = new Line(x4,y4,x3,y3,x1,y1,x2,y2);
if((x1==x3&&y1==y3)||(x2==x4&&y2==y4)||(x1==x2&&y1==y2)||(x4==x3&&y4==y3)||a1.three()||a2.three())
return false;
else if((x1==x2&&x1==x3)||(x1==x2&&x1==x4)||(x1==x3&&x1==x4)||(x3==x2&&x3==x4))
return false;
else if(Math.abs((y1-y2)/(x1-x2)-(y1-y3)/(x1-x3))<0.01||Math.abs((y1-y2)/(x1-x2)-(y1-y4)/(x1-x4))<0.01||Math.abs((y1-y4)/(x1-x4)-(y1-y3)/(x1-x3))<0.01||Math.abs((y3-y2)/(x3-x2)-(y2-y4)/(x2-x4))<0.01)
return false;
else return true;
}

public boolean isParallelogram() {
//判断是否为平行四边形
if((x1==x2&&x3==x4||(y1-y2)/(x1-x2)==(y4-y3)/(x4-x3))&&(x3==x2&&x1==x4||(y3-y2)/(x3-x2)==(y4-y1)/(x4-x1)))
return true;
else return false;
}
else return false;
}

public boolean isDiamond() {
//判断是否为菱形
if(isParallelogram()) {
if(((y1==y3&&x2==x4)||(y2==y4&&x1==x3))||(((y1-y3)/(x1-x3))*((y4-y2)/(x4-x2))==-1)) return true;
else return false;
}
else return false;
}

public boolean isRectangle() {
//判断是否为矩形
if(isParallelogram()) {
if((x1==x2&&y2==y3)||(y1==y2&&x2==x3)||((y1-y2)/(x1-x2))*((y3-y2)/(x3-x2))==-1) return true;
else return false;
}
else return false;
}

public boolean isSquare() {
//判断是否为正方形
if(isRectangle()&&isDiamond()) return true;
else return false;
}
public boolean isBump() {
//判断是否为凸四边形
double x = -((x2-x4)*(x3*y1-x1*y3)-(x1-x3)*(x4*y2-x2*y4))/((y2-y4)*(x1-x3)-(y1-y3)*(x2-x4));
double y = -((y2-y4)*(y3*x1-y1*x3)-(y1-y3)*(y4*x2-y2*x4))/((x2-x4)*(y1-y3)-(x1-x3)*(y2-y4));
if ((((x>=x3&&x<=x1)||(x<=x3&&x>=x1))&&((y>=y3&&y<=y1)||(y<=y3&&y>=y1)))&&(((x>=x2&&x<=x4)||(x<=x2&&x>=x4))&&((y>=y2&&y<=y4)||(y<=y2&&y>=y4))))
return true;
else return false;
}
}

class Line{
//定义直线类
private double x1,y1,x2,y2,x3,y3,x4,y4;

public Line() {
super();
}

public Line(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
super();
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
this.x4 = x4;
this.y4 = y4;
}

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 boolean isCoincide(){
if(Math.abs((y4-y3)*x1+(x3-x4)*y1+x4*y3-y4*x3)/Math.sqrt((y4-y3)*(y4-y3)+(x4-x3)*(x4-x3))<0.01&&(Math.abs((y1-y2)/(x1-x2)-(y3-y4)/(x3-x4))<0.01||((x1==x2)&&(x3==x4))))
return true;
else return false;
}
public boolean three(){
//小数点后三位
double x = -((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4));
double y = -((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4));
if(Math.abs((y1-y2)/(x1-x2)-(y3-y4)/(x3-x4))<0.01)
return false;
else if (((x>=x3&&x<=x4)||(x<=x3&&x>=x4))&&((y>=y3&&y<=y4)||(y<=y3&&y>=y4))&&((x>=x1&&x<=x2)||(x<=x1&&x>=x2))&&((y>=y1&&y<=y2)||(y<=y1&&y>=y2)))
return true;
else return false;
}
}
``````

3、7-3 设计一个银行业务类

（1）公有、静态的属性：银行名称bankName，初始值为“中国银行”。
（3）银行对用户到来的欢迎（welcome）动作（静态、公有方法），显示“中国银行欢迎您的到来！”，其中“中国银行”自动使用bankName的值。
（4）银行对用户离开的提醒（welcomeNext）动作（静态、公有方法），显示“请收好您的证件和物品，欢迎您下次光临！”
（6）用户的存款（deposit）操作（公有方法，需要密码和交易额信息），密码不对时无法存款且提示“您的密码错误！”；密码正确、完成用户存款操作后，要提示用户的账户余额，例如“您的余额有1000.0元。”。
（7）用户的取款（withdraw）操作（公有方法，需要密码和交易额信息）。密码不对时无法取款且提示“您的密码错误！”；密码正确但余额不足时提示“您的余额不足！”；密码正确且余额充足时扣除交易额并提示用户的账户余额，例如“请取走钞票，您的余额还有500.0元。”。

（3）调用account的存款方法，输入正确的密码，存入若干元。密码及存款金额从键盘输入。
（4）调用account的取款方法，输入错误的密码，试图取款若干元。密码及取款金额从键盘输入。
（5）调用account的取款方法，输入正确的密码，试图取款若干元（取款金额大于余额）。密码及取款金额从键盘输入。
（6）调用account的取款方法，输入正确的密码，试图取款若干元（取款金额小于余额）。密码及取款金额从键盘输入。

123456 1000
654321 2000
123456 2000
123456 500

``````import java.util.Scanner;

public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String name = in.next();//账户名
int input = in.nextInt();//密码
int rightPassword = in.nextInt();//正确密码
double save = in.nextDouble();//存款金额
int wrongPassword = in.nextInt();//错误密码
double withdrawal = in.nextDouble();//取款金额
int right1 = in.nextInt();//取款金额大于余额时的正确密码
double withdrawal1 = in.nextDouble();//取款金额（大于余额）
int right2 = in.nextInt();//取款金额小于余额时的正确密码
double withdrawal2 = in.nextDouble();//取款金额（小于余额）
bank.withdraw(right1,withdrawal1);
bank.withdraw(right2,withdrawal2);
}
}

//编写一个银行业务类
public static String bankName = "中国银行";//银行名称
private String name;//账户名
private double balance;//账户余额

super();
this.name = name;
balance =0;
}
public static void welcome(){
System.out.println(bankName + "欢迎您的到来！");
}

public static void welcomeNext() {
System.out.println("请收好您的证件和物品，欢迎您下次光临！");
}

public void deposit(int input,double save){
//参数是用户输入的密码和存款
if (input != password)
System.out.println("您的密码错误！");
else {
balance = balance + save;
System.out.println("您的余额有" + balance + "元。");
}
}

public void withdraw(int input,double withdrawal){
//参数是用户输入的密码和取款
if (input != password)
System.out.println("您的密码错误！");
else{
if (balance < withdrawal)
System.out.println("您的余额不足！");
else {
balance = balance - withdrawal;
System.out.println("请取走钞票，您的余额还有" + balance + "元。");
}
}
}
}
``````

3.PTA第5次大作业总结
1、7-1 点线形系列5-凸五边形的计算-1

1：输入五个点坐标，判断是否是五边形，判断结果输出true/false。
2：输入五个点坐标，判断是凹五边形（false）还是凸五边形(true)，如果是凸五边形，则再输出五边形周长、面积，结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形，输出"not a pentagon"
3：输入七个点坐标，前两个点构成一条直线，后五个点构成一个凸五边形、凸四边形或凸三角形，输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个，再按面积从小到大输出被直线分割成两部分的面积（不换行）。若直线与多边形形的一条边线重合，输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入，若前两点重合，输出"points coincide"。

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

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

wrong number of points

``````import java.util.Scanner;

public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String[] s = str.split(" ");//将字符串按照空格分开，便于后面判断格式合法的坐标点数量
boolean flag = true;
String regStr = "([\+\-]?([0]|[1-9]+))(\.\d+)?\,([\+\-]?([0]|[1-9]+))(\.\d+)?(\s([\+\-]?([0]|[1-9]+))(\.\d+)?\,([+\-]?([0]|[1-9]+))(\.\d+)?)+";
if (str.matches(regStr))
flag = true;
else flag = false;
if (!flag){
System.out.println("Wrong Format");
return;
}else {
if (s.length != 2)
System.out.println("wrong number of points");
else {
String[] s1 = s[0].split(",");
String[] s2 = s[1].split(",");
double x1 = Double.parseDouble(s1[0]);
double y1 = Double.parseDouble(s1[1]);
double x2 = Double.parseDouble(s2[0]);
double y2 = Double.parseDouble(s2[1]);
//double x1 = Double.parseDouble(String.valueOf(str.charAt(0)));//取出字符串第一个字符强制转化为double
//double y1 = str.charAt(2);
//double x2 = str.charAt(5);
//double y2 = str.charAt(7);
double a = x1 - x2;
double b = y1 - y2;
//System.out.println(x1);
System.out.println(Math.sqrt(a*a+b*b));
}
}
}
}

``````