cs 200 - programming i: branches · 2017-09-30 · cs200-programmingi:branches marcrenault...

73
CS 200 - Programming I: Branches Marc Renault Department of Computer Sciences University of Wisconsin – Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624

Upload: others

Post on 21-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

CS 200 - Programming I: Branches

Marc Renault

Department of Computer SciencesUniversity of Wisconsin – Madison

Fall 2017TopHat Sec 3 (PM) Join Code: 719946TopHat Sec 4 (AM) Join Code: 891624

Page 2: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Boolean Statements

Page 3: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Booleans

Primitiveboolean b;

Values: true or falseWrapper class: Boolean

1/29

Page 4: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Boolean Expressions

An expression that evaluates to a boolean. I.e. true or false.

2/29

Page 5: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Boolean Expressions

An expression that evaluates to a boolean. I.e. true or false.

Comparison OperatorsBinary (expr1 oper expr2 ):

== : Equal to!= : Not equal to> : Greater than< : Less than>= : Greater than or equal<= : Less than or equal

2/29

Page 6: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Boolean Expressions

An expression that evaluates to a boolean. I.e. true or false.

Logical OperatorsUnary (oper expr ):

! : Logical NOTBinary (expr1 oper expr2 ):

&& : Logical AND|| : Logical OR

Don’t confuse with the bitwise operators: &, |, and ˆ.

2/29

Page 7: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Operator Precedence

3/29

Page 8: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0

0 0 0 1

0 1

0 1 1 1

1 0

0 1 1 0

1 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 9: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0

0 0 1

0 1

0 1 1 1

1 0

0 1 1 0

1 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 10: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0

0 1

0 1

0 1 1 1

1 0

0 1 1 0

1 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 11: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0

1

0 1

0 1 1 1

1 0

0 1 1 0

1 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 12: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1

0 1 1 1

1 0

0 1 1 0

1 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 13: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0

1 1 1

1 0

0 1 1 0

1 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 14: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1

1 1

1 0

0 1 1 0

1 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 15: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1

1

1 0

0 1 1 0

1 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 16: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0

0 1 1 0

1 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 17: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0

1 1 0

1 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 18: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1

1 0

1 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 19: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1 1

0

1 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 20: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1

1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 21: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1 1

1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 22: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1 1 1

0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 23: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1 1 1 0

0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 24: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Truth Tables

a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1 1 1 0 0

Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)

operand(s) is true.¬ NOT (!) – Flips the truth value.

4/29

Page 25: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

TopHat Question 1

If A is 1 and B is 1, what is !A || B ?a. 1b. 0

5/29

Page 26: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Bitwise OperationsPerforms the logical operations on each of the correspondingbits of the operands.

E.g. 7 & 5 = 5Consider the bits:

111& 101-----

101

Bitwise OperatorsBinary (expr1 oper expr2 ):

& – Bitwise AND| – Bitwise ORˆ – Bitwise XOR

6/29

Page 27: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Bitwise OperationsPerforms the logical operations on each of the correspondingbits of the operands.E.g. 7 & 5 = 5Consider the bits:

111& 101-----

101

Bitwise OperatorsBinary (expr1 oper expr2 ):

& – Bitwise AND| – Bitwise ORˆ – Bitwise XOR

6/29

Page 28: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

TopHat Question 2

What is 7 | 5 ?a. 12b. 7c. 5d. 3

7/29

Page 29: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Comparing References

Page 30: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Reference ComparisonEquality Operator (==)

[Resp: Inequality Operator (!=)]

Generally, the equality operator compares the values oftwo variables.Recall: value of a reference type is the referral information.

Stack

ijk

6

6

Heap Integer i = 6;Integer j = 6;Integer k =

new Integer (6);

8/29

Page 31: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Reference ComparisonEquality Operator (==)

[Resp: Inequality Operator (!=)]

Generally, the equality operator compares the values oftwo variables.Recall: value of a reference type is the referral information.

Stack

ijk

6

6

Heap Integer i = 6;Integer j = 6;Integer k =

new Integer (6);

8/29

Page 32: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Reference ComparisonEquality Operator (==) [Resp: Inequality Operator (!=)]

Generally, the equality operator compares the values oftwo variables.Recall: value of a reference type is the referral information.

Stack

ijk

6

6

Heap Integer i = 6;Integer j = 6;Integer k =

new Integer (6);

8/29

Page 33: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Comparing Values of Primitive WrappersInteger i = 6;Integer j = 5;Integer k = new Integer (6);int m = 5;

