1 / 25

Introduction to If Statements and Comparing Values in Object-Oriented Programming

Learn about the if statement, comparing values, multiple alternatives, and using boolean expressions in object-oriented programming. Understand how to implement choices, compare values accurately, and use boolean operators effectively.

Download Presentation

Introduction to If Statements and Comparing Values in Object-Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 7.1.5 Code Coverage

  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 <= this.balance) this.balance -= amount;

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

  5. 7.1.2 Comparing ValuesRelational Operators

  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

  19. 7.1.4 Using Boolean Expressions Cont.Logical OR operator

  20. 7.1.4 Using Boolean Expressions Cont.Logical NOT operator

  21. 7.1.5 Code Coverage • Unit tests are used to test classes in isolation of one another • A Unit Testing framework typically allows the programmer to compare a predetermined expected result against the result produced by the program • Unit tests are white box tests in that they exploit knowledge of the implementation

  22. 7.1.5 Code Coverage Cont. • Black-box testing describes a testing method that does not take the structure of the implementation into account, as typical of customer acceptance testing • Test coverage is a measure of taking into account how many parts of a program have been tested • For example, you should test every branch of all if/else code

  23. 7.1.5 Code Coverage Cont. • Logging is a way of tracing the execution of a program through all the method calls • More than a useful debugging tool, logging can be a business requirement in many situations • Consider that all bank transactions should be logged for accounting purposes

  24. 7.1.5 Code Coverage Cont. • However, the most common use for logging is debugging • A logging API allows a programmer to log messages at various levels of severity, capture the log to a file, and even turn logging on and off entirely

  25. Reference: Big Java 4th Edition by Cay Horstmann 7.1.1 The if statement (section 5.1 in Big Java) 7.1.2 Comparing Values (section 5.2 in Big Java) 7.1.3 Multiple Alternatives (section 5.3 in Big Java) 7.1.4 Using Boolean Expressions (section 5.4 in Big Java) 7.1.5 Code Coverage (section 5.5 in Big Java)

More Related