(→Solução) |
|||
Line 8: | Line 8: | ||
Adapte a função main definida anteriormente, por forma a integrar alguns testes com a nova porta lógica. | Adapte a função main definida anteriormente, por forma a integrar alguns testes com a nova porta lógica. | ||
− | = Solução = | + | = Solução 2: Composição de duas portas AndGate2 = |
+ | |||
+ | = Solução 2: Composição de duas portas AndGate2 = | ||
+ | |||
+ | Nesta solução, a porta de 3 entradas usa duas de duas entradas para efectuar a operação. Note-se que as entradas das duas portas são calculadas sempre que uma das entradas (a, b, c) da porta de 3 entradas é modificada. | ||
+ | |||
+ | <java5> | ||
+ | /** Logical AND gate with 3 inputs. */ | ||
+ | public class AndGate3c { | ||
+ | /** The first gate takes inputs 'a' and 'b' */ | ||
+ | private AndGate2 _gate1; | ||
+ | |||
+ | /** | ||
+ | * The second gate takes as inputs the output of the first gate (input 'a') | ||
+ | * and 'c' (input 'b') | ||
+ | */ | ||
+ | private AndGate2 _gate2; | ||
+ | |||
+ | /** | ||
+ | * Default constructor: false for all inputs. | ||
+ | */ | ||
+ | public AndGate3c() { | ||
+ | _gate1 = new AndGate2(); | ||
+ | _gate2 = new AndGate2(_gate1.getOutput(), false); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Inputs receive same value. | ||
+ | * | ||
+ | * @param v | ||
+ | * the input value. | ||
+ | */ | ||
+ | public AndGate3c(boolean v) { | ||
+ | _gate1 = new AndGate2(v); | ||
+ | _gate2 = new AndGate2(_gate1.getOutput(), v); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Arbitrary input value combinations. | ||
+ | * | ||
+ | * @param a | ||
+ | * input value | ||
+ | * @param b | ||
+ | * input value | ||
+ | * @param c | ||
+ | * input value | ||
+ | */ | ||
+ | public AndGate3c(boolean a, boolean b, boolean c) { | ||
+ | _gate1 = new AndGate2(a, b); | ||
+ | _gate2 = new AndGate2(_gate1.getOutput(), c); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @return first input value. | ||
+ | */ | ||
+ | public boolean getA() { | ||
+ | return _gate1.getA(); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Set input value. | ||
+ | * | ||
+ | * @param a | ||
+ | * input value. | ||
+ | */ | ||
+ | public void setA(boolean a) { | ||
+ | _gate1.setA(a); | ||
+ | _gate2.setA(_gate1.getOutput()); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @return second input value. | ||
+ | */ | ||
+ | public boolean getB() { | ||
+ | return _gate1.getB(); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Set input value. | ||
+ | * | ||
+ | * @param b | ||
+ | * input value. | ||
+ | */ | ||
+ | public void setB(boolean b) { | ||
+ | _gate1.setB(b); | ||
+ | _gate2.setA(_gate1.getOutput()); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @return second input value. | ||
+ | */ | ||
+ | public boolean getC() { | ||
+ | return _gate2.getB(); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Set input value. | ||
+ | * | ||
+ | * @param c | ||
+ | * input value. | ||
+ | */ | ||
+ | public void setC(boolean c) { | ||
+ | _gate2.setB(c); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Since the two gates are always kept in sync, we only need to ask for the | ||
+ | * output of the second gate. | ||
+ | * | ||
+ | * @return value of logical AND operation. | ||
+ | */ | ||
+ | public boolean getOutput() { | ||
+ | return _gate2.getOutput(); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @see java.lang.Object#equals(java.lang.Object) | ||
+ | */ | ||
+ | @Override | ||
+ | public boolean equals(Object other) { | ||
+ | if (other instanceof AndGate3c) { | ||
+ | AndGate3c ag = (AndGate3c) other; | ||
+ | return getA() == ag.getA() && getB() == ag.getB() && getC() == ag.getC(); | ||
+ | } | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @see java.lang.Object#toString() | ||
+ | */ | ||
+ | @Override | ||
+ | @SuppressWarnings("nls") | ||
+ | public String toString() { | ||
+ | return "A:" + getA() + " B:" + getB() + " C:" + getC(); | ||
+ | } | ||
+ | } | ||
+ | </java5> | ||
[[category:Ensino]] | [[category:Ensino]] | ||
[[category:PO]] | [[category:PO]] |
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.
Nesta solução, a porta de 3 entradas usa duas de duas entradas para efectuar a operação. Note-se que as entradas das duas portas são calculadas sempre que uma das entradas (a, b, c) da porta de 3 entradas é modificada.
<java5> /** Logical AND gate with 3 inputs. */ public class AndGate3c { /** The first gate takes inputs 'a' and 'b' */ private AndGate2 _gate1;
/** * The second gate takes as inputs the output of the first gate (input 'a') * and 'c' (input 'b') */ private AndGate2 _gate2;
/** * Default constructor: false for all inputs. */ public AndGate3c() { _gate1 = new AndGate2(); _gate2 = new AndGate2(_gate1.getOutput(), false); }
/** * Inputs receive same value. * * @param v * the input value. */ public AndGate3c(boolean v) { _gate1 = new AndGate2(v); _gate2 = new AndGate2(_gate1.getOutput(), v); }
/** * Arbitrary input value combinations. * * @param a * input value * @param b * input value * @param c * input value */ public AndGate3c(boolean a, boolean b, boolean c) { _gate1 = new AndGate2(a, b); _gate2 = new AndGate2(_gate1.getOutput(), c); }
/** * @return first input value. */ public boolean getA() { return _gate1.getA(); }
/** * Set input value. * * @param a * input value. */ public void setA(boolean a) { _gate1.setA(a); _gate2.setA(_gate1.getOutput()); }
/** * @return second input value. */ public boolean getB() { return _gate1.getB(); }
/** * Set input value. * * @param b * input value. */ public void setB(boolean b) { _gate1.setB(b);
_gate2.setA(_gate1.getOutput());
}
/** * @return second input value. */ public boolean getC() { return _gate2.getB(); }
/** * Set input value. * * @param c * input value. */ public void setC(boolean c) { _gate2.setB(c); }
/** * Since the two gates are always kept in sync, we only need to ask for the * output of the second gate. * * @return value of logical AND operation. */ public boolean getOutput() { return _gate2.getOutput(); }
/** * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object other) { if (other instanceof AndGate3c) { AndGate3c ag = (AndGate3c) other; return getA() == ag.getA() && getB() == ag.getB() && getC() == ag.getC(); } return false; }
/** * @see java.lang.Object#toString() */ @Override @SuppressWarnings("nls") public String toString() { return "A:" + getA() + " B:" + getB() + " C:" + getC(); } } </java5>