001package composite.one; 002public interface Expr { 003 int eval(); 004} 005 006final class Const implements Expr { 007 private final int v; 008 public Const(int v) { 009 this.v = v; 010 } 011 public int eval() { 012 return v; 013 } 014 public String toString() { 015 return Integer.toString(v); 016 } 017} 018 019final class Plus implements Expr { 020 private final Expr l; 021 private final Expr r; 022 public Plus(Expr l, Expr r) { 023 if ((l == null) || (r == null)) { 024 throw new IllegalArgumentException(); 025 } 026 this.l = l; 027 this.r = r; 028 } 029 public int eval() { 030 return l.eval() + r.eval(); 031 } 032 public String toString() { 033 return l.toString() + " " + r.toString() + " +"; 034 } 035} 036 037final class Minus implements Expr { 038 private final Expr l; 039 private final Expr r; 040 public Minus(Expr l, Expr r) { 041 if ((l == null) || (r == null)) { 042 throw new IllegalArgumentException(); 043 } 044 this.l = l; 045 this.r = r; 046 } 047 public int eval() { 048 return l.eval() - r.eval(); 049 } 050 public String toString() { 051 return l.toString() + " " + r.toString() + " -"; 052 } 053} 054 055final class Mult implements Expr { 056 private final Expr l; 057 private final Expr r; 058 public Mult(Expr l, Expr r) { 059 if ((l == null) || (r == null)) { 060 throw new IllegalArgumentException(); 061 } 062 this.l = l; 063 this.r = r; 064 } 065 public int eval() { 066 return l.eval() * r.eval(); 067 } 068 public String toString() { 069 return l.toString() + " " + r.toString() + " *"; 070 } 071} 072 073final class Quot implements Expr { 074 private final Expr l; 075 private final Expr r; 076 public Quot(Expr l, Expr r) { 077 if ((l == null) || (r == null)) { 078 throw new IllegalArgumentException(); 079 } 080 this.l = l; 081 this.r = r; 082 } 083 public int eval() { 084 return l.eval() / r.eval(); 085 } 086 public String toString() { 087 return l.toString() + " " + r.toString() + " /"; 088 } 089}