@@ -0,0 +1,3077 @@
+//*****************************************************************************%0A//    Filename: Binary.java%0A//%0A//      Author: David C. Drake (http://davidcdrake.com)%0A//%0A// Description: Contains a Binary class for converting numbers from binary to%0A//              decimal and vice versa.%0A//*****************************************************************************%0A%0Aimport java.io.*;%0Aimport java.lang.*;%0A%0Apublic class Binary%0A%7B%0A  public static void main(String%5B%5D args) throws IOException%0A  %7B%0A    final int BINARY_LENGTH = 16,%0A              INT_MAX = 32767,%0A              INT_MIN = 0;%0A    String option,%0A           repeat = %22y%22,%0A           binary;%0A    int x, n, power, num, remainder;%0A    BufferedReader stdin = new BufferedReader%0A      (new InputStreamReader(System.in));%0A%0A    do%0A    %7B%0A      num = 0;%0A      binary = %22%22;%0A      System.out.print(%22Type 'b' for binary to decimal, 'd' for decimal to %22%0A                       %22binary: %22);%0A      option = stdin.readLine();%0A%0A      // Binary to decimal:%0A      if (option.equalsIgnoreCase (%22b%22))%0A      %7B%0A        do%0A        %7B%0A          x = 0;%0A          System.out.print(%22Enter a %22 + BINARY_LENGTH +%0A                           %22-bit binary number: %22);%0A          binary = stdin.readLine();%0A          if (binary.length() != BINARY_LENGTH)%0A          %7B%0A            System.out.println(%22You must enter exactly %22 + BINARY_LENGTH +%0A                               %22 digits.%5Cn%22);%0A            x = 1;%0A          %7D%0A          else%0A          %7B%0A            n = 0;%0A            while (x == 0 && n %3C BINARY_LENGTH)%0A            %7B%0A              if (binary.charAt(n) != '0' && binary.charAt(n) != '1')%0A              %7B%0A                System.out.println (%22All digits must be 0 or 1.%5Cn%22);%0A                x = 1;%0A              %7D%0A              n++;%0A            %7D%0A          %7D%0A        %7Dwhile (x == 1);%0A        power = 0;%0A        for (x = BINARY_LENGTH - 1; x %3E= 0; x--)%0A        %7B%0A          num += ((binary.charAt(x) - 48) * Math.pow(2, power));%0A          power++;%0A        %7D%0A        System.out.println(%22Your number in decimal: %22 + num);%0A      %7D%0A%0A      // Decimal to binary:%0A      if (option.equalsIgnoreCase (%22d%22))%0A      %7B%0A        do%0A        %7B%0A          System.out.print(%22Enter a decimal number between %22 + INT_MIN +%0A                           %22 and %22 + INT_MAX + %22: %22);%0A          num = Integer.parseInt(stdin.readLine());%0A          if (num %3C INT_MIN %7C%7C num %3E INT_MAX)%0A          %7B%0A            System.out.println(%22The number must be no less than %22 + INT_MIN +%0A                               %22 and no greater than %22 + INT_MAX + %22.%5Cn%22);%0A            x = 1;%0A          %7D%0A          else%0A          %7B%0A            x = 0;%0A          %7D%0A        %7Dwhile (x == 1);%0A        for (x = 0; x %3C BINARY_LENGTH; x++)%0A        %7B%0A          remainder = num %25 2;%0A          binary = remainder + binary;%0A          num = num / 2;%0A        %7D%0A        System.out.println(%22Your number in binary: %22 + binary);%0A      %7D%0A      System.out.print(%22%5CnDo you want to convert another number? (y/n) %22);%0A      repeat = stdin.readLine();%0A      System.out.println(%22%5Cn%22);%0A    %7Dwhile(repeat.equalsIgnoreCase(%22y%22));%0A  %7D%0A%7D%0A

Add Binary.java