- Sections 5.1 - 5.2

- The
`if`statement lets a program carry out different actions depending on a conditionif (amount <= balance) balance = balance - amount;

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

- Simple statement:
balance = balance - amount;

- Compound statement:
if (balance >= amount) balance = balance - amount;

Also loop statements – Chapter 6

- Block statement:
{ double newBalance = balance - amount; balance = newBalance; }

- Relational operators compare values

Java Math Notation Description `>`> Greater than `>=`≥ Greater than or equal `<`< Less than `<=`≤ Less than or equal `==`= Equal `!=`≠ Not equal - The
`==`denotes equality testing:a = 5; // Assign 5 to a if (a == 5) . . . // Test whether a equals 5

- Relational operators have lower precedence than arithmetic operators:
amount + fee <= balance

Consider the code fragment that is intended to fix the withdraw method so that the account balance can't be negative.

public void withdraw(double amount) { if (amount <= balance) // Make sure the account isn't overdrawn newBalance = balance - amount; balance = newBalance; }

What is wrong with this code?

- It should be <, not <=
- It should be >=, not <=
- It should be >, not <=
- Something else

- Consider this code:
double r = Math.sqrt(2); double d = r * r -2; if (d == 0) System.out.println("sqrt(2)squared minus 2 is 0"); else System.out.println("sqrt(2)squared minus 2 is not 0 but " + d);

- It prints:

`sqrt(2)squared minus 2 is not 0 but 4.440892098500626E-16`

- To avoid roundoff errors, don't 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

- ε is a small number such as 10
^{-14}

- To test whether two strings are equal to each other, use
`equals`method:`if (string1.equals(string2)) . . .`

- Don't use
`==`for strings!if (string1 == string2) // Not useful

`==`tests identity,`equals`tests equal contents- Case insensitive test:
if (string1.equalsIgnoreCase(string2))

`string1.compareTo(string2) < 0`means:

`string1`comes before`string2`in the dictionary`string1.compareTo(string2) > 0`means:

`string1`comes after`string2`in the dictionary`string1.compareTo(string2) == 0`means:

`string1`and`string2`are equal`"car"`comes before`"cargo"`- All uppercase letters come before lowercase:

`"Hello"`comes before`"car"`

`==`tests for identity,`equals`for identical contentRectangle 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

`null`reference refers to no object:String middleInitial = null; // Not set if ( . . . ) middleInitial = middleName.substring(0, 1);

- Can be used in tests:

if (middleInitial == null) System.out.println(firstName + " " + lastName); else System.out.println(firstName + " " + middleInitial + ". " + lastName);

- Use
`==`, not`equals`, to test for`null` `null`is not the same as the empty string`""`

Consider the following definitions.

String a = "1"; String b = "one"; double x = 1; double y = 3 * (1.0 / 3);

How many of the following comparisons are syntactically incorrect? How many of them are syntactically correct, but logically questionable?

`a == "1"``a == null``a.equals("")``a == b``a == x``x == y``x - y == null``x.equals(y)`

- Two incorrect, two questionable
- Two incorrect, three questionable
- Three incorrect, two questionable
- Three incorrect, three questionable