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.
Sections 6.2 - 6.4 (pp. 182-194) from the course textbook.
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.
!
”) operator.if
statements both with and without an else
clause.else if
clauses and an else
clause.overloaded
.The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.
if
statements and a single if
/else
statement.if
/else if
/else
statement.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.
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
).
How does the number of statements/blocks differ between if, if/else, and if/“else if”/else conditional statements?
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.
Repeat the last question, but have it print with either its red or its green component is above 200.
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.