(→Solução) |
|||
Line 14: | Line 14: | ||
= Solução = | = Solução = | ||
+ | |||
+ | <java5> | ||
+ | /** | ||
+ | * Logical AND gate. | ||
+ | */ | ||
+ | public class AndGate2 { | ||
+ | private boolean _a = false; | ||
+ | private boolean _b = false; | ||
+ | |||
+ | /** | ||
+ | * Default constructor: false for all inputs. | ||
+ | */ | ||
+ | public AndGate2() { | ||
+ | // nothing to do | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Inputs receive same value. | ||
+ | * | ||
+ | * @param v | ||
+ | * the input value. | ||
+ | */ | ||
+ | public AndGate2(boolean v) { | ||
+ | _a = _b = v; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Arbitrary input value combinations. | ||
+ | * | ||
+ | * @param a | ||
+ | * input value | ||
+ | * @param b | ||
+ | * input value | ||
+ | */ | ||
+ | public AndGate2(boolean a, boolean b) { | ||
+ | _a = a; | ||
+ | _b = b; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @return first input value. | ||
+ | */ | ||
+ | public boolean getA() { | ||
+ | return _a; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Set input value. | ||
+ | * | ||
+ | * @param a | ||
+ | * input value. | ||
+ | */ | ||
+ | public void setA(boolean a) { | ||
+ | _a = a; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @return second input value. | ||
+ | */ | ||
+ | public boolean getB() { | ||
+ | return _b; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Set input value. | ||
+ | * | ||
+ | * @param b | ||
+ | * input value. | ||
+ | */ | ||
+ | public void setB(boolean b) { | ||
+ | _b = b; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @return value of logical AND operation. | ||
+ | */ | ||
+ | public boolean getOutput() { | ||
+ | return _a && _b; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @see java.lang.Object#equals(java.lang.Object) | ||
+ | */ | ||
+ | @Override | ||
+ | public boolean equals(Object other) { | ||
+ | if (other instanceof AndGate2) { | ||
+ | AndGate2 andGate = (AndGate2) other; | ||
+ | return _a == andGate.getA() && _b == andGate.getB(); | ||
+ | } | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @see java.lang.Object#toString() | ||
+ | */ | ||
+ | @Override | ||
+ | @SuppressWarnings("nls") | ||
+ | public String toString() { | ||
+ | return "A:" + _a + " B:" + _b; | ||
+ | } | ||
+ | } | ||
+ | </java5> | ||
[[category:Ensino]] | [[category:Ensino]] | ||
[[category:PO]] | [[category:PO]] |
Contents |
Realize uma classe Java que represente uma porta lógica AND com duas entradas. Esta classe deve chamar-se AndGate2 e ter, entre outras que sejam (justificadamente) julgadas necessárias, a seguinte funcionalidade:
Após ter definido a classe, codifique uma aplicação e a função main respectiva, por forma a demonstrar o correcto funcionamento desta classe. Escolha diferentes combinações de entradas por forma a ter valores distintos na saída dos dois objectos.
<java5> /**
* Logical AND gate. */
public class AndGate2 { private boolean _a = false; private boolean _b = false;
/** * Default constructor: false for all inputs. */ public AndGate2() { // nothing to do }
/** * Inputs receive same value. * * @param v * the input value. */ public AndGate2(boolean v) { _a = _b = v; }
/** * Arbitrary input value combinations. * * @param a * input value * @param b * input value */ public AndGate2(boolean a, boolean b) { _a = a; _b = b; }
/** * @return first input value. */ public boolean getA() { return _a; }
/** * Set input value. * * @param a * input value. */ public void setA(boolean a) { _a = a; }
/** * @return second input value. */ public boolean getB() { return _b; }
/** * Set input value. * * @param b * input value. */ public void setB(boolean b) { _b = b; }
/** * @return value of logical AND operation. */ public boolean getOutput() { return _a && _b; }
/** * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object other) { if (other instanceof AndGate2) { AndGate2 andGate = (AndGate2) other; return _a == andGate.getA() && _b == andGate.getB(); } return false; }
/** * @see java.lang.Object#toString() */ @Override @SuppressWarnings("nls") public String toString() { return "A:" + _a + " B:" + _b; } } </java5>