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

Lambda calculus, Type theory, Formal semantics, Program analysis

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.*; class KalotanPuzzle extends Puzzle implements Variable { Variable prnt1, prnt2, kid, kid_lied; Exception_10Frame f; public KalotanPuzzle (Exception_10Frame f) { this.f = f; } public void checkit () throws Up, Out { // Parents must be of different sex assert_(!prnt1.value().equals(prnt2.value())); // If the first parent responding is male // then either the kid is male and did not // lie or the kid is female and lied assert_(prnt1.value().equals("female") || (kid.value().equals("male") && kid_lied.value().equals("false")) || (kid.value().equals("female") && kid_lied.value().equals("true"))); // If the second parent responding is male // then the kid is female and lied assert_(prnt2.value().equals("female") || (kid.value().equals("female") && kid_lied.value().equals("true"))); // If the second parent responding is female // then either the kid is male and lied or // the kid is female and did not lie assert_(prnt2.value().equals("male") || (kid.value().equals("male") && kid_lied.value().equals("true")) || (kid.value().equals("female") && kid_lied.value().equals("false"))); // If all assertions are true, print the // values of parents, and kid f.out.append("Parent1:"+prnt1.value()+" "+ "Parent2:"+prnt2.value()+" "+ "Kid:"+kid.value()+"\n"); throw new Out(); } public void solve () { try { prnt1 = new Choose("male female", this); prnt2 = new Choose("male female", prnt1); kid = new Choose("male female", prnt2); kid_lied = new Choose("true false", kid); kid_lied.checkit(); } catch (Up up) { f.out.append("No solution\n"); } catch (Out out) { f.out.append("Done\n"); } } } /*** The following remains the same for all problems of this type ***/ interface Variable { void checkit () throws Up, Out; String value (); } class Up extends Exception { } class Out extends Exception { } class Choose implements Variable { Variable next_for; String [] strs; String val; int n; 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 value () { return val; } public void checkit () throws Up, Out { for (int i=0 ; i < n ; i++) { val = strs[i]; try { next_for.checkit(); } catch (Up up) { } } throw new Up(); } } class Puzzle { public String value () { return null; } public void assert_ (boolean e) throws Up { if (!e) throw new Up(); } } /*** end of the common code section ***/ class Exception_10Frame extends JFrame implements ActionListener { JTextArea out; JButton button; public Exception_10Frame () { 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() { KalotanPuzzle kalotan = new KalotanPuzzle(this); kalotan.solve(); } public void actionPerformed (ActionEvent evt) { doit(); } } public class exception_10 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) { Exception_10Frame hf = new Exception_10Frame(); hf.setSize(550,450); hf.setVisible(true); } } ``` - The Kalotans are a tribe with a peculiar quirk: their males always tell the truth. Their females never make two consecutive true statements, or two consecutive untrue statements. An anthropologist (let's call him Worf) has begun to study them. Worf does not yet know the Kalotan language. One day, he meets a Kalotan (heterosexual) couple and their child Kibi (also called "kid"). Worf asks the kid: ``Are you a boy?'' The kid answers in Kalotan, which of course Worf doesn't understand. Worf turns to the parents (who know English) for explanation. One of them says: "Kibi said: `I am a boy.'" The other adds: "Kibi is a girl. Kibi lied." Solve for the sex of the parents and Kibi. Let variable prnt1 be the first responding parent, let variable prnt2 be the second responding parent, let variable kid be the sex of Kibi, let variable kid-lied be true if and only if Kibi lied.