Instance Methods for Comparisonequals(otherObj) – True if values of the objects are equal.False otherwise.E.g. i.equals(k) // true

compareTo(otherObj) – 0 if equal, negative if less thanotherObj, and positive if greater than otherObj.E.g. i.compareTo(j) // +ve

Other Comparison Operators(In)Equality exception: j == m // true

Other operators work as expected: <, <=, >, >=.

9/29

Page 34: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Comparing Values of Primitive WrappersInteger i = 6;Integer j = 5;Integer k = new Integer (6);int m = 5;

Instance Methods for Comparisonequals(otherObj) – True if values of the objects are equal.False otherwise.E.g. i.equals(k) // true

compareTo(otherObj) – 0 if equal, negative if less thanotherObj, and positive if greater than otherObj.E.g. i.compareTo(j) // +ve

Other Comparison Operators(In)Equality exception: j == m // true

Other operators work as expected: <, <=, >, >=.

9/29

Page 35: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Comparing Values of Primitive WrappersInteger i = 6;Integer j = 5;Integer k = new Integer (6);int m = 5;

Instance Methods for Comparisonequals(otherObj) – True if values of the objects are equal.False otherwise.E.g. i.equals(k) // true

compareTo(otherObj) – 0 if equal, negative if less thanotherObj, and positive if greater than otherObj.E.g. i.compareTo(j) // +ve

Other Comparison Operators(In)Equality exception: j == m // true

Other operators work as expected: <, <=, >, >=.9/29

Page 36: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

String Comparison

Only Instance Methodsequals(otherStr)

compareTo(otherStr)

equalsIgnoreCase(otherStr)

compareToIgnoreCase(otherStr)

Lexicographical ComparisonStarting from index 0 and compare each pair of characters atindex x according to the following rules:

1 The character with the smaller Unicode value is consideredsmaller lexicograpically.

2 No character is smaller than an existing character. (E.g."foo" is smaller than "foobar")

10/29

Page 37: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

String Comparison

Only Instance Methodsequals(otherStr)

compareTo(otherStr)

equalsIgnoreCase(otherStr)

compareToIgnoreCase(otherStr)

Lexicographical ComparisonStarting from index 0 and compare each pair of characters atindex x according to the following rules:

1 The character with the smaller Unicode value is consideredsmaller lexicograpically.

2 No character is smaller than an existing character. (E.g."foo" is smaller than "foobar")

10/29

Page 38: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

String Comparison

Only Instance Methodsequals(otherStr)

compareTo(otherStr)

equalsIgnoreCase(otherStr)

compareToIgnoreCase(otherStr)

Lexicographical ComparisonStarting from index 0 and compare each pair of characters atindex x according to the following rules:

1 The character with the smaller Unicode value is consideredsmaller lexicograpically.

2 No character is smaller than an existing character. (E.g."foo" is smaller than "foobar")

10/29

Page 39: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

TopHat Question 3

Arrange these words in lexicographical order (smallest tolargest):a. Javascriptb. Cc. Javad. C++e. C#

11/29

Page 40: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Conditional Statements

Page 41: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

If Statement

if (cond) {trueStatement;

.

.

.lastTrueStatement;

}

Control Flow

cond

True Stmt Block

Followingstatements

truefalse

12/29

Page 42: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

If Statement

if (cond) {trueStatement;

.

.

.lastTrueStatement;

}

Control Flow

cond

True Stmt Block

Followingstatements

truefalse

12/29

Page 43: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

If-Else Statement

if (cond) {trueStatement;

.

.

.lastTrueStatement;

}else {

falseStatement;...

lastFalseStatement;}

Control Flow

cond

TrueStmtBlock

FalseStmtBlock

Followingstatements

true false

13/29

Page 44: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

If-Else Statement

if (cond) {trueStatement;

.

.

.lastTrueStatement;

}else {

falseStatement;...

lastFalseStatement;}

Control Flow

cond

TrueStmtBlock

FalseStmtBlock

Followingstatements

true false

13/29

Page 45: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

TopHat Question 4

What is printed out in be following code block:

boolean toBe = false;if(toBe || ! toBe) {

System.out.print("Tautology");}else {

System.out.print("Contradiction");}

14/29

Page 46: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

TopHat Question 5

What is the boolean expression to complete the followingcode?Assume that i is an int variable. Replace ?????? in the codewith the appropriate boolean expression.if( ?????? ) {

System.out.println(i + " is divisible by 3");}else {

System.out.println(i + " is not divisible by 3");}

