Big Java 4

Chapter 5 – Decisions

Chapter Goals

The if Statement

The if/else Statement

if (amount <= balance)
   balance = balance - amount;
else
   balance = balance - OVERDRAFT_PENALTY;

Statement Types

Syntax 5.1 The if Statement

Syntax 5.1 if Statement

Self Check 5.1

Why did we use the condition amount <= balance and not amount < balance in the example for the if/else statement?

Self Check 5.2

What is logically wrong with the statement
if (amount <= balance)
   newBalance = balance - amount; balance = newBalance;
and how do you fix it?

Comparing Values: Relational Operators

Comparing Floating-Point Numbers

Comparing Floating-Point Numbers

Comparing Strings

Comparing Strings

Lexicographic Comparison

Syntax 5.2 Comparisons

Syntax 5.2 Comparisons

Comparing Objects

Object Comparison

Testing for null

Relational Operator Examples

Relational Operator Examples

Self Check 5.3

What is the value of s.length() if s is
  1. the empty string ""?
  2. the string " " containing a space?
  3. null?

Self Check 5.4

Which of the following comparisons are syntactically incorrect? Which of them are syntactically correct, but logically questionable?
String a = "1";
String b = "one";
double x = 1;
double y = 3 * (1.0 / 3);
  1. a == "1"
  2. a == null
  3. a.equals("")
  4. a == b
  5. a == x
  6. x == y
  7. x - y == null
  8. x.equals(y)

Multiple Alternatives: Sequences of Comparisons

ch05/quake/Earthquake.java

Your browser does not support the <object> tag.

ch05/quake/EarthquakeRunner.java

Your browser does not support the <object> tag. Program Run:

Multiple Alternatives: Nested Branches

Tax Schedule

If your filing status is Single If your filing status is Married
Tax Bracket Percentage Tax Bracket Percentage
$0 . . . $32,000 10% $0 . . . $64,000 10%
Amount over $32,000 25% Amount over $64,000 25%

Nested Branches

ch05/tax/TaxReturn.java

Your browser does not support the <object> tag.

ch05/tax/TaxCalculator.java

Your browser does not support the <object> tag. Program Run:

Self Check 5.5

The if/else/else statement for the earthquake strength first tested for higher values, then descended to lower values. Can you reverse that order?

Self Check 5.6

Some people object to higher tax rates for higher incomes, claiming that you might end up with less money after taxes when you get a raise for working hard. What is the flaw in this argument?

Using Boolean Expressions: The boolean Type

Using Boolean Expressions: Predicate Method

Using Boolean Expressions: The Boolean Operators

&& and || Operators

Boolean Operators

Boolean Operators

Truth Tables

A B A && B
true true true
true false false
false Any false

A B A || B
true Any true
false true true
false false false

A ! A
true false
false true

Using Boolean Variables

Self Check 5.7

When does the statement
system.out.println (x > 0 || x < 0);
print false?

Self Check 5.8

Rewrite the following expression, avoiding the comparison with false:
if (Character.isDigit(ch) == false) . . .

Code Coverage

Self Check 5.9

How many test cases do you need to cover all branches of the getDescription method of the Earthquake class?

Self Check 5.10

Give a boundary test case for the EarthquakeRunner program. What output do you expect?