/* An instance of a concrete descendant of this class represents an expression ** (perhaps arithmetic, or boolean). ** ** Author: R. McCloskey ** Date: Feb. 2022 */ public abstract class ExprTree { // class constants // --------------- public static final int PrefixMode = 0; public static final int InfixMode = 1; public static final int PostfixMode = 2; // instance variable // ----------------- // The value of this variable (which is necessarily one among the three // "print mode values" defined above) dictates the form of the String // produced by the toString() method. private int printMode; // constructor // ----------- /* Initializes this expression to be in the specified print mode. */ public ExprTree(int printMode) { this.printMode = printMode; } /* Initializes this expression to be in infix print mode. */ public ExprTree() { this(InfixMode); } // observers // --------- /* Returns the value of this expression. */ public abstract int valueOf(); /* Returns this expression's current print mode value. */ protected int printMode() { return printMode; } /* Returns a String describing this expression in the form specified ** by the parameter, whose value is expected to be one of the "print ** mode" values defined above. */ public abstract String toString(int mode); /* Returns a String describing this expression in the form corresponding ** to this expression's current print mode value. */ @Override public String toString() { return toString(printMode()); } // mutators // -------- // Methods by which to set this expression's print mode. public void setToPrefixMode() { printMode = PrefixMode; } public void setToInfixMode() { printMode = InfixMode; } public void setToPostfixMode() { printMode = PostfixMode; } }