Overview

In the previous reading, we learned about conditionals in the form of if statements. These statements allow us to execute a block of instructions only when a certain condition is true.

In this reading we’ll generalize if statements to include multiple options. For example, we might do one block when a condition is true, and another block of the condition is false. We can go even further, specifying multiple conditions, and executing one of several blocks of code based on which condition is true.

When we start dealing with conditionals with many different options, it will be beneficial to draw flowcharts to indicate which block should be executed and under which condition(s). Pay special attention to the flowcharts in your reading.

Required Reading

Sections 6.2 - 6.4 (pp. 182-194) 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. Evaluate an expression that uses the negation (i.e. “!”) operator.
  2. Write a Java statement that prints out a message when a specific condition is met but otherwise prints out a different message.
  3. Compare and contrast the flowcharts of if statements both with and without an else clause.
  4. Draw a flowchart to represent a Java conditional statement with one or more else if clauses and an else clause.
  5. Describe the algorithms used to generate a sepia-toned picture and to posterize a picture.
  6. Describe how the edge detection algorithm finds “edges” in a picture.
  7. Write a Java expression that evaluates to true only when two conditions are met.
  8. Write a Java expression that evaluates to true when one or both of two conditions are met.
  9. Given two method headers, determine if those methods may be overloaded.

ADVANCED Learning Objectives

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

  1. Compare and contrast the efficiency of two if statements and a single if/else statement.
  2. 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/else if/else statement.

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. Write a Pixel method named printAboutBlue. This method should print out a message “I’m a blue pixel :(” if it’s blue component is more than 150, otherwise it prints out “I’m not so blue!”.

    Note that this is a Pixel method so you will work with only one Pixel, the calling object Pixel (i.e. this).

  2. How does the number of statements/blocks differ between if, if/else, and if/“else if”/else conditional statements?

  3. Write a Pixel method named printRedGreen, which prints out “I’ve got plenty of red and green” when both its red and green components are above 200.

  4. Repeat the last question, but have it print with either its red or its green component is above 200.

  5. Could you have both versions of your printRedGreen function (from the previous two questions) in your Pixel.java file at the same time? Explain why or why not.