20-CS-4003-001 Organization of Programming Languages Fall 2017
Exceptions

Lambda calculus, Type theory, Formal semantics, Program analysis

    Previous     Next     All lectures        Code     Exception Hierarchy    

Color Map of Europe

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

// Color coutries of Europe
//   Color countries of Europe so no two bordering countries have 
//   the same color.
class ColorPuzzle extends Puzzle implements Variable {
   ColorFrame problem;
   Variable portugal, spain, germany, austria, italy, switzerland,
      france, belgium, luxemburg, holland;

   public ColorPuzzle (ColorFrame problem) {  this.problem = problem; }
	
   public void checkit () throws Up, Out {
      assert_(!portugal.val().equals(spain.val()));
      assert_(!spain.val().equals(france.val()));
      assert_(!france.val().equals(italy.val()));
      assert_(!france.val().equals(switzerland.val()));
      assert_(!france.val().equals(belgium.val()));
      assert_(!france.val().equals(germany.val()));
      assert_(!france.val().equals(luxemburg.val()));
      assert_(!belgium.val().equals(holland.val()));
      assert_(!belgium.val().equals(germany.val()));
      assert_(!belgium.val().equals(luxemburg.val()));
      assert_(!holland.val().equals(germany.val()));
      assert_(!germany.val().equals(switzerland.val()));
      assert_(!germany.val().equals(austria.val()));
      assert_(!germany.val().equals(luxemburg.val()));
      assert_(!italy.val().equals(switzerland.val()));
      assert_(!italy.val().equals(austria.val()));
      assert_(!switzerland.val().equals(austria.val()));
      // If all assertions are true, print the colors
      problem.out.append(
          " Portugal = "    +portugal.val()    +"\n"+
          " Spain = "       +spain.val()       +"\n"+
          " France = "      +france.val()      +"\n"+
          " Belgium = "     +belgium.val()     +"\n"+
          " Holland = "     +holland.val()     +"\n"+
          " Germany = "     +germany.val()     +"\n"+
          " Luxemburg = "   +luxemburg.val()   +"\n"+
          " Italy = "       +italy.val()       +"\n"+
          " Switzerland = " +switzerland.val() +"\n"+
          " Austria = "     +austria.val()     +"\n");
      throw new Out();
   }

   public void solve () {
      portugal    = new Choose("red green blue yellow", this);
      spain       = new Choose("red green blue yellow", portugal);
      france      = new Choose("red green blue yellow", spain);
      belgium     = new Choose("red green blue yellow", france);
      holland     = new Choose("red green blue yellow", belgium);
      germany     = new Choose("red green blue yellow", holland);
      luxemburg   = new Choose("red green blue yellow", germany);
      italy       = new Choose("red green blue yellow", luxemburg);
      switzerland = new Choose("red green blue yellow", italy);
      austria     = new Choose("red green blue yellow", switzerland);
      try { austria.checkit(); } 
      catch (Out out) { 
         problem.out.append(" Done\n"); 
      }
      catch (Up up) { 
         problem.out.append(" No solution found\n"); 
      }
   }
}

class ColorFrame extends JFrame implements ActionListener {
   JTextArea out;
   JButton button;
	
   public ColorFrame () {
      setLayout(new BorderLayout());
      setBackground(new Color(255,255,224));
      add("Center", new JScrollPane(out = new JTextArea()));
      JPanel p = new JPanel();
      p.setLayout(new FlowLayout());
      p.add(button = new JButton("Press me"));
      p.setBackground(new Color(255,255,223));
      add("South", p);
      button.addActionListener(this);
      out.setFont(new Font("TimesRoman", Font.PLAIN, 18));
   }
	
   public void doit () {
      ColorPuzzle puzzle = new ColorPuzzle(this);
      puzzle.solve();
   }
	
   public void actionPerformed (ActionEvent evt) {  doit();  }
}

public class Colors extends Applet implements ActionListener {
   JButton go;
	
   public void init () {
      setLayout(new BorderLayout());
      add("Center",go = new JButton("Applet"));
      go.addActionListener(this);
   }
	
   public void actionPerformed (ActionEvent evt) {
      ColorFrame cf = new ColorFrame();
      cf.setSize(600,600);
      cf.setVisible(true);
   }
}

//-----------------------------------------------------

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

//-----------------------------------------------------

import java.util.*;

public class Choose implements Variable {
   Variable next_for;
   String [] strs;
	int n;
   String value;
   
   public Choose(String s, Variable next_for) {  
      this.next_for = next_for;
      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 val () { return value; }

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

//-----------------------------------------------------

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

//-----------------------------------------------------

public class Out extends Exception { }

//-----------------------------------------------------

public class Up extends Exception { }

//-----------------------------------------------------

public class BooleanExpression {
   public void eval () throws Up {}

   public void assert_ (boolean e) throws Up { if (!e) throw new Up(); }

   public boolean distinct (Variable a[]) {
      int count=0;
      Variable b;
      try { for (count=0 ; ; count++) b = a[count]; } 
      catch (ArrayIndexOutOfBoundsException e) {}
      for (int i=0 ; i < count-1 ; i++) {
         for (int j=i+1 ; j < count ; j++) {
            if (a[i].val().equals(a[j].val())) return false; 
         }
      }
      return true;
   }

   public boolean leftOf (Variable a, Variable b, Variable c[]) {
      return ((c[0].val().equals(a.val())) &&
	      ((c[1].val().equals(b.val())) ||
	       (c[2].val().equals(b.val())) ||
	       (c[3].val().equals(b.val())) ||
	       (c[4].val().equals(b.val())))) ||
	     ((c[1].val().equals(a.val())) &&
	      ((c[2].val().equals(b.val())) ||
	       (c[3].val().equals(b.val())) ||
	       (c[4].val().equals(b.val())))) ||
	     ((c[2].val().equals(a.val())) &&
	      ((c[3].val().equals(b.val())) ||
	       (c[4].val().equals(b.val())))) ||
	     ((c[3].val().equals(a.val())) &&
	      (c[4].val().equals(b.val())));
   }

   public boolean neighborOf (String a, String b, Variable c[]) {
      return ((c[0].val().equals(b)) &&
	      (c[1].val().equals(a))) ||
	     ((c[1].val().equals(b)) &&
	      ((c[0].val().equals(a)) ||
	       (c[2].val().equals(a)))) ||
	     ((c[2].val().equals(b)) &&
	      ((c[1].val().equals(a)) ||
	       (c[3].val().equals(a)))) ||
	     ((c[3].val().equals(b)) &&
	      ((c[2].val().equals(a)) ||
	       (c[4].val().equals(a)))) ||
	     ((c[4].val().equals(b)) &&
	      (c[3].val().equals(a)));
   }
}

//-----------------------------------------------------

public class Constraint implements Variable {
   Variable variable;
   BooleanExpression expression;

   public String val () { return null; }

   public Constraint (BooleanExpression exp, Variable var) { 
      expression = exp; 
      variable = var; 
   }

   public void checkit () throws Up, Out {
      expression.eval();
      try { variable.checkit(); } catch (Up up) {}
   }
}
 -  Color a map of western Europe so that no two adjacent countries have the same color.