Problem 9.2

CIS 210 Home Page Last updated 20%E% %U%

Problem 9.2: Binary Numbers

In this problem, you will implement a class to encapsulate binary numbers. Objects of your BinaryNumber class will be initialized from a string of zeroes and ones. The BinaryNumber objects will be immutable, i.e., no methods will change their data. BinaryNumber objects will have methods to perform some simple calculations and comparisons.

The specific requirements for the BinaryNumber class are:

The testing code in the provided skeleton gets two binary numbers on the command line or prompts for them, and then performs some operations with them. You could adjust the driver code to do some other arithmetic as well. Here are some examples:

> java BinaryNumber 10011 101111
b1 is 00010011 (19)
b2 is 00101111 (47)
b1.equals(b2) is false
b3=b1.add(b2) is 01000010 (66)
b3=b3.add(b1) is 01010101 (85)
b2=b2.add(b1).add(b1) is 01010101 (85)
b2.equals(b3) is true

> java BinaryNumber 00000001 11111111  
b1 is 00000001 (1)
b2 is 11111111 (255)
b1.equals(b2) is false
b3=b1.add(b2) is 00000000 (0)
b3=b3.add(b1) is 00000001 (1)
b2=b2.add(b1).add(b1) is 00000001 (1)
b2.equals(b3) is true



Constraints and Hints

Turn-in

Turn in your BinaryNumber.java.