Assignment 11 JumpStart

Note: the assignment is now due Friday April 16 @23:59

Here's the code discussed in class today:

import java.io.*;
import java.util.*; 

class Help {
  public static void main(String[] args) {
    try { 
      BufferedReader b = 
        new BufferedReader(new InputStreamReader(System.in)); 
      String token, command, identifier; 
      Hashtable h = new Hashtable(); 
      while (true) {
        System.out.print("hello> "); 
        StringTokenizer s = new StringTokenizer(b.readLine()); 
        if (! s.hasMoreTokens()) { 
          System.out.println("Empty line."); 
        } else { 
          token = s.nextToken(); // s is "shorter" now
          if (token.equals("reverse")) {
            List m = makeList(s); // get it 
            List l = m.reverse(); // reverse it 
            if (l == null) {      // [1], used below 
              System.out.println("Empty list"); 
            } else { // if it's not null 
              System.out.println(l.show()); // show it 
            } 
          } else if (token.equals("quit")) {
            System.exit(0); 
          } else { // it's an identifier!! 
            identifier = token; 
            if (! s.hasMoreTokens()) { // look up its value 
              if (h.containsKey(identifier)) { 
                // check here if it's null or not like at [1]
                // to avoid calling show() on a null pointer 
                System.out.println(((List)h.get(identifier)).show()); 
              } else { 
                System.out.println(identifier + ": undefined"); 
              } 
            } else { // identifier followed by something else 
                     // better that something be an "=" sign 
              token = s.nextToken(); 
              if (! token.equals("=")) { // error, report it 
                System.out.println("Invalid assignment statement."); 
              } else { // good, the "=" sign was found, let's parse some more 
                if (! s.hasMoreTokens()) { // incomplete assignment statement 
                  System.out.println("Keyword expected."); 
                } else { // let's check the token  
                  command = s.nextToken(); 
                  if (command.equals("reverse")) {
                    List m = makeList(s); // create it 
                    List l = m.reverse(); // reverse it
                    h.put(identifier, l); // store it 
                  } else { // it's not our keyword... 
                    System.out.println(command + " unimplemented."); 
                  }
                }
              } 
	    }  
          } 
        } 
      }    
    } catch (Exception e) {
      System.out.println("Error: " + e.toString()); 
    } 
  } 
  static List makeList(StringTokenizer s) {
    if (s.hasMoreTokens()) {
      String value = s.nextToken(); 
      return new List(value, makeList(s)); 
    } else {
      return null; 
    } 
  } 
}

class List {
  String value; 
  List nextElement; 
  List(String value, List nextElement) {
    this.value = value; 
    this.nextElement = nextElement; 
  } 
  public String show() {
    if (nextElement == null) {
      return value; 
    } else {
      return value + " " + nextElement.show(); 
    } 
  } 
  public String showR() { 
    // Credit goes to Michael Anderson for this idea. 
    if (nextElement == null) {
      return value; 
    } else {
      return nextElement.showR() + " " + value; 
    } 
  } 
  public List reverse() {
    return Help.makeList(new StringTokenizer(this.showR())); 
  } // Other options: write a makeReversedList function that 
    // builds the list already reversed, as if through inserting 
    // the elements at the top of the list as you read them from 
    // the StringTokenizer. Thanks to Igal Elias for this idea.  
}
Remember that parsing involves: Now you need to add remove.

First, this command will come with its own pattern. But the example above will make it easier to integrate the command within the framework.

Second you will need to come up with a definition of what it means to remove all occurences of an element from a list of elements.