Driver.java
—————————————–
import java.io.*;
public class Driver
{
public static void main(String[] args) throws IOException{
int choice;
PrintWriter pw = new PrintWriter (new FileWriter(“csis.txt”));
Decimal dec = new Decimal (pw);
Binary bin = new Binary (pw);
Hexadecimal hex = new Hexadecimal (pw);
Menu menu = new Menu(pw);
do {
menu.display();
choice = menu.getSelection();
switch (choice) {
case 1 : dec.decToBin(); break;
case 2 : dec.decToHex(); break;
case 3 : bin.binToDec(); break;
case 4 : bin.binToHex(); break;
case 5 : hex.hexToDec(); break;
case 6 : hex.hexToBin(); break;
}
} while (choice != 7);
pw.close();
}
}
—————————————————————-
Menu.java
————————————
import java.io.*;
import java.util.Scanner;
public class Menu
{
private PrintWriter pw;
/**
* Class constructor
*/
public Menu(PrintWriter pw)
{
// initialize instance PrinWriter
this.pw = pw;
}
/**
* Displays the menu options
*/
public void display()
{
System.out.println(” MENU “);
System.out.println(“1. Press 1 for decimal to binary transformation. “);
System.out.println(“2. Press 2 for decimal to hexadecimal transformation. “);
System.out.println(“3. Press 3 for binary to decimal transformation. “);
System.out.println(“4. Press 4 for binary to hexadecimal transformation. “);
System.out.println(“5. Press 5 for hexadecimal to decimal transformation. “);
System.out.println(“6. Press 6 for hexadecimal to binary transformation. “);
System.out.println(“7. Press 7 to exit the program. “);
}
/**
* Extracts the user inserted option
*/
public int getSelection() {
Scanner keyboard = new Scanner(System.in);
int input = keyboard.nextInt();
boolean check_quality = qualityCheck(input);
if (!check_quality) {
System.out.println(“Please enter a value between 1 and 7: “);
keyboard = new Scanner(System.in);
input = keyboard.nextInt();
check_quality = qualityCheck(input);
}
return input;
}
/**
* Method that check that the user inserts a correct value
*/
public boolean qualityCheck(int input) {
if (input != 1 || input != 2 ||input != 3 ||input != 4 ||input != 5 ||input != 6 ||input != 7) {
return true;
} else {
return false;
}
}
}
—————————————————————————————–
Binary.java
————————————————-
import java.io.*;
import java.util.Scanner;
public class Binary
{
private PrintWriter pw;
/**
* Constructor for objects of class Binary
*/
public Binary(PrintWriter pw)
{
// initialise instance PrinWriter
this.pw = pw;
}
/**
* Method that transforms a binary number to decimal
*/
public void binToDec()
{
System.out.println(“Please enter a binary number:”);
Scanner keyboard = new Scanner(System.in);
String bin = keyboard.nextLine();
String oldbin = bin;
bin = bin.replace(” “, “”);
bin = new StringBuilder(bin).reverse().toString();
int decimal = 0;
for (int i = 0; i<bin.length(); i++) {
//int power = bin.length() – 1- i;
int temp = (int)Math.pow(2, i);
decimal = decimal + Character.getNumericValue(bin.charAt(i)) * temp;
}
System.out.println(“Binary ” + oldbin + ” = ” + decimal + ” in decimal”);
pw.write(“Binary ” + oldbin + ” = ” + decimal + ” in decimaln”);
}
/**
* Method that transforms a binary to hexadecimal
*/
public void binToHex()
{
System.out.println(“Please enter a binary number”);
Scanner keyboard = new Scanner(System.in);
String bin = keyboard.nextLine();
String oldbin = bin;
bin = bin.replace(” “, “”).trim();
StringBuffer hex = new StringBuffer(“00000000”);
//String hex1 = “”;
int j = 0;
for (int i = 0; i < bin.length();) {
if (bin.substring(i, i+4).equals(“0000”)) {
hex.setCharAt(j, ‘0’);
} else if (bin.substring(i, i+4).equals(“0001”)) {
hex.setCharAt(j, ‘1’);
} else if (bin.substring(i, i+4).equals(“0010”)) {
hex.setCharAt(j, ‘2’);
} else if (bin.substring(i, i+4).equals(“0011”)) {
hex.setCharAt(j, ‘3’);
} else if (bin.substring(i, i+4).equals(“0100”)) {
hex.setCharAt(j, ‘4’);
} else if (bin.substring(i, i+4).equals(“0101”)) {
hex.setCharAt(j, ‘5’);
} else if (bin.substring(i, i+4).equals(“0110”)) {
hex.setCharAt(j, ‘6’);
} else if (bin.substring(i, i+4).equals(“0111”)) {
hex.setCharAt(j, ‘7’);
} else if (bin.substring(i, i+4).equals(“1000”)) {
hex.setCharAt(j, ‘8’);
} else if (bin.substring(i, i+4).equals(“1001”)) {
hex.setCharAt(j, ‘9’);
} else if (bin.substring(i, i+4).equals(“1010”)) {
hex.setCharAt(j, ‘A’);
} else if (bin.substring(i, i+4).equals(“1011”)) {
hex.setCharAt(j, ‘B’);
} else if (bin.substring(i, i+4).equals(“1100”)) {
hex.setCharAt(j, ‘C’);
} else if (bin.substring(i, i+4).equals(“1101”)) {
hex.setCharAt(j, ‘D’);
} else if (bin.substring(i, i+4).equals(“1110”)) {
hex.setCharAt(j, ‘E’);
} else if (bin.substring(i, i+4).equals(“1111”)) {
hex.setCharAt(j, ‘F’);
}
i = i+4;
j = j + 1;
}
System.out.println(“Binay ” + oldbin + ” = ” + hex + ” in hexadecimal”);
pw.write(“Binary ” + oldbin + ” = ” + hex + ” in hexadecimaln”);
}
}
——————————————————————————-
Decimal.java
—————————————–
import java.io.*;
import java.util.Scanner;
public class Decimal
{
private PrintWriter pw;
/**
* Constructor for objects of class Decimal
*/
public Decimal(PrintWriter pw)
{
// initialise instance PrinWriter
this.pw = pw;
}
/**
* Method that transforms a decimal to binary
*/
public void decToBin()
{
//pw.write(“Decimal To Binary:”);
System.out.println(“Please enter a decimal number: “);
Scanner keyboard = new Scanner(System.in);
Long input = keyboard.nextLong();
String binary=””;
for ( Long decimal = input ; decimal > 0 ; decimal/=2 )
{
binary = decimal%2 + binary ;
}
System.out.println(“Decimal ” + input + ” = ” + binary + ” in binary”);
pw.write(“Decimal ” + input + ” = ” + binary + ” in binaryn”);
}
/**
* Method that transforms a decimal to Hexadecimal
*/
public void decToHex()
{
System.out.println(“Please enter a decimal number: “);
Scanner keyboard = new Scanner(System.in);
int decimal = keyboard.nextInt();
int input = decimal;
String hex=””;
while (input != 0) {
if (input%16 == 10) {
hex = hex.concat(“A”);
} else if (input%16 == 11) {
hex = hex.concat(“B”);
} else if (input%16 == 12) {
hex = hex.concat(“C”);
} else if (input%16 == 13) {
hex = hex.concat(“D”);
} else if (input%16 == 14) {
hex = hex.concat(“E”);
} else if (input%16 == 15) {
hex = hex.concat(“F”);
} else if (input%16 == 16) {
hex = hex.concat(“d”);
} else {
hex = hex.concat(Integer.toString(input%16));
}
input = (int) input/16;
}
hex = new StringBuilder(hex).reverse().toString();
System.out.println(“Decimal ” + decimal + ” = ” + hex + ” in hexadecimal”);
pw.write(“Decimal ” + decimal + ” = ” + hex + ” in hexadecimaln”);
}
}
—————————————————————————-
Hexadecimal.java
————————————-
import java.io.*;
import java.util.Scanner;
public class Hexadecimal
{
private PrintWriter pw;
/**
* Constructor for objects of class Binary
*/
public Hexadecimal(PrintWriter pw)
{
// initialise instance PrinWriter
this.pw = pw;
}
/**
* Method that transforms a binary number to decimal
*/
public void hexToDec()
{
System.out.println(“Please enter a hexadecimal number:”);
Scanner keyboard = new Scanner(System.in);
String bin = keyboard.nextLine();
String oldbin = bin;
bin = bin.toLowerCase().trim();
int decimal = 0;
for (int i = bin.length()-1; i >= 0; i–) {
int power = bin.length() – 1- i;
int temp = (int)Math.pow(16, power);
int value = 0;
if (bin.charAt(i) == ‘a’) {
value = 10;
} else if (bin.charAt(i) == ‘b’) {
value = 11;
} else if (bin.charAt(i) == ‘c’) {
value = 12;
} else if (bin.charAt(i) == ‘d’) {
value = 13;
} else if ((bin.charAt(i) == ‘e’)) {
value = 14;
} else if (bin.charAt(i) == ‘f’) {
value = 15;
} else {
value = Character.getNumericValue(bin.charAt(i));
}
decimal = decimal + value * temp;
}
System.out.println(“Hexadecimal ” + oldbin + ” = ” + decimal + ” in decimal”);
pw.write(“Hexadecimal ” + oldbin + ” = ” + decimal + ” in decimaln”);
}
/**
* Method that transforms a binary to hexadecimal
*/
public void hexToBin()
{
System.out.println(“Please enter a hexadecimal number:”);
Scanner keyboard = new Scanner(System.in);
String bin = keyboard.nextLine();
bin = bin.trim();
String binary = “”;
for (int i = 0; i < bin.length(); i++) {
if (bin.charAt(i) == ‘0’) {
binary = binary.concat(“0000 “);
} else if (bin.charAt(i) == ‘1’) {
binary = binary.concat(“0001 “);
} else if (bin.charAt(i) == ‘2’) {
binary = binary.concat(“0010 “);
} else if (bin.charAt(i) == ‘3’) {
binary = binary.concat(“0011 “);
} else if (bin.charAt(i) == ‘4’) {
binary = binary.concat(“0100 “);
} else if (bin.charAt(i) == ‘5’) {
binary = binary.concat(“0101 “);
} else if (bin.charAt(i) == ‘6’) {
binary = binary.concat(“0110 “);
} else if (bin.charAt(i) == ‘7’) {
binary = binary.concat(“0111 “);
} else if (bin.charAt(i) == ‘8’) {
binary = binary.concat(“1000 “);
} else if (bin.charAt(i) == ‘9’) {
binary = binary.concat(“1001 “);
} else if (bin.charAt(i) == ‘A’) {
binary = binary.concat(“1010 “);
} else if (bin.charAt(i) == ‘B’) {
binary = binary.concat(“1011 “);
} else if (bin.charAt(i) == ‘C’) {
binary = binary.concat(“1100 “);
} else if (bin.charAt(i) == ‘D’) {
binary = binary.concat(“1101 “);
} else if (bin.charAt(i) == ‘E’) {
binary = binary.concat(“1110 “);
} else if (bin.charAt(i) == ‘F’) {
binary = binary.concat(“1111 “);
}
}
System.out.println(“Hexadecimal ” + bin + ” = ” + binary + ” in binary”);
pw.write(“Hexadecimal ” + bin + ” = ” + binary + ” in binaryn”);
}
}
—————————————————————————
csis.txt
————————————–
Decimal 65535 = 1111111111111111 in binary
Decimal 1000000 = 11110100001001000000 in binary
Decimal 1234567890 = 1001001100101100000001011010010 in binary
Decimal 65535 = FFFF in hexadecimal
Decimal 1000000 = F4240 in hexadecimal
Decimal 1234567890 = 499602D2 in hexadecimal
Binary 0110 0101 1100 1101 1010 1110 0000 0101 = 1707978245 in decimal
Binary 0111 1111 1111 1111 0010 0001 1101 0011 = 2147426771 in decimal
Binary 0000 0000 0000 0000 0000 0000 0000 0010 = 2 in decimal
Binary 0110 0101 1100 1101 1010 1110 0000 0101 = 65CDAE05 in hexadecimal
Binary 0111 1111 1111 1111 0010 0001 1101 0011 = 7FFF21D3 in hexadecimal
Binary 0010 1011 1010 1101 0111 0110 0011 1010 = 2BAD763A in hexadecimal
Hexadecimal 12345678 = 305419896 in decimal
Hexadecimal 2A3DF8A7 = 708704423 in decimal
Hexadecimal 00FF00FF = 16711935 in decimal
Hexadecimal 12345678 = 0001 0010 0011 0100 0101 0110 0111 1000 in binary
Hexadecimal 2A3DF8A7 = 0010 1010 0011 1101 1111 1000 1010 0111 in binary
Hexadecimal 00FF00FF = 0000 0000 1111 1111 0000 0000 1111 1111 in binary
