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

From Wiki**3

< Herança e Composição

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
 1 /** Logical AND gate (inheritance). */
 2 public class AndGate3i extends AndGate2 {
 3 	/** A new input is needed (the other two are inherited). */
 4 	private boolean _c = false;
 5 
 6 	/**
 7 	 * Default constructor: false for all inputs.
 8 	 */
 9 	public AndGate3i() {
10 		//super(); //default: explicit call not needed
11 	}
12 
13 	/**
14 	 * Inputs receive same value.
15 	 * 
16 	 * @param v
17 	 *            the input value.
18 	 */
19 	public AndGate3i(boolean v) {
20 		super(v);
21 		_c = v;
22 	}
23 
24 	/**
25 	 * Arbitrary input value combinations.
26 	 * 
27 	 * @param a
28 	 *            input value
29 	 * @param b
30 	 *            input value
31 	 * @param c
32 	 *            input value
33 	 */
34 	public AndGate3i(boolean a, boolean b, boolean c) {
35 		super(a, b);
36 		_c = c;
37 	}
38 
39 	/**
40 	 * @return third input value.
41 	 */
42 	public boolean getC() {
43 		return _c;
44 	}
45 
46 	/**
47 	 * Set input value.
48 	 * 
49 	 * @param c
50 	 *            input value.
51 	 */
52 	public void setC(boolean c) {
53 		_c = c;
54 	}
55 
56 	/**
57 	 * @return value of logical AND operation.
58 	 */
59 	@Override
60 	public boolean getOutput() {
61 		return super.getOutput() && _c;
62 	}
63 
64 	/**
65 	 * @see java.lang.Object#equals(java.lang.Object)
66 	 */
67 	@Override
68 	public boolean equals(Object other) {
69 		if (other instanceof AndGate3i) {
70 			AndGate3i andGate = (AndGate3i) other;
71 			return super.equals(other) && _c == andGate.getC();
72 		}
73 		return false;
74 	}
75 
76 	/**
77 	 * @see java.lang.Object#toString()
78 	 */
79 	@Override
80 	@SuppressWarnings("nls")
81 	public String toString() {
82 		return super.toString() + " C:" + _c;
83 	}
84 }

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
  1 /** Logical AND gate with 3 inputs (composition). */
  2 public class AndGate3c {
  3 	/** The first gate takes inputs 'a' and 'b' */
  4 	private AndGate2 _gate1;
  5 
  6 	/**
  7 	 * The second gate takes as inputs the output of the first gate (input 'a')
  8 	 * and 'c' (input 'b')
  9 	 */
 10 	private AndGate2 _gate2;
 11 
 12 	/**
 13 	 * Default constructor: false for all inputs.
 14 	 */
 15 	public AndGate3c() {
 16 		_gate1 = new AndGate2();
 17 		_gate2 = new AndGate2(_gate1.getOutput(), false);
 18 	}
 19 
 20 	/**
 21 	 * Inputs receive same value.
 22 	 * 
 23 	 * @param v
 24 	 *            the input value.
 25 	 */
 26 	public AndGate3c(boolean v) {
 27 		_gate1 = new AndGate2(v);
 28 		_gate2 = new AndGate2(_gate1.getOutput(), v);
 29 	}
 30 
 31 	/**
 32 	 * Arbitrary input value combinations.
 33 	 * 
 34 	 * @param a
 35 	 *            input value
 36 	 * @param b
 37 	 *            input value
 38 	 * @param c
 39 	 *            input value
 40 	 */
 41 	public AndGate3c(boolean a, boolean b, boolean c) {
 42 		_gate1 = new AndGate2(a, b);
 43 		_gate2 = new AndGate2(_gate1.getOutput(), c);
 44 	}
 45 
 46 	/**
 47 	 * @return first input value.
 48 	 */
 49 	public boolean getA() {
 50 		return _gate1.getA();
 51 	}
 52 
 53 	/**
 54 	 * Set input value.
 55 	 * 
 56 	 * @param a
 57 	 *            input value.
 58 	 */
 59 	public void setA(boolean a) {
 60 		_gate1.setA(a);
 61 		_gate2.setA(_gate1.getOutput());
 62 	}
 63 
 64 	/**
 65 	 * @return second input value.
 66 	 */
 67 	public boolean getB() {
 68 		return _gate1.getB();
 69 	}
 70 
 71 	/**
 72 	 * Set input value.
 73 	 * 
 74 	 * @param b
 75 	 *            input value.
 76 	 */
 77 	public void setB(boolean b) {
 78 		_gate1.setB(b);
 79                 _gate2.setA(_gate1.getOutput());
 80 	}
 81 
 82 	/**
 83 	 * @return second input value.
 84 	 */
 85 	public boolean getC() {
 86 		return _gate2.getB();
 87 	}
 88 
 89 	/**
 90 	 * Set input value.
 91 	 * 
 92 	 * @param c
 93 	 *            input value.
 94 	 */
 95 	public void setC(boolean c) {
 96 		_gate2.setB(c);
 97 	}
 98 
 99 	/**
100 	 * Since the two gates are always kept in sync, we only need to ask for the
101 	 * output of the second gate.
102 	 * 
103 	 * @return value of logical AND operation.
104 	 */
105 	public boolean getOutput() {
106 		return _gate2.getOutput();
107 	}
108 
109 	/**
110 	 * @see java.lang.Object#equals(java.lang.Object)
111 	 */
112 	@Override
113 	public boolean equals(Object other) {
114 		if (other instanceof AndGate3c) {
115 			AndGate3c ag = (AndGate3c) other;
116 			return getA() == ag.getA() && getB() == ag.getB() && getC() == ag.getC();
117 		}
118 		return false;
119 	}
120 
121 	/**
122 	 * @see java.lang.Object#toString()
123 	 */
124 	@Override
125 	@SuppressWarnings("nls")
126 	public String toString() {
127 		return "A:" + getA() + " B:" + getB() + " C:" + getC();
128 	}
129 }