Write the bin2Dec(String binaryString) method to convert a binary string into a decimal number. Implement the bin2Dec method to throw a NumberFormatException if the string is not a binary string. Write a test program that prompts the user to enter a binary number as a string and displays decimal equivalent of the string. If the method throws an exception, display "Not a binary number". Sample Run 1 Enter a binary number: 101 5 Sample Run 2 Enter a binary number: 41 Not a binary number: 41

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter14: Exception Handling
Section: Chapter Questions
Problem 2PE
icon
Related questions
Question

(NumberFormatException)

Write the bin2Dec(String binaryString) method to convert a binary string into a decimal number. Implement the bin2Dec method to throw a NumberFormatException if the string is not a binary string. Write a test program that prompts the user to enter a binary number as a string and displays decimal equivalent of the string. If the method throws an exception, display "Not a binary number".


Sample Run 1
Enter a binary number: 101
5

Sample Run 2
Enter a binary number: 41
Not a binary number: 41

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer