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

Assigned: WED, Sept 15.
First Version Due (by email to etutor): (Marateck) TUES, Sept 28, 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:

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. 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 48). 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.

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.