Byron's CSC212 Web Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
Byron's CSC212 Web Site  
 
 
 
Class Notes

Wednesday November 15 , 2000
 
Word Dictionary Searching & Sorting  

 
  Class Notes  -- Wednesday November 15 , 2000

   
   CSC212 - November 15, 2000
   ======================================
  
   Lecture Topic: SEARCHING & SORTING
  
   Programming Challenge #5
    Implementation of the Word Class
  
    Dictionary Class # 1,2,3,4
  
   Part A:
  
     In the lab this week, you "Implement" (Download & Run)
     some of the word class...
  
        new Word() -> <Word>
  
        new Word (<String>,<String[]>) -> <Word>
  
       <Word>.word() -> <String>
    
       <Word>.syllables() -> <String[]>
  
       <Word>.length() -> <int>
  
       <Word>.nrSyllables() -> <int>
  
       <Word>.describe() ->
  
       <Word>.print ->
  
       <Word>.less (<Word>,<Word>) -> <boolean>
  
       <Word>.lastSyllables() -> <String>
  
   Part B:
  
      By analogy with the Lab, start implementing the 
      Dictionary class.
  
         new Dictionary (<String>) -> <Dictionary>
  
        <Dictionary>.add (<Word>)
  
        <Dictionary>.add (<String>)   // filename
  
        <Dictionary>.display()
  
   Part C:
  
      Add and test one element of functionality of the Word
      class at a time until it is complete.
  
      implement and test:  <Word>.equal()
          ________________       _______________________
         | "add test code |     |                       |
      2) |  for equal"    |  1) | "add Def of equal"    |
         |________________|     |_______________________|
         | Shell:         |     |                       |
      3) | Compile & Run  |  4) | "add Def of Greatrer" |
         |________________|     |_______________________|
  
           Xemacs                  Xemacs
           Test Program            Library class
  
   Part D:  One at a time, implement & test the items of
            functionality of the Dictionary Class.
  
  
   A Dictionary ... d
     ______________________________________________
    |  name  -> "Animals"                          |
    |            ________________________________  |
    |  Words -> | "cat"      | "cat"             | | [0]
    |           |____________|___________________| |
    |           | "tiger"    | "ti" |"ger"       | | [1]
    |           |____________|______|____________| |
    |           | "elephant" | "el" |"e"|"phant" | | [2]
    |           |____________|______|___|________| |
    |           | "zebra"    | "ze" |"bra"       | | [3]
    |           |____________|______|____________| |
    |______________________________________________|
  
  
     d.name()  -> "animals"
                   _______________________
     d.words() -> | "cat"   | "cat"       |
                  |_________|_____________|
                  | "tiger" |"ti" | "ger" |
                  |_________|_____|_______|
  
     d.size() -> 4    // Instance Method
  
           Retrieve number of elements in an array.
                   _____________________________
     d.element(2) | "elephant" | "el" | "phant" | [2]
                  |____________|______|_________| 
  
                    indexing begins at zero
  
     d.display()   Animal   Syllables
                    cat      (cat)
                    tiger    (ti ger)
                    elepant  (el e phant)
                    zebra    (ze bra)
  
    Name: This is easy - use a for statemat.
  
    Print Methods for Word Objects
  
  
   Searching:
  
    Write a method to search an array of integers for a 
    given integer.  Unlike the search methods or your 
    Dictionary class, this will be a static method.
  
       static private boolean search (int x, int a[])
    {
       for ( int i = 0; i < a.length; i++)
     {
        if (a[i] == x)
      {
        return True;
      }
     } 
        return False;
    }
  
   // for our program use method
  
      public instance Word(     )
  
      == -> use "Word" equal
  
      if (Word.equal(w[i], x))
  
  
   SORTING: Simple Exchange Sort
  
   "primary" index    pass 1             pass 2
       ____          _________     ___________________
    a | 15 | a[0]   |  8 |  4 |   |  4 |  4 |  4 |  4 |
      |____|        |____|____|   |____|____|____|____|  
      |  8 | a[1]   | 15 | 15 |   | 15 |  8 |  7 |  6 | 
      |____|        |____|____|   |____|____|____|____|
      |  4 | a[2]   |  4 |  8 |   |  8 | 15 | 15 | 15 |
      |____|        |____|____|   |____|____|____|____|
      | 20 | a[3]   | 20 | 20 |   | 20 | 20 | 20 | 20 |
      |____|        |____|____|   |____|____|____|____|
      |  7 | a[4]   |  7 |  7 |   |  7 |  7 |  8 |  8 |
      |____|        |____|____|   |____|____|____|____|
      | 11 | a[5]   | 11 | 11 |   | 11 | 11 | 11 | 11 |
      |____|        |____|____|   |____|____|____|____|
      |  6 | a[6]   |  6 |  6 |   |  6 |  6 |  6 |  7 |
      |____|        |____|____|   |____|____|____|____|
      | 19 | a[7]   | 19 | 19 |   | 19 | 19 | 19 | 19 |
      |____|        |____|____|   |____|____|____|____|
     
  
  
   The Code - Example of Sort
  
      for ( int p = 0; p< a.length; p++)  // Primary Index
   {
      for ( int s = p+1; s< a.length; s++)// Secondary Index
    { 
       if ( a[p] > a[s])
     {
       int temp = a [p];
       a[p] = a[s]
       s[s] = temp;
     }
    }
   }
  
  
   =========================================================
   123456789112345678921234567893123456789412345678951234567