public class Binary {
// We now declare our instance variables.
String original;
String quadra;
String octal;
int decimal;
String hexadecimal;
public Binary(String original) {
// The purpose of the constructor is to initialize the instance variable
// original and initialize the other instance variables by making calls
// to the corresponding instance method.
this.original = original;
quadra = toQuadra();
octal = toOctal();
decimal = toDecimal();
hexadecimal = toHexadecimal();
}
public String toQuadra() {
// We follow the strategy discussed in class. We determine whether
// or not the length of the string is evenly divisible by 2.
String outputString = "";
String temp = "";
int r = original.length() % 2;
// If it is not, then we take the first bit and get its quadradecimal
// equivalent and initialize our outputString. Otherwise we initialize
// our output string to an empty string.
if (r == 1)
outputString = Binary.equiv("0",original.substring(0,1));
else
outputString = "";
// We now step through the rest of the string grabbing groups of 2 bits,
// converting them to their quadradecimal equivalent, and then
// concatenating that equivalent to the outputString.
for (int counter = r;counter < original.length();counter+=2) {
String bit1 = original.substring(counter,counter+1);
String bit2 = original.substring(counter+1,counter+2);
outputString = outputString.concat(Binary.equiv(bit1,bit2));
}
return(outputString);
}
public String toOctal() {
// We follow almost the same strategy as in toQuadra(). We determine
// whether or not the length of the string is evenly divisible by 3.
String outputString = "";
String temp = "";
int r = original.length() % 3;
// If it is not, then either it has remainders 2 or 1 when divided by 3.
// If it has remainder 2, then we left pad the string with a 0 and
// get the octal equivalent of the first three bits. If it has remainder
// 1, then we left pad the string with 00 and get the octal equivalent of
// the first three bits. We initialize the outputString to the equivalent.
// If it is evenly divisible by 3, then we initialize the outputString to
// an empty string.
if (r == 2)
outputString = Binary.equiv("0",original.substring(0,1),original.substring(1,2));
else if (r == 1)
outputString = Binary.equiv("0","0",original.substring(0,1));
else
outputString = "";
// We now step through the string grabbing groups of 3 bits and converting
// them to their octal equivalent and concatenating the equivalent to
// the outputString.
for (int counter = r;counter < original.length();counter+=3) {
String bit1 = original.substring(counter,counter+1);
String bit2 = original.substring(counter+1,counter+2);
String bit3 = original.substring(counter+2,counter+3);
outputString = outputString.concat(Binary.equiv(bit1,bit2,bit3));
}
return(outputString);
}
public String toHexadecimal() {
// We follow almost the same strategy as in toQuadra() and toOctal(). We
// determine whether or not the length of the string is evenly divisible
// by 4.
String outputString = "";
String temp = "";
int r = original.length() % 4;
// Depending on the remainder we initialize the outputString.
if (r == 3)
outputString = Binary.equiv("0",original.substring(0,1),original.substring(1,2),original.substring(2,3));
else if (r == 2)
outputString = Binary.equiv("0","0",original.substring(0,1),original.substring(1,2));
else if (r == 1)
outputString = Binary.equiv("0","0","0",original.substring(0,1));
else
outputString = "";
// We now step through the string grabbing groups of 4 bits and converting
// them to their hexadecimal equivalent and concatenating that equivalent
// to the outputString.
for (int counter = r;counter < original.length();counter+=4) {
String bit1 = original.substring(counter,counter+1);
String bit2 = original.substring(counter+1,counter+2);
String bit3 = original.substring(counter+2,counter+3);
String bit4 = original.substring(counter+3,counter+4);
outputString = outputString.concat(Binary.equiv(bit1,bit2,bit3,bit4));
}
return(outputString);
}
public int toDecimal() {
// We step through the bits in the string, and based on whether or not
// the bit is a 1, we accumulate 2 raised to a certain power.
int power = original.length() - 1;
int sum = 0;
for (int counter = power;counter >= 0;counter--)
if (original.charAt(original.length()-1-counter) == '1')
sum += Math.pow(2,counter);
return(sum);
}
public String toString() {
String line = "\n";
line += "Original" + "\t";
line += "QuadraDecimal" + "\t";
line += "Octal" + "\t";
line += "Decimal" + "\t";
line += "Hexadecimal" + "\n";
line += original + "\t";
line += quadra + "\t";
line += octal + "\t";
line += decimal + "\t";
line += hexadecimal + "\n";
return(line);
}
public static String equiv(String bit1,String bit2) {
int tens = new Integer(bit1).intValue();
int units = new Integer(bit2).intValue();
int number = 2*tens + units;
String returnString="";
switch (number) {
case 0 : returnString = "0"; break;
case 1 : returnString = "1"; break;
case 2 : returnString = "2"; break;
case 3 : returnString = "3"; break;
}
return(returnString);
}
public static String equiv(String bit1,String bit2,String bit3) {
int hundreds = new Integer(bit1).intValue();
int tens = new Integer(bit2).intValue();
int units = new Integer(bit3).intValue();
int number = 4*hundreds + 2*tens + units;
String returnString = "";
switch(number) {
case 0 : returnString = "0"; break;
case 1 : returnString = "1"; break;
case 2 : returnString = "2"; break;
case 3 : returnString = "3"; break;
case 4 : returnString = "4"; break;
case 5 : returnString = "5"; break;
case 6 : returnString = "6"; break;
case 7 : returnString = "7"; break;
}
return(returnString);
}
public static String equiv(String bit1,String bit2,String bit3,String bit4) {
int thousands = new Integer(bit1).intValue();
int hundreds = new Integer(bit2).intValue();
int tens = new Integer(bit3).intValue();
int units = new Integer(bit4).intValue();
int number = 8*thousands + 4*hundreds + 2*tens + units;
String returnString = "";
switch (number) {
case 0 : returnString = "0"; break;
case 1 : returnString = "1"; break;
case 2 : returnString = "2"; break;
case 3 : returnString = "3"; break;
case 4 : returnString = "4"; break;
case 5 : returnString = "5"; break;
case 6 : returnString = "6"; break;
case 7 : returnString = "7"; break;
case 8 : returnString = "8"; break;
case 9 : returnString = "9"; break;
case 10 : returnString = "A"; break;
case 11 : returnString = "B"; break;
case 12 : returnString = "C"; break;
case 13 : returnString = "D"; break;
case 14 : returnString = "E"; break;
case 15 : returnString = "F"; break;
}
return(returnString);
}
public static void main(String[] args) {
Binary myBinary = new Binary("1010");
System.out.println(myBinary);
}
}
Recall our discussion of preincrementing and postincrementing.