package lab.art; /** * This class represents a binary expression. * * @author Franck van Breugel */ public abstract class BinaryExpression extends Expression { private Expression first; private Expression second; //@ invariant this.first != null && this.second != null /** * Initializes this binary expression with the given two subexpressions. * * @param first the first subexpression. * @pre. first != null * @param second the second subexpression. * @pre. second != null */ public BinaryExpression(Expression first, Expression second) { super(); this.setFirst(first); this.setSecond(second); } /** * Returns the first subexpression of this binary expression. * * @return the first subexpression of this binary expression. */ private Expression getFirst() { return this.first; } /** * Returns the second subexpression of this binary expression. * * @return the second subexpression of this binary expression. */ private Expression getSecond() { return this.second; } /** * Sets the first subexpression of this binary expression to the given expression. * * @param first the new first subexpression of this binary expression. */ private void setFirst(Expression first) { this.first = first; } /** * Sets the second subexpression of this binary expression to the given expression. * * @param second the new second subexpression of this binary expression. */ private void setSecond(Expression second) { this.second = second; } /** * Tests whether this binary expression is the same as the given object. * Two binary expressions are the same if their subexpressions are the same, * that is, the first expression of the one is equal to the first expression * of the other and the second expression of the one is equal to the second * expression of the other, or the first expression of the one is equal to * the second expression of the other and the second expression of the one * is equal to the first expression of the other. * * @param object an object. * @return true if this binary expression is the same as the given object, * false otherwise. */ public boolean equals(Object object) { boolean equal; if (object != null && this.getClass() == object.getClass()) { BinaryExpression other = (BinaryExpression) object; equal = (this.getFirst().equals(other.getFirst()) && this.getSecond().equals(other.getSecond())) || (this.getFirst().equals(other.getSecond()) && this.getSecond().equals(other.getFirst())); } else { equal = false; } return equal; } /** * Returns the hash code of this binary expression. * The hash code of a binary expression is the sum * of the hash codes of its subexpressions. * * @return the hash code of this binary expression. */ public int hashCode() { return this.getFirst().hashCode() + this.getSecond().hashCode(); } /** * Evaluates this binary expression at the given x- and y-coordinate * with the given binary operator. * * @param x the x-coordinate. * @pre. x >= -1 && x <= 1 * @param y the y-coordinate. * @pre. y >= -1 && y <= 1 * @param operator a binary operator. * @pre. operator != null * @return the value of this binary expression at the given x- and y-coordinate * with the given binary operator. */ public double evaluate(double x, double y, BinaryOperator operator) { return operator.operation(this.getFirst().evaluate(x, y), this.getSecond().evaluate(x, y)); } /** * Returns a string representation of this binary expression for the given pattern. * * @param pattern the pattern describing how the two subexpressions should be combined. * @pre. pattern contains %s twice * @return a string representation of this binary expression for the given pattern. */ public String toString(String pattern) { return String.format(pattern, this.getFirst().toString(), this.getSecond().toString()); } }