V22.0101 ......Homework Assignment 1 ...... Fall 2003
Binary Representation of Numbers
Assigned: WED, Sept 14.
First Version Due (by email to etutor):
(Marateck) TUES, Sept 27, at midnight
Final Version Due (by email to etutor): 7 days after receiving comments from
Please read the assignment carefully and (very important) look
at the Java Template for it: click
The template will be very helpful in getting started.
Download the file onto your computer and then open it in JCreator and edit it
there. (The indentation doesn't look quite right in the browser but it should
look right in JCreator.) 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 4. However,
you do not need to read all of these chapters yet. Read the parts you need,
especially Sections 3.2.1 and 3.2.2 for the
if statement and Section 3.3.2 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:
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.
|0 || 00000000 |
|1 || 00000001 |
|2 || 00000010 |
|3 || 00000011 |
|7 || 00000111 |
| 15 || 00001111 |
| 16 || 00010000 |
| 25 || 00011001 |
|128 || 10000000 |
|129 || 10000001 |
|254 || 11111110 |
|255 || 11111111 |
This assignment asks you to write a Java class called Binary.
Every object of the class Binary will have a numerical value, which will be
stored in the instance variable numVal. Another instance variable binStr will
be used to store the binary string corresponding to numVal. In addition to its
constructor, the class Binary will also have two methods that you must write.
Here are the details.
The template also has a class called TestBinary with just one method, the
main one. This goes in the same file as class Binary, but at the top.
Because TestBinary contains the main method, the Java file
name MUST be called TestBinary.java. TestBinary is public but Binary cannot be
public because Java permits only one public method per file. The main method
of TestBinary is supposed to test the methods in class Binary. You should run
TestBinary on lots of examples to make sure everything is working correctly.
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 855). 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, remember to end the main method with
System.exit(0) otherwise the program will hang. Notice that you cannot
access the instance variables numVal and binStr outside the
Binary class because their access is private.
- the public instance variable numVal (type int) (see the top of the
template) is where the value of a number is stored by the class constructor
(see below). Although it's tempting to think of this as the decimal
representation of the number, since that is how System.out.println displays it,
it is actually stored internally in a binary format that you cannot see.
- the public instance 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 constructor Binary (one parameter; notice that a constructor has the
the same name as the class name and does not return a value)
assigns a value to numVal.
That's all the constructor has to do, using the int parameter that is
passed to it. (See page 215 of the text.) However, the constructor should
check to make sure the parameter 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 (returns void, no parameters) should take the
value that is in the instance variable numVal, generate the corresponding
binary string and store it in the instance variable binStr. YOU MUST WRITE
THIS METHOD BY YOURSELF. YOU MAY NOT CALL A METHOD FROM THE JAVA LIBRARY TO DO
THIS JOB. YOU MAY NOT COPY FROM A CLASSMATE. HOWEVER, YOU MAY TALK TO YOUR
CLASSMATES ABOUT HOW TO DO IT. 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. YOU HAVE TO FIGURE OUT HOW TO EXPRESS THIS IDEA USING A FOR LOOP
THAT WORKS AND IS WELL DOCUMENTED, EXPLAINING HOW IT WORKS ***WITH AN
EXAMPLE***. 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.) This explanation MUST APPEAR
IN YOUR COMMENTS.
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
See the Powerpoint presentation at the last link for the homework
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 (returns Boolean, no parameters) 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. 256) to get the
relevant char value and you can then test this against the char
values '0' and '1' to complete the job. As with getBin, you must give
DETAILED COMMENTS with an EXAMPLE (use the same example you used for your
comments in getBin).
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.
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.