Howdy Mark, Logan, Daniel, Jared, Aleksa, Adam, Gabriela, Brennan, Jacquelyn, 
      Nick, Yiming, Jingzhe, Paul, Morgan, Grant, Mohan, Judy, Walter, James,
      Yudou, Austin, William, Alex Ong, Lauren and Jon, 

Here's a reminder of what we did in class in the morning: 

public class Frequency {
  private String word; 
  private int count;   
  public void addOne() {
    this.count++; 
  } 
  public Frequency(String word, int number) {
    this.word = word; 
    this.count = number; 
  }
  public String getWord() {
    return this.word; 
  }
  public int getCount() {
    return this.count; 
  }
  public String toString() {
    return "(" + this.word + ", " + this.count + ")";
  }
  public static void main(String[] args) {
    Frequency a = new Frequency("mango", 3); 
    Frequency b = new Frequency("pear", 7); 
    System.out.println( a.toString() ); 
    System.out.println( b ); 
  } 
}

public class ListOfString {
  private String       first;
  private ListOfString rest;    
  public String toString() {
    return this.first + " " + this.rest;  
  }
  public ListOfString(String first, ListOfString rest) {
    this.first = first; 
    this.rest = rest;
  } 
  public String getFirst() {
    return this.first; 
  }
  public ListOfString getRest() {
    return this.rest; 
  } 
  public static void main(String[] args) {
    ListOfString los1 = null; 
    ListOfString los2 = new ListOfString("narwal" , los1); 
    ListOfString los3 = new ListOfString("paper"  , los2); 
    ListOfString los4 = new ListOfString("word"   , los3); 
    ListOfString los5 = new ListOfString("laptop" , los4); 
    System.out.println( los1 ); 
    System.out.println( los2 ); 
    System.out.println( los3 ); 
    System.out.println( los4 ); 
    System.out.println( los5 ); 
  } 
}

public class ListOfFrequency {
  private Frequency       first;
  private ListOfFrequency rest;    
  public String toString() {
    return this.first + " " + this.rest;  
  }
  public ListOfFrequency(Frequency first, ListOfFrequency rest) {
    this.first = first; 
    this.rest = rest;
  } 
  public Frequency getFirst() {
    return this.first; 
  }
  public ListOfFrequency getRest() {
    return this.rest; 
  } 
  public static void main(String[] args) {
    ListOfFrequency lof1 = null; 
    ListOfFrequency lof2 = new ListOfFrequency(new Frequency("narwal" , 2), lof1); 
    ListOfFrequency lof3 = new ListOfFrequency(new Frequency("paper"  , 5), lof2); 
    ListOfFrequency lof4 = new ListOfFrequency(new Frequency("word"   , 1), lof3); 
    ListOfFrequency lof5 = new ListOfFrequency(new Frequency("laptop" , 3), lof4); 
    System.out.println( lof1 ); 
    System.out.println( lof2 ); 
    System.out.println( lof3 ); 
    System.out.println( lof4 ); 
    System.out.println( lof5 ); 
  } 
}

public class LectureEight {
  public static ListOfFrequency countWord(ListOfFrequency lof, String word) {
    if (lof == null) {
      Frequency f = new Frequency(word, 1); 
      return new ListOfFrequency(f, lof); 
    } else {
      if (lof.getFirst().getWord().equals( word )) {
        lof.getFirst().addOne();
        return lof; 
      } else {
        return new ListOfFrequency( lof.getFirst() , 
                                     LectureEight.countWord( lof.getRest() ,
                                                             word  )
                                  ); 
      }
    }          
  }
  public static void main(String[] args) {
    ListOfFrequency lof1 = null; 
    System.out.println( lof1 ); 
    ListOfFrequency lof2 = LectureEight.countWord(lof1, "narwal");
    System.out.println( lof2 ); 
                    lof2 = LectureEight.countWord(lof2, "narwal");
    System.out.println( lof2 ); 
    ListOfFrequency lof3 = new ListOfFrequency(new Frequency("paper"  , 5), lof2); 
    System.out.println( lof3 ); 
    ListOfFrequency lof4 = new ListOfFrequency(new Frequency("word"   , 1), lof3); 
    System.out.println( lof4 ); 
    ListOfFrequency lof5 = new ListOfFrequency(new Frequency("laptop" , 3), lof4); 
    System.out.println( lof5 ); 
    ListOfFrequency lof6 = LectureEight.countWord(lof5, "word"); 
    System.out.println( lof6 ); 
    ListOfFrequency lof7 = LectureEight.countWord(lof6, "mouse"); 
    System.out.println( lof7 ); 
  } 
}

Exercise 4 Design a function count-all-words which takes a ListOfString and produces a 
           ListOfFrequency with the appropriate frequencies counted from the entire list 
           of strings.

public class LabEight {
  public static ListOfFrequency countAllWords(ListOfString los) {
    return null; 
  } 
  public static void main(Sring[] args) {
    ListOfString los = null; 
                 los = new ListOfString("narwal" , los); 
                 los = new ListOfString("word"   , los); 
                 los = new ListOfString("paper"  , los); 
                 los = new ListOfString("word"   , los); 
                 los = new ListOfString("laptop" , los); 
                 los = new ListOfString("narwal" , los); 
                 los = new ListOfString("word"   , los); 
                 los = new ListOfString("paper"  , los); 
                 los = new ListOfString("narwal" , los); 
                 los = new ListOfString("word"   , los); 
    System.out.println( los ); 
  } 
}

That's the starting point, eventually the function becomes:

public class LabEight {
  public static ListOfFrequency countAllWords(ListOfString los) {
    ListOfFrequency lof = null;
    while (los != null) {
      lof = LectureEight.countWord(lof, los.getFirst()); 
      los = los.getRest(); 
    }
    return lof; 
  }
  public static void main(String[] args) {
    ListOfString los = null;
                 los = new ListOfString("narwal" , los);
                 los = new ListOfString("word"   , los);
                 los = new ListOfString("paper"  , los);
                 los = new ListOfString("word"   , los);
                 los = new ListOfString("laptop" , los);
                 los = new ListOfString("narwal" , los);
                 los = new ListOfString("word"   , los);
                 los = new ListOfString("paper"  , los);
                 los = new ListOfString("narwal" , los);
                 los = new ListOfString("word"   , los);
    System.out.println( los );
    ListOfFrequency lof = LabEight.countAllWords( los ); 
    System.out.println( lof ); 
  }
}

This compiles and runs as follows: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run LabEight
word narwal paper word narwal laptop word paper word narwal null
(word, 4) (narwal, 3) (paper, 2) (laptop, 1) null


import java.util.ArrayList;

public class Exercise {
  public static ArrayList<Pair> countAllWords(ArrayList<String> los) {
    ArrayList<Pair> result = new ArrayList<Pair>(); 
    for (String s : los) 
      result = countWord(result, s);
    return result; 
  }
  public static ArrayList<Pair> countWord(ArrayList<Pair> lof, String w) {
    boolean found = false; 
    for (Pair p : lof) {
      if (p.word.equals(w)) {
        p.number++; 
        found = true;
        break; 
      }
    }
    if (! found) 
      lof.add(new Pair(w, 1)); 
    return lof; 
  }
  public static void main(String[] args) {
    Pair a = new Pair("sofa", 2); 
    System.out.println( a ); 
    ArrayList<String> los = new ArrayList<String>(); 
    los.add("narwal"); 
    los.add("word"); 
    los.add("peach"); 
    los.add("word"); 
    los.add("narwal"); 
    los.add("narwal"); 
    los.add("peach"); 
    los.add("word"); 
    los.add("word"); 
    los.add("holiday");
    los.add("word");     
    System.out.println( los ); 
    // ArrayList<Pair> lof = new ArrayList<Pair>();     
    // lof.add( new Pair( "narwal", 3 ) ); 
    // lof.add( new Pair( "word", 4 ) ); 
    // lof.add( new Pair( "peach", 2 ) ); 
    // System.out.println( lof );     
    // lof = countWord(lof, "sofa"); 
    // System.out.println( lof );     
    // lof = countWord(lof, "word"); 
    // System.out.println( lof );    
    ArrayList<Pair> lof = countAllWords( los ); 
    System.out.println( lof );    
  }
}

If you run this program you get:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Exercise
(sofa, 2)
[narwal, word, peach, word, narwal, narwal, peach, word, word, holiday, word]
[(narwal, 3), (word, 5), (peach, 2), (holiday, 1)]


So now you are ready for your lab assignment. 

--