OO第三阶段实验报告

一、     前言

1.    知识点

1.类的继承

2.继承

继承是面向对象语法的三大特征之一。继承可以降低代码编写的冗余度,提高编程的效率。通过继承,子类获得了父类的成员变量和方法。  

继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。

继承的作用:通过继承可以快速创建新的类,实现代码的重用,提高程序的可维护性,节省大量创建新类的时间,提高开发效率和开发质量。

在 Java 中通过 extends 关键字可以申明一个类是从另外一个类继承而来的。

注意:

子类不能选择性继承父类;

Java不支持多重继承,但一个类可以实现多个接口,从而克服单继承的缺点;

构造方法不会被子类继承,但可以从子类中调用父类的构造方法。

继承的优点

继承过来的字段和方法,可以像任何其他字段和方法一样被直接使用;

在子类中可以声明一个与父类中同名的新字段或静态方法,从而“隐藏”父类中的字段或方法;

可以在子类中声明一个在父类中没有的新字段和方法;

可以在子类中编写一个父类当中具有相同名的新实例方法,这称为“方法重写”或“方法覆盖”;

可以在子类中编写一个调用父类构造方法的子类构造方法,既可以隐式地实现,也可以通过使用关键字super来实现。

使用super调用父类的无参数构造方法/有参数构造方法

子类不继承其父类的构造方法。

当使用无参数的super()时,父类的无参数构造方法就会被调用;

当使用带有参数的super()方法时,父类的有参数构造方法就会被调用。

注意

如果要初始化父类中的字段,可以在子类的构造方法中通过关键字super调用父类的构造方法;

对父类的构造放的调用必须放在子类构造方法的第一行;

如果父类构造器没有参数,则在子类的构造器中不需要使用 super 关键字调用父类构造器,系统会自动调用父类的无参构造器;

如果父类的构造器带有参数,则必须在子类的构造器中显式地通过 super 关键字调用父类的构造器并配以适当的参数列表;

子类是不继承父类的构造器(构造方法或者构造函数)的,它只是调用(隐式或显式)。

2.类的多态性

多态性:对象的多态性是指在父类中定义的属性或方法被子类继承之后,可以具有不同的数据类型或表现出不同的行为。这使得同一个属性或方法在父类及其各个子类中具有不同的语义。例如:"几何图形"的"绘图"方法,"椭圆"和"多边形"都是"几何图"的子类,其"绘图"方法功能不同。
方法:Java的多态性体现在两个方面:由方法重载实现的静态多态性(编译时多态)和方法重写实现的动态多态性(运行时多态)。编译时多态:在编译阶段,具体调用哪个被重载的方法,编译器会根据参数的不同来静态确定调用相应的方法。运行时多态:由于子类继承了父类所有的属性(私有的除外),所以子类对象可以作为父类对象使用。程序中凡是使用父类对象的地方,都可以用子类对象来代替。一个对象可以通过引用子类的实例来调用子类的方法。
注:①多态存在的三个必要条件
一、要有继承;
二、要有重写;
三、父类引用指向子类对象。
②实现多态的技术:动态连接或动态绑定(dynamic binding):对于父类中定义的方法,如果子类中重写了该方法,那么父类类型的引用将会调用子类中的这个方法

补充:既然说了面向对象的基本特征,那就再说说和上述多态息息相关的重写(override)即覆盖和重载吧。
重载:让类以统一的方式处理不同类型数据的一种手段。多个同名函数同时存在,具有不同的参数个数/类型。
重载Overloading是一个类中多态性的一种表现。重载的时候,方法名要一样,但是参数类型和个数不一样,返回值类型可以相同也可以不相同。无法以返回型别作为重载函数的区分标准。
重写:重写又叫覆盖,以下是要注意的要点。
1、重写方法的访问修饰符一定要大于被重写方法的访问修饰符(public>protected>default>private)。
2、重写的方法的返回值必须和被重写的方法的返回一致;
3、重写方法的参数列表必须完全与被重写的方法的相同,否则不能称其为重写而是重载.
4、重写的方法所抛出的异常必须和被重写方法的所抛出的异常一致,或者是其子类;
5、被重写的方法不能为private,否则在其子类中只是新定义了一个方法,并没有对其进行重写。
6、静态方法不能被重写为非静态的方法(会编译出错)。
java接口中的方法访问修饰符不可以为protected,private,抽象类中抽象方法不能为private
3.接口Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能)。

有时必须从几个类中派生出一个子类,继承它们所有的属性和方法。但是,Java不支持多重继承。有了接口,就可以得到多重继承的效果。

接口(interface)是抽象方法和常量值的定义的集合。

从本质上讲,接口是一种特殊的抽象类,这种抽象类中只包含常量和方法的定义,而没有变量和方法的实现。

接口定义举例

public interface Runner
int id = 1
public void start()
public void run()
public void stop()
}
案例:

蝙蝠会飞,又会用牙齿咬

首先定义一个飞行的接口:

package cn.com.Classwork190124;

/*
如果一个类中,既有抽象方法,又有非抽象方法,那么该类只能定义为抽象类,不能定义为接口
如果一个类中,只有抽象方法,没有非抽象方法,那么该类可以定义为接口
一般就定义为接口
定义一个接口,使用interface关键字 接口,类,枚举,注解等都是java中的类型
接口中所有的方法,都是抽象方法
所以也可以说,接口就是一个特殊的抽象类

接口中除了定义方法外,还可以定义成员变量
特点;方法和属性默认都是public修饰,也可以使用protected,但不能用private
所有的属性都是静态的常量,默认省略了static和final修饰符,属性的值必须实例化(初始化)

*/

public interface Flyable {
public final int wingsNumber = 2;
public abstract void fly();

}

接着定义一个咬人的接口:

package cn.com.Classwork190124;

public interface Bitable {
public int teethNumber = 0;
public abstract void bite();
}

最后定义蝙蝠类去实现这两个接口:

package cn.com.Classwork190124;

/*
在JAVA中,一个类无法继承自多个类,但是可以实现多个接口,使用关键字implements
多个接口之间使用“,”隔开 多个接口之间,没有先后顺序
这个类叫做实现类,这个类必须实现所有接口的所有方法
*/

public class Bat implements Flyable,Bitable {
@Override
public void bite() {
System.out.println("吸血");
}

@Override
public void fly() {
System.out.println("用翅膀飞");
}

public static void main(String[] args) {
System.out.println(Flyable.wingsNumber);
}
}
接口的特点:
用 interface 来定义。

接口中的所有成员变量都默认是由public static final修饰的。

接口中的所有方法都默认是由public abstract修饰的。

接口没有构造方法。构造方法用于创建对象

实现接口的类中必须提供接口中所有方法的具体实现内容。

多个无关的类可以实现同一个接口

一个类可以实现多个无关的接口

与继承关系类似,接口与实现类之间存在多态性

接口也可以继承另一个接口,使用extends关键字。

实现接口的类中必须提供接口中所有方法的具体实现内容。

多个无关的类可以实现同一个接口

一个类可以实现多个无关的接口

与继承关系类似,接口与实现类之间存在多态性

定义Java类的语法格式:

< modifier> class < name> [extends < superclass>]

[implements < interface> [,< interface>]* ] {
< declarations>*

}
此外,接口还常常被用来当做定义常量的集合。

4.List

List是Java中比较常用的集合类,关于List接口有很多实现类,下面就来简单介绍下其中几个重点的实现ArrayList、LinkedList和Vector之间的关系和区别。

List 是一个接口,它继承于Collection的接口。它代表着有序的队列。当我们讨论List的时候,一般都和Set作比较。

下面是Java中的集合类的关系图。从中可以大致了解集合类之间

ArrayList、 LinkedList 和 Vector之间的区别

从上图可以看出,ArrayList、 LinkedList 和 Vector都实现了List接口,是List的三种实现,所以在用法上非常相似。他们之间的主要区别体现在不同操作的性能上。后面会详细分析。

 ArrayList

 

ArrayList底层是用数组实现的,可以认为ArrayList是一个可改变大小的数组。随着越来越多的元素被添加到ArrayList中,其规模是动态增加的。

LinkedList

LinkedList底层是通过双向链表实现的。所以,LinkedList和ArrayList之前的区别主要就是数组和链表的区别。

所以,LinkedList和ArrayList相比,增删的速度较快。但是查询和修改值的速度较慢。同时,LinkedList还实现了Queue接口,所以他还提供了offer(), peek(), poll()等方法。

Vector

Vector和ArrayList一样,都是通过数组实现的,但是Vector是线程安全的。和ArrayList相比,其中的很多方法都通过同步(synchronized)处理来保证线程安全。

如果你的程序不涉及到线程安全问题,那么使用ArrayList是更好的选择(因为Vector使用synchronized,必然会影响效率)。

二者之间还有一个区别,就是扩容策略不一样。在List被第一次创建的时候,会有一个初始大小,随着不断向List中增加元素,当List认为容量不够的时候就会进行扩容。Vector缺省情况下自动增长原来一倍的数组长度,ArrayList增长原来的50%。

5。链表

A Linked List implementation must have following common purpose methods.

链表实现必须具有以下通用方法。

