Results 1 to 1 of 1
- 11-05-2011, 06:16 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
Merging code with Dr. Java. Binary to hexadecimal converter. IB computer science
Hello. I am taking IB comp sci currently and I have 2 codes at the moment (working), Binary2decimal and Decimal2Hex, and I need to combine them to make a Binary2Hexadecimal converter.
How exactly can I merge these two codes?
Thank you!
BINARY TO DECIMAL
DECIMAL TO HEXADECIMALJava Code:/** * Auto Generated Java Class. */ public class Binary2decimal { public static void main(String[] args) { String bs = ""; bs = input("Input value in Binary to convert: "); char bit; // will hold a 0or a 1 int p2 = bs.length()-1; // a power of 2, initially set to 6 (ie, 2^6) int dec = 0; // the decimal number we will convert to for (int i = 0; i < bs.length(); i++) { bit = bs.charAt(i); // stores the current bit of the String if (bit == '1') { // we only care if the bit is a 1, ignore 0s dec += Math.pow(2, p2); } p2--; // decrement the power of 2 } output(dec); } //=========================================================== // IBIO Standard Input and Output //=========================================================== static void output(String info) { System.out.println(info); } static void output(char info) { System.out.println(info); } static void output(byte info) { System.out.println(info); } static void output(int info) { System.out.println(info); } static void output(long info) { System.out.println(info); } static void output(double info) { System.out.println(info); } static void output(boolean info) { System.out.println(info); } static String input(String prompt) { String inputLine = ""; System.out.print(prompt); try {inputLine = (new java.io.BufferedReader( new java.io.InputStreamReader(System.in))).readLine(); } catch (Exception e) { String err = e.toString(); System.out.println(err); inputLine = ""; } return inputLine; } static String inputString(String prompt) { return input(prompt); } static String input() { return input(""); } static int inputInt() { return inputInt(""); } static double inputDouble() { return inputDouble(""); } static char inputChar(String prompt) { char result=(char)0; try{result=input(prompt).charAt(0);} catch (Exception e){result = (char)0;} return result; } static byte inputByte(String prompt) { byte result=0; try{result=Byte.valueOf(input(prompt).trim()).byteValue();} catch (Exception e){result = 0;} return result; } static int inputInt(String prompt) { int result=0; try{result=Integer.valueOf( input(prompt).trim()).intValue();} catch (Exception e){result = 0;} return result; } static long inputLong(String prompt) { long result=0; try{result=Long.valueOf(input(prompt).trim()).longValue();} catch (Exception e){result = 0;} return result; } static double inputDouble(String prompt) { double result=0; try{result=Double.valueOf( input(prompt).trim()).doubleValue();} catch (Exception e){result = 0;} return result; } static boolean inputBoolean(String prompt) { boolean result=false; try{result=Boolean.valueOf( input(prompt).trim()).booleanValue();} catch (Exception e){result = false;} return result; } }
Java Code:/** * Auto Generated Java Class. */ public class Decimal2Hex { public static void main(String[] args) { String total = ""; //decimal value int dc; dc = inputInt("Input value in decimal"); int rem; //hexString is the while (dc != 0){ rem = dc%16; dc = dc/16; if (rem == 1){ total = "1" + total; } else if (rem == 2){ total = "2" + total; } else if (rem == 3){ total = "3" + total; } else if (rem == 4){ total = "4" + total; } else if (rem == 5){ total = "5" + total; } else if (rem == 6){ total = "6"+ total; } else if (rem == 7){ total = "7" + total; } else if (rem == 8){ total = "8" + total; } else if (rem == 9){ total = "9" + total; } else if (rem == 10){ total = total + "A"; } else if (rem == 11){ total = total + "B"; } else if (rem == 12){ total = total + "C"; } else if (rem == 13){ total = total + "D"; } else if (rem == 14){ total = total + "E"; } else if (rem == 15){ total = total + "F"; } else { total = total + "0"; } } output(total); } //=========================================================== // IBIO Standard Input and Output //=========================================================== static void output(String info) { System.out.println(info); } static void output(char info) { System.out.println(info); } static void output(byte info) { System.out.println(info); } static void output(int info) { System.out.println(info); } static void output(long info) { System.out.println(info); } static void output(double info) { System.out.println(info); } static void output(boolean info) { System.out.println(info); } static String input(String prompt) { String inputLine = ""; System.out.print(prompt); try {inputLine = (new java.io.BufferedReader( new java.io.InputStreamReader(System.in))).readLine(); } catch (Exception e) { String err = e.toString(); System.out.println(err); inputLine = ""; } return inputLine; } static String inputString(String prompt) { return input(prompt); } static String input() { return input(""); } static int inputInt() { return inputInt(""); } static double inputDouble() { return inputDouble(""); } static char inputChar(String prompt) { char result=(char)0; try{result=input(prompt).charAt(0);} catch (Exception e){result = (char)0;} return result; } static byte inputByte(String prompt) { byte result=0; try{result=Byte.valueOf(input(prompt).trim()).byteValue();} catch (Exception e){result = 0;} return result; } static int inputInt(String prompt) { int result=0; try{result=Integer.valueOf( input(prompt).trim()).intValue();} catch (Exception e){result = 0;} return result; } static long inputLong(String prompt) { long result=0; try{result=Long.valueOf(input(prompt).trim()).longValue();} catch (Exception e){result = 0;} return result; } static double inputDouble(String prompt) { double result=0; try{result=Double.valueOf( input(prompt).trim()).doubleValue();} catch (Exception e){result = 0;} return result; } static boolean inputBoolean(String prompt) { boolean result=false; try{result=Boolean.valueOf( input(prompt).trim()).booleanValue();} catch (Exception e){result = false;} return result; } }
Similar Threads
-
computer science newbie
By sixxvirus in forum New To JavaReplies: 17Last Post: 11-18-2011, 08:30 AM -
What essay style format do Computer Science majors use?
By Dev23 in forum Forum LobbyReplies: 1Last Post: 02-21-2011, 12:33 PM -
Basic Java Computation Help Ap Computer Science
By Sean_J in forum New To JavaReplies: 6Last Post: 02-10-2010, 01:10 PM -
Software Engineer...Computer Science Major
By giganews35 in forum IntroductionsReplies: 2Last Post: 09-14-2008, 09:19 AM -
Help On Computer Science Final!!!! Java Program Help!!!!
By BSOS in forum New To JavaReplies: 1Last Post: 12-11-2007, 04:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks