lec26 - simple expressions abstract class Exp abstract...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // simple expressions abstract class Exp { abstract public int eval(); abstract public String makeString(); // could override toString abstract public boolean hasZero(); } class Int extends Exp { int i; Int(int _i) { i = _i; } public int eval() { return i; } public String makeString() { return Integer.toString(i); } public boolean hasZero() { return i==0; } } class Negate extends Exp { Exp e; Negate(Exp _e) { e = _e; } public int eval() { return - e.eval(); } public String makeString() { return "-(" + e.makeString() + ")"; } public boolean hasZero() { return e.hasZero(); } } class Add extends Exp { Exp e1; Exp e2; Add(Exp _e1, Exp _e2) { e1 = _e1; e2 = _e2; } public int eval() { return e1.eval() + e2.eval();} public String makeString() { return "(" + e1.makeString() + " + " + e2.makeString() + ")"; } public boolean hasZero() { return e1.hasZero() || e2.hasZero(); } } class Mult extends Exp { Exp e1; Exp e2; Mult(Exp _e1, Exp _e2) { e1 = _e1; e2 = _e2; } public int eval() { return e1.eval() * e2.eval();}public int eval() { return e1....
View Full Document

This note was uploaded on 10/12/2009 for the course CSE 341 taught by Professor Staff during the Spring '08 term at University of Washington.

Page1 / 2

lec26 - simple expressions abstract class Exp abstract...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online