Monthly Archives: September 2014

OOD Project: LFSR by parts: generate a bit

Screen Shot 2015-02-23 at 12.19.21 AM

THIS ASSIGNMENT WAS NOT GIVEN ON 2014-2015 SCHOOL YEAR

1. Simulate one step. The step() method simulates one step of the LFSR and returns the rightmost bit as an integer (0 or 1). For example,

// LFSR lfsr1 = new LFSR("01101000010", 9);
// for this assignment the seed and tap can be hardcoded or input
String lfsr1 = "01101000010";
int tap = 9;
StdOut.println(lfsr1);
for (int i = 0; i < 10; i++) {
    // int bit = lfsr1.step();
    // code to create a sequence of 0s and 1s like the example below
    StdOut.println(lfsr1 + " " + bit);
}

outputs
01101000010
11010000101 1
10100001011 1
01000010110 0
10000101100 0
00001011001 1
00010110010 0
00101100100 0
01011001001 1
10110010010 0
01100100100 0

2. Extracting multiple bits. The method generate() takes a positive integer k as an argument and returns a k-bit integer obtained by simulating k steps of the LFSR. This task is easy to accomplish with a little arithmetic:

  • initialize a variable to zero
  • for each bit extracted, double the variable
  • add the bit returned by step()

For example, extracting (from left to right) each bit from the sequence 1 1 0 0 1, the variable takes on the values 1, 3, 6, 12, and 25, ending with the binary representation of the bit sequence.

gen = 0
gen = gen * 2 + new_bit = 0 * 2 + 1 = 1
gen = gen * 2 + new_bit = 1 * 2 + 1 = 3
gen = gen * 2 + new_bit = 3 * 2 + 0 = 6
gen = gen * 2 + new_bit = 6 * 2 + 0 = 12
gen = gen * 2 + new_bit = 12 * 2 + 1 = 25

For example,

// LFSR lfsr2 = new LFSR("01101000010", 9); 
// for this assignment the seed and tap can be hardcoded or input
String lfsr2 = "01101000010";
int tap = 9;
StdOut.println(lfsr2);
for (int i = 0; i < 10; i++) {
    // int r = lfsr2.generate(5);
    // code to generate a number like 25 in the example above
    StdOut.println(lfsr2 + " " + r);
}

outputs
01101000010
00001011001 25
01100100100 4
10010011110 30
01111011011 27
01101110010 18
11001011010 26
01101011100 28
01110011000 24
01100010111 23
01011111101 29

Asignment:
Write a java program, LFSRSim_YI.java to generate an output like the one right above for a given seed.
NOTE: It doesn’t have to be OOP

OOD Project: LFSR Imaging Assignment

Animations using java
duke

Screen Shot 2015-02-23 at 12.19.21 AM

THIS ASSIGNMENT WAS NOT GIVEN ON 2014-2015 SCHOOL YEAR

/**
 *  Thr Picture.java class provides methods for manipulating individual pixels of
 *  an image. The original image can be read from a .jpg, .gif,
 *  or .png file or the user can create a blank image of a given size.
 *  This class includes methods for displaying the image in a window on
 *  the screen or saving it to a file.
 *

Picture.java (if this file is not the latest version, visit the Standard Library Site)

* Pixel (x, y) is column x and row y. * By default, the origin (0, 0) is upper left, which is a common convention * in image processing. * The method setOriginLowerLeft() change the origin to the lower left. *

* For additional documentation, see * Section 3.1 of * Introduction to Programming in Java: An Interdisciplinary Approach

* by Robert Sedgewick and Kevin Wayne. * * @author Robert Sedgewick * @author Kevin Wayne

More information on processing images is in the link below

 

Assignment: Write a java program, Pixels30x30_YI.java to display the RGB colors of the top area of the image in this format:

R     G    B
196   200  105
155   0    50
...

N-Body simulation student discussions

September 17th, 2014

N-Body simulation student discussions

Screen Shot 2014-09-15 at 8.43.34 PM

An immutable data type has the property that the value of an object never changes once constructed.

An assertion is a boolean expression that you are affirming is true at that point in the program.

Next assignment:
Rational numbers. Implement an immutable data type Rational for rational
numbers that supports addition, subtraction, multiplication, and division.

public class Rational

Rational(int numerator. int denominator)

Rational plus(Rational b)    sum of this number and b

Rational minus(Rational b)   difference of this number and b

Rational times(Rational b)   product of this number and b

Rational divides(Rational b) quotient of this number and b

boolean equals(Rational b)   is this number equal to that ?

String toString()            string representation

You do not have to worry about testing for overflow, but use as instance variables two long values that represent the numerator and denominator to limit the possibility of overflow. Use Euclid’s algorithm discussed earlier to ensure that the numerator and denominator never have any common factors. Include a test client that exercises all of your methods.

Rational v2: Robust implementation of rational numbers. Use assertions to develop an implementation of Rational that is immune to overflow.

 

 

 

 

Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne, Addison-Wesley Professional, 2011, ISBN 0-321-57351-X