Clay Shell Version 16

Version 16 of the Clay Shell program is like the previous version except that (1) in addition to executing commands from the text field sequences of commands can be executed from the text field, and (2) a "pause" command is implemented which suggests some interesting programming possibilities.

Demos

java -jar ".../Clay16.jar" -h 300 -w 700 -c random

Code

/*
 * Main.java
 *
 * Created on June 1, 2006, 3:17 PM
 */

package clay16;

import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;

public class Main {
    
    <<<Just like in the previous version!>>>

}

class ClayFrame extends Frame  implements ActionListener {
  
    <<<Just like in the previous version!>>>
  
}

class Interpreter {
    
    private ClayFrame clayFrame;
        
    public Interpreter(ClayFrame cf) {

        <<<Just like in the previous version!>>>

    }
    
    private String[] quotes;
    
    private String[] words;
    
    private void establishWords() {

        <<<Just like in the previous version!>>>

    }
    
    private void establishQuotes() {

        <<<Just like in the previous version!>>>

    }
    
    public void interpret(String input) {
        if ( isSimpleCommand(input) ) {
            interpretCommand(input);
        } else if ( isSimpleCommandSequence(input) ) {
            interpretSimpleCommandSequence(input);
        } else if ( isClayDefinition(input) ) {
            interpretClayDefinition(input);
        } else {
            clayFrame.ta().append("### Clay error:  Unrecognizable input: " + input + "\n");
        }
    }

    public void interpretCommand(String command) {
        if ( command.equals("red") ) {
            clayFrame.pa().setBackground(Color.red);
        } else if ( command.equals("yellow") ) {
            clayFrame.pa().setBackground(Color.yellow);
        } else if ( command.equals("green") ) {
            clayFrame.pa().setBackground(Color.green);
        } else if ( command.equals("random") ) {
            clayFrame.pa().setBackground(randomColor());
        } else if ( command.equals("clear") ) {
            clayFrame.ta().setText("");
        } else if ( command.equals("clean") ) {
            clayFrame.pa().setBackground(Color.white);
        } else if ( command.equals("quote") ) {
            interpretQuoteCommand();
        } else if ( command.equals("word") ) {
            interpretWordCommand();
        } else if ( command.equals("pause") ) {
            pause();
        } else {
            clayFrame.ta().append("### Clay error:  Unrecognizable command: " + command + "\n");
        }
    }

    private void interpretQuoteCommand() {

        <<<Just like in the previous version!>>>

    }
       
    private void interpretWordCommand() {

        <<<Just like in the previous version!>>>

    }
       
    public Color randomColor() {

        <<<Just like in the previous version!>>>

    }

    private boolean isSimpleCommand(String input) {
        input = input.trim();
        return identifier(input);
    }

    private boolean identifier(String input) {
        if ( input.equals("") ) {
            return false;
        } else if ( ! alpha(input.charAt(0)) ) {
            return false;
        }
        return id(input);
    }

    private boolean id(String input) {
        if ( input.equals("") ) {
            return true;
        } else {
            char ch = input.charAt(0);
            if ( ! isAlphaNumeric(ch) ) {
                return false;
            } else {
                return id(input.substring(1));
            }
        }
    }

    private boolean isAlphaNumeric(char ch) {
        boolean a = alpha(ch);
        boolean n = numeric(ch);
        return alpha(ch) | numeric(ch);
    }


    private boolean alpha(char ch) {
        return ( ch >= 'a'  ) & ( ch <= 'z' );
    }

    private boolean numeric(char ch) {
        return ( ch >= '0'  ) & ( ch <= '9' );
    }

    private void interpretSimpleCommandSequence(String input) {
        StringTokenizer st = new StringTokenizer(input);
        while ( st.hasMoreTokens() ) {
            String token = st.nextToken();
            interpretCommand(token);
        }
    }

    private boolean isSimpleCommandSequence(String input) {
        StringTokenizer st = new StringTokenizer(input);
        while ( st.hasMoreTokens() ) {
            String token = st.nextToken();
            if ( ! identifier(token) ) {
                return false;
            }
        }
        return true;
    }
    
    private void pause() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
    
    private boolean isClayDefinition(String input) {
        return false;
    }
    
    private void interpretClayDefinition(String input) {
        throw new UnsupportedOperationException("Not yet implemented");
    }


}

Questions

  1. What is a thread?
  2. What does the sleep method do?
  3. What is a token?
  4. What is a tokenizer?
  5. What is a scanner?
  6. What is a recognizer?
  7. What is an interpreter?
  8. What is the StringTokenizer class specification?
  9. How many stubs appear in this program?
  10. What will the stubs do in the next version?
  11. What is a predicate?
  12. How many predicates appear in this program?
  13. "Who" do you think wrote the stubs in this program?
  14. How do you think that the stub programmer was asked to program the stubs in this program?