15/29

Page 47: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

If-Else If StatementEnding Else

if (cond1) {trueStatements1;

}else if (cond2) {

trueStatements2;}...

else {allFalseStatements;

}

Control Flow

cond1True 1StmtBlock

cond2True 2StmtBlock

Else StmtBlock

Followingstatements

All false

true

truefalse

16/29

Page 48: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

If-Else If StatementEnding Else

if (cond1) {trueStatements1;

}else if (cond2) {

trueStatements2;}...

else {allFalseStatements;

}

Control Flow

cond1True 1StmtBlock

cond2True 2StmtBlock

Else StmtBlock

Followingstatements

All false

true

truefalse

16/29

Page 49: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

If-Else If StatementNo Ending Else

if (cond1) {trueStatements1;

}else if (cond2) {

trueStatements2;}...

else if (condN) {trueStatementsN;

}

Control Flow

cond1True 1StmtBlock

cond2True 2StmtBlock

condNTrue NStmtBlock

Followingstatements

true

true

true

false

false

17/29

Page 50: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

If-Else If StatementNo Ending Else

if (cond1) {trueStatements1;

}else if (cond2) {

trueStatements2;}...

else if (condN) {trueStatementsN;

}

Control Flow

cond1True 1StmtBlock

cond2True 2StmtBlock

condNTrue NStmtBlock

Followingstatements

true

true

true

false

false

17/29

Page 51: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Multiple ifs

if (cond1) {trueStatements1;

}if (cond2) {

trueStatements2;}...

if (condN) {trueStatementsN;

}

Control Flow

cond1True 1StmtBlock

cond2True 2StmtBlock

condNTrue NStmtBlock

Followingstatements

true

true

true

false

false

18/29

Page 52: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Multiple ifs

if (cond1) {trueStatements1;

}if (cond2) {

trueStatements2;}...

if (condN) {trueStatementsN;

}

Control Flow

cond1True 1StmtBlock

cond2True 2StmtBlock

condNTrue NStmtBlock

Followingstatements

true

true

true

false

false

18/29

Page 53: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

TopHat Question 6

What does the following code print?

int v1 = 3, v2 = 6;if (v1 + 2 < v2) System.out.print(" tic " );else if (v1 + 4 < v2) System.out.print(" tac " );else if (v1 + 4 > v2) System.out.print(" toe " );

19/29

Page 54: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

TopHat Question 7

What does the following code print?

int v1 = 3, v2 = 6;if (v1 + 2 < v2) System.out.print(" tic " );if (v1 + 4 < v2) System.out.print(" tac " );if (v1 + 4 > v2) System.out.print(" toe " );

20/29

Page 55: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Switch Statements

Control flow depends onbreak;

switch (expr) {case const1:

case1Statements;break;

case const2:case2Statements;break;

.

.

.default:

defStatements;break;

}

Control Flow

expr == const1Case 1StmtBlock

expr == const2Case 2StmtBlock

DefaultBlock

Followingstatements

No match

true

true

false

21/29

Page 56: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Switch Statements

Control flow depends onbreak;

switch (expr) {case const1:

case1Statements;break;

case const2:case2Statements;break;

.

.

.default:

defStatements;break;

}

Control Flow

expr == const1Case 1StmtBlock

expr == const2Case 2StmtBlock

DefaultBlock

Followingstatements

No match

true

true

false

21/29

Page 57: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Switch StatementsControl flow depends onbreak;

switch (expr) {case const1:

case1Statements;break;

case const2:case2Statements;break;

.

.

.default:

defStatements;break;

}

Control Flow

expr == const1Case 1StmtBlock

expr == const2Case 2StmtBlock

DefaultBlock

Followingstatements

No match

true

true

false

21/29

Page 58: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Switch StatementsControl flow depends onbreak;

switch (expr) {case const1:

case1Statements;

case const2:case2Statements;break;

.

.

.default:

defStatements;break;

}

Control Flow

expr == const1Case 1StmtBlock

expr == const2Case 2StmtBlock

DefaultBlock

Followingstatements

No match

true

true

falseno break;

21/29

Page 59: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Switch Statements Keys

RulesFinds the firstmatch going from the top to the bottom inorder.Fall through – A case without a breakwill fall through tothe next case’s statements.

Good PracticesOnly use fall though when the cases execute the same code.Generally, it is best to always have a default case at theend.

22/29

Page 60: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Switch Statements Keys

