From 321b4b142ace38a826a61a8facdffcb587fdff63 Mon Sep 17 00:00:00 2001 From: JiuweiW Date: Wed, 8 Mar 2017 10:13:58 -0500 Subject: [PATCH 01/14] Update Heap.java Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/Heap.java | 45 ++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/Heap.java b/cas2xb3_A2_jiang_zj/src/sort/Heap.java index 8c7e860..7de6229 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/Heap.java +++ b/cas2xb3_A2_jiang_zj/src/sort/Heap.java @@ -2,7 +2,7 @@ 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 +14,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 +31,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 Date: Wed, 8 Mar 2017 10:23:11 -0500 Subject: [PATCH 02/14] Update Insertion.java Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/Insertion.java | 36 ++++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/Insertion.java b/cas2xb3_A2_jiang_zj/src/sort/Insertion.java index 93444f5..3528cca 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/Insertion.java +++ b/cas2xb3_A2_jiang_zj/src/sort/Insertion.java @@ -2,27 +2,49 @@ public class Insertion { - // compare two Word's, if a has a higher rank than b + /** + * Compares two words a and b. + * @param a - the first word to be compared + * @param b - the second word to be compared + * @return {@code true} if word a has bigger score than b; + * {@code false} otherwise + */ private static boolean higher(Word a, Word b) { if (a.getScore() > 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 Date: Wed, 8 Mar 2017 10:24:20 -0500 Subject: [PATCH 03/14] Add Documentation Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/Heap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/Heap.java b/cas2xb3_A2_jiang_zj/src/sort/Heap.java index 7de6229..f919afa 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/Heap.java +++ b/cas2xb3_A2_jiang_zj/src/sort/Heap.java @@ -2,7 +2,7 @@ public class Heap { /** - * Heap sort using comparable object + * 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) { From 5be55d661c783553106b48085ff9e0727105e281 Mon Sep 17 00:00:00 2001 From: JiuweiW Date: Wed, 8 Mar 2017 10:24:52 -0500 Subject: [PATCH 04/14] Add Documentation Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/Insertion.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/Insertion.java b/cas2xb3_A2_jiang_zj/src/sort/Insertion.java index 3528cca..8fa389e 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/Insertion.java +++ b/cas2xb3_A2_jiang_zj/src/sort/Insertion.java @@ -28,7 +28,7 @@ private static boolean higher(Comparable a, Comparable b) { } /** - * Exchange x[a] and x[b] + * 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. @@ -106,7 +106,7 @@ public static void sortBinary ( Comparable[] x) { } } /** - * Tests for insertion sort + * Tests for insertion sort. * @param args the command-line arguments */ public static void main(String[] args){ From 93f9678ff06d9cd827a46f8a0fa5b2d4af29039a Mon Sep 17 00:00:00 2001 From: JiuweiW Date: Wed, 8 Mar 2017 10:32:48 -0500 Subject: [PATCH 05/14] Add Documentation Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/Merge.java | 35 +++++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/Merge.java b/cas2xb3_A2_jiang_zj/src/sort/Merge.java index 10a3ca5..0caec27 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/Merge.java +++ b/cas2xb3_A2_jiang_zj/src/sort/Merge.java @@ -13,7 +13,13 @@ public static void sortMerge (Comparable[] x ) { aux = new Comparable[x.length]; sort(x, 0, x.length - 1); } - + /** + * Stably merge a[lo .. mid] with a[mid+1 ..hi] using aux[lo .. hi] + * @param a - array for merge + * @param lo - the beginning index of array a for merge + * @param mid - the middling index of array a + * @param hi - the ending index of array a for merge + */ private static void merge(Comparable[] a, int lo, int mid, int hi) { // merge a[lo..mid] with a[mid+1..hi] int i = lo, j = mid+1; @@ -30,11 +36,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 +62,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 Date: Wed, 8 Mar 2017 10:40:39 -0500 Subject: [PATCH 06/14] Add Documentation Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/Word.java | 39 +++++++++++++++++++++----- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/Word.java b/cas2xb3_A2_jiang_zj/src/sort/Word.java index 100eb37..fa658d4 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/Word.java +++ b/cas2xb3_A2_jiang_zj/src/sort/Word.java @@ -3,33 +3,55 @@ public class Word implements Comparable{ 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 +64,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 + "}"; From adfb0c6f325f20a21e5ce0daad0fd2731c303008 Mon Sep 17 00:00:00 2001 From: JiuweiW Date: Wed, 8 Mar 2017 10:53:46 -0500 Subject: [PATCH 07/14] Add Documentation Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/SortTest.java | 50 ++++++++++++++++++---- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/SortTest.java b/cas2xb3_A2_jiang_zj/src/sort/SortTest.java index 5ae23a1..1642a0a 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/SortTest.java +++ b/cas2xb3_A2_jiang_zj/src/sort/SortTest.java @@ -22,14 +22,20 @@ public class SortTest { // create a array of Strings to store the output static String[] output = {"16", "256", "4096"}; - // a method written to test if an array is empty, not used anymore + /** + * Test the array x written to test is empty or not + */ public boolean isEmpty(Word[] x) { for (int i=0; i Date: Wed, 8 Mar 2017 11:04:02 -0500 Subject: [PATCH 08/14] Add Documentation Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/WordTest.java | 40 ++++++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/WordTest.java b/cas2xb3_A2_jiang_zj/src/sort/WordTest.java index d87c55b..f3a51ad 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/WordTest.java +++ b/cas2xb3_A2_jiang_zj/src/sort/WordTest.java @@ -9,47 +9,73 @@ 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()); From 0656968c9848cebfb085389a17cf9b82cbc6bd45 Mon Sep 17 00:00:00 2001 From: JiuweiW Date: Wed, 8 Mar 2017 11:53:25 -0500 Subject: [PATCH 09/14] Add Documentation Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/Heap.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/Heap.java b/cas2xb3_A2_jiang_zj/src/sort/Heap.java index f919afa..7ed4de8 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/Heap.java +++ b/cas2xb3_A2_jiang_zj/src/sort/Heap.java @@ -1,5 +1,6 @@ package sort; - +/**The {@code Heap} class provides a static methods for heap sort + */ public class Heap { /** * Heap sort using comparable object. From 552ec5ef89820744412f4743b8bbf662eb60501c Mon Sep 17 00:00:00 2001 From: JiuweiW Date: Wed, 8 Mar 2017 11:54:14 -0500 Subject: [PATCH 10/14] Add Documentation Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/Insertion.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/Insertion.java b/cas2xb3_A2_jiang_zj/src/sort/Insertion.java index 8fa389e..7536ba1 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/Insertion.java +++ b/cas2xb3_A2_jiang_zj/src/sort/Insertion.java @@ -1,5 +1,12 @@ package sort; - +/**The {@code Insertion} class provides static methods for sorting an + * array using insertion sort. + * This implementation makes ~ 1/2 n^2 compares and exchanges in + * the worst case, so it is not suitable for sorting large arbitrary arrays. + * More precisely, the number of exchanges is exactly equal to the number + * of inversions. So, for example, it sorts a partially-sorted array + * in linear time. + */ public class Insertion { /** From b14009035d65e94a1fd4a54d691d93bcde6f3da8 Mon Sep 17 00:00:00 2001 From: JiuweiW Date: Wed, 8 Mar 2017 11:55:06 -0500 Subject: [PATCH 11/14] Add Documentation Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/Merge.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/Merge.java b/cas2xb3_A2_jiang_zj/src/sort/Merge.java index 0caec27..2e0db2b 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/Merge.java +++ b/cas2xb3_A2_jiang_zj/src/sort/Merge.java @@ -1,5 +1,7 @@ package sort; - +/**The {@code Merge} class provides static methods for sorting an + * array using merge sort. + */ public class Merge { // the auxiliary array for merges @@ -23,8 +25,10 @@ public static void sortMerge (Comparable[] x ) { private static void merge(Comparable[] a, int lo, int mid, int hi) { // merge a[lo..mid] with a[mid+1..hi] int i = lo, j = mid+1; + // copy to aux[] for (int k=lo; k<=hi; k++) aux[k] = a[k]; + // merge back to a[] for (int k=lo; k<=hi; k++) { if (i > mid) a[k] = aux[j++]; From 510365a4531e6a8cc9a6eb014e8bb9a0a3792e49 Mon Sep 17 00:00:00 2001 From: JiuweiW Date: Wed, 8 Mar 2017 11:55:34 -0500 Subject: [PATCH 12/14] Add Documentation Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/SortTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/SortTest.java b/cas2xb3_A2_jiang_zj/src/sort/SortTest.java index 1642a0a..49bf750 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/SortTest.java +++ b/cas2xb3_A2_jiang_zj/src/sort/SortTest.java @@ -15,7 +15,9 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; - +/** + *The {@code SortTest} class provides JUnit tests for test sort + */ public class SortTest { // create a 2d array to store all the words private Word[][] inputWord = {new Word[16], new Word[256], new Word[4096]}; From ac496252e8a5df184d80de628da6166494881100 Mon Sep 17 00:00:00 2001 From: JiuweiW Date: Wed, 8 Mar 2017 11:56:03 -0500 Subject: [PATCH 13/14] Add Documentation Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/Word.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/Word.java b/cas2xb3_A2_jiang_zj/src/sort/Word.java index fa658d4..d8daf3c 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/Word.java +++ b/cas2xb3_A2_jiang_zj/src/sort/Word.java @@ -1,5 +1,7 @@ package sort; - +/** + * The @code Word class is an abstract data type called word that implements the Comparable interface. + */ public class Word implements Comparable{ private String word; private int score; From 8d182d5862f32bddccf0950fdd884095ed167af9 Mon Sep 17 00:00:00 2001 From: JiuweiW Date: Wed, 8 Mar 2017 11:56:28 -0500 Subject: [PATCH 14/14] Add Documentation Add Documentation --- cas2xb3_A2_jiang_zj/src/sort/WordTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cas2xb3_A2_jiang_zj/src/sort/WordTest.java b/cas2xb3_A2_jiang_zj/src/sort/WordTest.java index f3a51ad..a40916d 100644 --- a/cas2xb3_A2_jiang_zj/src/sort/WordTest.java +++ b/cas2xb3_A2_jiang_zj/src/sort/WordTest.java @@ -4,7 +4,9 @@ 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;