Programming Assignment Checklist: LFSR

Preparation

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

Where can I review the concepts involved? Review Lecture 0 to reacquaint yourself with the basic ideas behind LFSRs. Read Sections 3.1 and 3.2 of the textbook to learn the basics of object-oriented programming and how to use Color and Picture data types.

Frequently Asked Questions (LFSR)

Does the API that I define for LFSR need to be exactly the same as the one prescribed? 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. Producing undocumented side effects (such as printing to standard output in any of the construcor or instance methods) is also an API violation. You may not add public methods to the API; however, you may add private methods (which are accessible only in the class in which they are declared).

How should I represent the LFSR? There are several possible approaches. One approach is to use an int[] array of length 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). However, you are allowed to use any efficient representation that you like.

How do I create an instance variable that 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. Additionally, you must comment the purpose of each instance variable.

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 argument to int values for the array? The characters zero and one are represented by the char literals '0' and '1'. The char data type is a Java primitive type, so you can compare two of them with if (c == '0').

Why am I getting 48s and 49s when I print out values for debugging? Internally, characters are represented as integers. The code for '0' is 48, and the code for '1' is 49. (Read about ASCII or Unicode for more information.) When you perform arithmetic operations on values of type char, they are promoted to type int. So, the expressions '0' + 1 and '1' ^ '1' ^ '1' both evaluate to the value 49 of type int. We recommend converting from the characters '0' and '1' to the integers 0 and 1, so that you don't have to deal with such type conversion anomalies.

In toString(), I loop the right number of times, but the returned String looks too short. What can cause this? This can be the reverse of the problem above. If you take the integer 0 or 1 and cast it to a char, it is a special control character that may appear blank when printed.

My generate() 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 code. The LFSR constructor arguments must 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);

Every object in Java has a toString() method, which is automatically called by StdOut.println().

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 re-declare 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 ^ operator.

Frequently Asked Questions (PhotoMagic)

Does the API that I define for PhotoMagic need to be exactly the same as the one prescribed? 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. Producing undocumented side effects (such as printing to standard output or mutating the Picture argument to transform()) is also an API violation. You may not add public methods to the API; however, you may add private methods (which are accessible only in the class in which they are declared).

Why must I create a new Picture object in transform(), instead of mutating the one passed in as argument? The API does not specify this is an allowable side effect, so it is not permitted. In practice, a client might be very annoyed if an object they passed to a method was mutated with proper documentation.

Why are we using the .png format instead of .jpg? It is a lossless format that preserves the precise bits in the colors.

How do I read and process the .png files? Use the Color and Picture data types described in Section 3.1 of the textbook. Review Program 3.1.3 (Luminance.java) for using the Color data type; review either Program 3.1.4 (Grayscale.java) or ColorSeparation.java for using the Picture data type.

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 linear feedback shift registers: 5-bit (tap 2), 6-bit (tap 4), 9-bit (tap 4), 10-bit (tap 6), 11-bit (tap 8), 20-bit (tap 16), 30-bit (tap 22), 36-bit (tap 24), 48-bit (tap 42), 60 bit (tap 58), 100 bit (tap 62), and 150 bit (tap 96).

How can I save an Picture that is displayed in a window? Select File -> Save -> filename.png from the menu in the window where the image is displayed, where filename is whatever name you want to give the image.

What is a java.awt.HeadlessException and how did I cause it? The assignment specifies that you should call show() only once, in PhotoMagic.main(). Calling it in other places can give this error.

Using the Color data type

The Color data type is built into Java in the "Abstract Window Toolkit" package.

Using the Picture data type

The Picture data type was created for this course.

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.

Possible Progress Steps

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

  1. Do the reading specified at the top of this page.

  2. Read the template LFSR.java. Your program will resemble it. You will need to specify the instance variables and fill in the constructor and methods.

  3. One reasonable way to represent the LFSR is to use an int[] array and the following instance variables:
    private int n;           // number of bits in the LFSR
    private int[] reg;       // reg[i] = ith bit of LFSR, reg[0] is rightmost bit
    private int tapPosition; // tap position
    

  4. Write the constructor. Your constructor for LFSR will initialize all the instance variables. Use new to allocate memory for the int[] array. Observe that you must do this in the constructor (and not when you declare the instance variables) since otherwise you wouldn't know how big to make the array.

  5. Write the toString() method. This will enable you to test the constructor.

    As you fill in the methods for LFSR, use the test code described in the assignment.

  6. Write the step() method. Test.

  7. Write the generate() method. Test.

  8. PhotoMagic. Your PhotoMagic class is a client of the Picture, Color, and LFSR classes. Place LFSR.java in the same directory as PhotoMagic.java, so the compiler will be able to find both of them. See also the sections on using the Color and Picture data types.

Enrichment