Number Base Converter Write a program that allows numbers to be entered in decim
ID: 3768206 • Letter: N
Question
Number Base Converter
Write a program that allows numbers to be entered in decimal (base 10), hexadecimal (base 16), or binary (base 2), and then prints out the number again in all three bases. To distinguish between different bases we'll say that hexadecimal numbers should always be preceded by a "$" and binary numbers by a "%". Other numbers are assumed to be decimal. When the program is run it should repeatedly allow a number to be entered and then print it out in all three bases until "q" is typed in. Here's an example of how your program should operate:
Enter #: 45
Decimal: 45
Hexadecimal: 2D
Binary: 101101
Enter #: %101
Decimal: 5
Hexadecimal: 5
Binary: 101
Enter #: $31
Decimal: 49
Hexadecimal: 31
Binary: 110001
Enter #: q
Approach
A good way to structure this program is to read in each "number" as a string of characters, look at the first character to determine what kind of number it is (hex, binary, decimal), and then call an appropriate function to convert the string into a hex, binary, or decimal number Then, print those numbers out next to each of their corresponding bases.
You will have to use these two functions and their variations to convert between bases:
dec2bin( {user input as integer} ); % e.g. 4 “0100”
hex2dec( {user input as string} ); % e.g. 1D “14”
Also, use one or more of the following functions and their variations to convert from an integer to a string and vice versa:
str2num( {user input as string} );
str2double( {user input as string} );
str2single( {user input as string} );
Some tips:
When asking for an input that will contain more than just numbers, be sure to define that input as a string.
This project seems tougher at first than it actually is. Once you get one of the if/elseif statements working, the others are just simple variations.
Explanation / Answer
Here is a little bit different variant for you code. If you are not satisfied with this, just let me know, so that I can customize for you.
import java.io.*;
import java.util.*;
class NumberBases
{
public static void main(String[] args)
{
System.out.println("1. Binary number.");
System.out.println("2. Hexadecimal number.");
System.out.println("3. Decimal number.");
System.out.println("4. Octal number.");
System.out.print("Enter your choice: ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
String number, octal, binary, hexadecimal;
int decimal;
if(choice == 1)
{
System.out.print("Enter the binary number: ");
number = sc.next(); //Reads a binary integer as string.
decimal = Integer.parseInt(number, 2); //Converts the binary string number to its decimal equivalent .
octal = Integer.toOctalString(decimal); //Converts the decimal number decimal to its octal equivalent.
hexadecimal = Integer.toHexString(decimal); //Converts the decimal number decimal to its hexadecimal equivalent.
System.out.println("The decimal equivalent is: "+decimal);
System.out.println("The octal equivalent is: "+octal);
System.out.println("The hexadecimal equivalent is: "+hexadecimal);
}
else if(choice == 2)
{
System.out.print("Enter the hexadecimal number: ");
number = sc.next(); //Reads a hexadecimal integer as string.
decimal = Integer.parseInt(number, 16); //Converts the hexadecimal string number to its decimal equivalent.
binary = Integer.toBinaryString(decimal); //Converts the decimal number decimal to its binary equivalent.
octal = Integer.toOctalString(decimal); //Converts the decimal number decimal to its octal equivalent.
System.out.println("The binary equivalent is: "+binary);
System.out.println("The decimal equivalent is: "+decimal);
System.out.println("The octal equivalent is: "+octal);
}
else if(choice == 3)
{
System.out.print("Enter the decimal number: ");
number = sc.next(); //Reads a hexadecimal integer as string.
decimal = Integer.parseInt(number); //Converts the decimal string number to its decimal equivalent.
binary = Integer.toBinaryString(decimal); //Converts the decimal number decimal to its binary equivalent.
octal = Integer.toOctalString(decimal); //Converts the decimal number decimal to its octal equivalent.
hexadecimal = Integer.toHexString(decimal); //Converts the decimal number decimal to its hexadecimal equivalent.
System.out.println("The binary equivalent is: "+binary);
System.out.println("The octal equivalent is: "+octal);
System.out.println("The hexadecimal equivalent is: "+hexadecimal);
}
else if(choice == 4)
{
System.out.print("Enter the octal number: ");
number = sc.next(); //Reads a octal integer as string.
decimal = Integer.parseInt(number, 8); //Converts the octal string number to its decimal equivalent.
binary = Integer.toBinaryString(decimal); //Converts the decimal number decimal to its binary equivalent.
hexadecimal = Integer.toHexString(decimal); //Converts the decimal number decimal to its hexadecimal equivalent.
System.out.println("The binary equivalent is: "+binary);
System.out.println("The decimal equivalent is: "+decimal);
System.out.println("The hexadecimal equivalent is: "+hexadecimal);
}
else
System.out.println("Invalid choice. Please try again.");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.