Question

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

*NOTE* Please use only void main method, if while and for statements. Please do not use the mathpow and the boolean is valid thing

Homework Answers

Answer #1
SOLUTION-
I have solve the problem in Java code  screenshot for easy understanding :)

CODE-
//java code 
import javax.swing.*;

public class BinToDec {
//main method 
    public static void main(String[] args) {

        String bin;
        int dec = 0;

        do {


            bin = JOptionPane.showInputDialog("Enter a 16-bit binary number.");

            dec = 0;
            int power = 1;
            for (int i = bin.length() - 1; i >= 0; i--) {
                if (bin.charAt(i) == '1') {
                    dec += power;
                } else if (bin.charAt(i) == '0') {

                } else {
                    System.out.println("Error: Entered binary " + bin + " is invalid binary number.");
                    dec = -1;
                    break;
                }
                power *= 2;
            }


            if (dec != 0 && dec != -1) {
                System.out.println("Base 2: " + bin);
                System.out.println("Base 10: " + dec + "\n");
            }

        } while (dec != 0);

        System.out.println("Program Terminating");

    }
}

SCREENSHOT -

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program in java that: 1. takes any number in any base between 2 and...
Write a program in java that: 1. takes any number in any base between 2 and 16 and converts it to base 10. 2. converts base 10 numbers to any given base between 2 & 16. The program should have a gui with an input box and two buttons, one button to convert any base between 2 and 16 to base 10; and one button to convert base 10 to any base between 2 and 16. **Please DO NOT upload...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
Write a program that reads a binary string (string of 0’s and 1’s) converting the binary...
Write a program that reads a binary string (string of 0’s and 1’s) converting the binary value into decimal. Allow the user to type in as many numbers as they want (one at a time) and end the program when they type in “0”. Use Horner’s method (given in step 3, below) to convert from binary to decimal. Sample Run Binary to Decimal Conversion Enter a binary string: 1101 1101 = 13 decimal Enter a binary string: 10011001 10011001 =...
2 Convert each of the following octal numbers to 10-bit binary, hexadecimal, and decimal. Show your...
2 Convert each of the following octal numbers to 10-bit binary, hexadecimal, and decimal. Show your work. (a) 368 (b) 7568 3 Convert the following binary values into Decimal, Octal and hexadecimal. Show your work. (a) 111010101011112 (b) 1010111011001102 (c) 1011101010001112 (d) 1111101011102 4 Convert the following hexadecimal numbers to 16-bit binary and decimal numbers. Show your work. (a) FE9816 (b) FCAD16 (c) B00C16 (d) FEDF16 5 Perform the addition on the following unsigned binary numbers. Indicate whether or not...
Binary Search Tree Code in Java Write a program that prompts user to enter however many...
Binary Search Tree Code in Java Write a program that prompts user to enter however many numbers they want to add to the binary search tree. Then have the user input numbers until the tree contains that many elements. If the user enters a number that already exists in the tree, then a message should be displayed "Number is already in the tree". Once all elements are added to the tree print its contents in sorted order. Assume all user...
WITH JAVA Follow the instructions in the attached to complete Task#2 and submit work along with...
WITH JAVA Follow the instructions in the attached to complete Task#2 and submit work along with screenshots of your output. I have attached the class code for Task#1 that you can use while completing Task#2. Task#1 CODE /** SocSecException exception class */ public class SocSecException extends Exception { public SocSecException(String error) { super("Invalid the social security number, " + error); } } Task #2 Writing Code to Handle an Exception 1. In the main method: a. The main method should...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is valid. Input: Interactive Output: Valid date is printed or user is alerted that an invalid date was entered. */ import java.util.Scanner; public class BadDate { public static void main(String args[]) { // Declare variables Scanner userInput = new Scanner (System.in); String yearString; String monthString; String dayString; int year; int month; int day; boolean validDate = true; final int MIN_YEAR = 0, MIN_MONTH = 1,...
Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly...
Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly dividing by b. Each time a division is performed the remainder and quotient are saved. At each step, the dividend is the quotient from the preceding step; the divisor is always b. The algorithm stops when the quotient is 0. The number in the new base is the sequence of remainders in reverse order (the last one computed goes first; the first one goes...
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal....
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal. 1. Request input data type. 2. Request input data. 3. Request output data type. 4. Output the data. The suggested approach was to take the input data as a string. But I am really lost as to how to process the string and then converting it to another data type. Thanks for the help!
WRITE A JAVA PROGRAM: The last digit of a credit card number is the check digit,...
WRITE A JAVA PROGRAM: The last digit of a credit card number is the check digit, which protects against transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Following steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if...