要討論的類:先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 }
(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函數。
