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 etutor

Please read the assignment carefully and (very important) look
at the Java Template for it: click
here.
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:

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 |

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 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 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 (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 thechar 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.