Professional Documents
Culture Documents
Java Programming 8th Edition Joyce Farrell Solutions Manual 1
Java Programming 8th Edition Joyce Farrell Solutions Manual 1
Java Programming 8th Edition Joyce Farrell Solutions Manual 1
Chapter 5
Making Decisions
A Guide to this Instructor’s Manual:
We have designed this Instructor’s Manual to supplement and enhance your teaching
experience through classroom activities and a cohesive chapter summary.
This document is organized chronologically, using the same headings that you see in the
textbook. Under the headings you will find: lecture notes that summarize the section, Teaching
Tips, Class Discussion Topics, and Additional Projects and Resources. Pay special attention to
teaching tips and activities geared towards quizzing your students and enhancing their critical
thinking skills.
In addition to this Instructor’s Manual, our Instructor’s Resources also contain PowerPoint
Presentations, Test Banks, and other supplements to aid in your teaching experience.
At a Glance
Objectives
Teaching Tips
Quick Quizzes
Additional Projects
Additional Resources
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 5-2
Key Terms
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 5-3
Lecture Notes
Overview
Chapter 5 introduces decision structures using the if, if…else, and switch
statements. Students will learn to execute program statements based on the result of a
Boolean expression. They will also learn to use the logical operators AND, OR, and
NOT, as well as the conditional operator. This chapter presents a number of tips on
avoiding common programming errors when making decisions.
Objectives
• Plan decision-making logic
• Make decisions with the if and if…else statements
• Use multiple statements in if and if…else clauses
• Nest if and if…else statements
• Use AND and OR operators
• Make accurate and efficient decisions
• Use the switch statement
• Use the conditional and NOT operators
• Assess operator precedence
• Add decisions and constructors to instance methods
Teaching Tips
Planning Decision-Making Logic
1. Define pseudocode and flowcharts. Suggest uses for each tool.
2. Explain the difference between a sequence structure and a decision structure. Note
that Figure 5-1 illustrates a sequence structure, while Figure 5-2 shows a decision
structure.
3. Remind students that a Boolean value can have one of two values: true or false.
Every computer decision results in a Boolean value.
Teaching Note that a decision in a program can only have one of two possible outcomes:
Tip true or false.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 5-4
1. Introduce the if statement. Figure 5-3 shows the decision structure represented by a
single if statement.
2. Remind students about the relational operators ( ==, <, >, <=, >=, and != ) from Chapter
2.
Teaching Discuss how if statements make video games fun. Draw your discussion from
Tip popular games you know.
1. Review Figure 5-4, which shows a common mistake of ending the if statement with a
semicolon. Note that this will cause the empty statement containing only the
semicolon to be executed regardless of whether the condition is true or false.
During class, write code that illustrates this pitfall.
1. Discuss the problems with using a single equal sign instead of a double equal sign when
trying to make an equality comparison. Write code that shows this pitfall in class.
1. Remind students that the relational operators can only be used to compare primitive
values and variables, not objects. During class, write code that shows this pitfall.
Teaching To emphasize the last pitfall, it can be helpful to teach students about comparing
Tip strings here rather than waiting until Chapter 7.
1. Introduce the if…else statement. Note that while an if statement is called a single-
alternative if, an if…else structure is called a dual-alternative if. The flowchart
for this logic is shown in Figure 5-5.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 5-5
2. During class, write code that uses an if…else statement. A good example is a
pass/fail scenario.
You Do It
1. Students should follow the steps in the book on pages 253–254 to create a Java
application that uses an if…else statement.
1. Using Figure 5-7, inform students that an if statement can be followed by more than
one statement that will execute when the condition is true as long as the statements are
enclosed in a block.
2. Point out that a programmer must enclose the statements within curly braces.
Otherwise, only the first statement will be executed when the condition is true. Execute
code similar to Figure 5-8 during class to demonstrate this point.
3. Review Figure 5-9, which illustrates a fully functional if…else statement. Execute
similar code in your class environment.
4. Review scope issues by examining the code on page 258. Explain why the sum variable
is out of scope and how to fix the problem.
You Do It
1. Students should follow the steps in the book on pages 259–260 to create a Java
application that uses multiple statements in if and else clauses.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 5-6
Quick Quiz 1
1. Programmers often use ____, a tool that helps them plan a program’s logic by writing
plain English statements.
Answer: pseudocode
2. A(n) ____ structure involves choosing between alternative courses of action based on
some value within a program.
Answer: decision
4. True or False: Just as you can block statements to depend on an if, you can also block
statements to depend on an else.
Answer: True
5. True or False: The following code illustrates a valid condition to check whether your
age is at least 21.
if(age > 21)
Answer: False
2. Use Figure 5-13 to illustrate the importance of correctly matching the else statements.
Remind students that the first if statement written is the last to match to an else.
You Do It
1. Students should follow the steps in the book on page 263 to add a nested if statement
to the existing AssignVolunteer2 project created in the previous You Do It activity.
2. Briefly define AND and OR operators, providing a simple example. For instance,
compare and contrast using AND and OR to refine a search for the perfect mate.
Avoid using business examples to illustrate the logical operators. Students may
not understand how a business operates, and they can get bogged down in
Teaching
superfluous details. Instead, draw from their knowledge base to illustrate AND
Tip
and OR. Use sports, video games, dating, or anything else students are likely to
understand.
1. Introduce the logical AND operator ( && ). Explain that this operator is used to
combine two Boolean values. If both values are true, the combined value is also
true; otherwise, it is false. An example is shown in Figure 5-15.
Teaching Students may find it helpful to draw a chart of the outcome of using the logical
Tip AND operator for all different possible values of the Boolean expressions.
2. Using the code in Figure 5-15, explain that the logical AND operator can often be used
in place of nested if statements.
3. Discuss the Don’t do it callout on page 264. Contrast this with the valid example shown
in the code block preceding the callout.
The OR operator
1. Introduce the logical OR operator ( || ). This operator combines two Boolean values
and evaluates to true when either or both of the values is true.
2. Using the code in Figure 5-16, explain how to replace a nested if statement with a
logical OR.
Short-Circuit Evaluation
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 5-8
2. In an if statement containing an && operator, Java will not evaluate the second part of
the && test when the first part is false.
3. In an if statement containing an || operator, Java will not evaluate the second part of
the || test when the first part is true.
You Do It
1. Students should follow the steps in the book on pages 267–269 to create a Java
application that demonstrates how short-circuiting works with the && operator.
1. Define a range check. Using the program examples in Figures 5-19 through 5-21,
explain how to code an accurate range check.
2. During class, code a range check on a topic that will resonate with students, such as
assigning letter grades based on percentages.
Do not use the trailing else to pick up the last condition. Your nested
Teaching
if…else if structures should test all possible conditions, leaving the else
Tip
for an error clause.
1. Describe how Java processes nested ifs. Java will compare each if condition until it
either finds a match or has exhausted all possible conditions.
2. For more efficient if statements, you should place the more commonly used conditions
near the top of the nested if…else if structure.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 5-9
1. Using the examples on page 273, point out how logical errors can result from the
improper use of && and ||.
Teaching
Remind students to always carefully evaluate whether to use && or ||.
Tip
Quick Quiz 2
1. ____ if statements are particularly useful when two conditions must be met before
some action is taken.
Answer: Nested
3. A(n) ____ is a series of if statements that determine whether a value falls within a
specified range.
Answer: range check
4. True or False: Under some circumstances, the following println() statement will
execute.
if (payRate < 5.65 && payRate > 60)
System.out.println("Error in pay rate");
Answer: False
2. Switches are appropriate when you have a series of values to match. They do not work
well when matching ranges of values.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 5-10
3. Switches in general are a little more efficient to run than nested if…else if
structures.
4. Discuss the four keywords listed on page 275 that are used in a switch statement.
Note that Figure 5-24 contains the same logic as Figure 5-23 but uses a switch
statement rather than a series of if…else statements.
Teaching Point out that the switch statement cannot be used for non-discrete data such
Tip as float or double. It can be used for int, long, char, and String.
You Do It
1. Students should follow the steps in the book on pages 279–280 to create a Java
application that uses a switch statement.
2. Suggest that students avoid using the conditional operator. It is convenient but leads to
code that is harder to read.
Teaching
The conditional operator is the only ternary operator in the Java language.
Tip
1. Describe the use of the NOT operator ( ! ) to give the opposite value to a Boolean
expression. An example program using this operator is shown in Figure 5-30.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 5-11
2. Remind students that, just as with mathematical operator precedence, parentheses can
be used to clarify or change the order in which operators are evaluated. Review Figure
5-31 for an example.
3. If possible, write code during class that demonstrates common logical errors that require
parentheses to fix.
1. Discuss how to use if statements in set methods and constructors for data validation.
2. During your lecture, create a class that validates data in the set and constructor methods.
Add a method that utilizes a decision structure to calculate a value.
Teaching Data validation is extremely important. Spend a good portion of time having
Tip students verify data.
You Do It
1. Students should follow the steps in the book on pages 286–288 to create a Java
application that uses a switch statement.
Don’t Do It
1. Review the section, discussing each point in class.
Quick Quiz 3
1. In a switch statement, ____ is followed by one of the possible values for the test
expression and a colon.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 5-12
Answer: case
2. True or False: You are never required to use a switch structure; you can always
achieve the same results with nested if statements.
Answer: True
3. You use the ____, which is written as the exclamation point ( ! ), to negate the result of
any Boolean expression.
Answer: NOT operator
2. Under what circumstances might you choose to replace a nested if statement with a
logical AND or OR statement?
3. Provide examples in science, medicine, or any other topic where if statements are
required.
4. Have the class list their favorite video games. Let them determine where decisions are
made in the program while they play the game.
Additional Projects
1. Create a program that prompts the user to enter two values: a movie rating and his or
her age. Using a decision structure, determine whether the user would be allowed to see
the movie in a theater based on the rating and age entered. Finally, display the result of
this decision to the user.
2. In this chapter, you have been introduced to sequence and decision control structures.
There is one other control structure that is commonly used in programming: the
repetition structure. Using the Internet, research this structure and write a one-paragraph
description of it.
3. Create the card game Hi-Low. The winner of a hand is the player with the highest card.
Ties may be ignored.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 5-13
Additional Resources
1. Summary of Java operators:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html
Key Terms
Boolean values: true or false values. Every computer decision results in a Boolean
value.
Conditional operator: requires three expressions separated with a question mark and a
colon, and is used as an abbreviated version of the if…else structure.
Conditional OR operator: used between Boolean expressions to determine whether
either expression is true. The OR operator is written as two pipes ( || ).
Decision structure: a logical structure that involves choosing between alternative
courses of action based on some value within a program.
Dual-alternative if: a decision structure that takes one of two possible courses of
action.
else clause: the portion of the if…else statement that executes when the condition
is false.
Empty statement: contains only a semicolon.
Equivalency operator: written as two equal signs ( == ); compares values and returns
true if they are equal.
Flowchart: a tool that helps programmers plan a program’s logic by writing the steps in
diagram form as a series of shapes connected by arrows.
if clause: the portion of the if…else statement that executes when the condition is
true.
if statement: the simplest statement you can use to make a decision. You use it to
write a single-alternative decision.
if…else statement: provides the mechanism to perform one action when a Boolean
expression evaluates as true, and to perform a different action when a Boolean
expression evaluates as false.
Logical AND operator: used between Boolean expressions to determine whether both
are true. The AND operator is written as two ampersands ( && ).
Logical OR operator: used between Boolean expressions to determine if either is
true. The OR operator is written as two ampersands ( || ).
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 5-14
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.