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...
Write a program that calculates the salary of employees. The program should prompt the user to...
Write a program that calculates the salary of employees. The program should prompt the user to enter hourly rate and number of hours of work a day. Then, the program should display the salary daily, bi-weekly (5 days a week), and monthly. Sample program: Enter your hourly rate: >>> 20 Enter how many hours you work a day: >>> 8 Your daily salary is: $160 Your bi-weekly salary is: $1600 Your monthly: $3200
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
(b) You will write a program that will do the following: prompt the user enter characters...
(b) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘Q’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘Q’. Example input mJ0*5/]+x1@3qcxQ The ‘Q’ should be included when computing the statistics properties of the input. Since characters are integers...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT