20-CS-694 Advanced Programming Techniques Spring 2012
Exceptions

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

    Previous     Next     All lectures        Code     Exception Hierarchy    

Final touches - generalize Choose

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.applet.*;

interface Variable { 
   void checkit() throws Up, Out;  String v();  
}

class Up extends Exception { }

class Out extends Exception { }

class Puzzle {
   public String v() { return ""; }
   public void assert_ (boolean e) throws Up { 
      if (!e) throw new Up(); 
   }
}

class Choose implements Variable {
   String [] strs;
   Variable variable;
   String val;
   int n;
   
   public Choose(String s, Variable var) {  
      variable = var;
      StringTokenizer t = new StringTokenizer(s," ");
      n = t.countTokens();
      strs = new String[n];
      for (int i=0 ; i < n ; i++) 
         strs[i] = t.nextToken();
   }

   public String v() { return val; }

   public void checkit () throws Up, Out {
      for (int i=0 ; i < n ; i++)  {
         val = strs[i];
         try {  variable.checkit();  } catch (Up up) { }
      }
      throw new Up();
   }
}

class Evaluate extends Puzzle implements Variable {
   Variable iv,jv,kv;
   PuzzleFrame f;

   public Evaluate (PuzzleFrame f) {  
      this.f = f; 
      f.out.append("\nSolve: ((1-i)*j+i*(1-k)+(1-j)*k)");
      f.out.append("\n\n");
   }
   
   public void checkit() throws Up, Out {
      int i = Integer.parseInt(iv.v());
      int j = Integer.parseInt(jv.v());
      int k = Integer.parseInt(kv.v());

      assert_((((1-i)*j+i*(1-k)+(1-j)*k) % 2) == 1);
      f.out.append("i="+i+" j="+j+" k="+k+"\n");

      throw new Out();
   }

   public void solve () {
      try {
         kv = new Choose("0 1", this);
         jv = new Choose("0 1", kv);
         iv = new Choose("0 1", jv);
         iv.checkit();
      } catch (Up up) {
         f.out.append("No solution\n");
      } catch (Out out) {  }
   }
}

class PuzzleFrame 
   extends JFrame implements ActionListener {
   JTextArea out;
   JButton button;

   public PuzzleFrame () {
      setLayout(new FlowLayout());
      add(new JScrollPane(out = new JTextArea(16,30)));
      add(button = new JButton("Press Me"));
      button.addActionListener(this);
      out.setFont(new Font("TimesRoman", Font.PLAIN, 18));
   }

   public void doit() {
      Evaluate eval = new Evaluate(this);
      eval.solve();
   }

   public void actionPerformed (ActionEvent evt) {  
      doit(); 
   }
}

public class puzzle 
   extends Applet implements ActionListener {
   JButton go;

   public void init () {
      setLayout(new BorderLayout());
      setBackground(new Color(255,255,223));
      add("Center", go = new JButton("Applet"));
      go.addActionListener(this);
   }

   public void actionPerformed (ActionEvent evt) {
      PuzzleFrame hf = new PuzzleFrame();
      hf.setSize(550,450);
      hf.setVisible(true);
   }
}
 -  Final cleanup - Generalize Choose to take a string of tokens as constructor argument. Every time through the loop of a Choose object the object's value changes to the next token in the argument string. After all tokens are used, an Up is thrown.