Herança e Composição/Exercício 02: Porta AND Ternária

From Wiki**3

Problema

Defina uma nova classe que represente uma porta lógica AND com três entradas. Esta classe deve chamar-se AndGate3 e apresenta a mesma funcionalidade que a de duas entradas. A apresentação (toString) é A: valor B: valor C: valor.

A classe AndGate3 deve ser definida reutilizando o conceito AndGate2 (definido no Exercício 1).

Adapte a função main definida anteriormente, por forma a integrar alguns testes com a nova porta lógica.

Solution 1: AndGate3i defined as a subclass of AndGate2

In this solution we observe that the operation is a specialization of the previous one. This allows us to reuse the previous functionality.

'Ficheiro AndGate3i.java'
/** Logical AND gate (inheritance). */
public class AndGate3i extends AndGate2 {
	/** A new input is needed (the other two are inherited). */
	private boolean _c = false;

	/**
	 * Default constructor: false for all inputs.
	 */
	public AndGate3i() {
		//super(); //default: explicit call not needed
	}

	/**
	 * Inputs receive same value.
	 * 
	 * @param v
	 *            the input value.
	 */
	public AndGate3i(boolean v) {
		super(v);
		_c = v;
	}

	/**
	 * Arbitrary input value combinations.
	 * 
	 * @param a
	 *            input value
	 * @param b
	 *            input value
	 * @param c
	 *            input value
	 */
	public AndGate3i(boolean a, boolean b, boolean c) {
		super(a, b);
		_c = c;
	}

	/**
	 * @return third input value.
	 */
	public boolean getC() {
		return _c;
	}

	/**
	 * Set input value.
	 * 
	 * @param c
	 *            input value.
	 */
	public void setC(boolean c) {
		_c = c;
	}

	/**
	 * @return value of logical AND operation.
	 */
	@Override
	public boolean getOutput() {
		return super.getOutput() && _c;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object other) {
		if (other instanceof AndGate3i) {
			AndGate3i andGate = (AndGate3i) other;
			return super.equals(other) && _c == andGate.getC();
		}
		return false;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	@SuppressWarnings("nls")
	public String toString() {
		return super.toString() + " C:" + _c;
	}
}

Solução 2: AndGate3c defined as a composition of two AndGate2

In this solution, the 3-input gate is defined as a composition of 2-input gates. Note that the A-input of the second gate is updated whenever the inputs of the first gate are changed.

'Ficheiro AndGate3c.java'
{{{2}}}