In: Computer Science
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers.
The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered.
Once the 16-bit number has been entered your program should make sure that the input is valid, that is, make sure that it has 16 bits and make sure that there are no digits other than 0 and 1 in the input. If the input is invalid an appropriate message should be printed, the current, invalid, 16-bit base 2 number should not be converted, and your program should ask for a new 16-bit number.
Now that you’ve got a valid 16-bit base 2 number (remember that it’s actually a String) loop through the number to convert it to base 10. You must process each binary number digit by digit. No built-in conversion functions allowed.
Your program should print the output in a message dialog or the terminal window.
Sample input/output:
Enter a 16-bit binary number. 1111000011110000
Base 2: 1111000011110000
Base 10: 61680
enter a 16-bi binary number. 0000000000000000
Program Terminating
The proper use of loop and selection structures is the main focus of this assignment. Style and format making your program more readable is, as always, of considerable importance.
Test your program with several 16-bit base 2 numbers, to ensure that it is functioning correctly. You should also test your validation routine to make sure that it catches invalid input.
Here is the answer for your question in Java Programming Language.
Kindly upvote if you find the answer helpful.
#####################################################################
CODE :
public class BinaryConversion { |
########################################################################
SCREENSHOTS :
Please see the screenshots of the code below for the indentations of the code.
#####################################################################
OUTPUT :
Initially the JOptionPane looks like,
When user clicks "cancel" or "X" without enterning any input,
When you click on "OK",prompts again,
If you click on "OK",
Again on clicking "OK", and entered 16 bits,
If you click "OK" and entered valid input,
Any doubts regarding this can be explained with pleasure :)