/**
* Sample project that converts a decimal number to a hexadecimal
* and binary number.
*
* Converter is a single-class application and is aimed at novice students
* who have just started their Java course.
*/
public class Converter
{
/**
* Main method for the Converter class.
*/
public static void main (String arguments[])
{
/**
* First we ensure that the user has provided us with
* a parameter that we can use to do the conversion with.
*/
if (arguments.length == 0)
{
System.out.println ("Usage :");
System.out.println ("javac Converter ");
System.exit (1);
}
/**
* Now that we have a parameter, let's try to convert it
* to a number. If we fail, we give an error message and
* terminate program execution.
*/
int number = 0;
try
{
number = Integer.parseInt (arguments [0]);
}
catch (NumberFormatException e)
{
System.out.println ("The number specified is not a valid decimal!");
System.exit (2);
}
/**
* Once we get here we know the number is a valid decimal number.
* Output the different formats.
*/
System.out.print ("The number specified is : ");
System.out.println (number);
System.out.print ("The hexadecimal value is : ");
System.out.println (Integer.toHexString (number));
System.out.print ("The binary value is : ");
System.out.println (Integer.toBinaryString (number));
System.out.print ("The octal value is : ");
System.out.println (Integer.toOctalString (number));
}
}
|