Overview

In talking about loops, we discussed the role of the “condition”. Conditions were expressions that evaluated to a boolean (i.e. true/false) value, e.g. “x < 100”. Conditions aren’t limited to loops though: in this reading we’ll explore how they are used by if statements.

if statements use boolean conditions to determine whether some Java code will be executed or not. Learning about this type of conditional execution will open up a new world of possibilities for us, both in terms of working with pictures and in solving many types of problems.

Lots of fun awaits!

Required Reading

Section 6.1 (pp. 173-181) 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. Write a Java statement that prints out a message only when a specific condition is met.
  2. Apply the algorithm used by the colorDistance method to quantify the similarity between two colors.

ADVANCED Learning Objectives

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

  1. Write, read, and trace code that either loops over a subset of pixels, or loops over all pixels and controls changes to pixels with an if statement.
  2. Modify a method to make it more “flexible” by replacing constants with parameters.

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. Complete the following Pixel method so that it prints “Woah, that’s seriously red!” if the red component of a calling object is greater than 200.

    public void printIfVeryRed()
    {
        // Fill in the code that goes here.
        // Remember that this is a Pixel method so "this" refers to a Pixel
        // object.
    }
  2. What is the color distance between the color with (R,G,B) = (100, 20, 50) and one whose (R,G,B) = (110, 20, 30)?