V22.0101 ......Homework Assignment 1 Draft #2 ...... Fall 2007
Binary Representation of Numbers

Assigned: WED, Sept 19.
First and final version Due (by email to etutor): (Marateck) MON, OCT 1, 11:59pm

Rewrite your first draft so that a class called Binary stores the decimal number in the instance variable numVal and the binary string corresponding to numVal will be stored in the instance variable binStr. The value of the decimal number is passed by the driver class Hw1 to the constructor.

The driver will have the following statements in an if block in the main method:

```Binary bin = new Binary(num);
bin.getBin();
boolean valid= bin.checkBin();
```

Note that getBin() and checkBin() have no parameters. The necessary information is passed through the constructor.

The constructor has the heading public Binary(int num) and will assign num to numVal. . The class Binary will have the same two methods you have already written for draft #1. They now have the following form:

• public void getBin(). This produces the string consisting of the binary equivalent of numVal prints it and assigns it to the instance variable binStr.
• public boolean checkBin()). This take the binary string in binStr and checks to see if it correctly represents the number in numVal. If it does, it returns true to the driver class, Hw1, else it returns false.
• The main method in Hw1should check to make sure the decimal value is between 0 and 255. If it is not, it should print an informative error message (using System.out.println) and store the number 0 instead.
You can "hard-wire" the example numbers into the main method, or you can read them from the the keyboard using the JOptionPane class described in the text book (page 45). the simplest form to use for assigning input to a string, let's say number is String number = JOptionPane.showInputDialog("Type your decimal number");. If you use this approach, and are using Java 1.5 or lower remember to end the main method with System.exit(0) otherwise the program will hang.