Question

In: Computer Science

Problem 4: Reading Binary You must write a program that willcontinuously prompt the user for...

Problem 4: Reading Binary You must write a program that will continuously prompt the user for a string representing a binary number. Then you should validate whether that string is valid binary number, and in case it is you must print the corresponding number in decimal base. Otherwise, you should state that the input string is not a valid binary number. Use the string “QUIT” to end the program. You should be aware that the input string may contain noise in the form of space characters. You must remove these characters from the string before testing whether it is a valid binary number. Your solution to this problem must contain three methods: • A method to remove the spaces. • A method to validate whether the string is a binary number • A method to convert the binary number string into an integer.

IN JAVA.

Solutions

Expert Solution

  1. Function for removing the string spaces.
  2. Checking whether the entered binary is valid or not.
  3. Find the value of binary to integer.

Code

import java.util.Scanner;

public class BinaryNumberCheck {
   public static String removeSpaces(String input) {
       input = input.replaceAll(" ", "");
       return input;
   }
   public static boolean checkBinary(String input) {
       for(int i = 0;i            if(!(input.charAt(i) == '1' || input.charAt(i) == '0')) {
               return false;
           }
       }
       return true;
   }
   public static int binaryToInteger(String input) {
       int integer = 0;
       for(int i = input.length()-1, j=0;i>=0;i--,j++) {
           int val = input.charAt(i)=='1'?1:0;
           integer += Math.pow(2, j) * val;
       }
       return integer;
   }
   public static void main(String args[]) {
       boolean quit = false;
       Scanner scan = new Scanner(System.in);
       while(!quit) {
           System.out.println("Enter the binary number(QUIT to quit)");
           String input = scan.nextLine();
           input = removeSpaces(input);
          
           if(checkBinary(input)) {
               System.out.println("The Decimal is: " + binaryToInteger(input));
               quit = true;
           }
       }
   }
}


Related Solutions

IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
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.
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT