Overview

In your previous reading, we learned how to interact with a 2D representation of a Picture using nested loops. Now that we can work with the 2D representation, you will read about some of the many applications that work on this representation. You’ll learn how to do things like copy regions of a picture from one place to another and how to mirror a picture.

Required Reading

Sections 5.1.2 - 5.2.1 (pp. 134-149) from the course textbook.

Learning Objectives

BASIC Learning Objectives

Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.

  1. Describe what it means to mirror a picture vertically and horizontally.
  2. Given a specific region of a picture to mirror, determine: the range of x values to be mirrored, the range of y values to be mirrored, and the mirror point.
  3. Employ the technique of drawing examples to explore various cases with 2D array manipulation.
  4. Read and trace the execution of Java code that modifies the same value across multiple iterations of a loop.
  5. Write a for loop header that initializes more than one variable in the initialization area and changes more than one variables in the change area.

ADVANCED Learning Objectives

The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.

  1. Read, trace, and write code that performs a transformation over a restricted set of pixels (like a “box” of pixels).
  2. Read, trace and write a method that uses more than one Picture object.

Pre-class Exercises

These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.

  1. Part A: Draw a 5x2 matrix of Pixels, writing the (x,y) coordinate inside of each pixel. It should look something like the following table, but with real numbers instead of a literal “(x,y)” for the coordinates.

    (x,y) (x,y) (x,y) (x,y) (x,y)
    (x,y) (x,y) (x,y) (x,y) (x,y)

    Part B: Draw another 5x2 matrix of pixels that will represent the pixels from Part A after that have been mirrored vertically. Inside of each pixel, put the (x,y) coordinate where that pixel was found in the original matrix of pixels.

    Part C: Repeat Part B, but this time the matrix should represent what happens after you mirror horizontally. Note it should be the mirror of the original (Part A) not the already mirrored picture (Part B).

  2. I want to vertically mirror a part of a Picture centered at (100,100). The region to mirror should be 20 pixels wide and 50 pixels high. Based on this, calculate the following quantities:

  3. Consider the following Java code.

    int val = 0;
    
    for (int i = 0; i < 10; i += 2) {
        val = val + i;
    }

    Part A: For both i and val, list the sequence of values that those variables will be as we trace though the code. Your answer should look something like the following (with different values, of course):

    i = 1, 2, 3, 4
    val = 10, 20, 30, 40

    Part B: Describe what this code does. (Remember: think “high level” for your description.)