/* Renata Lara */ /* Computer Architecture */ /* Dr. Abraham */ /* February 22, 1999 */ /* Class HalfAdder performs the basic */ /* ANDgate, ORgare, and NOTgate functions. */ /* It inputs the bits from the keyboard and */ /* parses them to make them booleans. Then */ /* the individual functions are calles from */ /* Class1.java and they return the sum and */ /* carrybit accordingly. */ import java.io.*; public class HalfAdder { public HalfAdder()throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(isr); System.out.print("Enter bit A: "); inA = in.readLine(); System.out.flush(); System.out.print("Enter bit B: "); inB = in.readLine(); System.out.flush(); System.out.print("Enter bit C: "); inC = in.readLine(); System.out.flush(); System.out.println(); //StringToBinary(); parseBitString(); } public void parseBitString() { for (int j = 0; j<2; j++) { temp = inA.substring(1-j,1-j+1); intA = Integer.parseInt(temp); arrayA[j] = intA; temp = inB.substring(1-j,1-j+1); intB = Integer.parseInt(temp); arrayB[j] = intB; temp = inC.substring(1-j,1-j+1); intC = Integer.parseInt(temp); arrayC[j] = intC; } } public void StringToBinary() { intA = Integer.parseInt(inA); intB = Integer.parseInt(inB); intC = Integer.parseInt(inC); } public boolean BinaryToBoolean(int bit) { if (bit == 1) return true; else return false; } public int getA(int m) {return arrayA[m];} public int getB(int m) {return arrayB[m];} public int getC(int m) {return arrayC[m];} public int ANDgate(int num1, int num2) { bit1 = BinaryToBoolean(num1); bit2 = BinaryToBoolean(num2); if (bit1 && bit2) return 1; else return 0; } public int ORgate(int num1, int num2) { bit1 = BinaryToBoolean(num1); bit2 = BinaryToBoolean(num2); if (bit1 || bit2) return 1; else return 0; } public int NOTgate(int num) { bit1 = BinaryToBoolean(num); if (bit1) return 0; else return 1; } private String inA; private String inB; private String inC; private String temp; private int[] arrayA = new int[2]; private int[] arrayB = new int[2]; private int[] arrayC = new int[2]; private int intA; private int intB; private int intC; //private int m; private boolean bit1; private boolean bit2; }