<<

CS 170 Section 003 HW4 – Fall 2013

Due: Tuesday, Oct. 8th 2013 by the beginning of class.

Honor Code: Like all work for this class, the Emory Honor Code applies. You should do your own work on all problems, unless you are explicitly instructed otherwise. If you get stuck or have questions, ask your instructor or a TA for help. For EACH of the files you submit, be sure to put the appropriate honor code statement (as specified on the course syllabus) at the top of the in comments. Preparation: This is your first programming assignment and you must learn to protect your computer files from illegal access by other students. To disallow other students from reading your homework programs, you must save your file(s) in a directory inside your cs170 directory. If you follow the below commands, your work will be protected. 1. Create a directory called hw4 directory inside your cs170 project directory to save your hw4 files. ~/cs170/hw4 2. You must use ~/cs170/hw4 directory as your current directory when editing any for hw4. Change your current directory to your newly created hw4 directory: ~/cs170/hw4 3. You can now run gedit to edit your programs: gedit yourProgramName.java & The name yourProgramName is the name of the Java program (and also the name of the class!).

Part I: As we discussed in class, all data in a computer is represented in bits. We represent these bits with the symbols 0 or 1. All data stored in a computer is represented as bits: music, .doc files, your web browser application, videos, EVERYTHING. In this homework, you'll represent integers in binary (base 2) rather than our usual decimal (base 10) representation.

First, we need to understand how to convert a decimal number to it's binary representation. Fortunately, this is easy. We just divide by 2 repeatedly until the number is 0, collect the remainders, and reverse the order! (We also discussed this in class!) Here's an example with the decimal number 52.

Calculation Quotient Remainder 52 / 2 26 0 26 / 2 13 0 13 / 2 6 1 6 / 2 3 0 3 / 2 1 1 1 / 2 0 1

1 When we collect the remainders in reverse order, we have the binary sequence 110100 which is the binary representation of the number 52. Notice that the bits are generated first when we begin dividing by 2, but in the wrong order.

Write a program named BinaryRep.java. Your program should prompt the for an integer and then its binary representation.

On pg 4, there are extensive instructions on how to break down this problem piece by piece and solve it. Also, there are some examples of running the program, but your program should work with any valid, positive integer.

>>> java BinaryRep Please enter a number: 52 The binary representation of 52 is: 110100

>>>java BinaryRep Please enter a number: 3 The binary representation of 3 is: 11

>>>java BinaryRep Please enter a number: 89 The binary representation of 89 is: 1011001

Part II: Now that you have your program calculating binary representations, it's time to identify “magic numbers”. Magic numbers are those in which the last three binary digits are 111. Add statement(s) to your program which print out “ is a magic number!” if the last three binary digits are 111. Examples:

>>> java BinaryRep Please enter a number: 7 The binary representation of 7 is: 111 7 is a magic number!

>>>java BinaryRep Please enter a number: 43 The binary representation of 43 is: 10111 43 is a magic number!

>>>java BinaryRep Please enter a number: 88 The binary representation of 88 is: 1011000

Grading: Your grade will be determined based on the correctness of your programs as well as program style. Program style includes such things as comments, variable/method names, and readability/spacing. Late penalties apply as per the class syllabus.

Submission: • Be sure to include the Honor Code statement as a comment at the top of EACH file you submit. Failure to do so will result in a 10 point deduction.

2 • Only your last submission will be graded. Scores will be assigned based on the last submission only. • Submit your work using the following command. You need to be in your ~/cs170/hw4 directory when you issue it. ◦ /home/cs170003/turnin-hw BinaryRep.java hw4 • Your homework is not turned unless the above commands are successful (you will get a "success" message when turn in was successful).

3 Problem Decomposition

Decomposing a English problem statement into running code is difficult for most beginning students. I'm providing you with an explicit series of steps to solve Part I below. (Part II is on your own.) Realize that this IS NOT the only way to solve this problem. There are other, equally valid ways of translating this problem into Java code. This is simply one way to help you to see how to break down a problem into small, easily solvable steps. Note the emphasis on writing small bits of code, frequently compiling, and fixing errors as we go along.

1. Make the basic BinaryRep.java file in the appropriate directory. Make sure you can compile it and run it (even if it doesn't do anything interesting yet). 2. Set up a Scanner object. Make your scanner in an integer that's typed by the user. You will want to store this value into a variable. For now, just print variable out. Then compile, fix errors, run etc. to make sure everything is ok. 3. You have to print out the binary representation. This is accomplished by doing something over and over again until some condition is met. We need to use repetition. This means either a while loop or a for loop! You can use either, but in this case, a while loop is probably easier. We are repeatedly dividing by 2. Set up a loop which repeatedly divides the user input by 2 until it equals 0. Print out the number, compile, run and . If you accidentally an infinite loop, type Ctrl-c in the terminal window to terminate your program. 4. Now we need to collect and store the remainders when the number is divided by 2 since that is what forms our binary representation. Use a variable to store the the remainder. Print out the remainders in your loop. Compile, fix errors, run, check, etc to make sure you're still on track. 5. Now all you need to do is collect the remainders as they are generated each time the loop repeats. Use a String variable! Just remember that you're appending your newly generated remainder to the LEFT of your previously collected remainders. Define your String variable outside you loop, and each loop iteration should add to that variable. Compile, run, test, etc 6. Test and verify. Check that your program works for small numbers like 1 and 3. Then check it works for larger numbers like 25 or 52 (example above). In class, we talked about test cases, so think about appropriate test cases for your code. 7. Comment your code. Remember that comments focus on the “big picture.” Your comments should not simply restate what your code is doing line-by-line. 8. Celebrate. (And then on to Part II)

4