Java Programming Numeric Conversions

This programming assignment is designed to evaluate your application of binary number conversions to different representations and visa versa. Knowledge, comprehension and application of some Java programming constructs - simple methods, expressions, string manipulation, arrays, conditionals and loops - will also be evaluated. It is expected that a program can be written that compiles and runs correctly. You should work on this programming assignment on your own. However, collaboration with others on general logic flow, techniques and Java Core API methods available may be useful. It is most beneficial if you do not share code.

Program: Write Java code that implements an object to convert a binary number into decimal, octal and hexadecimal. Do NOT use any of the Java Core API methods that does these functions for you - code the details of the conversions yourself. The name of the object must be BinConverter. The Java source code file will be named BinConverter.java. When it is compiled the object that is run will be called BinConverter.class. The BinConverter object will contain six public methods. These methods are bin2Decimal, bin2Octal, bin2Hex, decimal2Bin, hex2Bin and octal2Decimal; their purpose is described in the next section. The skeleton of the source code is presented below and should be used as a starting point. The six public methods should be completed to do their intended purpose.

Public Methods:

bin2Decimal the method will be passed a string containing a valid binary number of arbitrary length. The binary number should be converted into its decimal equivalent and returned as a string.

bin2Octal the method will be passed a string containing a valid binary number of arbitrary length. The binary number should be converted into its octal equivalent and returned as a string.

bin2Hex the method will be passed a string containing a valid binary number of arbitrary length. The binary number should be converted into its hexadecimal equivalent and returned as a string.

decimal2Bin this procedure will be passed a string containing a valid decimal number of arbitrary length. The decimal number should be converted into its binary equivalent and returned as a string.

hex2Bin this procedure will be passed a string containing a valid hexadecimal number of arbitrary length. The hexadecimal number should be converted into its binary equivalent and returned as a string.

octal2Decimal this procedure will be passed a string containing a valid octal number of arbitrary length. The octal number should be converted into its decimal equivalent and returned as a string.

To run the program, the BinDriver Java object is invoked. This program will prompt for a binary number to be inputted. The inputted value is assumed to be a valid binary number. The number is passed to the BinConverter object by invoking each of the six public methods. As each method returns the converted value it is displayed on the screen by the BinDriver program. The BinDriver source code is also presented below and should be used. This source code should be put in a file named BinDriver.java and then compiled.

BinDriver.java code:


import java.io.*;

public class BinDriver
{
  BufferedReader console;

  BinDriver()
  {
    console = new BufferedReader(new InputStreamReader(System.in));
  }

  private String getBinInput()
  {
    String binnum = "";

    try
    {
      System.out.print("Input valid binary number ");
      binnum = console.readLine();
    }
    catch (IOException e)
    {
      //should never happen
    }
    return (binnum);
  }
   
  private void cleanup()
  {
    try
    {
      console.close();
    }
    catch (IOException e)
    {
      //ignore exception
    }
  }

  public static void main(String args[])
  {
    String binnum = "";
    String temp = "";

    BinDriver bd = new BinDriver();
    BinConverter bc = new BinConverter();

    while (! (binnum = bd.getBinInput()).equals("") )
    {
      System.out.println("");
      temp = bc.bin2Decimal(binnum);
      System.out.println("The binary number "+binnum+
	" is "+temp+" in decimal.");
      System.out.println("The decimal number "+temp+
	" is "+bc.decimal2Bin(temp)+" in binary.");
      temp = bc.bin2Octal(binnum);
System.out.println("The binary number "+binnum+ " is "+temp+" in octal.");
System.out.println("The octal number "+temp+ " is "+bc.octal2Decimal(temp)+" in decimal."); temp = bc.bin2Hex(binnum); System.out.println("The binary number "+binnum+ " is "+temp+" in hexadecimal.");
System.out.println("The hexadecimal number "+temp+ " is "+bc.hex2Bin(temp)+" in binary."); System.out.println(""); } bd.cleanup(); System.out.println("All Done!"); } //end main } //end of BinDriver class

BinConverter.java skeleton code:

public class BinConverter
{
  BinConverter() //object constructor
  {
  }
   
  public String bin2Decimal(String binnum)
  {
    String decimal = "";
      
    //insert code to convert binnum to decimal
    //binnum is a String, return variable decimal is a String


    //the variable decimal should be a String that has the decimal
    //value of the binary#
    return (decimal);
  }
   
  public String bin2Octal(String binnum)
  {
    String octal = "";
      
    //insert code to convert binnum to octal
    //binnum is a String, return variable octal is a String
      

    //the variable octal should be a String that has the octal value
    // of the binary#
    return (octal);
  }
   
  public String bin2Hex(String binnum)
  {
    String hex = "";
      
    //insert code to convert binnum to hex
    //binnum is a String, return variable hex is a String
      

    //the variable hex should be a String that has the 
    //hexadecimal value of the binary#
    return (hex);
  }

public String decimal2Bin(String decimalnum)
  {
    String binnum = "";
      
    //insert code to convert decimalnum to binnum
    //decimalnum is a String, return variable binnum is a String
      

    //the variable binnum should be a String that has the binary 
    //value of the decimal#
    return (binnum);
  }

public String hex2Bin(String hexnum)
  {
    String binnum = "";
      
    //insert code to convert hexnum to binnum
    //hexnum is a String, return variable binnum is a String
      

    //the variable binnum should be a String that has the binary 
    //value of the hex#
    return (binnum);
  }

public String octal2Decimal(String octalnum)
  {
    String decimalnum = "";
      
    //insert code to convert octalnum to decimalnum
    //octalnum is a String, return variable decimalnum is a String
      

    //the variable decimalnum should be a String that has the 
    //decimal value of the octal#
    return (decimalnum);
  }

} //end of BinConverter class