RulesFinds the firstmatch going from the top to the bottom inorder.Fall through – A case without a breakwill fall through tothe next case’s statements.

Good PracticesOnly use fall though when the cases execute the same code.Generally, it is best to always have a default case at theend.

22/29

Page 61: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

If-Else if and Switch Statementsif (i == 1) {

System.out.println("one");}else if (i == 2) {

System.out.println("two");}else {

System.out.println("Not 1 or 2");}

switch (i) {case 1:

System.out.println("one");break;

case 2:System.out.println("two");break;

default:System.out.println("Not 1 or 2");break;

}

23/29

Page 62: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

If-Else if and Switch Statementsif (i == 1) {

System.out.println("one");}else if (i == 2) {

System.out.println("two");}else {

System.out.println("Not 1 or 2");}

switch (i) {case 1:

System.out.println("one");break;

case 2:System.out.println("two");break;

default:System.out.println("Not 1 or 2");break;

} 23/29

Page 63: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

TopHat Question 8What does the following code print?int month = 8;switch (month) {

case 1: System.out.println("January");break;

case 2: System.out.println("February");break;

case 3: System.out.println("March");break;

case 4: System.out.println("April");break;

case 5: System.out.println("May");break;

case 6: System.out.println("June");break;

case 7: System.out.println("July");break;

case 8: System.out.println("August");break;

case 9: System.out.println("September");break;

case 10: System.out.println("October");break;

case 11: System.out.println("November");break;

case 12: System.out.println("December");break;

default: System.out.println("Invalid month");break;

}

24/29

Page 64: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

TopHat Question 9What does the following code print?

int month = 8;switch (month) {

case 1: System.out.println("January");case 2: System.out.println("February");case 3: System.out.println("March");case 4: System.out.println("April");case 5: System.out.println("May");case 6: System.out.println("June");case 7: System.out.println("July");case 8: System.out.println("August");case 9: System.out.println("September");case 10: System.out.println("October");case 11: System.out.println("November");case 12: System.out.println("December");

break;default: System.out.println("Invalid month");

break;

25/29

Page 65: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Ordinal Abbreviation Exercise

Write a method that takes an integer as input and returns astring with the integer converted to an abbreviated ordinal. I.e.1 becomes 1st, 2 becomes 2nd, 3 becomes, 3rd, etc...

26/29

Page 66: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Using Eclipse

Page 67: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Eclipse

EclipseIntegrated development environment (IDE) that we willuse in the course.In 2017, 40.5% use Eclipse (48% use IntelliJ) a

In 2016, 48.2% use Eclipse (43.6% use IntelliJ) b

aSource: http://www.baeldung.com/java-in-2017bSource: http://www.baeldung.com/java-ides-2016

Installing and UsingInstallation:https://cs200-www.cs.wisc.edu/wp/install-eclipse/

Tutorials: https://cs200-www.cs.wisc.edu/wp/resources/eclipse-tutorial/

27/29

Page 68: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Eclipse

EclipseIntegrated development environment (IDE) that we willuse in the course.In 2017, 40.5% use Eclipse (48% use IntelliJ) a

In 2016, 48.2% use Eclipse (43.6% use IntelliJ) b

aSource: http://www.baeldung.com/java-in-2017bSource: http://www.baeldung.com/java-ides-2016

Installing and UsingInstallation:https://cs200-www.cs.wisc.edu/wp/install-eclipse/

Tutorials: https://cs200-www.cs.wisc.edu/wp/resources/eclipse-tutorial/

27/29

Page 69: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Style Guidehttps://cs200-www.cs.wisc.edu/wp/style-guide/Examples:

http://pages.cs.wisc.edu/~cs200/resources/Frame.java

http://pages.cs.wisc.edu/~cs200/resources/Circle.java

Key ElementsFile Comment HeaderClass Comment HeaderMethod Comment HeaderGood names with proper formattingProper use of whitespace

28/29

Page 70: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Boolean Statements Comparing References Conditional Statements Using Eclipse

Further Reading

COMP SCI 200: Programming IzyBooks.com, 2015.zyBook code:WISCCOMPSCI200Fall2017

Chapter 3. Using ObjectsChapter 4. Branches

29/29

Page 71: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Appendix References

Appendix

Page 72: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Appendix References

References

Page 73: CS 200 - Programming I: Branches · 2017-09-30 · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2017 TopHatSec3(PM)JoinCode:719946

Appendix References

Image Sources I

https://brand.wisc.edu/web/logos/

http://www.zybooks.com/

30/29