Java Write a program that prompts the user to enter an integer literal from 0 to
ID: 3587658 • Letter: J
Question
Java
Write a program that prompts the user to enter an integer literal from 0 to 15 (inclusive) as either a decimal, hexadecimal, or binary literal. When entering values as hexadecimal or binary literals the user should follow the notation specified in the first note on page 49 of the textbook. For example, the user might enter the value 15 as the hexadecimal literal 0xF, or as the binary literal 0b1111, or the decimal literal 15. The program should output the value entered by the user in all three bases: i.e., decimal, hexadecimal, and binary. For example, if the user entered 0b1100, the program should output 12, 0xC, and 0b1100 as the decimal, hexadecimal, and binary representations. The user’s input should be read as a string. Do NOT use loops, the parseInt method, or any pre-written method that converts from one base to another.
Explanation / Answer
ANSWER:
import java.util.Scanner;
public class ConversionOfNum {
public static String convertDecimalToHexaORBinary(String input, int val) {
int num = Integer.valueOf(input);
int rem;
String hexadecimalResult = "";
char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
while (num > 0) {
rem = num % val;
if(val == 16) {
hexadecimalResult = hex[rem] + hexadecimalResult;
}
else {
hexadecimalResult = rem + hexadecimalResult;
}
num = num / val;
}
return hexadecimalResult;
}
public static String convertHexaToDecimal(String input) {
String digits = "0123456789ABCDEF";
input = input.toUpperCase();
Integer val = 0;
for (int i = 0; i < input.length(); i++)
{
char c = input.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
}
return val.toString();
}
public static String convertBinaryToDecimal(String input) {
Integer decimalResult = 0, index = 0;
Integer num = Integer.valueOf(input);
while (num != 0) {
decimalResult = (int) (decimalResult + ((num % 10) * Math.pow(2, index)));
num = num / 10;
index++;
}
return decimalResult.toString();
}
public static String convertBinaryToHexORHexToBinary(String input, int val) {
if (val == 16) {
input = convertBinaryToDecimal(input);
} else {
input = convertHexaToDecimal(input);
}
Integer binnum, rem;
String result = "";
char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
binnum = Integer.valueOf(input);
// converting the number in hexadecimal format
while (binnum > 0) {
rem = binnum % val;
if (val == 16) {
result = hex[rem] + result;
} else {
result = rem + result;
}
binnum = binnum / val;
}
return result.toString();
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String input = null;
String decimalVal = null;
String binaryVal = null;
String hexadecimalVal = null;
System.out.println("Enter decimal(0-15) or hexadecimal(0x) or binary(0b)");
input = scan.next();
scan.close();
if (input.startsWith("0x")) {
hexadecimalVal = input;
decimalVal = convertHexaToDecimal(input.substring(2, input.length()));
binaryVal = "0b"+convertBinaryToHexORHexToBinary(input.substring(2, input.length()), 2);
} else if (input.startsWith("0b")) {
binaryVal = input;
decimalVal = convertBinaryToDecimal(input.substring(2, input.length()));
hexadecimalVal = "0x"+convertBinaryToHexORHexToBinary(input.substring(2, input.length()), 16);
} else if (Integer.valueOf(input) >= 0 && Integer.valueOf(input) <= 15) {
decimalVal = input;
binaryVal ="0b"+ convertDecimalToHexaORBinary(input, 2);
hexadecimalVal ="0x"+ convertDecimalToHexaORBinary(input, 16);
} else {
System.out.println("Invalid input, please try again");
}
System.out.println("Decimal: " + decimalVal + " Hexadecimal: "
+ hexadecimalVal + " Binary: " + binaryVal);
}
}
OUTPUT1:
Enter decimal(0-15) or hexadecimal(0x) or binary(0b)
15
Decimal: 15 Hexadecimal: 0xF Binary: 0b1111
OUTPUT2:
Enter decimal(0-15) or hexadecimal(0x) or binary(0b)
0xF
Decimal: 15 Hexadecimal: 0xF Binary: 0b1111
OUTPUT2:
Enter decimal(0-15) or hexadecimal(0x) or binary(0b)
0b1010
Decimal: 10 Hexadecimal: 0xA Binary: 0b1010
Let me know any concerns. Thank you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.