/**
* The Convert4Positions class will convert a string of length four
* into a positive integer, and a positive integer into string of
* length four. The radix of the string is specified with the
* a Convert4Positions object is created.
*
* @author Daniel Stratton
*/
public class Convert4Positions {
private int radix;
/**
* Construct a Convert4Positions object with the specified
* radix.
* @param radix the radix for the conversions.
*/
public Convert4Positions( int radix ) {
Convert4Positions decimal = new Convert4Positions( 10 );
assert decimal.int2string( 8976 ).equals( "8976" );
assert decimal.string2int( "6384" ) == 6384;
Convert4Positions octal = new Convert4Positions( 8 );
assert octal.int2string( 01777 ).equals( "1777" );
assert octal.string2int( "02534" ) == 2534;
Convert4Positions binary = new Convert4Positions( 2 );
assert binary.int2string( 1101 ).equals( "1101" );
assert binary.string2int( "1001" ) == 1001;
}
/**
* int2string converts an integer to 4 position string.
*
* @param value, the value to convert
* @return a string with four digits that represents the integer
*/
public String int2string( int value ) {
// hint: see Character.forDigit
String str = "";
return str;
}
/**
* string2int converts a string of length four into an integer.
* The class uses the defined radix for the converion.
*
* @param str, the string to convert
* @return the integer value of the converted string.
*/
public int string2int( String str ) {
// hint: use charAt(0) for first char, charAt(1) for next, ...
// hint: see Character.digit
int v = 0;
return v;
}
// code for testing
public static void printInt2StringDecimal(Convert4Positions conv, int v ) {
String sv = conv.int2string( v );
int iv = conv.string2int( sv );
// %04d converts an integer into a decimal number with 4 postions
// with zero padding
System.out.printf("int2string: %04d is %s%n", v, sv );
System.out.printf("string2int: %s is %04d%n", sv, iv );
}
public static void printInt2StringOctal(Convert4Positions conv, int v ) {
String sv = conv.int2string( v );
int iv = conv.string2int( sv );
// %04o converts an integer into a octal number with 4 postions
// with zero padding
System.out.printf("int2string: %04o is %s%n", v, sv );
System.out.printf("string2int: %s is %04o%n", sv, iv );
}
public static void printInt2StringBinary(Convert4Positions conv, String sv ) {
int iv = conv.string2int( sv );
String ssv = conv.int2string( iv );
// there will be no leading zeros
System.out.printf("int2string: %s is %s%n", sv, Integer.toBinaryString(iv) );
// I am using the orginal sv
System.out.printf("int2string: %s is %s%n", sv, ssv );
}
/**
* Provide simple console output based testing.
*/
public static void main( String[] args ) {
System.out.println("Decimal");
Convert4Positions decimal = new Convert4Positions( 10 );
printInt2StringDecimal( decimal, 1234 );
printInt2StringDecimal( decimal, 9876 );
printInt2StringDecimal( decimal, 47 );
System.out.println();
System.out.println("Octal");
Convert4Positions octal = new Convert4Positions( 8 );
// In Java, octal numbers start with 0
printInt2StringOctal( octal, 01234 );
printInt2StringOctal( octal, 07654 );
printInt2StringOctal( octal, 034 );
System.out.println();
System.out.println("Binary");
Convert4Positions binary = new Convert4Positions( 2 );
printInt2StringBinary( binary, "0101" );
printInt2StringBinary( binary, "1001" );
}
}