/* ---------------------------------------------------------------
Trivial conversion: match the input string to every possible
number string and return the corresponding
2's complement binary number
--------------------------------------------------------------- */
public static int parseInt( String s )
{
if ( s.equals("0") )
return 0; // The return statement will return the binary number
// 00000000000000000000000000000000 !!
else if ( s.equals("1") )
return 1; // returns: 00000000000000000000000000000001
else if ( s.equals("2") )
return 2; // returns: 00000000000000000000000000000010
...
else if ( s.equals("10") )
return 10; // returns: 00000000000000000000000000001010
else if ( s.equals("11") )
return 11; // returns: 00000000000000000000000000001011
...
else if ( s.equals("-1") )
return -1; // returns: 11111111111111111111111111111111
else if ( s.equals("-2") )
1eturn -2; // returns: 11111111111111111111111111111110
...
}
|
public static String toString( int x )
{
if ( x == 0 )
return "0"; // Returns the ASCII representation for 0 !!
else if ( x == 1 )
return "1";
else if ( x == 2 )
return "2";
...
else if ( x == 10 )
return "10";
else if ( x == 11 )
return "11";
...
else if ( x == -1 )
return "-1";
else if ( x == -2 )
return "-2";
...
}
|
|
But the trivial solutions are not practical because it's impossible to write out 232 different cases !!!!
But before I can do so, I will present the necessary pre-requisite in character/string manipulation operations of Java (that you have had in CS170/CS171) -- i.e.: refresher)