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
Word.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  -- Word.java

   // File header
   // ---------------------------------------------------
  
   // File:  Word.java
   // Type:  java library file
   // Date:  Tue Dec 05, 2000
   // Name:  Jeffrey R. Norkoli
   // Line:  Model of a "word"
  
   // Library class description
   // ---------------------------------------------------
  
   /*
      A "word" will be modeled in terms of the word
      itself and its syllables.
   */
  
   // Package Identification
   // ---------------------------------------------------
  
      package white.words;
  
  
   // Required Packages
   // ---------------------------------------------------
  
      import blue.io.*;
  
   // Library class
   // ---------------------------------------------------
  
      public class Word
      {
      // instance variables
         private String word;
         private String[] syllables;
         
      // constructors
         public Word()
         {
            word = IO.readString();
            syllables = new String[IO.read_int()];
            for ( int i = 0; i < syllables.length; i++ )
            {
               syllables[i] = IO.readString();
            }
         }
  
         public Word(String s, String[] a)
         {
            word = s;
            syllables = a;
         }
  
  
      // static methods
         public static boolean less(Word x, Word y)
         {
             return ( x.word().compareTo(y.word()) < 0 );
         }
  
         public static boolean equal(Word x, Word y)
         {
     return ( x.word().equals(y.word()) );
         }
  
         public static boolean greater(Word x, Word y)
         {
     return ( x.word().compareTo(y.word()) > 0 );
         }
  
  
      // instance methods
         public String word()
         {
            return word;
         }
  
         public String[] syllables()
         {
            return syllables;
         }
  
         public void display()
         {
            IO.print(word);
            IO.print(" (");
            for ( int i = 0; i < syllables.length; i++ )
            {
                IO.print(" ");
                IO.print(syllables[i]);
            }
            IO.print(" )");
            IO.println();
         }
  
         public int length()
         {
            return word.length();
         }
  
         public int nrSyllables()
         {
            return syllables.length;
         }
  
         public void println()
         {
            IO.println(word);
         }
          
         public String lastSyllable()
         {
    return syllables[syllables.length-1];
         }
  
         public void describe()
         {
            IO.print(word);
            IO.print(" (");
            for ( int i = 0; i < syllables.length; i++ )
            {
                IO.print(" ");
                IO.print(syllables[i]);
            }
            IO.print(" )");
            IO.println();
         }
  
      }