getFirst: It returns the data (if any, else it throws an exception) present at the first Node. It simply returns the “this.head.data” provided “if(this. Size!=0)” i.e. if the size of the Linked list is not zero.
getFirst :它返回第一个Node上存在的数据(如果有的话,否则抛出异常)。 它只是返回“ if( this。Size != 0) ”所提供的“ this.head.data ”,即,如果链接列表的大小不为零。getLast: It returns the data (if any, else it throws an exception) present at the last Node. It simply returns the “this.tail.data” provided “if(this. Size!=0)” i.e. if the size of the Linked list is not zero.getLast :它返回最后一个节点上存在的数据(如果有的话,否则抛出异常)。 它只是返回“ if( this。Size != 0) ”所提供的“ this.tail.data ”,即,如果链接列表的大小不为零。getAt: It takes an Integer index as an argument and if the index is valid(i.e. if index>=0 and index <size of the Linked list) it returns the data of the element present at that index.getAt :它使用一个整数索引作为参数,并且如果索引有效(即,如果index> = 0且index <链接列表的大小),则它返回该索引处存在的元素的数据 。getNodeAt: It takes an Integer index as an argument and if the index is valid(i.e. if index>=0 and index <size of the Linked list) it returns the Node at that index. getNodeAt :它将一个整数索引作为参数,并且如果索引有效(即,如果index> = 0且index <链接列表的大小),则返回该索引处的Node 。addLast: It takes an input of type of which Linked list is, and then appends it at the last position of the Linked list, thereby increasing the size by ‘+1’ and pointing the “tail” at the newly added Node.addLast :接受链接列表类型的输入,然后将其追加到链接列表的最后一个位置,从而将大小增加“ +1”,并将“尾部”指向新添加的Node。addFirst: It takes an input of type of which Linked list is, and then appends it at the First position of the Linked list, thereby increasing the size by ‘+1’ and pointing the “head” at the newly added Node.addFirst :它接受链接列表类型的输入,然后将其追加到链接列表的第一个位置,从而将大小增加“ +1”,并将“ head”指向新添加的Node。addAt: It takes an input of type of which Linked list is and it also takes an Integer index (let it be equal to ‘k’) as arguments, and then appends it (data) at the kth position of the Linked list, thereby increasing the size by ‘+1’. In case the element to be added is “First” or “Last” then it calls “addFirst” or “addLast” respectively.addAt :它接受链接列表类型的输入,还接受一个整数索引(使其等于“ k”)作为参数,然后将其(数据)附加到链接列表的第k个位置,从而将大小增加“ +1”。 如果要添加的元素是“ First ”或“ Last ”,则分别调用“ addFirst ”或“ addLast ”。removeFirst: It removes the first element of a Linked list (Provided the list is not an empty list, in which it throws an exception) and moves the “head” to point towards the 2nd element (if any ) and decreases the size by ‘1’. Also, it returns the removed data. removeFirst :删除链接列表的第一个元素(假设列表不是空列表,在其中抛出异常),并将“ head”指向第二个元素(如果有的话),并减小大小'1'。 同样,它返回删除的数据。removeLast: It removes the last element of the Linked list (Provided the list is not an empty list, in which it throws an exception) and moves the “tail” to point towards the 2nd last element (if any ) and decreases the size by ‘1’. Also, it returns the removed data. removeLast :删除链接列表的最后一个元素(假设列表不是一个空列表,在其中抛出异常),然后将“ tail”移动到最后一个第二个元素(如果有的话),并减小其大小按“ 1”。 同样,它返回删除的数据。removeAt: It takes an Integer index (let’s say k) as an argument and if the index is valid(i.e. if index>=0 and index <size of Linked list) removes the ‘kth’ element of Linked list (Provided the list is not an empty list, in which it throws an exception)  and decreases the size by ‘1’. Also, it returns the removed data. In case the element to be removed is “First” or “Last” then it calls “removeFirst” or “removeLast” respectively.removeAt :它使用一个整数索引(比如说k)作为参数,并且如果索引有效(即,如果index> = 0且index <链接列表的大小),则删除链接列表的“ kth”元素(假设列表为(不是一个空列表,它会引发异常)并将大小减小1。 同样,它返回删除的数据。 如果要删除的元素是“ First ”或“ Last ”,则分别调用“ removeFirst ”或“ removeLast ”。display: This method prints the whole Linked list by traversing it once starting from a pointer which points to “head” until it points to “null”.display :此方法通过从指向“ head”的指针开始直到指向“ null”的指针遍历,来打印整个Linked列表。

java实现链表

A linked list is a linear data structure that contains a sequence of elements such that each element has reference to the next element in the sequence.

链表是一个线性数据结构,包含一个元素序列,这样每个元素都可以引用序列中的下一个元素。

Each element in the linked list is called “Node”. Each Node contains a data element and a Node next which points to the next node in the linked list.

链接列表中的每个元素称为“节点”。 每个节点都包含一个数据元素和一个下一个Node,它指向链表中的下一个节点。

In a linked list we can create as many nodes as we want according to our requirement.

在链表中,我们可以根据需要创建任意数量的节点。

2.    题量

第7-9次题目集,题量适中,根据题目难度决定时间和题目数量。

3.    难度情况

第七八次题目集难度较大,第九次主要是对第八次题目集进行修改,并对知识及逆行巩固,题目的发布具有逻辑性,能够很好的对同学的知识掌握进行练习和巩固。

二 、   设计与分析

题目集七7-1

题目代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
abstract class Shape {
private String shapeName;
public Shape() {
super();
}
public Shape(String shapeName) {
super();
this.shapeName = shapeName;
}
public String getShapeName() {
return shapeName;
}
public void setShapeName(String shapeName) {
this.shapeName = shapeName;
}
public double getArea() {
return 1;
}
public boolean vaildate() {
return true;
}
public String toString() {
return getShapeName() + ":" + String.format("%.2f ", getArea());
}
}
class Circle extends Shape {
private double radius;
public Circle() {
super();
}
public Circle(double radius) {
super();
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public boolean validate() {
if (radius>=0) {
return true;
}
else {
return false;
}
}
public Circle(String shapeName) {
super(shapeName);
}
}
class Rectangle extends Shape {
private double width;
private double length;
public Rectangle() {
super();
}
public Rectangle(double width, double length) {
super();
this.width = width;
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getArea() {
return length * width;
}
public boolean validate() {
if (width>=0&&length>=0) {
return true;
}
else {
return false;
}
}
public Rectangle(String shapeName) {
super(shapeName);
}
}

class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
public Triangle() {
super();
}
public Triangle(double side1, double side2, double side3) {
super();
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
public double getArea() {
double a,b;
a = (double) (side1 + side2 + side3) / 2;
b = Math.sqrt(a * (a - side1) * (a - side2) * (a - side3));
return b;
}
public boolean vaildate() {
if ((side1 + side2)>side3&&(side1 + side3)>side2&&(side2 + side3)>side1&&side1>=0&&side2>=0&&side3>=0) {
return true;
}
else {
return false;
}
}
public Triangle(String shapeName) {
super(shapeName);
}
}
class Trapezoid extends Shape {
private double topSide;
private double bottomSide;
private double height;
public Trapezoid() {
super();
}
public Trapezoid(double topSide, double bottomSide, double height) {
super();
this.topSide = topSide;
this.bottomSide = bottomSide;
this.height = height;
}
public double getArea() {
return (height*(topSide + bottomSide))/2;
}
public boolean validate() {
if (topSide>=0&&bottomSide>=0&&height>=0) {
return true;
}
else {
return false;
}
}
public Trapezoid(String shapeName) {
super(shapeName);
}
}
class Card implements Comparable<Card> {
Shape shape;
public Card() {
super();
}
public Card(Shape shape) {
super();
this.shape = shape;
}
public Shape getShape() {
return shape;
}
public void setShape(Shape shape) {
this.shape = shape;
}
public int compareTo(Card card) {
if(this.shape.getArea()==card.shape.getArea()) {
return 0;
}
else if(this.shape.getArea()<card.shape.getArea()) {
return 1;
}
else {
return -1;
}
}
}
class DealCardList {
List<Card> List = new ArrayList<Card>();
public DealCardList() {
}
public DealCardList(ArrayList<Integer> list) {
for(int i=0; i<list.size();i++) {
if(list.get(i)==1)
{
double r=Main.input.nextDouble();
Circle circle=new Circle(r);
Card card=new Card(circle);
card.getShape().setShapeName("Circle");
List.add(card);
}
if(list.get(i)==2) {
double a=Main.input.nextDouble();
double b=Main.input.nextDouble();
Rectangle rectangle=new Rectangle(a,b);
Card card=new Card(rectangle);
card.getShape().setShapeName("Rectangle");
List.add(card);
}
if(list.get(i)==3) {
double c=Main.input.nextDouble();
double d=Main.input.nextDouble();
double e=Main.input.nextDouble();
Triangle triangle=new Triangle(c,d,e);
Card card=new Card(triangle);
card.getShape().setShapeName("Triangle");
List.add(card);
}
if(list.get(i)==4) {
double f=Main.input.nextDouble();
double g=Main.input.nextDouble();
double h=Main.input.nextDouble();
Trapezoid traperoid=new Trapezoid(f,g,h);
Card card=new Card(traperoid);
card.getShape().setShapeName("Trapezoid");
List.add(card);
}
}
}
public boolean validate() {
for (Card card:List) {
if(!card.getShape().vaildate()) {
return false;
}
}
return true;
}
public void cardSort() {
Collections.sort(List);
}
public double getAllArea() {
double a;
a=0;
for (Card card:List) {
a=a+card.getShape().getArea();
}
return a;
}
public void showResult() {
System.out.println("The original list:");
for (Card card : List) {
System.out.print(card.getShape());
}
System.out.println("\nThe sorted list:");
cardSort();
for (Card card : List) {
System.out.print(card.getShape());
}
System.out.printf("\nSum of area:%.2f", getAllArea());
}
}

public class Main {
//在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
//使用Main.input.next…即可(避免采坑)
public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
int num = input.nextInt();
while(num != 0){
if(num < 0 || num > 4){
System.out.println("Wrong Format");
System.exit(0);
}
list.add(num);
num = input.nextInt();
}
DealCardList dealCardList = new DealCardList(list);
if(!dealCardList.validate()){
System.out.println("Wrong Format");
System.exit(0);
}
dealCardList.showResult();
input.close();
}
}

类图:

复杂度:

Metrics Details For File 'Main.java'
--------------------------------------------------------------------------------------------

Parameter Value
========= =====
Project Directory C:\Users\zhaoboyi\eclipse-workspace\ke\src\农夫过河\
Project Name
Checkpoint Name Baseline
File Name Main.java
Lines 301
Statements 213
Percent Branch Statements 11.3
Method Call Statements 58
Percent Lines with Comments 0.7
Classes and Interfaces 8
Methods per Class 6.38
Average Statements per Method 2.69
Line Number of Most Complex Method 136
Name of Most Complex Method Triangle.vaildate()
Maximum Complexity 8
Line Number of Deepest Block 211
Maximum Block Depth 4
Average Block Depth 1.96
Average Complexity 1.65

--------------------------------------------------------------------------------------------
Most Complex Methods in 8 Class(es): Complexity, Statements, Max Depth, Calls

Card.Card() 1, 2, 2, 1
Card.Card() 1, 1, 2, 1
Card.compareTo() 4, 6, 3, 4
Card.getShape() 1, 1, 2, 0
Card.setShape() 1, 1, 2, 0
Circle.Circle() 1, 1, 2, 1
Circle.Circle() 1, 2, 2, 1
Circle.Circle() 1, 1, 2, 1
Circle.getArea() 1, 1, 2, 0
Circle.getRadius() 1, 1, 2, 0
Circle.setRadius() 1, 1, 2, 0
Circle.validate() 3, 4, 3, 0
DealCardList.cardSort() 1, 1, 2, 1
DealCardList.DealCardList() 6, 30, 4, 13
DealCardList.DealCardList() 1, 0, 0, 0
DealCardList.getAllArea() 2, 5, 3, 0
DealCardList.showResult() 3, 8, 3, 6
DealCardList.validate() 3, 4, 4, 2
Main.main() 5, 14, 4, 11
Rectangle.getArea() 1, 1, 2, 0
Rectangle.getLength() 1, 1, 2, 0
Rectangle.getWidth() 1, 1, 2, 0
Rectangle.Rectangle() 1, 1, 2, 1
Rectangle.Rectangle() 1, 3, 2, 1
Rectangle.Rectangle() 1, 1, 2, 1
Rectangle.setLength() 1, 1, 2, 0
Rectangle.setWidth() 1, 1, 2, 0
Rectangle.validate() 4, 4, 3, 0
Shape.getArea() 1, 1, 2, 0
Shape.getShapeName() 1, 1, 2, 0
Shape.setShapeName() 1, 1, 2, 0
Shape.Shape() 1, 2, 2, 1
Shape.Shape() 1, 1, 2, 1
Shape.toString() 1, 1, 2, 2
Shape.vaildate() 1, 1, 2, 0
Trapezoid.getArea() 1, 1, 2, 1
Trapezoid.Trapezoid() 1, 1, 2, 1
Trapezoid.Trapezoid() 1, 4, 2, 1
Trapezoid.Trapezoid() 1, 1, 2, 1
Trapezoid.validate() 5, 4, 3, 0
Triangle.getArea() 1, 4, 2, 1
Triangle.getSide1() 1, 1, 2, 0
Triangle.getSide2() 1, 1, 2, 0
Triangle.getSide3() 1, 1, 2, 0
Triangle.setSide1() 1, 1, 2, 0
Triangle.setSide2() 1, 1, 2, 0
Triangle.setSide3() 1, 1, 2, 0
Triangle.Triangle() 1, 1, 2, 1
Triangle.Triangle() 1, 4, 2, 1
Triangle.Triangle() 1, 1, 2, 1
Triangle.vaildate() 8, 4, 3, 0

--------------------------------------------------------------------------------------------
Block Depth Statements

0 12
1 64
2 85
3 24
4 28
5 0
6 0
7 0
8 0
9+ 0
--------------------------------------------------------------------------------------------

复杂度分析:本题最大复杂度为8,平均复杂度为1.65,达到基本要求。

解释:本题通过书写类和自己的算法,然后对代码进行输出从而达到题目要求。

心得:本题主要考验类的继承、多态性使用方法以及接口的应用,很好的锻炼了语句的运用能力和自己的逻辑思维。

题目集七7-2

 

 

题目代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
abstract class Shape {
private String shapeName;
public Shape() {
super();
}
public Shape(String shapeName) {
super();
this.shapeName = shapeName;
}
public String getShapeName() {
return shapeName;
}
public void setShapeName(String shapeName) {
this.shapeName = shapeName;
}
public double getArea() {
return 1;
}
public boolean vaildate() {
return true;
}
public String toString() {
return getShapeName() + ":" + String.format("%.2f ", getArea());
}
}
class Circle extends Shape {
private double radius;
public Circle() {
super();
}
public Circle(double radius) {
super();
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public boolean validate() {
if (radius>=0) {
return true;
}
else {
return false;
}
}
public Circle(String shapeName) {
super(shapeName);
}
}
class Rectangle extends Shape {
private double width;
private double length;
public Rectangle() {
super();
}
public Rectangle(double width, double length) {
super();
this.width = width;
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getArea() {
return length * width;
}
public boolean validate() {
if (width>=0&&length>=0) {
return true;
}
else {
return false;
}
}
public Rectangle(String shapeName) {
super(shapeName);
}
}

class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
public Triangle() {
super();
}
public Triangle(double side1, double side2, double side3) {
super();
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
public double getArea() {
double a,b;
a = (double) (side1 + side2 + side3) / 2;
b = Math.sqrt(a * (a - side1) * (a - side2) * (a - side3));
return b;
}
public boolean vaildate() {
if ((side1 + side2)>side3&&(side1 + side3)>side2&&(side2 + side3)>side1&&side1>=0&&side2>=0&&side3>=0) {
return true;
}
else {
return false;
}
}
public Triangle(String shapeName) {
super(shapeName);
}
}
class Trapezoid extends Shape {
private double topSide;
private double bottomSide;
private double height;
public Trapezoid() {
super();
}
public Trapezoid(double topSide, double bottomSide, double height) {
super();
this.topSide = topSide;
this.bottomSide = bottomSide;
this.height = height;
}
public double getArea() {
return (topSide + bottomSide) * height / 2;
}
public boolean validate() {
if (topSide>=0&&bottomSide>=0&&height>=0) {
return true;
}
else {
return false;
}
}
public Trapezoid(String shapeName) {
super(shapeName);
}
}
class Card implements Comparable<Card> {
Shape shape;
public Card() {
super();
}
public Card(Shape shape) {
super();
this.shape = shape;
}
public Shape getShape() {
return shape;
}
public void setShape(Shape shape) {
this.shape = shape;
}
public int compareTo(Card card) {
if(this.shape.getArea()==card.shape.getArea()) {
return 0;
}
else if(this.shape.getArea()<card.shape.getArea()) {
return 1;
}
else {
return -1;
}
}
}
class DealCardList {
List<Card> List = new ArrayList<Card>();
List <Card> List1 = new ArrayList<>();
List <Card> List2 = new ArrayList<>();
List <Card> List3 = new ArrayList<>();
List <Card> List4 = new ArrayList<>();
double[] m = new double[4];
public DealCardList() {
}
public DealCardList(ArrayList<Integer> list) {
for(int i=0; i<list.size();i++) {
if(list.get(i)==1)
{
double r=Main.input.nextDouble();
Circle circle=new Circle(r);
Card card=new Card(circle);
card.getShape().setShapeName("Circle");
List.add(card);
List1.add(card);
m[0]=m[0]+card.getShape().getArea();
}
if(list.get(i)==2) {
double a=Main.input.nextDouble();
double b=Main.input.nextDouble();
Rectangle rectangle=new Rectangle(a,b);
Card card=new Card(rectangle);
card.getShape().setShapeName("Rectangle");
List.add(card);
List2.add(card);
m[1]=m[1]+card.getShape().getArea();
}
if(list.get(i)==3) {
double c=Main.input.nextDouble();
double d=Main.input.nextDouble();
double e=Main.input.nextDouble();
Triangle triangle=new Triangle(c,d,e);
Card card=new Card(triangle);
card.getShape().setShapeName("Triangle");
List.add(card);
List3.add(card);
m[2]=m[2]+card.getShape().getArea();
}
if(list.get(i)==4) {
double f=Main.input.nextDouble();
double g=Main.input.nextDouble();
double h=Main.input.nextDouble();
Trapezoid traperoid=new Trapezoid(f,g,h);
Card card=new Card(traperoid);
card.getShape().setShapeName("Trapezoid");
List.add(card);
List4.add(card);
m[3]=m[3]+card.getShape().getArea();
}
}
}
public boolean validate() {
for (Card card:List) {
if(card.getShape().vaildate()) {
return true;
}
}
return false;
}
public void cardSort() {
Collections.sort(List);
}
public double getAllArea() {
double a;
a=0;
for (Card card:List) {
a=a+card.getShape().getArea();
}
return a;
}
public void showResult() {
System.out.println("The original list:");
System.out.print("[");
for (Card card : List) {
System.out.print(card.getShape());
}
System.out.print("]\n");
System.out.println("The Separated List:");
System.out.print("[");
for(int i=0;i<List1.size();i++) {
System.out.print(List1.get(i).getShape().getShapeName()+":"+String.format("%.2f",List1.get(i).getShape().getArea())+" ");
}
System.out.print("][");
for(int i=0;i<List2.size();i++) {
System.out.print(List2.get(i).getShape().getShapeName()+":"+String.format("%.2f",List2.get(i).getShape().getArea())+" ");
}
System.out.print("][");
for(int i=0;i<List3.size();i++) {
System.out.print(List3.get(i).getShape().getShapeName()+":"+String.format("%.2f",List3.get(i).getShape().getArea())+" ");
}
System.out.print("][");
for(int i=0;i<List4.size();i++) {
System.out.print(List4.get(i).getShape().getShapeName()+":"+String.format("%.2f",List4.get(i).getShape().getArea())+" ");
}
System.out.print("]\n");
System.out.println("The Separated sorted List:");
Collections.sort(List1);
System.out.print("[");
for(int i=0;i<List1.size();i++) {
System.out.print(List1.get(i).getShape().getShapeName()+":"+String.format("%.2f",List1.get(i).getShape().getArea())+" ");
}
System.out.print("]");
Collections.sort(List2);
System.out.print("[");
for(int i=0;i<List2.size();i++) {
System.out.print(List2.get(i).getShape().getShapeName()+":"+String.format("%.2f",List2.get(i).getShape().getArea())+" ");
}
System.out.print("]");
Collections.sort(List3);
System.out.print("[");
for(int i=0;i<List3.size();i++) {
System.out.print(List3.get(i).getShape().getShapeName()+":"+String.format("%.2f",List3.get(i).getShape().getArea())+" ");
}
System.out.print("]");
Collections.sort(List4);
System.out.print("[");
for(int i=0;i<List4.size();i++) {
System.out.print(List4.get(i).getShape().getShapeName()+":"+String.format("%.2f",List4.get(i).getShape().getArea())+" ");
}
System.out.print("]\n");
double b=0;
for(int i=0;i<m.length ;i++) {
if(b<m[i]) {
b = m[i];
}
}
System.out.printf("The max area:%.2f\n",b);
}
}

public class Main {
//在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
//使用Main.input.next…即可(避免采坑)
public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
int num = input.nextInt();
while(num != 0){
if(num < 0 || num > 4){
System.out.println("Wrong Format");
System.exit(0);
}
list.add(num);
num = input.nextInt();
}
DealCardList dealCardList = new DealCardList(list);
if(!dealCardList.validate()){
System.out.println("Wrong Format");
System.exit(0);
}
dealCardList.showResult();
input.close();
}
}

类图:

复杂度:

Metrics Details For File 'Main.java'
--------------------------------------------------------------------------------------------

Parameter Value
========= =====
Project Directory C:\Users\zhaoboyi\eclipse-workspace\ke\src\农夫过河\
Project Name
Checkpoint Name Baseline
File Name Main.java
Lines 360
Statements 263
Percent Branch Statements 12.5
Method Call Statements 103
Percent Lines with Comments 0.6
Classes and Interfaces 8
Methods per Class 6.38
Average Statements per Method 3.57
Line Number of Most Complex Method 277
Name of Most Complex Method DealCardList.showResult()
Maximum Complexity 12
Line Number of Deepest Block 216
Maximum Block Depth 4
Average Block Depth 2.05
Average Complexity 1.82

--------------------------------------------------------------------------------------------
Most Complex Methods in 8 Class(es): Complexity, Statements, Max Depth, Calls

Card.Card() 1, 2, 2, 1
Card.Card() 1, 1, 2, 1
Card.compareTo() 4, 6, 3, 4
Card.getShape() 1, 1, 2, 0
Card.setShape() 1, 1, 2, 0
Circle.Circle() 1, 1, 2, 1
Circle.Circle() 1, 2, 2, 1
Circle.Circle() 1, 1, 2, 1
Circle.getArea() 1, 1, 2, 0
Circle.getRadius() 1, 1, 2, 0
Circle.setRadius() 1, 1, 2, 0
Circle.validate() 3, 4, 3, 0
DealCardList.cardSort() 1, 1, 2, 1
DealCardList.DealCardList() 6, 38, 4, 17
DealCardList.DealCardList() 1, 0, 0, 0
DealCardList.getAllArea() 2, 5, 3, 0
DealCardList.showResult() 12, 45, 4, 48
DealCardList.validate() 3, 4, 4, 2
Main.main() 5, 14, 4, 11
Rectangle.getArea() 1, 1, 2, 0
Rectangle.getLength() 1, 1, 2, 0
Rectangle.getWidth() 1, 1, 2, 0
Rectangle.Rectangle() 1, 1, 2, 1
Rectangle.Rectangle() 1, 3, 2, 1
Rectangle.Rectangle() 1, 1, 2, 1
Rectangle.setLength() 1, 1, 2, 0
Rectangle.setWidth() 1, 1, 2, 0
Rectangle.validate() 4, 4, 3, 0
Shape.getArea() 1, 1, 2, 0
Shape.getShapeName() 1, 1, 2, 0
Shape.setShapeName() 1, 1, 2, 0
Shape.Shape() 1, 2, 2, 1
Shape.Shape() 1, 1, 2, 1
Shape.toString() 1, 1, 2, 2
Shape.vaildate() 1, 1, 2, 0
Trapezoid.getArea() 1, 1, 2, 0
Trapezoid.Trapezoid() 1, 1, 2, 1
Trapezoid.Trapezoid() 1, 4, 2, 1
Trapezoid.Trapezoid() 1, 1, 2, 1
Trapezoid.validate() 5, 4, 3, 0
Triangle.getArea() 1, 4, 2, 1
Triangle.getSide1() 1, 1, 2, 0
Triangle.getSide2() 1, 1, 2, 0
Triangle.getSide3() 1, 1, 2, 0
Triangle.setSide1() 1, 1, 2, 0
Triangle.setSide2() 1, 1, 2, 0
Triangle.setSide3() 1, 1, 2, 0
Triangle.Triangle() 1, 1, 2, 1
Triangle.Triangle() 1, 4, 2, 1
Triangle.Triangle() 1, 1, 2, 1
Triangle.vaildate() 8, 4, 3, 0

--------------------------------------------------------------------------------------------
Block Depth Statements

0 12
1 69
2 113
3 32
4 37
5 0
6 0
7 0
8 0
9+ 0
--------------------------------------------------------------------------------------------

复杂度分析:本题最大复杂度为12,平均复杂度为1.82,达到基本要求。

解释:本题通过7-1的代码,然后根据题目要求书写算法,对输出进行修改。

心得:本题同为考验类的继承、多态性使用方法以及接口的应用,主要是对7-1增加排序进行新的输出,很好的锻炼了语句的运用能力和自己的逻辑思维。

题目集四8

 

输出共两行,格式分别为:

[用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]

当前余额为¥[金额]

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

③存款业务输出

输出共两行,格式分别为:

[用户姓名]在[银行名称]的[ATM编号]上存款¥[金额]

当前余额为¥[金额]

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

④查询余额业务输出

¥[金额]

金额保留两位小数。

题目代码:


import java.util.Scanner;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

class ChinaUnionPay{
private List<Bank>listbank = new ArrayList<Bank>();
public List<Bank> getListbank() {
return listbank;
}

public void setListbank(List<Bank> listbank) {
this.listbank = listbank;
}

}
class Bank{
private List<ATM>listatm = new ArrayList<ATM>();
private List<User>listuser = new ArrayList<User>();
private String bankname;
public List<ATM> getListatm() {
return listatm;
}
public void setListatm(List<ATM> listatm) {
this.listatm = listatm;
}
public List<User> getListuser() {
return listuser;
}
public void setListuser(List<User> listuser) {
this.listuser = listuser;
}
public String getBankname() {
return bankname;
}
public void setBankname(String bankname) {
this.bankname = bankname;
}
}
class User{
private List<Account>listaccount = new ArrayList<Account>();
private String username;
public List<Account> getListaccount() {
return listaccount;
}
public void setListaccount(List<Account> listaccount) {
this.listaccount = listaccount;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}

}
class Account{
private List<Card>listcard = new ArrayList<Card>();
private String accountnum;
private double balance;
public List<Card> getListcard() {
return listcard;
}
public void setListcard(List<Card> listcard) {
this.listcard = listcard;
}
public String getAccountnum() {
return accountnum;
}
public void setAccountnum(String accountnum) {
this.accountnum = accountnum;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}

}
class ATM{
private String atmnum;

public String getAtmnum() {
return atmnum;
}

public void setAtmnum(String atmnum) {
this.atmnum = atmnum;
}

}
class Balance{
private double balance;

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

}
class Card{
private String cardnum;

public String getCardnum() {
return cardnum;
}

public void setCardnum(String cardnum) {
this.cardnum = cardnum;
}

}
class Information{
public void chinaunionpay(ChinaUnionPay chinaunionpay){
List<Bank> bank = new ArrayList<Bank>();
bank.add(bank1());
bank.add(bank2());
chinaunionpay.setListbank(bank);
}
public Bank bank1() {
Bank bank = new Bank();
ATM atm = new ATM();
List<ATM> latm = new ArrayList<ATM>();
List<User> user = new ArrayList<User>();
bank.setBankname("中国建设银行");

atm.setAtmnum("01");
latm.add(atm);
atm.setAtmnum("02");
latm.add(atm);
atm.setAtmnum("03");
latm.add(atm);
atm.setAtmnum("04");
latm.add(atm);
user.add(user1());
user.add(user2());
bank.setListuser(user);
return bank;
}
public Bank bank2() {
Bank bank = new Bank();
ATM atm = new ATM();
User user = new User();
List<ATM> latm = new ArrayList<ATM>();
List<User> luser = new ArrayList<User>();
bank.setBankname("中国工商银行");
atm.setAtmnum("05");
latm.add(atm);
atm.setAtmnum("06");
latm.add(atm);
luser.add(user3());
luser.add(user4());
bank.setListuser(luser);
bank.setListatm(latm);
return bank;
}
public User user1() {
User user = new User();
List<Account>account = new ArrayList<Account>();
account.add(account1());
account.add(account2());
user.setUsername("杨过");
user.setListaccount(account);
return user;
}
public User user2() {
User user = new User();
List<Account>account = new ArrayList<Account>();
account.add(account3());
user.setUsername("郭靖");
user.setListaccount(account);
return user;
}
public User user3() {
User user = new User();
List<Account>account = new ArrayList<Account>();
account.add(account4());
account.add(account5());
account.add(account6());
user.setUsername("张无忌");
user.setListaccount(account);
return user;
}
public User user4() {
User user = new User();
List<Account>account = new ArrayList<Account>();
account.add(account7());
account.add(account8());
user.setUsername("韦小宝");
user.setListaccount(account);
return user;
}
public Account account1() {
Account account = new Account();
List<Card>card = new ArrayList<Card>();
card.add(card1());
card.add(card2());
account.setBalance(10000.0);
account.setAccountnum("3217000010041315709");
account.setListcard(card);
return account;
}
public Account account2() {
Account account = new Account();
List<Card>card = new ArrayList<Card>();
card.add(card3());
account.setBalance(10000.0);
account.setAccountnum("3217000010041315715");
account.setListcard(card);
return account;
}
public Account account3() {
Account account = new Account();
List<Card>card = new ArrayList<Card>();
card.add(card4());
account.setBalance(10000.0);
account.setAccountnum("3217000010051320007");
account.setListcard(card);
return account;
}
public Account account4() {
Account account = new Account();
List<Card>card = new ArrayList<Card>();
card.add(card5());
account.setBalance(10000.0);
account.setAccountnum("3222081502001312389");
account.setListcard(card);
return account;
}
public Account account5() {
Account account = new Account();
List<Card>card = new ArrayList<Card>();
card.add(card6());
account.setBalance(10000.0);
account.setAccountnum("3222081502001312390");
account.setListcard(card);
return account;
}
public Account account6() {
Account account = new Account();
List<Card>card = new ArrayList<Card>();
card.add(card7());
card.add(card8());
account.setBalance(10000.0);
account.setAccountnum("3222081502001312399");
account.setListcard(card);
return account;
}
public Account account7() {
Account account = new Account();
List<Card>card = new ArrayList<Card>();
card.add(card9());
account.setBalance(10000.0);
account.setAccountnum("3222081502051320785");
account.setListcard(card);
return account;
}
public Account account8() {
Account account = new Account();
List<Card>card = new ArrayList<Card>();
card.add(card10());
account.setBalance(10000.0);
account.setAccountnum("3222081502051320786");
account.setListcard(card);
return account;
}
public Card card1() {
Card card = new Card();
card.setCardnum("6217000010041315709");
return card;
}
public Card card2() {
Card card = new Card();
card.setCardnum("6217000010041315715");
return card;
}
public Card card3() {
Card card = new Card();
card.setCardnum("6217000010041315718");
return card;
}
public Card card4() {
Card card = new Card();
card.setCardnum("6217000010051320007");
return card;
}
public Card card5() {
Card card = new Card();
card.setCardnum("6222081502001312389");
return card;
}
public Card card6() {
Card card = new Card();
card.setCardnum("6222081502001312390");
return card;
}
public Card card7() {
Card card = new Card();
card.setCardnum("6222081502001312399");
return card;
}
public Card card8() {
Card card = new Card();
card.setCardnum("6222081502001312400");
return card;
}
public Card card9() {
Card card = new Card();
card.setCardnum("6222081502051320785");
return card;
}
public Card card10() {
Card card = new Card();
card.setCardnum("6222081502051320786");
return card;
}
}
class Examine{
public boolean isCard(ChinaUnionPay chinaunionpay,String card){
Iterator<Bank> bankItr = chinaunionpay.getListbank().iterator();
while(bankItr.hasNext()){
Iterator<User> userItr = bankItr.next().getListuser().iterator();
while(userItr.hasNext()){
Iterator<Account> accountItr = userItr.next().getListaccount().iterator();
while(accountItr.hasNext()){
Iterator<Card> cardItr = accountItr.next().getListcard().iterator();
while(cardItr.hasNext()){
if(card.equals(cardItr.next().getCardnum())){
return true;
}
}
}
}
}
return false;
}
public boolean isATM(String number){
if(number.equals("01")||number.equals("02")||number.equals("03")||number.equals("04")||number.equals("05")||number.equals("06")){
return true;
}
else {
return false;
}
}
public boolean ispassword(String password) {
if(password.equals("88888888")) {
return true;
}
else {
return false;
}
}
public boolean isaccount(Account account,double balance) {
if(account.getBalance()-balance > 0 ) {
return true;
}
else {
return false;
}
}
public boolean isbank(Bank bank,String name) {
if(name.equals("01")||name.equals("02")||name.equals("03")||name.equals("04")){
name = "中国建设银行";
}
if(name.equals("05")||name.equals("06")) {
name = "中国工商银行";
}
if(bank.getBankname().equals(name)) {
return true;
}
else {
return false;
}
}
}
public class Main {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);
ChinaUnionPay chinaunionpay = new ChinaUnionPay();
Bank bank = new Bank();
User user = new User();
Account account = new Account();
ATM atm = new ATM();
Balance balance = new Balance();
Card card = new Card();
Information information = new Information();
Examine examine = new Examine();
information.chinaunionpay(chinaunionpay);
//System.out.println(chinaunionpay.getListbank().get(0).getListuser().get(0).getListaccount().get(0).getAccountnum());
String a = input.nextLine();
while(!a.equals("#")) {
String []x=a.split("\\s+");


for (int i = 0; i < chinaunionpay.getListbank().size(); i++) {
for (int j = 0; j < chinaunionpay.getListbank().get(i).getListuser().size(); j++) {
for (int k = 0; k < chinaunionpay.getListbank().get(i).getListuser().get(j).getListaccount().size(); k++) {

for (int l = 0; l < chinaunionpay.getListbank().get(i).getListuser().get(j).getListaccount().get(k).getListcard().size(); l++) {

if (x[0].equals(chinaunionpay.getListbank().get(i).getListuser().get(j).getListaccount().get(k)
.getListcard().get(l).getCardnum())) {
bank = chinaunionpay.getListbank().get(i);
user = chinaunionpay.getListbank().get(i).getListuser().get(j);
account = chinaunionpay.getListbank().get(i).getListuser().get(j).getListaccount().get(k);

}
}
}
}
}
if (x.length == 1) {
System.out.printf("¥%.2f\n", account.getBalance());
a = input.nextLine();
continue;
}
double m=Double.parseDouble(x[3]);
if(!examine.isCard(chinaunionpay, x[0])) {
System.out.print("Sorry,this card does not exist.");
System.exit(0);
}

if(!examine.ispassword(x[1])) {
System.out.print("Sorry,your password is wrong.");
System.exit(0);
}
if(!examine.isATM(x[2])) {
System.out.print("Sorry,the ATM's id is wrong.");
System.exit(0);
}
if(!examine.isaccount(account, m)) {
System.out.print("Sorry,your account balance is insufficient.");
System.exit(0);
}
if(!examine.isbank(bank, x[2])) {
System.out.print("Sorry,cross-bank withdrawal is not supported.");
System.exit(0);
}

//a = input.nextLine();



System.out.print(user.getUsername() + "在" + bank.getBankname() + "的" + x[2] + "号ATM机上");

if (m > 0) {
System.out.printf("取款¥%.2f\n", m);
} else {
System.out.printf("存款¥%.2f\n", -m);
}
account.setBalance(account.getBalance()-m);
System.out.printf("当前余额为¥%.2f\n", account.getBalance());

a = input.nextLine();
}
}
}

类图:

 

 

复杂度:

Metrics Details For File 'Main.java'

--------------------------------------------------------------------------------------------

Parameter Value
========= =====
Project Directory C:\Users\zhaoboyi\eclipse-workspace\ke\src\农夫过河\
Project Name
Checkpoint Name Baseline
File Name Main.java
Lines 464
Statements 329
Percent Branch Statements 8.8
Method Call Statements 183
Percent Lines with Comments 0.4
Classes and Interfaces 10
Methods per Class 5.40
Average Statements per Method 4.61
Line Number of Most Complex Method 382
Name of Most Complex Method Main.main()
Maximum Complexity 15
Line Number of Deepest Block 409
Maximum Block Depth 8
Average Block Depth 2.04
Average Complexity 1.70

--------------------------------------------------------------------------------------------
Most Complex Methods in 10 Class(es): Complexity, Statements, Max Depth, Calls

Account.getAccountnum() 1, 1, 2, 0
Account.getBalance() 1, 1, 2, 0
Account.getListcard() 1, 1, 2, 0
Account.setAccountnum() 1, 1, 2, 0
Account.setBalance() 1, 1, 2, 0
Account.setListcard() 1, 1, 2, 0
ATM.getAtmnum() 1, 1, 2, 0
ATM.setAtmnum() 1, 1, 2, 0
Balance.getBalance() 1, 1, 2, 0
Balance.setBalance() 1, 1, 2, 0
Bank.getBankname() 1, 1, 2, 0
Bank.getListatm() 1, 1, 2, 0
Bank.getListuser() 1, 1, 2, 0
Bank.setBankname() 1, 1, 2, 0
Bank.setListatm() 1, 1, 2, 0
Bank.setListuser() 1, 1, 2, 0
Card.getCardnum() 1, 1, 2, 0
Card.setCardnum() 1, 1, 2, 0
ChinaUnionPay.getListbank() 1, 1, 2, 0
ChinaUnionPay.setListbank() 1, 1, 2, 0
Examine.isaccount() 3, 4, 3, 1
Examine.isATM() 8, 4, 3, 6
Examine.isbank() 9, 8, 3, 8
Examine.isCard() 6, 11, 7, 11
Examine.ispassword() 3, 4, 3, 1
Information.account1() 1, 8, 2, 6
Information.account2() 1, 7, 2, 5
Information.account3() 1, 7, 2, 5
Information.account4() 1, 7, 2, 5
Information.account5() 1, 7, 2, 5
Information.account6() 1, 8, 2, 6
Information.account7() 1, 7, 2, 5
Information.account8() 1, 7, 2, 5
Information.bank1() 1, 17, 2, 14
Information.bank2() 1, 15, 2, 11
Information.card1() 1, 3, 2, 1
Information.card10() 1, 3, 2, 1
Information.card2() 1, 3, 2, 1
Information.card3() 1, 3, 2, 1
Information.card4() 1, 3, 2, 1
Information.card5() 1, 3, 2, 1
Information.card6() 1, 3, 2, 1
Information.card7() 1, 3, 2, 1
Information.card8() 1, 3, 2, 1
Information.card9() 1, 3, 2, 1
Information.chinaunionpay() 1, 4, 2, 4
Information.user1() 1, 7, 2, 5
Information.user2() 1, 6, 2, 4
Information.user3() 1, 8, 2, 6
Main.main() 15, 49, 8, 55
User.getListaccount() 1, 1, 2, 0
User.getUsername() 1, 1, 2, 0
User.setListaccount() 1, 1, 2, 0
User.setUsername() 1, 1, 2, 0

--------------------------------------------------------------------------------------------
Block Depth Statements

0 14
1 66
2 195
3 27
4 17
5 3
6 2
7 2
8 3
9+ 0
--------------------------------------------------------------------------------------------

复杂度分析:本题最大复杂度为8,平均复杂度为1.70,达到基本要求。

解释:本题通过题目要求进行类的书写和调用,并通过自己书写代码,在主函数调用输出。

心得:本题主要考验类的书写和调用和对数据进行存储,很好的锻炼了类的书写调用能力和自己的逻辑思维。

设计思路分析总结:本题主要是根据题目要求然后进行类的书写,然后使用链表对数据进行存储和调用,在调用过程中对存储的数据进行计算,然后对代码进行输出。我本题的代码达到输出要求,但是总体代码不够规范,需要进行加强和改进。

题目集九

 

 

 

当前余额为¥[金额]

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

③查询余额业务输出

业务:查询余额 ¥[金额]

金额保留两位小数。

题目代码:

import java.util.ArraayList;
import java.util.Scanner;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

UnionPay unionPay = new UnionPay();

Bank ccb = new Bank("1001", "中国建设银行");
Bank icbc = new Bank("1002", "中国工商银行");
Bank abc = new Bank("1003", "中国农业银行");
unionPay.addBank(ccb);
unionPay.addBank(icbc);
unionPay.addBank(abc);

ATM aTM1 = new ATM("01", ccb);
ATM aTM2 = new ATM("02", ccb);
ATM aTM3 = new ATM("03", ccb);
ATM aTM4 = new ATM("04", ccb);
ATM aTM5 = new ATM("05", icbc);
ATM aTM6 = new ATM("06", icbc);
ATM aTM7 = new ATM("07", abc);
ATM aTM8 = new ATM("08", abc);
ATM aTM9 = new ATM("09", abc);
ATM aTM10 = new ATM("10", abc);
ATM aTM11 = new ATM("11", abc);

ccb.addATM(aTM1);
ccb.addATM(aTM2);
ccb.addATM(aTM3);
ccb.addATM(aTM4);
icbc.addATM(aTM5);
icbc.addATM(aTM6);
abc.addATM(aTM11);
abc.addATM(aTM10);
abc.addATM(aTM9);
abc.addATM(aTM8);
abc.addATM(aTM7);

User Yangguo = new User("360101200102122324", "杨过", "13856223254");
User Guojing = new User("360101200012302552", "郭靖", "13800021124");
User Zhangwuji = new User("360502199805163221", "张无忌", "13952110011");
User Weixiaobao = new User("360201200513243326", "韦小宝", "13025996587");
User Zhangsanfeng = new User("362323200356465784", "张三丰", "19523465467");
User Linhuchong = new User("362323200549847362", "令狐冲", "19536345634");
User Qiaofeng = new User("365959466521535675", "乔峰", "19468534765");
User Hongqigong = new User("325656674658874938", "洪七公", "19564347635");
Account ccbAcc1 = new DebitAccount("3217000010041315709", 10000.00, Yangguo, ccb, "jieji");
Account ccbAcc2 = new DebitAccount("3217000010041315715", 10000.00, Yangguo, ccb, "jieji");
Account ccbAcc3 = new DebitAccount("3217000010051320007", 10000.00, Guojing, ccb, "jieji");
Account icbcAcc1 = new DebitAccount("3222081502001312389", 10000.00, Zhangwuji, icbc, "jieji");
Account icbcAcc2 = new DebitAccount("3222081502001312390", 10000.00, Zhangwuji, icbc, "jieji");
Account icbcAcc3 = new DebitAccount("3222081502001312399", 10000.00, Zhangwuji, icbc,"jieji");
Account icbcAcc4 = new DebitAccount("3222081502051320785", 10000.00, Weixiaobao, icbc,"jieji");
Account icbcAcc5 = new DebitAccount("3222081502051320786", 10000.00, Weixiaobao, icbc, "jieji");
Account ccbAcc4 = new CreditAccount("3640000010045442002", 10000.00, Zhangsanfeng, ccb, "daiji");
Account icbcAcc6 = new CreditAccount("3640000010045441009", 10000.00, Linhuchong, icbc, "daiji");
Account abcAcc1 = new CreditAccount("3630000010033431001", 10000.00, Qiaofeng, abc,"daiji");
Account abcAcc2 = new CreditAccount("3630000010033431008", 10000.00, Hongqigong, abc,"daiji");

ccb.addAccount(ccbAcc1);
ccb.addAccount(ccbAcc2);
ccb.addAccount(ccbAcc3);
ccb.addAccount(ccbAcc4);
icbc.addAccount(icbcAcc1);
icbc.addAccount(icbcAcc2);
icbc.addAccount(icbcAcc3);
icbc.addAccount(icbcAcc4);
icbc.addAccount(icbcAcc5);
icbc.addAccount(icbcAcc6);
abc.addAccount(abcAcc1);
abc.addAccount(abcAcc2);

Yangguo.addAccount(ccbAcc1);
Yangguo.addAccount(ccbAcc2);
Guojing.addAccount(ccbAcc3);
Zhangwuji.addAccount(icbcAcc1);
Zhangwuji.addAccount(icbcAcc2);
Zhangwuji.addAccount(icbcAcc3);
Weixiaobao.addAccount(icbcAcc4);
Weixiaobao.addAccount(icbcAcc5);
Zhangsanfeng.addAccount(ccbAcc4);
Linhuchong.addAccount(icbcAcc6);
Qiaofeng.addAccount(abcAcc1);
Hongqigong.addAccount(abcAcc2);

Card ccbCard1 = new Card("6217000010041315709", "88888888", ccbAcc1);
Card ccbCard2 = new Card("6217000010041315715", "88888888", ccbAcc1);
Card ccbCard3 = new Card("6217000010041315718", "88888888", ccbAcc2);
Card ccbCard4 = new Card("6217000010051320007", "88888888", ccbAcc3);
Card icbcCard5 = new Card("6222081502001312389", "88888888", icbcAcc1);
Card icbcCard6 = new Card("6222081502001312390", "88888888", icbcAcc2);
Card icbcCard7 = new Card("6222081502001312399", "88888888", icbcAcc3);
Card icbcCard8 = new Card("6222081502001312400", "88888888", icbcAcc3);
Card icbcCard9 = new Card("6222081502051320785", "88888888", icbcAcc4);
Card icbcCard10 = new Card("6222081502051320786", "88888888", icbcAcc5);
Card ccbCard11 = new Card("6640000010045442002", "88888888", ccbAcc4);
Card ccbCard12 = new Card("6640000010045442003", "88888888", ccbAcc4);
Card icbcCard13 = new Card("6640000010045441009", "88888888", icbcAcc6);
Card abcCard14 = new Card("6630000010033431001", "88888888", abcAcc1);
Card abcCard15 = new Card("6630000010033431008", "88888888", abcAcc2);

ccbAcc1.addCard(ccbCard1);
ccbAcc1.addCard(ccbCard2);
ccbAcc2.addCard(ccbCard3);
ccbAcc3.addCard(ccbCard4);
icbcAcc1.addCard(icbcCard5);
icbcAcc2.addCard(icbcCard6);
icbcAcc3.addCard(icbcCard7);
icbcAcc3.addCard(icbcCard8);
icbcAcc4.addCard(icbcCard9);
icbcAcc5.addCard(icbcCard10);
ccbAcc4.addCard(ccbCard11);
ccbAcc4.addCard(ccbCard12);
icbcAcc6.addCard(icbcCard13);
abcAcc1.addCard(abcCard14);
abcAcc2.addCard(abcCard15);

StringBuilder sb = new StringBuilder();

String data;

while (!((data = input.nextLine()).equals("#"))) {
sb.append(data + "\n");
}

String[] dt = sb.toString().split("\n");
for (int i = 0; i < dt.length; i++) {
String[] dataLine = dt[i].toString().split("\\s+");

if (dataLine.length == 1) {
GetBalance gb = new GetBalance(unionPay);
System.out.println("业务:查询余额 " + String.format("¥%.2f", gb.getBalance(dataLine[0])));
} else {
Withdraw wd = new Withdraw(unionPay, dataLine[0], dataLine[1], dataLine[2],
Double.parseDouble(dataLine[3]));
wd.withdraw();
}
}
}
}

