Number System Please I need the answer for this program in Java Construct an obj
ID: 3854119 • Letter: N
Question
Number System Please I need the answer for this program in JavaConstruct 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() Your program should send all output to a file called csis.txt. This output file will be submitted along with your source code for the lab. All information displayed in the terminal window, including input from the user, should also be sent to the output file.
Explanation / Answer
Java program :
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
class Main{
public static void main(String[] args) throws IOException {
Scanner obj = new Scanner(System.in);
char ch = 'Y';
int op, len, fans;
long x;
String n, ans;
FileWriter fw = new FileWriter("D:/csis.txt");
while (ch == 'Y') {
System.out.println("1.Converts a decimal to 32bit binary");
System.out.println("2.Converts decimal to 8bit hexadecimal");
System.out.println("3.32bit bin to decimal");
System.out.println("4.32bit bin to 8 hex");
System.out.println("5.8bit hexa to decimal");
System.out.println("6.8bit hexa to 32bit binary");
fw.write("1.Converts a decimal to 32bit binary ");
fw.write("2.Converts decimal to 8bit hexadecimal ");
fw.write("3.32bit bin to decimal ");
fw.write("4.32bit bin to 8 hex ");
fw.write("5.8bit hexa to decimal ");
fw.write("6.8bit hexa to 32bit binary ");
op = obj.nextInt();
fw.write(op + " ");
switch (op) {
case 1:
System.out.println("Enter the number");
n = obj.next();
fw.write(n + " ");
x = Long.parseLong(n);
ans = "";
while (x != 0) {
ans = x % 2 + ans;
x = x >> 1;
}
len = 32 - ans.length();
while (len > 0) {
ans = "0" + ans;
len--;
}
System.out.println(ans);
fw.write(ans + " ");
break;
case 2:
System.out.println("Enter the number");
n = obj.next();
fw.write(n + " ");
x = Long.parseLong(n);
ans = "";
while (x != 0) {
long f = x % 16;
if (f <= 9) {
ans = x % 16 + ans;
} else {
ans = (char) (55 + f) + ans;
}
x = x / 16;
}
len = 8 - ans.length();
while (len > 0) {
ans = "0" + ans;
len--;
}
System.out.println(ans);
fw.write(ans + " ");
break;
case 3:
System.out.println("Enter the number");
n = obj.next();
fw.write(n + " ");
fans = 0;
for (int i = 0; i < n.length(); i++) {
fans = 2 * fans + n.charAt(i) - '0';
}
System.out.println(fans);
fw.write(fans + " ");
break;
case 4:
System.out.println("Enter the number");
n = obj.next();
fw.write(n + " ");
fans = 0;
ans = "";
for (int i = 0; i < n.length(); i += 4) {
fans = 0;
fans = 2 * fans + n.charAt(i) - '0';
fans = 2 * fans + n.charAt(i + 1) - '0';
fans = 2 * fans + n.charAt(i + 2) - '0';
fans = 2 * fans + n.charAt(i + 3) - '0';
if (fans > 9) {
ans = ans + (char) (55 + fans);
} else {
ans = ans + (char) (fans + 48);
}
}
System.out.println(ans);
fw.write(ans + " ");
break;
case 5:
System.out.println("Enter the number");
n = obj.next();
fw.write(n + " ");
fans = 0;
for (int i = 0; i < n.length(); i++) {
int temp = n.charAt(i);
if (n.charAt(i) >= 'A') {
temp -= 55;
} else {
temp -= 48;
}
fans = 16 * fans + temp;
}
System.out.println(fans);
fw.write(fans + " ");
break;
case 6:
System.out.println("Enter the number");
n = obj.next();
fw.write(n + " ");
fans = 0;
ans = "";
for (int i = 0; i < n.length(); i++) {
int temp = n.charAt(i);
if (n.charAt(i) >= 'A') {
temp -= 55;
} else {
temp -= 48;
}
String tans = "";
while (temp != 0) {
tans = temp % 2 + tans;
temp /= 2;
}
len = 4 - tans.length();
while (len > 0) {
tans = "0" + tans;
len--;
}
ans = ans+tans;
}
System.out.println(ans);
fw.write(ans + " ");
break;
default:
System.out.println("");
break;
}
System.out.println("Do you want to enter again?");
fw.write("Do you want to enter again? ");
ch = obj.next().charAt(0);
fw.write(ch + " ");
}
fw.flush();
fw.close();
}
}
OUTPUT :
1.Converts a decimal to 32bit binary
2.Converts decimal to 8bit hexadecimal
3.32bit bin to decimal
4.32bit bin to 8 hex
5.8bit hexa to decimal
6.8bit hexa to 32bit binary
1
Enter the number
1201
00000000000000000000010010110001
Do you want to enter again?
Y
1.Converts a decimal to 32bit binary
2.Converts decimal to 8bit hexadecimal
3.32bit bin to decimal
4.32bit bin to 8 hex
5.8bit hexa to decimal
6.8bit hexa to 32bit binary
1
Enter the number
1208
00000000000000000000010010111000
Do you want to enter again?
Y
1.Converts a decimal to 32bit binary
2.Converts decimal to 8bit hexadecimal
3.32bit bin to decimal
4.32bit bin to 8 hex
5.8bit hexa to decimal
6.8bit hexa to 32bit binary
3
Enter the number
00000000000000000000000010110101
181
Do you want to enter again?
Y
1.Converts a decimal to 32bit binary
2.Converts decimal to 8bit hexadecimal
3.32bit bin to decimal
4.32bit bin to 8 hex
5.8bit hexa to decimal
6.8bit hexa to 32bit binary
4
Enter the number
00000000000000000000000010110101
000000B5
Do you want to enter again?
Y
1.Converts a decimal to 32bit binary
2.Converts decimal to 8bit hexadecimal
3.32bit bin to decimal
4.32bit bin to 8 hex
5.8bit hexa to decimal
6.8bit hexa to 32bit binary
5
Enter the number
000000AB
171
Do you want to enter again?
Y
1.Converts a decimal to 32bit binary
2.Converts decimal to 8bit hexadecimal
3.32bit bin to decimal
4.32bit bin to 8 hex
5.8bit hexa to decimal
6.8bit hexa to 32bit binary
6
Enter the number
000000AB
00000000000000000000000010101011
Do you want to enter again?
N
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.