| HalfAdder.java |
1 /**
2 * 183.592 Programmierpraxis TU Wien WS2014/15 H.Moritsch
3 * Halbaddierer
4 */
5 public class HalfAdder {
6 public static void main(String[] args) {
7
8 // Eingänge des Halbadierers: (Pseudo-) Gatter ohne Operation
9 Gate x = new Gate();
10 Gate y = new Gate();
11
12 // Aufbau des Halbadierers aus den Gattern
13 Gate not1 = new Gate("NOT", x); // NOT x
14 Gate not2 = new Gate("NOT", y); // NOT y
15 Gate and1 = new Gate(not1, "AND", y); // (NOT x) AND y
16 Gate and2 = new Gate(x, "AND", not2); // x and (NOT y)
17 Gate or = new Gate(and1, "OR", and2); // s = ((NOT x) AND y) OR (x and (NOT y))
18 Gate and3 = new Gate(x, "AND", y); // c = x AND y
19
20 // Belegung der Eingänge
21 x.setValue(true);
22 y.setValue(false);
23
24 // Halbadierer schaltet: Durchführung der Operationen aller Gatter (Reihenfolge!)
25 not1.operate();
26 not2.operate();
27 and1.operate();
28 and2.operate();
29 or.operate();
30 and3.operate();
31
32 // Ergebnis
33 boolean s = or.getValue(); // sum
34 boolean c = and3.getValue(); // carry
35
36 System.out.println("sum = " + s);
37 System.out.println("carry = " + c);
38
39 }
40
41 }
42