class Bank {
private String bankNO;
private String bankName;
private ArrayList<Account> accountList = new ArrayList<Account>();
private ArrayList<ATM> ATMList = new ArrayList<ATM>();

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

public Bank(String bankNO, String bankName) {
super();
this.bankNO = bankNO;
this.bankName = bankName;
}

public String getBankNO() {
return bankNO;
}

public void setBankNO(String bankNO) {
this.bankNO = bankNO;
}

public String getBankName() {
return bankName;
}

public void setBankName(String bankName) {
this.bankName = bankName;
}

public void addAccount(Account account) {
this.accountList.add(account);
}

public void removeAccount(Account account) {
this.accountList.remove(account);
}

public void addATM(ATM aTM) {
this.ATMList.add(aTM);
}

public void removeATM(ATM aTM) {
this.ATMList.remove(aTM);
}

public ArrayList<Account> getAccountList() {
return accountList;
}

public void setAccountList(ArrayList<Account> accountList) {
this.accountList = accountList;
}

public ArrayList<ATM> getATMList() {
return ATMList;
}

public void setATMList(ArrayList<ATM> aTMList) {
ATMList = aTMList;
}
}

class User {
private String ID;
private String name;
private String Phone;
ArrayList<Account> list = new ArrayList<Account>();

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

public User(String iD, String name, String phone) {
super();
ID = iD;
this.name = name;
Phone = phone;
}

public String getID() {
return ID;
}

public void setID(String iD) {
ID = iD;
}

public String getPhone() {
return Phone;
}

public void setPhone(String phone) {
Phone = phone;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void addAccount(Account account) {
this.list.add(account);
}

public void removeAccount(Account account) {
this.list.remove(account);
}
}
class Account {
private String accountNO;
private double balance = 0;
private User user = null;
private Bank bank = null;
private static ArrayList<Card> list = new ArrayList<Card>();
private String accounttype;

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

public Account(String accountNO, double balance, User user, Bank bank,String accounttype) {
super();
this.accountNO = accountNO;
this.balance = balance;
this.user = user;
this.bank = bank;
this.accounttype = accounttype;
}

public void addCard(Card card) {
list.add(card);
}

public void removeCard(Card card) {
list.remove(card);
}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public String getAccountNO() {
return accountNO;
}

public void setAccountNO(String accountNO) {
this.accountNO = accountNO;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public Bank getBank() {
return bank;
}

public void setBank(Bank bank) {
this.bank = bank;
}

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

public void setList(ArrayList<Card> list) {
Account.list = list;
}


public String getAccounttype() {
return accounttype;
}

public void setAccounttype(String accounttype) {
this.accounttype = accounttype;
}

public static Account getAmountbyCardNO(String cardNO) {
Iterator<Card> cardItr = Account.list.iterator();
Card card = null;

while(cardItr.hasNext()) {
card = cardItr.next();
if(card.getCardNO().equals(cardNO)) {
return card.getAccount();
}
}

return null;
}
}
class DebitAccount extends Account{

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

public DebitAccount(String accountNO, double balance, User user, Bank bank, String accounttype) {
super(accountNO, balance, user, bank, accounttype);
// TODO Auto-generated constructor stub
}

 



}
class CreditAccount extends Account{

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

public CreditAccount(String accountNO, double balance, User user, Bank bank, String accounttype) {
super(accountNO, balance, user, bank, accounttype);
// TODO Auto-generated constructor stub
}

 




}
class Card {
private String cardNO;
private String cardPassword;
private Account account = null;

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

public Card(String cardNO, String cardPassword, Account account) {
super();
this.cardNO = cardNO;
this.cardPassword = cardPassword;
this.account = account;
}

public String getCardNO() {
return cardNO;
}

public void setCardNO(String cardNO) {
this.cardNO = cardNO;
}

public String getCardPassword() {
return cardPassword;
}

public Account getAccount() {
return account;
}

public void setAccount(Account account) {
this.account = account;
}

public void setCardPassword(String cardPassword) {
this.cardPassword = cardPassword;
}

public boolean checkCard() {
Pattern p = Pattern.compile("\\d{16}+");
Matcher m = p.matcher(this.cardNO);
return m.matches();
}

public boolean checkPassword(String password) {
return this.cardPassword.equals(password);
}
}

class ATM {
private String ATMID;
private Bank bank = null;

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

public ATM(String aTMID, Bank bank) {
super();
ATMID = aTMID;
this.bank = bank;
}

public String getATMID() {
return ATMID;
}

public void setATMID(String aTMID) {
ATMID = aTMID;
}

public Bank getBank() {
return bank;
}

public void setBank(Bank bank) {
this.bank = bank;
}
}

class Withdraw {
private UnionPay unionPay;
private String cardNO;
private String cardPassword;
private String ATMID;
private double amount;
ATM atm;
public Withdraw() {
super();
// TODO Auto-generated constructor stub
}

public Withdraw(UnionPay unionPay, String cardNO, String cardPassword, String aTMID, double amount) {
super();
this.unionPay = unionPay;
this.cardNO = cardNO;
this.cardPassword = cardPassword;
ATMID = aTMID;
this.amount = amount;
}

public Withdraw(String cardNO, String cardPassword, String aTMID, double amount) {
super();
this.cardNO = cardNO;
this.cardPassword = cardPassword;
ATMID = aTMID;
this.amount = amount;
}


public void withdraw() {
/**
* 校验该卡是否存在
*/
Card card = ValidateData.getCardbyCardNO(unionPay, cardNO);
if(card == null) {
System.out.println("Sorry,this card does not exist.");
System.exit(0);
}

/**
* 校验ATM是否存在
*/
ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID);
atm = aTM;
if(aTM == null) {
System.out.println("Sorry,the ATM's id is wrong.");
System.exit(0);
}

Account account = Account.getAmountbyCardNO(cardNO);
double balance = account.getBalance();

/**
* 校验卡密码是否正确
*/
if(!card.getCardPassword().equals(cardPassword)) {
System.out.println("Sorry,your password is wrong.");
System.exit(0);
}
/**
* 校验取款金额是否大于余额
*/
double fees = 0;
if (aTM.getBank().getBankNO().equals("1001")){
fees = 0.02;
}
if (aTM.getBank().getBankNO().equals("1002")){
fees = 0.03;
}
if (aTM.getBank().getBankNO().equals("1003")){
fees = 0.04;
}
double a = 0;
if(account.getAccounttype().equals("jieji")&&amount > balance) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
if(card.getAccount().getAccounttype().equals("daiji")&&amount-balance>50000) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
if(amount == balance&&card.getAccount().getAccounttype().equals("jieji")&&account.getBank().getBankNO() != aTM.getBank().getBankNO()) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
if (amount>balance&&card.getAccount().getAccounttype().equals("daiji")&& balance>=0) {
a=(amount - balance)*0.05;
}
if (amount>balance&&card.getAccount().getAccounttype().equals("daiji")&&balance<0) {
a=amount*0.05;
}
if (account.getBank().getBankNO() != aTM.getBank().getBankNO()) {
a=a+amount*fees;
}
account.setBalance(balance -amount - a);
if(amount >= 0) {
showResult(account,1);
}else {
showResult(account,0);
}

}

public void showResult(Account account,int flag) {
String type = "";
if(flag == 1) {
type = "取款";
}else {
type = "存款";
amount *= -1;
}
ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID);
String userName = account.getUser().getName();
String bankName = aTM.getBank().getBankName();
System.out.println("业务:取款 "+userName + "在" +
bankName + "的" + ATMID + "号ATM机上" + type + String.format("¥%.2f", amount));
System.out.println("当前余额为" + String.format("¥%.2f", account.getBalance()));
}
}

