Quote.java


Below is the syntax highlighted version of Quote.java.


 /*************************************************************
  *  Compilation:  javac Quote.java
  *  Execution:    java Quote
  *  Dependencies: none
  *
  *  Code for a null terminated linked list. Each node of the linked 
  *  list is a Card class defined within Quote.
  *  Web Exercise 4.3.1
  *************************************************************/
public class Quote {

   // the first card in null-terminated linked list
   private Card start;

   // helper linked-list data type
   private class Card {
      private String word;
      private Card next;

      public Card(String word) {
         this.word = word;
         this.next = null;
      }
   }
   
   // constructor - create an empty quote
   public Quote() {
       start = null;
   }
   
   // add the word w to the end of the quote
   public void addWord(String w) {
       Card newWord = new Card(w);

       // degenerate case when w is first word
       if (start == null) start = newWord;
 
       // otherwise, traverse list until card points to last word
       else {
           Card card = start; 
           while(card.next != null) {
               card = _________________;
           }

           // add card for new word to end of list
           _________________ = newWord;
       } 
   }
   
   // number of words in the quote
   public int count() {
       int total = 0;
       for (Card card = start; ___________________;__________________)
           total++;
       return total;
   }

   // return the ith word where i = 1 is first word in quote
   public String getWord(int i) {
       // check for less than i words in quote or invalid index
       if (count() < i || i <= 0) {
           throw new RuntimeException("index out of bounds");
       }
       
       Card card = start;
       for (int count = 1;  __________________; __________________)
           card = card.next;
       return ___________________________;
   }

   // insert w after the ith word, where i = 1 is the first word 
   public void insertWord(int i, String w) {
       // check for less than i words in quote or invalid index
       if (count() < i || i <= 0) 
           throw new RuntimeException("index out of bounds");

       // make Card for the new word, place it after the ith card
       Card newWord = _________________________
       Card card = start;
       for (int j = 1; j < i; j++) {card = card.next; }
       ________________________________
       ________________________________
   }

   // string representation of the quote   
   public String toString(){
       String s = "";
       for (Card card = start; card != null; card = card.next)
           s = s + card.word + " ";
       return s;
   }

    public static void main(String[] args) { 
        Quote q = new Quote();
        q.addWord("A");
        q.addWord("rose");
        q.addWord("is");
        q.addWord("a");
        q.addWord("rose.");
        StdOut.println(q);
        StdOut.println(q.count());
        StdOut.println(q.getWord(2));
        q.insertWord(3, "just");
        StdOut.println(q);
        StdOut.println(q.count());
    }
}