Overview

Pictures are an excellent example of the abstraction process that is critical to computer science: a complex object is broken down into tiny pieces (Pixels) that are easy to work with.

In this reading we’ll look at how pictures are encoded, learn about the Pixel class, and learn how to manipulate thousands or millions of pixels with only a few lines of code. To accomplish these tasks we’ll delve into two major topics: arrays and loops. The former allows us to group together items of the same type using a single name, while the latter will allow us to repeat the same action over and over.

We’ve learned a fair amount of Java’s syntax and we’ll be learning even more for working with arrays and loops. This syntax will seem unnatural, which can be frustrating. However, as you gain more experience working with Java, the syntax will become second nature. One simple way to speed up the process of gaining experience is to have DrJava open and follow along with the examples in the book.

Required Reading

Sections 4.1-4.3.1 (pp. 75-93) 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 how computers represent pictures.
  2. Describe the RGB color model, identifying white, black, and gray.
  3. Declare an array of any type, and initialize the contents of the array if it is an array of primitives (e.g. double).
  4. Given a Picture object, write a Java expression that gets a specific pixel in that picture or all of the pixels in that picture.
  5. Create a new Color object, and be able to change it to be lighter or darker.
  6. Given a Pixel object, write a Java expression that either gets an attribute of that pixel or changes its color.
  7. Describe role of each component of a “For-Each” loop, including the body.

ADVANCED Learning Objectives

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

  1. Explain how bits (0s and 1s) in a computer’s memory can be interpreted in a variety of ways (depending on issues of type and representation).
  2. Read and understand a For-Each loop for modifying a Picture, including being able to draw a memory model of this code.
  3. Describe the single array representation of pixels in the Picture class.

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. Textbook problem: 4.1 (excluding “Flowchart”, “Infinite Loop,” and “Variable Scope”).

  2. Write a Java statement that creates a variable named myQuizGrades, which refers to a new array of int values that represents 3 quiz grades. You can use whatever values you want for these quiz grades.

  3. Assume that you have a Picture variable named mySorority. Write a Java expression that calculates the total number of pixels in the picture.

  4. Write a Java statement that creates a Pixel variable name upperRightPixel and makes it refer to the upper right Pixel in your mySorority picture.

  5. Write a Java statement that sets the color of your upper right pixel to a new Color object that is crimson. The (R, G, B) values for crimson are (220, 20, 60).

  6. Repeat the last exercise but use the setRed, setGreen, and setBlue methods to change the Pixel’s color instead of creating a Color object.

  7. The following Java method is missing a line of code to make it loop through all the pixels in the array, printing their current blue value. What is this missing line?

    public void printBlueVals()
    {
        Pixel[] thePixels = this.getPixels();
        int val = 0;
        // MISSING LINE OF CODE HERE
        {
            val = pixOb.getBlue();
            System.out.println(val);
        }
    }