class ValidateData {
/**
* 校验卡号是否存在
* @param unionPay
* @param cardNO
* @return
*/
public static Card getCardbyCardNO(UnionPay unionPay,String cardNO) {
Card card = null;
Iterator<Bank> bankItr = unionPay.getBankList().iterator();

while(bankItr.hasNext()) {
ArrayList<Account> accountList = bankItr.next().getAccountList();
Iterator<Account> accountItr = accountList.iterator();
while(accountItr.hasNext()) {
ArrayList<Card> cardList = accountItr.next().getList();
Iterator<Card> cardItr = cardList.iterator();
while(cardItr.hasNext()) {
card = cardItr.next();
if(card.getCardNO().equals(cardNO)) {
return card;
}
}
}
}
return null;
}

/**
* 校验ATM ID是否存在
* @param unionPay
* @param ATMID
* @return
*/
public static ATM getATMbyATMID(UnionPay unionPay,String ATMID) {
Iterator<Bank> bankItr = unionPay.getBankList().iterator();
Bank bank = null;
ATM aTM = null;

while(bankItr.hasNext()) {
bank = bankItr.next();
Iterator<ATM> aTMItr = bank.getATMList().iterator();

while(aTMItr.hasNext()) {
aTM = aTMItr.next();
if(aTM.getATMID().equals(ATMID)) {
return aTM;
}
}
}
return null;
}
}
class UnionPay {
private ArrayList<Bank> bankList = new ArrayList<Bank>();

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

public UnionPay(ArrayList<Bank> bankList) {
super();
this.bankList = bankList;
}

public ArrayList<Bank> getBankList() {
return bankList;
}

public void setBankList(ArrayList<Bank> bankList) {
this.bankList = bankList;
}

public void addBank(Bank bank) {
this.bankList.add(bank);
}

public void removeBank(Bank bank) {
this.bankList.remove(bank);
}
}

class GetBalance {
private UnionPay unionPay;

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

public GetBalance(UnionPay unionPay) {
super();
this.unionPay = unionPay;
}

public double getBalance(String cardNO) {
return ValidateData.getCardbyCardNO(unionPay, cardNO).getAccount().getBalance();
}
}

类图:

复杂度:

Metrics Details For File 'Main.java'
--------------------------------------------------------------------------------------------

Parameter Value
========= =====
Project Directory C:\Users\zhaoboyi\eclipse-workspace\ke\src\农夫过河\
Project Name
Checkpoint Name Baseline
File Name Main.java
Lines 697
Statements 368
Percent Branch Statements 6.0
Method Call Statements 158
Percent Lines with Comments 3.0
Classes and Interfaces 9
Methods per Class 7.44
Average Statements per Method 3.91
Line Number of Most Complex Method 507
Name of Most Complex Method Withdraw.withdraw()
Maximum Complexity 23
Line Number of Deepest Block 137
Maximum Block Depth 4
Average Block Depth 1.78
Average Complexity 1.42

--------------------------------------------------------------------------------------------
Most Complex Methods in 9 Class(es): Complexity, Statements, Max Depth, Calls

Account.Account() 1, 6, 2, 1
Account.Account() 1, 1, 2, 1
Account.addCard() 1, 1, 2, 1
Account.getAccountNO() 1, 1, 2, 0
Account.getAccounttype() 1, 1, 2, 0
Account.getAmountbyCardNO() 3, 7, 4, 6
Account.getBalance() 1, 1, 2, 0
Account.getBank() 1, 1, 2, 0
Account.getList() 1, 1, 2, 0
Account.getUser() 1, 1, 2, 0
Account.removeCard() 1, 1, 2, 1
Account.setAccountNO() 1, 1, 2, 0
Account.setAccounttype() 1, 1, 2, 0
Account.setBalance() 1, 1, 2, 0
Account.setBank() 1, 1, 2, 0
Account.setList() 1, 1, 2, 0
Account.setUser() 1, 1, 2, 0
ATM.ATM() 1, 3, 2, 1
ATM.ATM() 1, 1, 2, 1
ATM.getATMID() 1, 1, 2, 0
ATM.getBank() 1, 1, 2, 0
ATM.setATMID() 1, 1, 2, 0
ATM.setBank() 1, 1, 2, 0
Bank.addAccount() 1, 1, 2, 1
Bank.addATM() 1, 1, 2, 1
Bank.Bank() 1, 3, 2, 1
Bank.Bank() 1, 1, 2, 1
Bank.getAccountList() 1, 1, 2, 0
Bank.getATMList() 1, 1, 2, 0
Bank.getBankName() 1, 1, 2, 0
Bank.getBankNO() 1, 1, 2, 0
Bank.removeAccount() 1, 1, 2, 1
Bank.removeATM() 1, 1, 2, 1
Bank.setAccountList() 1, 1, 2, 0
Bank.setATMList() 1, 1, 2, 0
Bank.setBankName() 1, 1, 2, 0
Bank.setBankNO() 1, 1, 2, 0
Card.Card() 1, 4, 2, 1
Card.Card() 1, 1, 2, 1
Card.checkCard() 1, 3, 2, 3
Card.checkPassword() 1, 1, 2, 1
Card.getAccount() 1, 1, 2, 0
Card.getCardNO() 1, 1, 2, 0
Card.getCardPassword() 1, 1, 2, 0
Card.setAccount() 1, 1, 2, 0
Card.setCardNO() 1, 1, 2, 0
Card.setCardPassword() 1, 1, 2, 0
CreditAccount.CreditAccount() 1, 1, 2, 1
CreditAccount.CreditAccount() 1, 1, 2, 1
DebitAccount.DebitAccount() 1, 1, 2, 1
DebitAccount.DebitAccount() 1, 1, 2, 1
Main.main() 5, 114, 4, 62
User.addAccount() 1, 1, 2, 1
User.getID() 1, 1, 2, 0
User.getName() 1, 1, 2, 0
User.getPhone() 1, 1, 2, 0
User.removeAccount() 1, 1, 2, 1
User.setID() 1, 1, 2, 0
User.setName() 1, 1, 2, 0
User.setPhone() 1, 1, 2, 0
User.User() 1, 4, 2, 1
User.User() 1, 1, 2, 1
Withdraw.withdraw() 23, 42, 3, 52
Withdraw.Withdraw() 1, 5, 2, 1
Withdraw.Withdraw() 1, 6, 2, 1
Withdraw.Withdraw() 1, 1, 2, 1

--------------------------------------------------------------------------------------------
Block Depth Statements

0 14
1 92
2 228
3 29
4 5
5 0
6 0
7 0
8 0
9+ 0
--------------------------------------------------------------------------------------------

复杂度分析:最大复杂度为4,平均复杂度为1.42,基本达到复杂度要求。

解释:本题通过题指导书要求和老师所给的代码进行修改,通过增加数据和书写算法进行输出,从而达到要求。

心得:本题考验类的书写和调用和对数据进行存储,并用自己的逻辑思维能力进行输出,锻炼了自己的编程能力。

设计思路分析总结:本题主要是根据老师所给的题目集八的源码进行代码的修改并进行数据的添加,主要是区分借记卡和贷记卡,通过增加判断增加算法进行代码的输出,从而达到要求。本题我认为代码总体达到基本要求,但我的知识点不够牢固,需要增加练习。

三、     踩坑心得

 

 

 本题因为没有进行头文件的书写,从而出现错误。

 

 本处因为我对输出所缺少空格,从而输出存在错误。

 

 本题因为大括号位置放错,导致代码错误。

 

本题因为存储值错误,导致输出有误。

 

本题因为对算法的书写错误,倒是判断错误,从而导致输出错误。

心得:在编译过程中,需要思路清晰,而且书写代码不能半途而废,在书写代码的时候,要做到坚持把代码写完,不能断断续续。

四、     改进建议

  1. 要增加自己书写的规范性。
  2. 对于题目集七7-1和题目集七7-2算法进行简化,从而使代码的长度得到优化。
  3. 对于题目集八,代码还不是很完善,需要进一步改进和加强。
  4. 对于题目集九,学习老师的思路,从而增强自己的逻辑思维能力。
  5. 遵循对代码的书写规则,使代码的书写有一定的逻辑性,简洁易懂。

五、     总结

通过本阶段的三次题目集,我增强了对类的书写调用,并且学会的链表的运用和数据的存储,但还是不是很熟练,需要继续巩固练习。老师认真负责,积极主动帮助同学答疑解惑,并且个人知识过硬。课程的教学模式有一定的条理性,根据学生的情况布置上课所学知识。课程作业布置的非常有针对性,使学生可以对学习的知识进行运用,老师也可以根据学生的作业情况进行针对性的改进。实验课方面,老师通过进一步发布农夫过河的题目,是同学可以根据要求达到实验题目要求,增强了学生的逻辑思维能力。课上及课下组织方式,老师通过发布题目集完成情况,针对性的引导同学进行下一步编程练习,并且布置线上学习,线上线下结合,是同学有更好的学习效果,希望老师可以增加线上教学的督促和学习,提高学生学习积极性,从而使学生有更大的进步。

posted @ 2021-06-17 19:30  愤怒的老煎饼  阅读(116)  评论(0)    收藏  举报