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 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.
How to convert 12345 from base 10 to base 2?
How to convert 12345 from base 10 to base 2?
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...
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 –...
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.
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...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
Java question- Write a java program to process the number.txt file. Then count the numbers and...
Java question- Write a java program to process the number.txt file. Then count the numbers and calculate the total average, even numbers average, odd number average, then print the corresponding information. The result should be displayed in the following format there are XX numebers in the file there are xx even numbers there are xx odd numbers the total number average is xx the odd number average is xx the even number average is xx I am having trouble using...
Using Java, write a program named MyAngles that will prompt the user for the measure of...
Using Java, write a program named MyAngles that will prompt the user for the measure of the three sides of a triangle and then reports the measurement of each interior angle of the triangle and the area of the triangle.
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT