Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Write a method that parses a binary number as a string into a decimal integer. The function header is as follows: int parseBinary(String binaryString) Assume

Write a method that parses a binary number as a string into a decimal integer. The function header is as follows: int parseBinary(String binaryString) Assume that we do not support parsing of negative binary numbers.

* Make parseBinary() method throw an IllegalArgumentException in cases

- when the string that is being parsed is not a binary number (contains characters other that 0 or1).

- when the string is too long and the resulting number is too large to be stored as an integer

*Test your method in main(). Use try/catch block to test the exception-throwing part of the code. Do not use user input hard-code all of your test cases instead.

Example, binary string 10001 is 17: 1*24+ 0*23+1*22+0*21+1*20 = 16+1 = 17 parseBinary(10001) returns 17.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions