要讨论的类:先UML定义需要的方法 ---> ADT伪代码,确认操作和设计 ----> 考虑特殊情况 ---> 实现为Java接口(注意写好注释) ----> 写好用来测试的Java语句,更好的理解定义的方法 ---> 实现核心方法 -----> 测试核心方法 ---> 实现其他方法及测试。
分析ArrayBagDemo:
/**
* A demostration of the class ArrayBag
* @author Administrator
*
*/
public class ArrayBagDemo {
public static void main(String[] args) {
String[] contentsOfBag = {"A", "A", "B", "A", "C", "A"};
// Tests on an empty bag
BagInterface<String> aBag = new ArrayBag<>(contentsOfBag.length);
System.out.println("Testing an initially empty bag:");
testIsEmpty(aBag, true);
String[] testStrings1 = {"", "B"};
testFrequency(aBag, testStrings1);
testContains(aBag, testStrings1);
testRemove(aBag, testStrings1);
// Adding strings
System.out.println("Adding " + contentsOfBag.length + " strings to an initially empty bag "
+ "with the capacity to hold more than " + contentsOfBag.length + " strings:");
testAdd(aBag, contentsOfBag);
// Tests on a bag that is not empty
testIsEmpty(aBag, false);
String[] testStrings2 = {"A", "B", "C", "D", "A"};
testFrequency(aBag, testStrings2);
testContains(aBag, testStrings2);
// Removing strings
String[] testStrings3 = {"", "B", "A", "C", "Z"};
testRemove(aBag, testStrings3);
System.out.println("\nClearing the bag:");
aBag.clear();
testIsEmpty(aBag, true);
displayBag(aBag);
}
// Tests the method add.
public static void testAdd(BagInterface<String> aBag, String[] content) {
System.out.println("Adding ");
for(int index = 0; index < content.length; index++) {
aBag.add(content[index]);
System.out.print(content[index] + " ");
} // end for
System.out.println();
displayBag(aBag);
} // end testAdd
private static void testRemove(BagInterface<String> aBag, String[] tests) {
for (int index = 0; index < tests.length; index++) {
String aString = tests[index];
if (aString.equals("") || (aString == null)) {
// test remove()
System.out.println("\nRemoving a string from the bag:");
String removedString = aBag.remove();
System.out.println("remove() returns " + removedString);
}
else {
// test remove(aString)
System.out.println("\nRemoving \"" + aString + "\" from the bag:");
boolean result = aBag.remove(aString);
System.out.println("remove(\"" + aString + "\") returns " + result);
} // end if
displayBag(aBag);
} // end for
} // end testRemove
// Tests the method toArray while displaying the bag.
private static void displayBag(BagInterface<String> aBag) {
System.out.println("The bag contains " + aBag.getCurrentSize() +
" string(s), as follows:");
Object[] bagArray = aBag.toArray();
for (int index = 0; index < bagArray.length; index++) {
System.out.print(bagArray[index] + " ");
} // end for
System.out.println();
} // end diaplayBag
// Tests the method contains.
private static void testContains(BagInterface<String> aBag, String[] tests) {
System.out.println("\nTesting the method contains:");
for (int index = 0; index < tests.length; index++) {
String aString = tests[index];
if (!aString.equals("") && (aString != null)) {
System.out.println("Does this bag contain " + tests[index] +
" ? " + aBag.contains(aString));
} // end if
} // end for
} // end testContains
// Tests the method getFrequencyOf
private static void testFrequency(BagInterface<String> aBag, String[] tests) {
System.out.println("\nTesting the method getFrequencyOf:");
for (int index = 0; index < tests.length; index++) {
String aString = tests[index];
if (!aString.equals("") && (aString != null)) {
System.out.println("In this bag, the count of " + tests[index] +
" is " + aBag.getFrequencyOf(aString));
} // end if
} // end for
} // end testFrequency
// Tests the method isEmpty
// correctResult indicates what isEmpty should return.
private static void testIsEmpty(BagInterface<String> aBag, boolean correctResult) {
System.out.println("Testing isEmpty with ");
if(correctResult) {
System.out.println("an empty bag:");
}
else {
System.out.println("a bag that is not empty:");
} // end if
System.out.print("isEmpty finds the bag ");
if(correctResult && aBag.isEmpty()) {
System.out.println("empty: OK.");
}
else if(correctResult) {
System.out.println("not empty, but it is empty: ERROR.");
}
else if(!correctResult && aBag.isEmpty()) {
System.out.println("empty, but it is not empty: ERROR.");
}
else {
System.out.println("not empty: OK.");
} // if else
System.out.println();
} // end testIsEmpty
}
View Code
(1)判空isEmpty(): Boolean
设计函数testIsEmpty,将包和是否真正为空的事实作为参数,利用isEmpty函数和事实对bag做判断。
(2)测试getFrequencyOf
利用包和字符串数组做参数,判断数组中的元素在包中出现的次数并输出
(3)测试contains函数
参数为包和数组,判断包中是否包含数组中的元素并输出
(4)测试remove
将包和测试数组作为参数,如果当前数组元素是空串或是null,则测试remove(),否则用remove(T),最后再输出包中所有元素
(5)测试add
同样将包和测试数组作为参数,依次将数组中元素添加进包中,再输出包中所有元素
(6)都要使用的输出包中元素的函数displayBag
(7)main函数中:
数组中元素的个数、是否包含数组中元素、移除元素 ---> 向空包中添加元素,测试add函数 ---> 当包不为空时,继续测试:判空、元素个数、是否包含等函数 ---> 从包中移除元素,测试remove函数 ----> 将包清空,通过判空可以测试clear()
(8)总结
各个测试方法中均为将函数执行后输出包中元素,对于判空的测试需要使用事实条件继续对函数返回值进行详细判断说明。main函数测试时,先从空包开始测试除add以外的函数,再向包添加元素测试add,随后在包不为空的情况下继续测试所有函数,最后情况,通过判空测试clear函数。