
public class HalfAdder extends DigitalComponent {

	private OrGate or1 = new OrGate(2);
	private AndGate and1 = new AndGate(2), and2 = new AndGate(2), and3 = new AndGate(2);
	private NotGate not1 = new NotGate(), not2 = new NotGate();


	public HalfAdder() {
		super(2, 2);

		Wire w1 = new Wire();
		and1.addOutput(w1, 0);
		or1.addInput(w1, 0);

		Wire w2 = new Wire();
		and2.addOutput(w2, 0);
		or1.addInput(w2, 1);

		Wire w3 = new Wire();
		not1.addOutput(w3, 0);
		and1.addInput(w3, 0);

		Wire w4 = new Wire();
		not2.addOutput(w4, 0);
		and2.addInput(w4, 1);
	}

	public void addInput(Wire w, int index) {
		super.addInput(w, index);
		if (index == 0) {
			not1.addInput(w, 0);
			and2.addInput(w, 0);
			and3.addInput(w, 0);
		} else if (index == 1) {
			and1.addInput(w, 1);
			not2.addInput(w, 0);
			and3.addInput(w, 1);
		}
	}

	public void addOutput(Wire w, int index) {
		super.addOutput(w, index);
		if (index == 0) {
			or1.addOutput(w, index);
		} else if (index == 1) {
			and3.addOutput(w, 0);
		}
	}

}