Created
February 8, 2014 17:46
-
-
Save desrtfx/8887382 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Decimal { | |
// instance variables - replace the example below with your own | |
private static final String HEX_DIGITS = "0123456789ABCDEF"; | |
public String toBin(int numberToConvert) { | |
StringBuilder sb = new StringBuilder(); | |
int remain; | |
int denom = 2; | |
int quot; | |
int num = numberToConvert; | |
while (num != 0) { | |
quot = num / denom; | |
remain = num % denom; | |
sb.insert(0, remain); | |
num = quot; | |
} | |
return sb.toString(); | |
} | |
public String toHex(int numberToConvert) { | |
StringBuilder sb = new StringBuilder(); | |
int num = numberToConvert; | |
int remain; | |
int quot; | |
int denom = 16; | |
char hexRemain; | |
while (num != 0) { | |
quot = num / denom; | |
remain = num % denom; | |
hexRemain = HEX_DIGITS.charAt(remain); | |
sb.insert(0, hexRemain); | |
num = quot; | |
} | |
return sb.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import java.awt.Menu; // This import resulted from a name conflict | |
// import java.io.FileWriter; | |
// import java.io.IOException; | |
// import java.io.PrintWriter; | |
import java.util.Scanner; | |
public class Driver { | |
public static int getANumber() { | |
Scanner kbd = new Scanner(System.in); | |
int num = kbd.nextInt(); | |
kbd.close(); | |
return num; | |
} | |
public static void main(String [] args) { | |
int choice; | |
// PrintWriter pw = new PrintWriter (new FileWriter("csis.txt")); // Could be removed | |
Decimal dec = new Decimal(); /* | |
Binary bin = new Binary(); | |
Hexadecimal hex = new Hexadecimal(); */ | |
ChoiceMenu menu = new ChoiceMenu(); | |
do{ | |
menu.display(); | |
choice = menu.getSelection(); | |
switch (choice) { | |
case 1 : { | |
// Here, a method to retrieve a number for the conversion should go, | |
// then the conversion should be called with the number as parameter, | |
// then the result should be output | |
System.out.println("Input Decimal number to be converted:"); | |
int numberToConvert = getANumber(); | |
System.out.println("Decimal: " + numberToConvert); | |
System.out.println("Binary: " + dec.toBin(numberToConvert)); | |
} | |
case 2 : { | |
// Here, a method to retrieve a number for the conversion should go, | |
// then the conversion should be called with the number as parameter, | |
// then the result should be output | |
System.out.println("Input Decimal number to be converted:"); | |
int numberToConvert = getANumber(); | |
System.out.println("Decimal: " + numberToConvert); | |
System.out.println("Hexadecimal: " + dec.toHex(numberToConvert)); | |
}/* | |
case 3 : bin.binToDec(); break; | |
case 4 : bin.binToHex(); break; | |
case 5 : hex.hexToBin(); break; | |
case 6 : hex.hexToDec(); break;*/ | |
} | |
}while (choice != 7); | |
// pw.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment