fork(4) download
  1. /**
  2.   Implements a sorted list of words
  3.  
  4.   @author David Magnusson
  5.   @version 11/13/15
  6.  
  7.   @author Period - 5
  8.   @author Assignment - Java Methods 13.4 Lab: Keeping Things in Order
  9.  
  10.   @author Sources - TODO list collaborators
  11.  */
  12. public class SortedWordList extends java.util.ArrayList<String>
  13. {
  14. /**
  15.   * Constructor
  16.   */
  17. public SortedWordList()
  18. {
  19. super();
  20. }
  21.  
  22. /**
  23.   * Constructor
  24.   * @param capacity the capacity of the list
  25.   */
  26. public SortedWordList(int capacity)
  27. {
  28. super(capacity);
  29. }
  30.  
  31.  
  32. public boolean contains(String word)
  33. {
  34. return (indexOf(word) >= 0);
  35. }
  36.  
  37.  
  38. public int indexOf( String word )
  39. {
  40. int left = 0, right = size() - 1, middle;
  41.  
  42. while ( left <= right )
  43. {
  44. // Take the index of the middle element between
  45. // "left" and "right":
  46.  
  47. middle = ( left + right ) / 2;
  48.  
  49. // Compare this element to the target value
  50. // and adjust the search range accordingly:
  51. int diff = word.compareToIgnoreCase( get( middle ) );
  52. if ( diff > 0 ) // target > a [middle]
  53. {
  54. left = middle + 1;
  55. }
  56. else if ( diff < 0 )
  57. {// target < a[middle]
  58. right = middle - 1;
  59. }
  60. else // target is equal to a[middle]
  61. {
  62. return middle;
  63. }
  64. }
  65. return -1;
  66. }
  67.  
  68. public String set(int i, String word)
  69. {
  70. if ( i > 0)
  71. {
  72. if (!word.equals( get(i-1) ) && word.compareTo( get(i-1) ) > 0)
  73. {
  74. if ( i < size() -1 )
  75. {
  76. if (!word.equals( get(i+1) ) && word.compareTo( get(i+1) ) < 0)
  77. {
  78. return super.set( i, word );
  79. }
  80. }
  81. return super.set( i, word );
  82. }
  83. }
  84. if (i == 0 && !word.equals( get(i+1) ) && word.compareTo( get(i+1) ) < 0)
  85. {
  86. return super.set( i, word );
  87. }
  88. throw new IllegalArgumentException("word =" + word + " i =" + i);
  89. // if (indexOf(word) == i )
  90. // {
  91. // return super.set( i, word );
  92. // }
  93. // else
  94. // {
  95. // throw new IllegalArgumentException("word =" + word + " i =" + i);
  96. // }
  97.  
  98.  
  99. }
  100.  
  101. /**
  102.   * Adds the word to SortedWordList at index i if it fits there
  103.   * alphabetically.
  104.   * @param i the index
  105.   * @param word the word to be added
  106.   */
  107. public void add(int i, String word)
  108. {
  109. if (size() == 0)
  110. {
  111. super.add( i, word );
  112. return;
  113. }
  114. if ( i > 0)
  115. {
  116. if (!word.equals( get(i-1) ) && word.compareTo( get(i-1) ) > 0)
  117. {
  118. if ( i < size() )
  119. {
  120. if (!word.equals( get(i) ) && word.compareTo( get(i) ) < 0)
  121. {
  122. super.add( i, word );
  123. return;
  124. }
  125. }
  126. super.add( i, word );
  127. return;
  128. }
  129. }
  130. if (i == 0 && !word.equals( get(i) ) && word.compareTo( get(i) ) < 0)
  131. {
  132. super.add(i, word );
  133. return;
  134. }
  135. throw new IllegalArgumentException("word =" + word + " i =" + i);
  136. }
  137.  
  138. public boolean add(String word)
  139. {
  140. int checkpoint = indexOf(word);
  141. if (contains(word))
  142. {
  143. return false;
  144. }
  145. if (size() == 0 )
  146. {
  147. super.add( word );
  148. return true;
  149. }
  150. if (size() == 1 && word.compareTo( get(0) ) > 0)
  151. {
  152. super.add( word );
  153. return true;
  154. }
  155. else if(word.compareTo( get(0) ) < 0)
  156. {
  157. add(0,word);
  158. return true;
  159. }
  160. if (size() == 2 && word.compareTo( get(1) ) > 0)
  161. {
  162. super.add( word );
  163. return true;
  164. }
  165. else if (word.compareTo( get(0) ) > 0)
  166. {
  167. add(1,word);
  168. return true;
  169. }
  170. else if (word.compareTo( get(0) ) < 0)
  171. {
  172. add(0, word);
  173. return true;
  174. }
  175.  
  176. add(checkpoint,word);
  177.  
  178. return true;
  179. }
  180.  
  181. public void merge(SortedWordList additionalWords)
  182. {
  183. // int original = size();
  184. // int additional = additionalWords.size();
  185. // addAll(additionalWords);
  186. // while ()
  187. }
  188. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:12: error: class SortedWordList is public, should be declared in a file named SortedWordList.java
public class SortedWordList extends java.util.ArrayList<String>
       ^
1 error
stdout
Standard output is empty