CSCI213 Spring2013 Lectures Decisions
CSCI213 Spring2013 Lectures Decisions
Decisions
Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how to compare integers, floating-point numbers, strings, and objects To recognize the correct ordering of decisions in multiple branches To program conditions using Boolean operators and variables To understand the importance of test coverage
The if Statement The if statement lets a program carry out different actions depending on a condition
if (amount <= balance) balance = balance amount;
2013-02-17
2013-02-17
Self Check 4.1 Why did we use the condition amount <= balance and not amount < balance in the example for the if/else statement? Answer: If the withdrawal amount equals the balance, the result should be a zero balance and no penalty.
and how do you fix it? Answer: Only the first assignment statement is part of the if statement. Use braces to group both assignment statements into a block statement.
2013-02-17
It prints:
sqrt(2)squared minus 2 is not 0 but 4.440892098500626E-16
Comparing Floating-Point Numbers To avoid roundoff errors, dont use == to compare floating-point numbers To compare floating-point numbers test whether they are close enough: |x - y|
final double EPSILON = 1E-14; if (Math.abs(x - y) <= EPSILON) // x is approximately equal to y
2013-02-17
Comparing Strings To test whether two strings are equal to each other, use equals method:
if (string1.equals(string2)) . . .
Comparing Strings string1.compareTo(string2) < 0 means: string1 comes before string2 in the dictionary string1.compareTo(string2) > 0 means: string1 comes after string2 string1.compareTo(string2) == 0 means: string1 equals string2 "car" comes before "cargo" All uppercase letters come before lowercase: "Hello" comes before "car"
Lexicographic Comparison
2013-02-17
Comparing Objects == tests for identity, equals for identical content Rectangle box1 = new Rectangle(5, 10, 20, 30);
Rectangle box2 = box1; Rectangle box3 = new Rectangle(5, 10, 20, 30);
box1 != box3, but box1.equals(box3) box1 == box2 Caveat: equals must be defined for the class
Use ==, not equals, to test for null null is not the same as the empty string ""
2013-02-17
Multiple Alternatives: Sequences of Comparisons if (condition1) statement1; else if (condition2) statement2; ... else statement4; The first matching condition is executed Order matters:
if (richter >= 0) // always passes r = "Generally not felt by people"; else if (richter >= 3.5) // not tested r = "Felt by many people, no destruction"; ...
2013-02-17
Multiple Alternatives: Nested Branches Branch inside another branch: if (condition1) { if (condition1a) statement1a; else statement1b; } else statement2;
Self Check 4.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? Answer: Yes, if you also reverse the comparisons:
if (richter < 3.5) r = "Generally not felt by people"; else if (richter < 4.5) r = "Felt by many people, no destruction"; else if (richter < 6.0) r = "Damage to poorly constructed buildings"; ...
George Boole (1815-1864): pioneer in the study of logic value of expression amount < 1000 is true or false boolean type: one of these 2 truth values
2013-02-17
Using Boolean Expressions: Predicate Method A predicate method returns a boolean value:
public boolean isOverdrawn() { return balance < 0; }
Use in conditions:
if (harrysChecking.isOverdrawn())
Using Boolean Expressions: Predicate Method if (Character.isUpperCase(ch)) ... Useful predicate methods in Scanner class: hasNextInt() and hasNextDouble():
if (in.hasNextInt()) n = in.nextInt();
2013-02-17
Truth Tables
A
true true false
B
true false Any
A && B
true false false
A
true false false
B
Any true false
A
true false
!A
false true
Use in conditions:
if (married) ... else ... if (!married) ...
10
2013-02-17
Self Check 4.8 Rewrite the following expression, avoiding the comparison with false: if (character.isDigit(ch) == false) ... Answer: if (!Character.isDigit(ch)) ...
Code Coverage Black-box testing: Test functionality without consideration of internal structure of implementation White-box testing: Take internal structure into account when designing tests Test coverage: Measure of how many parts of a program have been tested Make sure that each part of your program is exercised at least once by one test case E.g., make sure to execute each branch in at least one test case
Code Coverage Include boundary test cases: Legal values that lie at the boundary of the set of acceptable inputs Tip: Write first test cases before program is written completely gives insight into what program should do
11
2013-02-17
Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how to compare integers, floating-point numbers, strings, and objects To recognize the correct ordering of decisions in multiple branches To program conditions using Boolean operators and variables To understand the importance of test coverage
12