/* * Problem 2, Lab 10, Wednesday 29th October * Write recursive method printIntInNewBase to print an integer in any base from 2 through * hexadecimal (base 16) without using an array. Input must be assumed to be in base 10 * from command line. Use characters ’A’, ’B’, ’C’, ’D’, ’E’, ’F’ for 10, 11, 12, 13, 14, 15 * respectively. For example, 512 in base 16 is 200, and 511 in base 16 is 1FF */ public class BaseConversion { public static void main(String args[]) { if (args.length != 2) { System.out.println("Invalid Command line arguements."); return; } int num, base; try { num = Integer.parseInt(args[0]); base = Integer.parseInt(args[1]); } catch (NumberFormatException ex) { System.out.println("Invalid Command line arguements."); return; } if(base < 2 || base > 16) { System.out.println("Invalid Command line arguement :: base " + base); return; } if(num < 0) { System.out.println("Invalid Command line arguement :: num " + base); System.out.println("Positive num required"); return; } System.out.print(num + " in base " + base + " is "); printInNewBase(num,base); } private static void printInNewBase(int num, int base) { if(num == 0) return; printInNewBase(num/base, base); int remainder = num%base; switch (remainder) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: System.out.print(remainder); break; case 10: System.out.print('A'); break; case 11: System.out.print('B'); break; case 12: System.out.print('C'); break; case 13: System.out.print('D'); break; case 14: System.out.print('E'); break; case 15: System.out.print('F'); break; } } }