V22.0101 ......Homework Assignment 1 ...... Fall 2012
Binary Representation of Numbers

Assigned: THURS, Sept 13.
First draft Due (by email to etutor): THURS, Sept 27, 11:59pm

Please read the assignment carefully. If you need help, don't hesitate to contact your E-TUTOR, but send the email EARLY, NOT just before the deadline. You can also see the lab tutor or the Professor during their office hours. Even though this is the first assignment, it uses concepts from all of Chapters 2 through 5. However, you do not need to read all of these chapters yet. Read the parts you need, especially Sections 3.3 for the if statement, Section 4.4 for the for loop and pages 37-40 for the Scanner class. We have discussed all of this in class.

Decimal numbers from 0 through 255 can be represented in the binary number system using 8 bits. Here are some examples:

DecimalBinary
0 00000000
1 00000001
2 00000010
3 00000011
7 00000111
15 00001111
16 00010000
25 00011001
128 10000000
129 10000001
254 11111110
255 11111111
Thus, for example, the decimal number 25 is represented in binary as 00011001, meaning 0 times 128 plus 0 times 64 plus 0 times 32 plus 1 times 16 plus 1 times 8 plus 0 times 4 plus 0 times 2 plus 1 times 1. Notice that EACH BIT CORRESPONDS TO A POWER OF 2. Make sure you understand how this works before you go any further.

This assignment asks you to write a Java class called Binary. The decimal number will be stored in the variable num declared in the main method. The class Binary will have two methods that you must write. Here are the details.

• the variable bin (type String) is where the string of bits corresponding to the binary representation of the number is stored by one of the methods (see below).

• The main method should check to make sure the decimal value is between 0 and 255. If it is not, it should request the user to re-enter the data until the data is valid.

• the method getBin (public static String getBin(int num )) should take the value of num that is passed to it, generate the corresponding binary string store it in bin and returns its value to the main method. YOU MUST WRITE THIS METHOD BY YOURSELF. YOU MAY NOT CALL A METHOD FROM THE JAVA LIBRARY TO DO THIS JOB. The method should consist of a single for loop. This loop generates the 8 bits of the bit string, one at a time. Do this by generating the bits from using repeated division. See Reverse1.java done on Sept 11 in class. As each bit is generated, you have to concatenate it to the current value of bin. Start with the empty string.

• the method checkBin (public static boolean checkBin(String bin, int num)) should take the binary string in bin and check to see if it correctly represents the number in num, essentially reversing the operation of getBin. First multiply the first bit by 2, then add the second bit and multiply all that by 2, and then add the third bit, and so on. Again, make sure you use a for loop and document it with COMMENTS. The method checkBin should return a Boolean value, indicating whether the result matches the number in num. To extract each bit, you need to use the String method charAt (see p. 265 6/e, p267 7/e, p339 9/e) to get the relevant char value and you can then test this against the char values '0' and '1' to complete the job.
Begin by "hard-wiring" the example number into the main method; but when you submit the program, read the number from the the keyboard using the Scanner class described in the text book (page 37-39).