teaching machines

CS1: Lecture 26 – Looping Patterns

November 1, 2019 by . Filed under cs1, fall 2019, lectures.

Dear students,

So far we’ve used loops to traverse 1D spaces: the number line, files, and a sequence of random events like coin flipping. Today we bust out into 2D spaces, traversing images and other Cartesian grids.

Nesting Loops

To transition to our next concept, let’s write a program to mimic the activity we see in this video:

If we consistently apply the ideas we’ve been talking about, we can write a program that produces this table of numbers with just five lines of code. The trick is to loop our loop.

We’ll use nested loops and get some practice with the BufferedImage class using these exercises:

TODO

Here’s your TODO list to complete before we meet again:

See you next class!

Sincerely,

P.S. It’s time for a haiku!

A ghost on its beat
For each avenue, each street
Ding dong, trick-or-treat

P.P.S. Here’s the code we wrote together in class…

Flipping.java

package lecture1101.cs145;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Flipping {
  public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new File("/Users/johnch/Desktop/uhoh.png"));

    for (int r = 0; r < image.getHeight() / 2; r++) {
      int rComplement = image.getHeight() - 1 - r;
      for (int c = 0; c < image.getWidth(); c++) {
        int thatPixel = image.getRGB(c, rComplement);
        int thisPixel = image.getRGB(c, r);
        image.setRGB(c, rComplement, thisPixel);
        image.setRGB(c, r, thatPixel);
      }
    }

    ImageIO.write(image, "png", new File("/Users/johnch/Desktop/uhohuhoh.png"));
  }
}

Video.java

package lecture1101.cs145;

public class Video {
  public static void main(String[] args) {
    for (int r = 1; r <= 8; r++) {
//      for (int c = r; c <= r * 8; c += r) {
//        System.out.printf("%3d", c);
//      }
      for (int c = 1; c <= 8; c++) {
        System.out.printf("%3d", c * r);
      }
      System.out.println();
    }

//    for (int i = 2; i <= 16; i += 2) {
//      System.out.printf("%3d", i);
//    }
//    System.out.println();
//
//
//    for (int i = 3; i <= 24; i += 3) {
//      System.out.printf("%3d", i);
//    }
//    System.out.println();
  }
}

Flip.java

package lecture1101.cs148;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Flip {
  public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new File("/Users/johnch/Desktop/uhoh.png"));

    for (int r = 0; r < image.getHeight() / 2; ++r) {
      for (int c = 0; c < image.getWidth(); ++c) {
        int rComplement = image.getHeight() - 1 - r;
        int thisPixel = image.getRGB(c, r);
        int thatPixel = image.getRGB(c, rComplement);
        image.setRGB(c, r, thatPixel);
        image.setRGB(c, rComplement, thisPixel);
      }
    }

    ImageIO.write(image, "png", new File("/Users/johnch/Desktop/uhohuhoh.png"));
  }
}

Grid.java

package lecture1101.cs148;

public class Grid {
  public static void main(String[] args) {
    for (int r = 1; r <= 8; r++) {
      for (int c = 1; c <= 8; c++) {
        System.out.printf("%3d", c * r);
      }
      System.out.println();
    }
  }
}

Recursion.java

package lecture1101.cs148;

public class Recursion {
  public static void main(String[] args) {
    System.out.println(has("Joe Mama", '!'));
  }

  public static boolean has(String haystack, char needle) {
    if (haystack.isEmpty()) {
      return false;
    } else if (haystack.charAt(0) == needle) {
      return true;
    } else {
      return has(haystack.substring(1), needle);
    }
  }
}