V22.0101 ......Homework Assignment 1 ...... Spring 2007
Binary Representation of Numbers

Assigned: WED, Jan 24.
First Version Due (by email to etutor): (Marateck) TUES, Feb 6, at midnight
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:

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

You can "hard-wire" the example numbers into the main method, or you can read them from the the keyboard using the Scanner class described in the text book (page 53). You must have JDK1.5 or later, and import java.util.Scanner to use the Scanner class. To read an integer you would write Scanner kbd = new Scanner(System.in); followed by int num = kbd.nextInt().

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.