diff --git a/cas2xb3_A2_jiang_zj/src/sort/Heap.java b/cas2xb3_A2_jiang_zj/src/sort/Heap.java index 8c7e860..7ed4de8 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/Heap.java +++ b/cas2xb3_A2_jiang_zj/src/sort/Heap.java @@ -1,8 +1,9 @@ package sort; - +/**The {@code Heap} class provides a static methods for heap sort + */ public class Heap { /** - * heap sort using Comparable + * Heap sort using comparable object. * @param x - the input array containing scores of words that need to be sorted. */ public static void sortHeap ( Comparable[] x) { @@ -14,7 +15,12 @@ public static void sortHeap ( Comparable[] x) { sink(x, 1, N); } } - + /** + * Helper functions to restore the heap invariant. + * @param x - the input array containing times of words that need to be sorted. + * @param k - the index of element needs to be sinked. + * @param n - the size of the input array + */ private static void sink(Comparable[] x, int k, int N) { while (2*k <= N) { int j = 2*k; @@ -26,22 +32,45 @@ private static void sink(Comparable[] x, int k, int N) { k = j; } } - + /** + * Compares two comparable x[i-1] and x[j-1]. + * @param x - the array for compared. + * @param i - the index of first comparable to be compared + * @param j - the index of second comparable to be compared + * @return {@code true} if comparable a[i-1] bigger than a[j-1]; + * {@code false} otherwise + */ private static boolean higher (Comparable[] x, int i, int j) { return x[i-1].compareTo(x[j-1]) > 0; } - + /** + * Compares two comparable a and b. + * @param a - the first comparable to be compared + * @param b - the second comparable to be compared + * @return {@code true} if comparable a bigger than b; + * {@code false} otherwise + */ private static boolean higher (Comparable a, Comparable b) { return (a.compareTo(b) > 0); } - + /** + * Exchange x[i-1] and x[j-1] + * @param x - the input array containing times of words that need to be sorted. + * @param i - the index of the first element for the array a to be exchanged. + * @param j - the index of the second element for the array a to be exchanged. + */ private static void swap(Comparable[] x, int i, int j) { Comparable t = x[i-1]; x[i-1] = x[j-1]; x[j-1] = t; } - // check if the array is sorted + /** + * Check if array is sorted - useful for debugging. + * @param x - the array for sort + * @return {@code true} if array a is sorted; + * {@code false} otherwise + */ public static boolean isSorted(Word[] x) { for (int i=0; i b.getScore()) return true; return false; } - + /** + * Compares two comparable object a and b. + * @param a - the first comparable to be compared + * @param b - the second comparable to be compared + * @return {@code true} if comparable a has bigger score than b; + * {@code false} otherwise + */ private static boolean higher(Comparable a, Comparable b) { if (((Word) a).getScore() >= ((Word) b).getScore()) return true; return false; } - // exchange two Wrod's in an array + /** + * Exchange x[a] and x[b]. + * @param x - the input array containing times of words that need to be sorted. + * @param a - the index of the first element for the array a to be exchanged. + * @param b - the index of the second element for the array a to be exchanged. + */ private static void swap(Comparable[] x, int a, int b) { Comparable t = x[a]; x[a] = x[b]; x[b] = t; } - // check if the array is sorted + /** + * Check if array is sorted - useful for debugging. + * @param x - the array for sort + * @return {@code true} if array a is sorted; + * {@code false} otherwise + */ public static boolean isSorted(Word[] x) { for (int i=0; i mid) a[k] = aux[j++]; @@ -30,11 +40,22 @@ else if (higher(aux[j], aux[i])) a[k] = aux[i++]; } } - + /** + * Compares two comparable a and b. + * @param a - the first comparable to be compared + * @param b - the second comparable to be compared + * @return {@code true} if comparable a bigger than b; + * {@code false} otherwise + */ private static boolean higher(Comparable a, Comparable b) { return a.compareTo(b) > 0; } - + /** + * Merge sort a[lo..hi] using auxiliary array aux[lo..hi] + * @param a - the input array containing scores of words that need to be sorted. + * @param lo - the beginning index of array a for merge + * @param hi - the ending index of array a for merge + */ private static void sort(Comparable[] a, int lo, int hi) { // sort a[lo..hi] if (hi <= lo) @@ -45,7 +66,12 @@ private static void sort(Comparable[] a, int lo, int hi) { merge(a, lo, mid, hi); } - // check if the array is sorted + /** + * Check if array is sorted - useful for debugging. + * @param x - the array for sort + * @return {@code true} if array x is sorted; + * {@code false} otherwise + */ public static boolean isSorted(Word[] x) { for (int i=0; i{ private String word; private int score; - + /** + * Initializes a new Word (word, score). + * @param w The String for word + * @param s The int for score + */ public Word(String w, int s) { this.word = w; this.score = s; } - + /** + * Return the score + * @return The score + */ public int getScore() { return score; } - + /** + * Set the score + * @param s The score + */ public void setScore(int s) { this.score = s; } - + /** + * Return the word + * @return The word + */ public String getWord() { return word; } - + /** + * Set the word + * @param w The word + */ public void setWord(String w) { this.word = w; } - + /** + * Compares this word to other word according to their score. + * @param w - The other word + * @return {@code 0} if this score equals {@code w}; + * {@code 1} if this score bigger than {@code w}; + * {@code -1} otherwise + */ @Override public int compareTo(Word w) { @@ -42,7 +66,10 @@ else if (this.score < w.score) // with the same ranking, return 0 return 0; } - + /** + * Return a string representation of this word. + * @return a string representation of this word in the format {word, score}. + */ public String toString() { return "{" + this.word + "," + this.score + "}"; diff --git a/cas2xb3_A2_jiang_zj/src/sort/WordTest.java b/cas2xb3_A2_jiang_zj/src/sort/WordTest.java index d87c55b..a40916d 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/WordTest.java +++ b/cas2xb3_A2_jiang_zj/src/sort/WordTest.java @@ -4,52 +4,80 @@ import org.junit.Before; import org.junit.Test; - +/** + *The {@code WordTest} class provides JUnit tests for word class + */ public class WordTest { private Word a; private Word b; private Word c; - + /** + * Create words for test + */ @Before public void setup() { a = new Word("Batman", 76); b = new Word("Iron man", 76); c = new Word("Spiderman", 98); } - + /** + * Tests for word class getScore function. + * If the score of word is equal to the word use getScore function, + * assertEquals() function will pass + */ @Test public void testGetScore() { assertEquals(76, a.getScore()); assertEquals(76, b.getScore()); assertEquals(98, c.getScore()); } - + /** + * Tests for word class setScore function. + * If the score of word is equal to the word use setScore function, + * assertEquals() function will pass + */ @Test public void testSetScore() { a.setScore(78); assertEquals(78, a.getScore()); } - + /** + * Tests for word class getWcore function. + * If the word is equal to the word use getWord function, + * assertEquals() function will pass + */ @Test public void testGetWord() { assertEquals("Batman", a.getWord()); assertEquals("Iron man", b.getWord()); assertEquals("Spiderman", c.getWord()); } - + /** + * Tests for word class setWcore function. + * If the word is equal to the word use setWord function, + * assertEquals() function will pass + */ @Test public void testSetWord() { a.setWord("Bruce Wayne"); assertEquals("Bruce Wayne", a.getWord()); } - + /** + * Tests for word class compareTo function. + * If the score of word is less than the other word, return -1 + * assertEquals() function will pass + */ @Test public void testCompareTo() { assertEquals(0, a.compareTo(b)); assertEquals(1, c.compareTo(a)); assertEquals(-1, b.compareTo(c)); } - + /** + * Tests for word class toString function. + * If the word format is same to the word use toString function + * assertEquals() function will pass + */ @Test public void testToString() { assertEquals("{Batman,76}", a.toString());