/* Programmer : Amanda Chaffin and Dottie Stowe User ID : achaffin and dastowe Section : ITCS 2181, Fall 2003 Assignment : Program 2 Due Date : 12/9/03 Purpose : Divider This program is designed to open a file where specified in the code,read the data (integers and whitespace only), convert the numbers to binary, divide the numbers by repeated addition of two's compliment, convert back to decimal and display the results.*/ import java.io.*; import java.util.StringTokenizer; public class DividerTest { public static void main (String args[]) { MyInfoachaffin myInfo = new MyInfoachaffin(); //calling the class holding my info myInfo.info(); File sourceFile = new File("/afs/uncc.edu/usr9/cma/courses/itcs2181/assign2/DivideData.txt"); // access the file // If the file does not exist, print out an error message and exit if (sourceFile.exists() == false) { System.err.println("Oops: " + sourceFile + ": No such file"); System.exit(1); } int a = 0; int b = 0; try //open the file, read the file into a string { String line; StringTokenizer toke; FileReader fr = new FileReader(sourceFile); BufferedReader br = new BufferedReader(fr); while ((line = br.readLine()) != null) { //tokenize the string and remove whitespace //line = line.replaceAll("/s",""); toke = new StringTokenizer(line," ;"); int numt = toke.countTokens(); int numr = (int)(numt/2.0); for(int x = 0; x < numr; x ++) //assign boolean values { a = Integer.parseInt(toke.nextToken()); b = Integer.parseInt(toke.nextToken()); Divider d = new Divider (a, b); d.Divide(); d.PrintAns(); System.out.println(); } } br.close(); //close the stream from the file System.out.println("MISSION ACCOMPLISHED!!!"); } catch (IOException e) { System.out.println("Whoops, there's a mistake somewhere."); } } } class MyInfoachaffin /*Just a seperate class holding the personal info*/ { public void info() { System.out.println("Amanda Chaffin"); System.out.println("Dottie Stowe"); System.out.println("ITCS2181, Fall 2003" ); System.out.println("Divider Program" ); System.out.println("Due: December 9, 2003"); System.out.println(); } } class Divider { private final int[] BinaryValueOf1; private boolean isAnsNeg; private int iDenominator; private int iNumerator; private final int origDenominator; private final int origNumerator; private int iQuotient; private int iRemainder; public Divider (int a, int b) { origNumerator = a; origDenominator = b; //binary value 1 used to get 2's compliment BinaryValueOf1 = new int[32]; BinaryValueOf1 [0] = 1; for (int loop = 1; loop < 32; loop++) BinaryValueOf1[loop] = 0; //if denominator or numerator is negative, answer is negative if (a <=0 || b <= 0) isAnsNeg = true; //if denominator and numerator is negative, answer is positive if ((a <= 0 && b <= 0) || (a > 0 && b >0)) isAnsNeg = false; if (a < 0) iNumerator = a * -1; else iNumerator = a; if (b < 0) iDenominator = b * -1; else iDenominator = b; iQuotient = 0; iRemainder = 0; if (b != 0) Divide(); else System.out.println("You cannot divide by zero!"); } public void setNumerator(int a) { iNumerator = a; } public int getNumerator() { return origNumerator; } public void setDenominator(int a) { iDenominator = a; } public int getDenominator() { return origDenominator; } public void setQuotient(int a) { iQuotient = a; } public int getQuotient() { return iQuotient; } public void setRemainder(int a) { iRemainder = a; } public int getRemainder() { return iRemainder; } // performs the division by compliment, shift, and then add public void Divide() { int[] denominator = new int[64]; int[] numerator = new int[64]; int[] sum = new int[64]; int[] quotient = new int[32]; for (int i = 0; i < 32; i++) { numerator[i] = IntToBin(iNumerator)[i]; numerator[i+32] = 0; denominator[i] = 0; denominator[i+32] = Compliment(IntToBin(iDenominator))[i]; } for (int i = 31; i >= 0; i--) { denominator = RightShift(denominator); sum = BinaryAdd(numerator, denominator); if (sum[63] == 1) { quotient[i] = 0; } else { quotient[i] = 1; numerator = sum; } } if(isAnsNeg) setQuotient(-1 * BinToInt(quotient)); else setQuotient(BinToInt(quotient)); setRemainder(BinToInt(numerator)); } //converts int to binary public int[] IntToBin(int a) { int[] iArray = new int[32]; int iRemain = 0; //converts to binary by repeated division of 2 for (int loop = 0; loop < 32; loop ++) { iRemain = a % 2; a = (a - iRemain)/2; iArray[loop] = iRemain; } return iArray; } //converts binary to int public int BinToInt(int[] iArray) { int bits = iArray.length; int out = 0; for (int i = 0; i < bits; i++) { if(iArray[i] == 1) out = out + (int)Math.pow(2,i); } return out; } //2's compliment private int[] Compliment(int[] iArray) { int[] iComplimented = new int[32]; //loop to do 1's compliment for(int i = 0; i < 32; i++) { if(iArray[i] == 0) iComplimented[i] = 1; else iComplimented[i] = 0; } //add 1 for 2's compliment iComplimented = BinaryAdd(iComplimented, BinaryValueOf1); return iComplimented; } //addition private int[] BinaryAdd(int[] iArray1, int[] iArray2) { int bits = iArray1.length; int[] out = new int [bits]; int iCarry = 0; for(int i = 0; i < bits; i++) { out[i] = (iArray1[i] + iArray2[i] + iCarry) % 2; if(iArray1[i] + iArray2[i] + iCarry > 1) iCarry = 1; else iCarry = 0; } return out; } //shifter (does consider neg and pos values) private int[] RightShift(int[] iArray) { int bits = iArray.length; int[] out = new int[bits]; for(int i = 1; i < bits - 1; i++) { out[i-1] = iArray[i]; } out[bits-1] = out[bits-3]; out[bits-2] = out[bits-3]; return out; } //prints binary number for the answer private void PrintArray(int[] iArray) { for(int i = iArray.length - 1; i >= 0; i--) System.out.println(iArray[i]); System.out.println(); } //prints the answer in decimal public void PrintAns() { System.out.println("If the numerator is " + getNumerator() + " and the denominator is " + getDenominator()); System.out.println("Then the quotient is " + getQuotient() + " and the remainder is " + getRemainder()); } }