public class BinaryCalculator { /* * Main method scans input from text and prints out the * expected results. */ /* * Converts a binary number to base 10 number */ private static String decimalConversion(String binaryNumber) { String decimal = "0"; String[] temp = new String[2];; for(int i = binaryNumber.length() - 1; i >= 0; i--) { if(binaryNumber.charAt(i) == '1') { temp[0] = decimal; temp[1] = split(binaryNumber.length() - i); decimal = addDecimal(temp); } } return decimal; } /* * Splits the decimal conversion into smaller segments * to prevent an overflow */ private static String split(int index) { if(index < 20) return decimalValueOfBit(index) + ""; int segment = index/2; return multiplyDecimal(split(segment), split(index - segment)); } /* * Returns the base 10 value of a single bit according to its index */ private static int decimalValueOfBit(int length) { if(length == 0) return 0; if(length == 1) return 1; else return decimalValueOfBit(length - 1) * 2; } /* * Multiplies the smaller segments to get the base 10 value of the * binary number. */ private static String multiplyDecimal(String number1, String number2) { String productRow = ""; String moveDownOne = ""; String tempProductColumn = ""; int carryNumber = 0; int a, b, productColumn; int differenceInLength = 0; String[] toBeAdded = new String[number2.length()]; for(int i = number2.length() - 1; i >= 0; i--) { productRow = moveDownOne + ""; for(int j = number1.length() - 1; j >= 0; j--) { a = Integer.parseInt(number2.charAt(i) + ""); b = Integer.parseInt(number1.charAt(j) + ""); productColumn = a * b; if(carryNumber > 0) { productColumn = productColumn + carryNumber; } tempProductColumn = productColumn + ""; productRow = tempProductColumn.charAt(tempProductColumn.length() - 1) + productRow; if(tempProductColumn.length() == 2) carryNumber = Integer.parseInt(tempProductColumn.charAt(0) + ""); else carryNumber = 0; } if(carryNumber > 0) { productRow = carryNumber + productRow; carryNumber = 0; } for(int j = 0; j < (number2.length() - 1) - i; j++) { moveDownOne += 0; } toBeAdded[i] = productRow + moveDownOne; moveDownOne = ""; } return addDecimal(toBeAdded); } /* * The second step in long multiplication, addition. */ private static String addDecimal(String[] toBeAdded) { int a, b, sum; int carryNumber = 0; int differenceInLength = 0; String tempSum; String decimalSum = ""; for(int i = 1; i < toBeAdded.length; i++) { if(toBeAdded[i - 1].length() != toBeAdded[i].length()) { if(toBeAdded[i - 1].length() > toBeAdded[i].length()) { differenceInLength = toBeAdded[i - 1].length() - toBeAdded[i].length(); toBeAdded[i] = shareItemLength(differenceInLength) + toBeAdded[i]; } else if(toBeAdded[i - 1].length() < toBeAdded[i].length()) { differenceInLength = toBeAdded[i].length() - toBeAdded[i - 1].length(); toBeAdded[i - 1] = shareItemLength(differenceInLength) + toBeAdded[i - 1]; } } for(int j = toBeAdded[i].length() - 1; j >= 0; j--) { a = Integer.parseInt(toBeAdded[i - 1].charAt(j) + ""); b = Integer.parseInt(toBeAdded[i].charAt(j) + ""); sum = a + b; if(carryNumber > 0) { sum += carryNumber; } tempSum = sum + ""; decimalSum = tempSum.charAt(tempSum.length() - 1) + decimalSum; if(tempSum.length() == 2) carryNumber = Integer.parseInt(tempSum.charAt(0) + ""); else carryNumber = 0; } toBeAdded[i] = decimalSum; decimalSum = ""; } if(carryNumber > 0) toBeAdded[toBeAdded.length - 1] = carryNumber + toBeAdded[toBeAdded.length - 1]; decimalSum = toBeAdded[toBeAdded.length - 1]; return decimalSum; } /* * Converts a decimal integer into a binary number. */ private static String binaryConversion(int decimal) { String backwardBinary = ""; int remainder; // the remainders form the binary number backwards while(decimal > 0) { remainder = decimal % 2; backwardBinary += remainder + ""; decimal = decimal / 2; } String binary = ""; int j = backwardBinary.length() - 1; // flips the backward binary number to the correct order for(int i = 0; i < backwardBinary.length(); i++) { binary += backwardBinary.charAt(j); j--; } return binary; } /* * Converts a binary number into a hexadecimal value. */ private static String hexadecimalConversion(String binary) { String singleHex = ""; String hexadecimalCode = ""; int decimal; int count = 1; for(int i = binary.length(); i > 0; i--) { if(count % FOUR_BIT_LENGTH == 0) { singleHex = binary.charAt(i - 1) + singleHex; decimal = hexaHelper(singleHex); hexadecimalCode = decimalToHexadecimal(decimal) + hexadecimalCode; singleHex = ""; // resets singleHex } else singleHex = binary.charAt(i - 1) + singleHex; count++; } // since count starts at 1 count = count - 1; if(count % FOUR_BIT_LENGTH != 0) { decimal = Integer.parseInt(decimalConversion(singleHex)); hexadecimalCode = decimalToHexadecimal(decimal) + hexadecimalCode; } return hexadecimalCode; } /* Accepts a decimal value from hexadecimalConversion() method to be used as an index for the hexAlphabet, and then returns that character. */ private static String decimalToHexadecimal(int decimal) { String hexAlphabet = "0123456789ABCDEF"; return hexAlphabet.charAt(decimal) + ""; } private static final int FOUR_BIT_LENGTH = 4; private static final int MAX_LENGTH = 20; }