Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.

Similar presentations


Presentation on theme: "Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham."— Presentation transcript:

1 Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

2 Week 7 Topics 7.1.1 The if statement 7.1.2 Comparing Values 7.1.3 Multiple Alternatives 7.1.4 Using Boolean Expressions

3 7.1.1 The if statement if (condition) statement; Condition returns a boolean true or false If the condition is true, the statement or statements in the body of the if statement will be executed if (amount <= balance) balance = balance – amount;

4 7.1.1 The if statement cont. To implement a choice between alternatives, use the if/else structure if (amount <= balance) balance = balance – amount; else balance = balance – OVERDRAFT_PENALTY; Use brackets if there are multiple statements in the body of the if statement if (amount <= balance) { double newBalance = balance – amount; balance = newBalance; }

5 7.1.2 Comparing Values Relational Operators OperatorDescription >Greater than >=Greater than or equal <Less than <=Less than or equal ==Equal !=Not equal

6 7.1.2 Comparing Values Cont. You must be careful when comparing floating-point numbers due to rounding errors. We test to see if close enough. final double EPSILON = 1E-14; if (Math.abs(x – y) <= EPSILON) // x is approximately equal to y assertEquals(x, y, EPSILON); // JUnit method

7 7.1.2 Comparing Values Cont. To test whether two strings are equal, do NOT use ==, use the equals method. if (s1.equals(s2)) … if (s1.equalsIgnoreCase(s2)) … Use the compareTo method to compare strings in dictionary order if (s1.compareTo(s2) < 0 // s1 is before s2 if (s1.compareTo(s2) > 0 // s1 is after s2 if (s1.compareTo(s2) == 0 // s1 equals s2

8 7.1.2 Comparing Values Cont. When comparing two object references with the == operator, you test whether the references refer to the same object Rectangle r1 = new Rectangle(5,7,9,9); Rectangle r2 = r1; Rectangle r3 = new Rectangle(5,7,9,9); r1 == r2 is true r1 == r3 is false

9 7.1.2 Comparing Values Cont. To compare whether the contents of two rectangles are the same, use the equals method r1.equals(r3) is true The Rectangle class like the String class has an equals method. If you write your own class, you will have to implement your own equals method (beyond the scope of this course).

10 7.1.2 Comparing Values Cont. To test if an object reference has been initialized, you can use null if (middleName == null) System.out.println("No middle name"); else System.out.println(middleName); Some feel it is a good ideal to set an object variable to null if it will not be instantiated until later String middleName = null;

11 7.1.3 Multiple Alternatives if (age >= 100) s = “What is your secret!”; else if (age >= 65) s = “Enjoying your golden years?”; else if (age >= 45) s = “Feeling some aches and pains?”; else if (age >= 30) s = “Oh to be in your prime again”; else s = “You young whipper snapper!”;

12 7.1.3 Multiple Alternatives Cont. Note that if the order of conditions for the example in the previous slide were reversed, the logic would not work as intended. Watch out for this trap when coding multiple conditions! if (age >= 0) // This will not work correctly! s = “You young whipper snapper!”; else if (age >= 30) s = “Oh to be in your prime again”; … You will never get past the first condition

13 7.1.3 Multiple Alternatives Cont. If you have the special case where you are just comparing a single int or char value, the switch statement can be used: switch (digit) // digit is an int for this example { case 1: System.out.println(“one”); break; case 2: System.out.println(“two”); break; case 3: System.out.println(“three”); break; case 4: System.out.println(“four”); break; default: System.out.println(“N/A”); break; }

14 7.1.3 Multiple Alternatives Cont. Conditional statements can have nested branches: if (vehicle == CAR) // int constant { if (cost <= BUDGET) // double constant // do something else if (cost <= MID_PRICED) // do something else … } else if (vehicle == TRUCK) {

15 7.1.4 Using Boolean Expressions An expression such as amount < 1000 has a value, either true or false System.out.println(amount < 1000); The above prints true if amount is less than 1000, otherwise prints false A predicate method returns a boolean value if (harrysChecking.isOverdrawn()) …

16 7.1.4 Using Boolean Expressions Cont. if (Character.isUpperCase(ch)) … if (in.hasNext()) n = in.nextInt(); The isOverdrawn() predicate method from the previous slide might look like this: public boolean isOverdrawn() { return this.balance < 0; }

17 7.1.4 Using Boolean Expressions Cont. How do I test if amount is between 0 and 1000 inclusive? Use the boolean AND operator (all conditions must be true): if (0 <= amount && amount <= 1000) … If just one condition must be true, use the boolean OR operator: if (in.equals(“S”) || in.equals(“L”)) … To invert a condition, use the boolean NOT operator: if (!in.equals(“S”)) …

18 7.1.4 Using Boolean Expressions Cont. Logical AND operator ABA && B true false truefalse

19 7.1.4 Using Boolean Expressions Cont. Logical OR operator ABA || B true falsetrue falsetrue false

20 7.1.4 Using Boolean Expressions Cont. Logical NOT operator A!A truefalse true

21 Reference: Big Java 2 nd Edition by Cay Horstmann 7.1.1 The if statement (section 6.1 in Big Java) 7.1.2 Comparing Values (section 6.2 in Big Java) 7.1.3 Multiple Alternatives (section 6.3 in Big Java) 7.1.4 Using Boolean Expressions (section 6.4 in Big Java)


Download ppt "Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham."

Similar presentations


Ads by Google