Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1.  
  2. /**
  3. * represents the ArrayList of IndexEntries
  4. * that gets written to the file
  5. */
  6. import java.util.*;
  7.  
  8. public class DocumentIndex extends ArrayList<IndexEntry>
  9. {
  10.  
  11.  
  12. /**constructors - one no args, one where capacity is plugged in*/
  13. public DocumentIndex()
  14. {
  15. super();
  16. }
  17.  
  18. public DocumentIndex(int c)
  19. {
  20. super(c);
  21. }
  22.  
  23.  
  24. /**addWord - two parameters - String word and int num
  25.  
  26. 1. Call foundOrInserted method to figure out position of the IndexEntry
  27. for word, and retrieve that IndexEntry from this DocumentIndex
  28.  
  29. 2. adds num to the IndexEntry that was either looked up
  30. or created in step one, by calling it's add(num) method.
  31.  
  32. */
  33. public void addWord(String w, int n)
  34. {
  35. IndexEntry entry = this.get(foundOrInserted(w));
  36. entry.add(n);
  37. }
  38.  
  39.  
  40. /**
  41. * foundOrInserted
  42. * Tried to find an IndexEntry with a given word in this DocumentIndex.
  43. * If not found, inserts a new IndexEntry for word in the appropriate
  44. * place alphabetically. Returns the index of the found or inserted InexEntry.
  45. * use compareTo
  46. * example: "CAT.compareTo("AND")>0
  47. */
  48. private int foundOrInserted(String word) //returns the line number
  49. {
  50. int count=0;
  51. IndexEntry test = new IndexEntry(word);
  52. this.add(test);
  53. DocumentIndex d = new DocumentIndex();
  54. for(int x=0;x<d.size();x++)
  55. {
  56. count++;
  57. if(d.get(x).getWord().equals(test))
  58. return count;
  59.  
  60. }
  61. return d.size(); //change this line where it returns
  62. }
  63.  
  64.  
  65. //for each word in str, calls addWord(word, num)
  66. public void addAllWords(String str, int num)
  67. {
  68. String words[] = str.split("\\W+"); //splits according to whitespace
  69. for(String w: words)
  70. {
  71. if(w.length()>0)
  72. addWord(w, num);
  73. //fill in!!!
  74.  
  75. }
  76.  
  77.  
  78.  
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement