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
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
Write a C program Your program will prompt the user to enter a value for the...
Write a C program Your program will prompt the user to enter a value for the amount of expenses on the credit card. Retrieve the user input using fgets()/sscanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 500, 500.00 and 500.10. The program will then prompt the user...
(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...
Create JAVA PROGRAM, and write comment for codes also. 4) Prompt the user for how much...
Create JAVA PROGRAM, and write comment for codes also. 4) Prompt the user for how much stuff a truck can carry, in pounds. Then ask them for the weight of each thing they add until they stop. Don't forget to be safe.
Write a program that uses input to prompt a user for their name and then welcomes...
Write a program that uses input to prompt a user for their name and then welcomes them. Note that input will pop up a dialog box. Enter Sarah in the pop-up box when you are prompted so your output will match the desired output.(In Python)
Write by hand a full java program that will prompt the user for the length and...
Write by hand a full java program that will prompt the user for the length and width of a square, calculate and display the area of that square rounded to the nearest tenth with an appropriate message.
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
Write a binary search algorithm in C language by performing following steps: Prompt the user to...
Write a binary search algorithm in C language by performing following steps: Prompt the user to enter the number of array elements (say, N).   Read the N different values (define these values to be of type integer).   Read the element (integer value) which you want to search. Invoke a function to display the values in the array. The function should take the array reference and number of elements as arguments and should have the return type as void. The function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT