Byron's CSC212 Web Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
Byron's CSC212 Web Site  
 
 
Programming Challenge Archive

Word Dictionary
Dictionary App
 
 

 
  JavaApplication  -- Dictionary App

   // File header
   // ---------------------------------------------------
  
   // File:  Dictionary.java
   // Type:  java library file
   // Date:  Mon Dec 11, 2000
   // Name:  Byron Bahr
   // Line:  Model of a "dictionary"
  
   // Library class description
   // ---------------------------------------------------
  
   /*
      A "dictionary" will be modeled in terms of a name
       and a list of words.
   */
  
   // Package Identification
   // ---------------------------------------------------
  
      package white.words;
  
  
   // Required Packages
   // ---------------------------------------------------
  
      import blue.io.*;
  
   // Library class
   // ---------------------------------------------------
  
    public class Dictionary
   {
    // constants
       static private final int LIMIT = 1000;
  
    // instance variables
       private String name;
       private Word[] words;
       private int nrElements;
         
    // constructors
       public Dictionary(String s)
       {
         name = s;
         words = new Word[LIMIT];
         nrElements = 0;
       }
  
    // instance methods
       public void add(Word w)
       {
         nrElements++;
         words[nrElements-1] = w;
       }
  
       public void add(String s)
       {
          IN in = new IN(s);
          int nrWords = in.read_int();
          for ( int i = 0; i < nrWords; i++ )
          {
            String word = in.readString();
            int ns = in.read_int();
            String syllables[] = new String[ns];
            for ( int x = 1; x <= ns; x++ )
            {
                String syllable = in.readString();
                syllables[x-1] = syllable;
            }
            words[nrElements] = new Word(word,syllables);
            nrElements++;
          }
       }
  
       public void display()
       {
         IO.println();
         IO.print(name.toUpperCase() + " Dictionary");
         IO.println();
         for ( int x = 0; x < nrElements; x++ )
         {
             words[x].display();
         }
       }
  
    }