V22.0101 ......Homework Assignment 1 ...... Fall 2007
Binary Representation of Numbers
Assigned: WED, Sept 12.
First Version Due (by email to etutor):
(Marateck) TUES, Sept 25, 11:59pm
Final Version Due (by email to etutor): 7 days after receiving comments from
etutor
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 TA 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.1 and 3.3.2 for the
if statement and Section 4.4 for the for loop.
Decimal numbers from 0 through 255 can be represented in the binary number
system using 8 bits. Here are some examples:
| Decimal | Binary |
| 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 numVal. Another variable
binStr will store the binary string corresponding to numVal. The
class Binary will have two methods that you must write. Here are the details.
- the variable binStr (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 print an informative error message (using System.out.println) and store
the number 0 instead.
- the method getBin (public static String getBin(int num ))
should take the value that is passed to it, generate the corresponding binary
string and returns it. 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.
There are two possible ways to do this. The easiest is to generate the bits
from LEFT to RIGHT, by successively subtracting powers of 2 from numVal. Take
the number 25 for example. If we subtract the biggest relevant power of 2, the
number 128, from this, we get a negative answer so we know the first bit must
be 0. The first power of 2 which is smaller than 25 is 16, so that tells us
that the bit corresponding to that power (that's actually the fourth bit from
the left) is 1. Once 16 is taken away from 25, we have 9 left. The next power
of 2 is 8, which is less than 9, so the next bit (the fifth from the left) is
also 1. Now we only have 1 left, so the sixth and seventh bits are 0, and the
final eights bit is 1. You should not type all the powers of 2 into the
program. You should start with 128, and then repeatedly divide this by 2 in
the loop. Don't use any of the numbers in the table above for an example.
Choose another number, say between 70 and 250 for your example. Don't use the
same example as a classmate, if you can avoid it. (Of course, some people may
choose the same example by accident -that's OK.)
There's an alternative way to code this method, also using a for loop but
generating the bits from RIGHT to LEFT, using repeated division. See p. 11 of
the text. See the Powerpoint presentation at the last link for the homework
http://cs.nyu.edu/courses/spring02/V22.0102-002/binary.ppt. Look at the
slides up to the the ASCII table.
As each bit is generated, either left to right or right to left, you have
to concatenate it to the current value of binStr. Start with the empty string.
- the method checkBin (public static boolean checkBin(String
binStr, int numVal)) should take the binary string in binStr and check to
see if it correctly represents the number in numVal, essentially
reversing the operation of getBin. This is easy, again using a loop: you need to extract the first bit of the string binStr, multiply it
by 128, then add this to the second bit multiplied by 64, and so on. Again,
don't type all the powers of 2 into the program: just start with 128 and
repeatedly divide it by 2. Another variation is to 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 numVal. To extract each bit, you need
to use the String method charAt (see p. 265) to get the relevant char
value and you can then test this against the char values '0'
and '1' to complete the job.
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");.
After your program runs properly, alter it slightly so that it can read the
data from the command line. If you have a mac, the terminal program gives you
the command line. If you have a PC, see
http://www.computerhope.com/issues/ch000549.htm (the link is on the web page
under the assignment) for how to include the java and javac programs in your
path. However, the easiest way is to place your program in the Bin directory
of the jdk directory. Hand this version into your etutor. If you can't
get this version to work, 0.2 points will be deducted from a possible high
of 4.0 points.. You should hand in two versions of the assignment.
One that hard-wires the input and one that uses the command line.
Please get started on the assignment immediately, and
contact your etutor as soon as possible with any questions. Don't expect
a response the day before the due date. Late homeworks will be accepted
up to 7 days late, but they will be penalized 25%.
Remember, you may submit a revised version to your etutor within 7 days of
receiving comments on your first version, in order to improve your grade.