Skip to content

Instantly share code, notes, and snippets.

@desrtfx
Created February 8, 2014 17:46
Show Gist options
  • Save desrtfx/8887382 to your computer and use it in GitHub Desktop.
Save desrtfx/8887382 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class ChoiceMenu
{
public void display()
{
System.out.println ();
System.out.println ("Please enter selection (1-7)");
System.out.println ("[1]Decimal to Binary");
System.out.println ("[2]Decimal To Hexidecimal");
System.out.println ("[3]Binary To Decimal");
System.out.println ("[4]Binary To Hexidecimal");
System.out.println ("[5]exidecimal To Binary");
System.out.println ("[6]Hexidecimal To Decimal");
System.out.println ("[7]Exit program" );
}
public int getSelection()
{
Scanner kb = new Scanner(System.in);
int choice;
do { // Enhanced Menu input handling - checks that the input is in the valid range
System.out.println( "Please enter selection (1-7):");
choice = kb.nextInt();
} while ((choice < 1) || (choice > 7));
kb.close(); // Close the Scanner when no longer needed
return choice;
}
}
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();
}
}
// 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