import java.io.*; public class GetBinaryMain { public static void main (String[ ] args) { try { final String PROMPT = "Please enter a base-10, non-negative integer: "; final String RESULT = "The binary equivalent is "; BufferedReader reader = new BufferedReader (new InputStreamReader (System.in)); System.out.print (PROMPT); int n = Integer.parseInt (reader.readLine()); System.out.print (RESULT + getBinary (n)); // RA1 } // try catch (IOException e) { System.out.println (e); } // catch catch (IllegalArgumentException e) { System.out.println (e); } // catch } // method main /** * * Determines the binary equivalent of a non-negative integer. The worstTime(n) * is O(log n), where n is the given integer. * * @param n the non-negative integer, in decimal notation. * * @return a String representation of the binary equivalent of n. * * @throws IllegalArgumentException if n is negative. */ public static String getBinary (int n) { if (n < 0) throw new IllegalArgumentException(); if (n <= 1) return Integer.toString (n); return getBinary (n / 2) + Integer.toString (n % 2); } // method getBinary } // class GetBinaryMain