/** * @(#)group-assignment.java * * group-assignment application * *Write a recursive method that *converts a decimal integer to a *binary string. Write a recursive *method that converts a binary string *to a decimal integer.. * * @author * @version 1.00 2007/11/26 */ import java.util.*; class convertdecimal { public static String decimalbinaryconvert(int decimalinput, StringBuffer tempstorage) { tempstorage.append(decimalinput % 2); decimalinput /= 2; if (decimalinput == 0) return tempstorage.reverse().toString(); else return decimalbinaryconvert(decimalinput, tempstorage); } // BINARY TO DECIMAL = 1 // DECIMAL TO BINARY = 2 // NOTHING = 0 public static void main(String[] args) { Scanner scan1 = new Scanner(System.in); Scanner scan2 = new Scanner(System.in); Scanner binaryscan = new Scanner(System.in); Scanner decimalscan = new Scanner(System.in); int input_result = 0; String binaryinput; //holds users binary number int decimalinput; //holds users decimal input System.out.println(input_result); System.out.println( "Hello; \nWould you like to convert a Binary Number to a Decimal String? (Y/N)"); String ynBinary = scan1.next(); if (ynBinary.toLowerCase().indexOf('y') != -1) { input_result = 1; System.out.println("Ok, please input your Binary String."); binaryinput = binaryscan.next(); } else if (ynBinary.toLowerCase().indexOf('n') != -1) { System.out.println( "Would you like to convert a Deciamal String to a Binary Number? (Y/N)"); String ynDecimal = scan2.next(); // } /////////////////////////////////////BEGIN INTERNAL IF STRUCTURE if (ynDecimal.toLowerCase().indexOf('y') != -1) { input_result = 2; System.out.println("Ok, please input your Decimal Number."); decimalinput = decimalscan.nextInt(); } else if (ynDecimal.toLowerCase().indexOf('n') != -1) { System.out.println( "Sorry, but looks like this program is not for you!"); } else { System.out.println("Your input is invalid!"); } } /////////////////////////////////////END INTERNAL IF STRUCTURE else { System.out.println("Your input is invalid!"); } System.out.println(" \n"); // System.out.println("input_result = " + input_result); if (input_result == 1) { System.out.println("Your Binary Input was: " + input_result); // BINARY TO DECIMAL = 1 ////////////////////METHOD CALL HERE////////////////////////// System.out.println("Your Resulting Decimal Number is:" + input_result); } else if (input_result == 2) { System.out.println("Your Decimal Input was: " + decimalinput); // DECIMAL TO BINARY = 2 ////////////////////METHOD CALL HERE////////////////////////// String binaryreturn = convertdecimal.decimalbinaryconvert(decimalinput); System.out.println("Your Resulting Binary Number is: " + binaryreturn); } else { System.out.println("There has been an error! please try again."); } } }