Return to Jeff's Main Page

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
My Intro to Object-Oriented Programming  
 
 
Programming Challenge Archive

Programming Assignment # 5
Dictionary.java
 
In this assignment,  CG provided us with test programs to run classes we refined.    The classes were Word.java and Dictionary.java .    The test programs were OfficialWordTestApp.java and OfficialDictionaryTestApp.java .  

 
  JavaApplication - Programming Assignment # 5  -- Dictionary.java

   // File header
   // ---------------------------------------------------
  
   // File:  Dictionary.java
   // Type:  java library file
   // Date:  Tue Dec 05, 2000
   // Name:  Jeffrey R. Norkoli
   // 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.*;
      import blue.math.*;
  
   // 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();
            }
         }
  
         public String name()
         {
    return name;
         }
         
         public Word[] words()
         {
    return words;
         }
         
         public int size()
         {
    return words.length;
         }
  
         public boolean search(Word w)
         {
    for ( int i = 0; i < nrElements; i++ )
    {
        if ( Word.equal(words[i], w) )
        {
           return true;
        }
    }
    return false;
         }
    
         public int search(String s)
         {
    for ( int i = 0; i < nrElements; i++ )
    {
        String temp = words[i].word();
        if ( temp.equals(s) )
        {
           return i;
        }   
    }
    int negative = -1;
    return negative;
         }
          
         public void sort()
         {
    for ( int i = 0; i < nrElements; i++ )
    {
        for ( int j = i + 1; j < nrElements; j++ )
        {
    if ( Word.less(words[j], words[i]) )
    {
       Word temp = words[j];
       words[j] = words[i];
       words[i] = temp;
    }
        }
    }
         }
        
         public void sortByLength()
         {
    for ( int i = 0; i < nrElements; i++ )
    {
        for ( int j = i + 1; j < nrElements; j++ )
        {
    if ( words[i].length() > words[j].length() )
    {
       Word temp = words[j];
       words[j] = words[i];
       words[i] = temp;
    }
        }
    }
         }
  
         public void sortBySyllableCount()
         {
    for ( int i = 0; i < nrElements; i++ )
    {
        for ( int j = i + 1; j < nrElements; j++ )
        {
    if ( words[i].nrSyllables() > words[j].nrSyllables() )
    {
       Word temp = words[j];
       words[j] = words[i];
       words[i] = temp;
    }
        }
    }
         }
  
         public Word select()
         {
    RandomGenerator pickWord = new RandomGenerator();
    int rn = pickWord.uniform( 0, nrElements - 1 );
    return words[rn];
         }
  
         public Word select(int n)
         {
    Dictionary is = filter(n);
    return is.select();
         }
  
         private Dictionary filter(int n)
         {
    Dictionary temp = new Dictionary("temp");
    for ( int i = 0; i < nrElements; i++ )
    {
       if ( words[i].nrSyllables() == n )
       {
          temp.add(words[i]);
       }
    }
    return temp;
         }
  
         public Word select(String[] vala)
         {
    Dictionary ss = filterSyllables(vala);
    return ss.select();
         }
  
         private Dictionary filterSyllables(String[] vala)
         {
    Dictionary fTemp = new Dictionary("fTemp");
    for ( int i = 0; i < nrElements; i++ )
    {
       String[] getSyllables = words[i].syllables();
       String lastSyllable = getSyllables[getSyllables.lengt+
   h - 1];
       for ( int x = 0; x < vala.length; x++ )
       {
   if ( vala[x].equals(lastSyllable) )
   {
              fTemp.add(words[i]);
   }
       }
    }
    return fTemp;
         }
  
         public Word select(int t, String[] invanthon)
         {
            Dictionary fs = filter(t);
    Dictionary fs2 = fs.filterSyllables(invanthon);
    return fs2.select();
         }
  
  
      }