week 7 introduction to computer science and object-oriented programming comp 111 george basham

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

Upload: oliver-wright

Post on 18-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

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

Week 7

Introduction to Computer Science and Object-Oriented Programming

COMP 111

George Basham

Page 2: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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

Page 3: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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;

Page 4: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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; }

Page 5: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

7.1.2 Comparing Values Relational Operators

Operator Description

> Greater than

>= Greater than or equal

< Less than

<= Less than or equal

== Equal

!= Not equal

Page 6: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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

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

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

Page 8: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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

Page 9: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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).

Page 10: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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;

Page 11: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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!”;

Page 12: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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

Page 13: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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;}

Page 14: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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){

Page 15: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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()) …

Page 16: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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;

}

Page 17: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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”)) …

Page 18: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

7.1.4 Using Boolean Expressions Cont.

Logical AND operator

A B A && B

true true true

true false false

false true false

false false false

Page 19: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

7.1.4 Using Boolean Expressions Cont.

Logical OR operator

A B A || B

true true true

true false true

false true true

false false false

Page 20: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

7.1.4 Using Boolean Expressions Cont.

Logical NOT operator

A !A

true false

false true

Page 21: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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

Page 22: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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

Page 23: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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

Page 24: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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

Page 25: Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

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)