Programming Assignment Checklist: LFSR

Pair programming. On this assignment, you are encouraged (not required) to work with a partner (who must be in your same precept) provided you practice pair programming. Pair programming "is a practice in which two programmers work side-by-side at one computer, continuously collaborating on the same design, algorithm, code, or test." One partner is driving (designing and typing the code) while the other is navigating (reviewing the work, identifying bugs, and asking questions). The two partners switch roles every 30-40 minutes, and on demand, brainstorm.

Before pair programming, you must read the article All I really need to know about pair programming I learned in kindergarten. You should choose a partner (of similar ability) from the same precept. You and your partner will turn in one copy of the assignment code and one readme.txt for the assignment. The code and the readme must list the names of both partners and the readme.txt should detail your experience.

Please note that writing code with a partner without following the pair programming instructions listed above is a serious violation of the course collaboration policy.

Frequently Asked Questions

What are the goals of this assignment? To introduce you to object-oriented programming and to reinforce the message of Lecture 1.

What preparations do I need to do before beginning this assignment? Review the Lecture 1 slides to reacquaint yourself with the basic ideas behind LFSRs. Also read Sections 3.1 and 3.2 of the textbook (to learn the basics of object-oriented programming).

Do I need to follow the prescribed API? Yes, we will be testing the methods in the API directly. If your method has a different signature or does not behave as specified, you will lose a substantial number of points.

How should I represent the LFSR? There are several possible approaches. We recommend that you use an int[] array of size N, each entry of which is either 0 or 1. If you follow this approach, the constructor amounts to creating the array and converting the char values in the string to int values for the array (and initializing your other instance variables).

How do I create an instance variable which is an array if I don't know its length at compile time? Declare it as an instance variable, but initialize it in constructor (where you will know its length). For example, see Program 3.2.3 in the textbook.

What extra comments should I include when writing object-oriented programs? You must comment the purpose of every method, using the names of the argument variables in your description, as below.

// simulate k steps of the LFSR and return corresponding k-bit integer
public int generate(int k) 
Additionally, you must comment the purpose of each instance variable, as below.
private int N;          // number of registers in the LFSR
private int[] reg;      // reg[i] = ith least significant (rightmost) bit of the LFSR
private int tap;        // index of the tap bit

In the initial register, fill.charAt(0) is the leftmost bit. But in the Lecture 1 slides and the assignment page, bit 0 is the rightmost bit. Do I have to arrange the bits in my register array that way? Strings are always indexed from left to right (the way we read English). Traditionally, binary digits are indexed from right to left, with bit 0 as the lowest order or rightmost bit. You are welcome to use either approach here—just be careful to do it correctly and consistently.

How do I convert the char values in the string to int values for the array? The number zero is represented by the char value '0'. The char data type is a Java primitive type, so you can compare two of them with if (c == '0').

My step() method is producing 19 for the binary number 11001. What am I doing wrong? You are calculating the bits in reverse order. 19 is the decimal value of the binary number 10011.

My generate() works with the 11 bit fill and the tap at position 8, but when I try generate()with the 20 bit fill and the tap at position 16, I get a different answer from the one shown in the example. What am I doing wrong? Make sure you are not hardwiring 11 or 8 in your LFSR code. The LFSR constructor arguments should set the size of the register and the tap position.

The toString() is not explicitly called in the test client code, but it still gets called. How does this work?

      LFSR lfsr = new LFSR("01101000010", 8);
      StdOut.println(lfsr);

When a Java object is passed to any of the print methods, a call is automatically made to that object's toString() method.

I get an ArrayOutOfBounds or NullPointerException error. What could cause this? Do your constructors initialize all of the instance variables (e.g., N, reg, and tap)? Did you allocate memory for your array with new? Did you inadvertently redeclare int N or int[] reg in a method or constructor, thereby hiding the instance variable with the same name?

How do I do exclusive or (XOR) in Java? Use the ^ Java symbol. The operation a ^ b, where a and b are int values, does a bit-by-bit exclusive or of the values.

How do I read in the .png files? Use our Picture.java data type, described in Section 3.1 of the textbook. You will only need the constructors and methods of the Picture API summarized on page 330. To see it in action, look at the program Grayscale.java which takes the name of a picture file as a command line argument, converts all pixels to gray, and displays all those pixels. If you download Grayscale.java, you will also want to download Luminance.java. Look at Luminance.java to see how to retrieve the r, g, and b values for the Color.

Sometimes my encrypted picture looks like a shadowy version of the original. How do I pick a good tap number? Here are suggested tap numbers for maximizing the cycle of different size binary registers: 5 bits, tap 2; 6 bits, tap 4; 9 bits, tap 4; 10 bits, tap 6; 11 bits, tap 8; 20 bits, tap 16; 25 bits, 21; 36 bits, tap 24; 60 bits, tap 58; 100 bits, tap 62; 150 bits, tap 96.

Testing

Be sure to thoroughly test each piece of your code as you write it. We offer some suggestions in the assignment specification. You should also test your data type with other parameters.

Submission

Your programs must be named LFSR.java and PhotoMagic.java. Use the following readme file template. Don't forget to hit the "Run Script" button on the submission system to test that it compiles cleanly.

Possible Progress Steps

These are purely suggestions for how you might make progress. You do not have to follow these steps.

Enrichment


COS 126 Home Page