Construct an object-oriented Java program that performs each of the following co
ID: 3787618 • Letter: C
Question
Construct an object-oriented Java program that performs each of the following conversions. Note that we’re only dealing with non-negative numbers.
• Converts a decimal integer into a 32-bit binary number• Converts a decimal integer into an 8-digit hexadecimal number• Converts a 32-bit binary number into a decimal integer
• Converts a 32-bit binary number into an 8-digit hexadecimal number
• Converts an 8-digit hexadecimal number into a decimal integer• Converts an 8-digit hexadecimal number into a 32-bit binary number
Your program should be interactive, requiring the user to select the appropriate option from a menu of options (no GUI required). Note that Java’s Scanner class will be very helpful reading data from the keyboard.
The conversions you code must be direct. In other words, if you had to convert a base 2 number into base 16, the conversion should be made directly from base 2 to base 16 and not from base 2 to base 10 to base 16. You may not use any methods or utilities from the Java library that do the conversions for you, such as:
Integer.toBinaryString()
Integer.toHexString()
Explanation / Answer
Converting Decimal to 32-bit binary without using methods like intefer.tobinarystring(), integer.tohexstring
The Source code is
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
/ Java program to convert Dec to Bin. /
public class convert
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int number = Integer.MAX_VALUE;
while (number != 0)
{
System.out.println(" please Enter a dec number to convert into bin");
number = sc.nextInt();
byte[] bits = toBinary(number);
printBinary(bits);
convert(number);
}
sc.close();
}
public static byte[] toBinary(int number)
{
byte[] binary = new byte[32];
int index = 0;
int copyOfInput = number;
while (copyOfInput > 0)
{
binary[index++] = (byte) (copyOfInput % 2);
copyOfInput = copyOfInput / 2;
}
return binary;
}
public static void printBinary(byte[] binary)
{
for (int i = binary.length - 1; i >= 0; i--)
{ System.out.print(binary[i]); }
}
public static void convert(int input)
{
int copyOfInput = input;
StringBuilder sb = new StringBuilder();
while (copyOfInput > 0)
{
byte bit = (byte) (copyOfInput % 2);
sb.append(bit);
copyOfInput = copyOfInput / 2;
}
System.out.printf(" Dec number %d in Bin format is %s %n", input, sb.toString());
}
}
converting decimal to hexa decimal
import java.util.Scanner;
class DecToHex
{
public static void main(String args[])
{
Scanner input = new Scanner( System.in );
System.out.print("Enter a decimal number : ");
int num =input.nextInt();
int rem;
String str="";
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(num>0)
{
rem=num%16;
str=hex[rem]+str;
num=num/16;
}
System.out.println("Decto hex: "+str2);
}
}
Converting binary to decimal
import java.util.Scanner;
public class DectoBin {
public static void main(String[] args) {
Scanner in = new Scanner( System.in );
System.out.println("Enter a bin number: ");
int binnum =in.nextInt();
int binary=binnum;
int decimal = 0;
int power = 0;
while(true)
{
if(binary == 0)
{
break;
}
else
{
int tmp = binary%10;
decimal += tmp*Math.pow(2, power);
binary = binary/10;
power++;
}
}
System.out.println("Bin="+binnum+" Dec="+decimal);
}
}