I have about 6 different projects due tomorrow and have been programming all week. The code for this specific program is below and if it seems a bit roundabout, that is because the constraints are requiring some different methods to solve this problem. Anyways here is the code below.
The problem I am having is with the add method. I am almost positive I am making it more complicated than it needs to be. The code currently in the method is not refactored. I have changed it a few times getting close to the results I need but never managing to get the results. Furthermore the return value is always going to be 8 bits for example, 11111111 + 00000001 = 11111111 and negatives do not apply.
Code:
package extraCredit;
// Encapsulation of binary numbers
import java.util.Scanner;
class BinaryNumber {
// Store the binary number as an array of boolean
static final int NBITS = 8;
private boolean [] bits = new boolean [NBITS];
/** Constructor initializes with low bits at end of array */
public BinaryNumber(String s) {
//if the length is to short we have to append that needed 0's to the front in order to avoid string length out of range errors
if (s.length() < 8){
while (s.length() != 8){
s = "0" + s;
}
}
for (int i = bits.length - 1, j = 1 ; i >= 0; i--, j++){
if( s.charAt(s.length() - j) == '1'){
this.bits[i] = true;
}else
this.bits[i] = false;
}
}
/** Convert to decimal integer value */
public int intValue() {
int intValue = 0;
for (int i = 0, j = 1; i < bits.length; i++, j *= 2){
if (this.bits[(bits.length - 1) - i] == true){
intValue += j;
}else
continue;
}
return intValue;
}
/** Determine if two BinaryNumbers are equivalent */
public boolean equals(BinaryNumber b2) {
int occurences = 0;
for (int i = 0; i < bits.length; i++){
if (this.bits[i] != b2.bits[i] ){
occurences++;
}else
continue;
}
if (occurences == 0){
return true;
}else
return false;
}
/** Produce String representation of BinaryNumber */
public String toString() {
String returnString = new String("");
for (int i = 0; i < bits.length; i++){
if (bits[i] == true){
returnString += 1;
}else
returnString += 0;
}
return returnString;
}
public BinaryNumber add(BinaryNumber b2){
boolean twoCompliment[] = new boolean [8];
String sumString = new String("null");
int length = bits.length - 1;
for (int i = 0, j = 1; i < bits.length; i++, j++){
if (this.bits[length - i] == false && b2.bits[length - i] == false && twoCompliment[length - i] == false){
sumString += "0";
twoCompliment[twoCompliment.length - j] = false;
} else if (this.bits[length - i] == false && b2.bits[length - i] == true && twoCompliment[length - i] == false){
sumString += "1";
twoCompliment[twoCompliment.length - j] = false;
} else if (this.bits[length - i] == false && b2.bits[length - i] == false && twoCompliment[length - i] == true){
sumString += "1";
twoCompliment[twoCompliment.length - j] = false;
} else if (this.bits[length - i] == false && b2.bits[length - i] == true && twoCompliment[length - i] == true){
sumString += "0";
twoCompliment[twoCompliment.length - j] = true;
} else if (this.bits[length - i] == false && b2.bits[length - i] == false && twoCompliment[length - i] == true){
sumString += "1";
twoCompliment[twoCompliment.length - j] = false;
} else if (this.bits[length - i] == true && b2.bits[length - i] == false && twoCompliment[length - i] == false){
sumString += "1";
twoCompliment[twoCompliment.length - j] = false;
} else if (this.bits[length - i] == true && b2.bits[length - i] == true && twoCompliment[length - i] == false){
sumString += "0";
twoCompliment[twoCompliment.length - j] = true;
} else if (this.bits[length - i] == true && b2.bits[length - i] == false && twoCompliment[length - i] == true){
sumString += "0";
twoCompliment[twoCompliment.length - j] = true;
} else if (this.bits[length - i] == true && b2.bits[length - i] == true && twoCompliment[length - i] == true){
sumString += "1";
twoCompliment[twoCompliment.length - j] = true;
}
}
BinaryNumber sum = new BinaryNumber(sumString);
return sum;
}
// Main method to test BinaryNumber
public static void main(String[] args) {
// Prompt for numbers if not on command line
String number1, number2;
if (args.length < 1) {
Scanner input = new Scanner(System.in);
System.out.print("Enter two eight bit binary numbers: ");
number1 = input.next();
number2 = input.next();
} else {
number1 = args[0];
number2 = args[1];
}
BinaryNumber b1 = new BinaryNumber(number1);
BinaryNumber b2 = new BinaryNumber(number2);
BinaryNumber b3;
System.out.println("b1 is " + b1 + " (" + b1.intValue() + ")");
System.out.println("b2 is " + b2 + " (" + b2.intValue() + ")");
System.out.println("b1.equals(b2) is " + b1.equals(b2));
b3 = b1.add(b2);
System.out.println("b3=b1.add(b2) is " + b3 + " (" + b3.intValue() + ")");
b3 = b3.add(b1);
System.out.println("b3=b3.add(b1) is " + b3 + " (" + b3.intValue() + ")");
b2 = b2.add(b1).add(b1);
System.out.println("b2=b2.add(b1).add(b1) is " + b3 + " (" + b2.intValue() + ")");
System.out.println("b2.equals(b3) is " + b2.equals(b3));
}
}