Title: Decisions
1Chapter 5
Decisions
2Figure 1 A Decision
3if statement
- if(condition) statement
- if (amount lt balance) balance balance -
amount - statement blockif(amount lt balance) double
newBalance balance - amount balance
newBalance
4Figure 2 Alternative Conditions
5if/else statement
- if(condition) statementelse statement
- if (amount lt balance) balance balance -
amountelse balance balance -
OVERDRAFT_PENALTY
6Relational operators
- lt gt like in math
- lt gt correspond to ³
- ! correspond to ¹
- is not the same as if (x 5) . . .x 5
7Comparing floating-point numbers
- Roundoff errorsdouble r Math.sqrt(2)r r is
2.0000000000000004, not 2r r 2 is false - Test if x and y are close enoughx - y e, e
a small value (e.g. 10-14.) - Not good enough for very small or large values.
Use x - y e max(x, y)
8String comparison
- Don't use for strings!if (input "Y") //
WRONG!!! - Use equals methodif (input.equals("Y"))
- tests identity, equals tests equal contents
- Case insensitive test ("Y" or "y")if
(input.equalsIgnoreCase ("Y"))
9Comparing Objects
- tests for identity, equals for identical
content - Rectangle a new Rectangle(5, 10, 20,
30)Rectangle b new Rectangle(5, 10, 20,
30) - a ! b, but a.equals(b)
- Caveat equals must be defined for the class (see
chapter 9)
10Figure 4 Comparing Objects
11Lexicographic Comparison
- s.compareTo(t) lt 0 means s comes before t in the
dictionary - "car"comes before "cargo" comes before
x"cathode". - All uppercase letters come before lowercase
"Hello" comes before "car"
12Figure 3 Lexicographic Comparison
13Multiple alternatives
- if (condition1) statement1else if
(condition2) statement2else if (condition3)
statement3else statement4 - The first matching condition is executed.Order
matters.
14Program Richter.java public class Richter
public static void main(String args)
ConsoleReader console new ConsoleReader(System.i
n) System.out.println ("Enter a
magnitude on the Richter scale") double
magnitude console.readDouble()
Earthquake quake new Earthquake(magnitude)
System.out.println(quake.getDescription())
class Earthquake public Earthquake(double
magnitude) richter magnitude
15 public String getDescription() String
r if (richter gt 8.0) r "Most
structures fall" else if (richter gt 7.0)
r "Many buildings destroyed"
else if (richter gt 6.0) r "Many
buildings considerably damaged, some collapse"
else if (richter gt 4.5) r "Damage
to poorly constructed buildings" else if
(richter gt 3.5) r "Felt by many
people, no destruction" else if (richter
gt 0) r "Generally not felt by
people" else r "Negative
numbers are not valid" return r
private double richter
16Nested branches
- if (condition1) if (condition1a)
statement1a else statement1b
17Figure 7 Income Tax Computation
18Program Tax.java public class Tax public
static void main(String args)
ConsoleReader console new ConsoleReader(System.i
n) System.out.println("Please enter your
income") double income
console.readDouble() System.out.println("P
lease enter S for single, " "M for
married") String status
console.readLine() TaxReturn aTaxReturn
new TaxReturn(income, status)
System.out.println("The tax is "
aTaxReturn.getTax())
19class TaxReturn public TaxReturn(double
anIncome, String aStatus) income
anIncome status aStatus
public double getTax() double tax 0
final double RATE1 0.15 final double
RATE2 0.28 final double RATE3 0.31
final double SINGLE_CUTOFF1 21450
final double SINGLE_CUTOFF2 51900
20final double SINGLE_BASE2 3217.50 final double
SINGLE_BASE3 11743.50 final double
MARRIED_CUTOFF1 35800 final double
MARRIED_CUTOFF2 86500 final double
MARRIED_BASE2 5370 final double MARRIED_BASE3
19566 if (status.equalsIgnoreCase("S")) if
(income lt SINGLE_CUTOFF1) tax RATE1
income else if (income lt SINGLE_CUTOFF2)
tax SINGLE_BASE2 RATE2 (income -
SINGLE_CUTOFF1) else tax
SINGLE_BASE3 RATE3 (income -
SINGLE_CUTOFF2)
21 else if (income lt
MARRIED_CUTOFF1) tax RATE1
income else if (income lt
MARRIED_CUTOFF2) tax MARRIED_BASE2
RATE2 (income -
MARRIED_CUTOFF1) else tax
MARRIED_BASE3 RATE3 (income -
MARRIED_CUTOFF2) return tax
private double income private String
status
22The boolean type
- George Boole (1815-1864) pioneer in the study of
logic - value of expression x lt 10 is true or false.
- boolean type one of these 2 truth values
- equals method has return type xboolean
23Boolean operators
- and or! not
- if (tday bday tmonth bmonth)...
- if (tmonth 4 tmonth 6 tmonth 9
tmonth 11)... - if (tmonth gt bmonth (tmonth bmonth tday
gt bday))...
24Figure 8 Flowcharts for and Combinations
25Truth tables
- A B ABtrue true truetrue false
falsefalse any false - A B ABtrue any truefalse true
truefalse false false - A !Atrue falsefalse true
26De Morgan's Law
- !(A B)x is the same as !A !B
- !(A B)x is the same as !A !B
- if (!(country.equals("USA") !state.equals("AK)
!state.equals("HI")))... - if (!country.equals("USA") !!state.equals("AK)
!!state.equals("HI"))...
27Boolean Variables
- boolean shipByAir falseif (!country.equals("US
A")) shipByAir trueelse if
(state.equals("AK") state.equals("HI"))
shipByAir trueif (shipByAir) ... else ... - Boolean variables are sometimes called flags
28Boolean do's and don'ts
- don't if (shipByAir true)...if (shipByAir
! false)... - doif (shipByAir)...
- don'tif (balance lt 0) return true else return
false - doreturn balance lt 0
29Useful tips
- Brace layout
- Indentation and tabs
- Copy and paste
- Prepare test cases
- Make a schedule