Question

In: Computer Science

Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....

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.

Solutions

Expert Solution

Here is the answer for your question in Java Programming Language.

Kindly upvote if you find the answer helpful.

#####################################################################

CODE :


import javax.swing.JOptionPane;

public class BinaryConversion {
public static void main(String[] args){
//Required variables
String userInput = "";
String decimal = "";
int flag,sum,k =0;
//Loops till user enters 16 0's
do{
//Reads user input
userInput = JOptionPane.showInputDialog("Enter a 16-bit binary number. ");
//If user clicks 'Cancel' or 'X'
if(userInput == null){
//calls handleCancel
userInput = handleCancel();
}
//Repeatedly asks input when user enters less than 16 bits
while(userInput.length() != 16){
JOptionPane.showMessageDialog(null, "Input should be a 16-bit binary number");
userInput = JOptionPane.showInputDialog("Enter a 16-bit binary number. ");
//If user clicks 'Cancel' or 'X'
if(userInput == null){
//calls handleCancel
userInput = handleCancel();
}
}
//Repeatedly asks input when user enters 16 bits but any digit other than 0's and 1's
do{
flag = 0;
for(int i = 0;i<userInput.length();i++){
if(userInput.charAt(i) != '0' && userInput.charAt(i) != '1')
flag = 1;
}
if(flag == 1){
JOptionPane.showMessageDialog(null, "Input should contain only 0's and 1's");
userInput = JOptionPane.showInputDialog("Enter a 16-bit binary number. ");
//If user clicks 'Cancel' or 'X'
if(userInput == null){
//calls handleCancel
userInput = handleCancel();
}
}else{
flag = 0;
}
}while(flag == 1);
//Calculates decimal value
sum = 0;
k = 0;
for(int j = (userInput.length()-1);j>=0;j--){
sum += Math.pow(2, k)*Integer.parseInt(String.valueOf(userInput.charAt(j)));
k++;
}
//DIsplays output
JOptionPane.showMessageDialog(null, "Base 2: " + userInput + "\nBase 10: " + sum);
  
}while(!userInput.equalsIgnoreCase("0000000000000000") );
}
//Method to handle when user clicks 'Cancel' or 'X' to close the window
public static String handleCancel(){
String userInput = "";
do{
JOptionPane.showMessageDialog(null, "Enter 16 0's to exit the program");
userInput = JOptionPane.showInputDialog("Enter a 16-bit binary number. ");
}while(userInput == null);
return userInput;
}
}

########################################################################

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 :)


Related Solutions

Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a Java program named CircleZapper that displays a circle with a radius of 10 pixels,...
Write a Java program named CircleZapper that displays a circle with a radius of 10 pixels, filled with a random color at a random location on the screen. When you click the circle, it disappears and a new random color circle is displayed at another random location (see display below). After twenty circles are clicked, display the time spent in the pane. To detect whether a point is inside the circle, use the contains method defined in the Node class....
How to convert 12345 from base 10 to base 2?
How to convert 12345 from base 10 to base 2?
Write a java program that will ask for a Star Date and then convert it into...
Write a java program that will ask for a Star Date and then convert it into the corresponding Calendar date. without using array and methods.
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
Convert 101 from base-2 number system to base-10 number system Convert 101 from base-2 number system...
Convert 101 from base-2 number system to base-10 number system Convert 101 from base-2 number system to base-16 number system Convert 100 from base-10 number system to base-2 number system Convert 100 from base-10 number system to base-16 number system Convert ef from base-16 number system to base-2 number system Convert ef from base-16 number system to base-10 number system
Write a c++ program to convert any decimal number to either binary base  or Hex base...
Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings: Convert 15 from decimal to binary.  Convert 255 from decimal to binary. Convert BAC4 from hexadecimal to binary Convert DEED from hexadecimal to binary.  Convert 311 from decimal to hexadecimal. Convert your age to hexadecimal.
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT