gp4, martin lillholm 1 introductory programming (gp) spring 2006 lecture 4 we start at 13:00 slides...

46
1 GP4, Martin Lillholm Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to print them now www.itu.dk/courses/GP/F2006 Martin Lillholm

Upload: amanda-gilbert

Post on 29-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

1GP4, Martin Lillholm

Introductory Programming (GP)Spring 2006

Lecture 4

We start at 13:00 Slides are available from the course home pageFeel free to print them now

www.itu.dk/courses/GP/F2006

Martin Lillholm

Page 2: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

2GP4, Martin Lillholm

Mandatory Assignment

• How did it go with last week’s mandatory assignment ?– Easy?– Hard?– Many didn’t hand in – why ?

Page 3: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

3GP4, Martin Lillholm

Last Week

• Classes, objects (instances)

• Attributes (instance variables)

• Methods and constructors

• A first look a static methods and variables (class variables)

• Encapsulation

• Object/reference variables – variable of non-primitive type

• The String, Random, Math og Scanner classes

• Formatted output

Page 4: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

4GP4, Martin Lillholm

The Scanner class again

Input several (e.g.) integers using one line of input:

Page 5: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

5GP4, Martin Lillholm

Formatting Phone Numbers

Assignment from last week: L&L Programming Project 3.4

23 00023

DecimalFormat fmt = new DecimalFormat("00000");

System.out.println(fmt.format(23));

Page 6: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

6GP4, Martin Lillholm

Java API Specification

– Appendix M

– http://java.sun.com/j2se/1.5.0/docs/api/index.html

Page 7: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

7GP4, Martin Lillholm

What Have We Skipped so Far?

• Earlier:

– Enumerated types– Wrapper classes and auto boxing– Some GUI

• Today:

– Iterators section 5.6

Page 8: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

8GP4, Martin Lillholm

Today

• More on flow of control

• Boolean Expressions

• Relational and logical operators

• if and switch statements

• Loop statement– for, do, while

• Block statements

• Graphics using loops and conditions – if time allows

Page 9: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

9GP4, Martin Lillholm

How Does Today’s Chapter Fit In ?

• Program execution (normally) begins in the main method. Statements in the main method are executed sequentially – flow of control.

• Method calls causes flow of control to execute statements in the method (and possible nested method calls) and then return to the next statement in the main method.

• Conditional statements:

• Loops

statement1

statement2statement3statement4

statement5

statement1statement2if (condition) statement3 // truestatement4

e.g. 10 times

Both used in methods bodies incl. main

Page 10: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

10GP4, Martin Lillholm

Boolean Expressions

• Boolean/logical expressions true or false

Has type boolean

• As opposed to arithmetic expressions 1, 45.78

Has type integer, double, ...

• Boolean expressions are normally formed using relational and logical operators.

Page 11: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

11GP4, Martin Lillholm

Operator Meaning Example

< Less than x < 60

<= Less than or equal x <= 60

> Greater than x > 60

>= Greater than or equal x >= 60

== Equal to x == 60

!= Not equal to x != 60

Relational Operators

Argument type typically integer, double, char (primitive types)Resultant type is boolean and has value true or false

Has lower precedence than the arithmetic operators

<, <=, >, >= has higher precedence than == and !=

Page 12: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

12GP4, Martin Lillholm

Boolean Expressions using Relational Operators

• Examples (x=2 and y=4):

Expression Value

false false

true true

true == false false

x != y true

x < 3 + y true

y < x + 3 true

(x + y > 3) == false false

false != x < 3 true

x == y == false true

Page 13: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

13GP4, Martin Lillholm

Logical Operators

Operator Meaning Eksempel

! Not (unary) !(x == 60)

&& And (binary) 0<= x && x <= 60

|| Or (binary) x < 0 || x>=60

Both argument and resultant type is boolean

! has higher precedence than && which has higher precedence than ||

! has higher precedence than both the relational and arithmetic operators

&& and || has lower precedence than both the relational and arithmetic operators

If expr1 is false in expr1 && expr2 then expr2 isn’t evaluatedIf expr1 is true in expr1 || expr2 then expr2 isn’t evaluated

Page 14: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

14GP4, Martin Lillholm

Logical Operators, Truth Tables

x !x

true false

false true

x y x && y x || y

false false false false

false true false true

true false false true

true true true true

Page 15: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

15GP4, Martin Lillholm

• Examples (x=2 and y=4):

Boolean Expressions with Logical Operators

Udtryk Værdi

!false true

!true false

!true == false true

!(true == false) true

true && false false

false || true true

(x + y > 3) && x < y true

x + y == 3 || x < 4 true

x < y && (3*4 == 2*6-1*2+2) == !(3<x) true

Page 16: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

16GP4, Martin Lillholm

The if Statement

• Used for affecting flow of control based on boolean expressions

• And is thus used to make choices based on data:– Is one number bigger than another? – Is a number within a certain interval or outside?– Are two Strings identical?

• Is typically used when we need to:– React to data - unknown at compile time– Entered numbers, strings etc.– Data stored in files– Random numbers– ...

condition

Sstatement(s)

true

false

Page 17: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

17GP4, Martin Lillholm

The if statement – Syntax

if ( expr ) statement;

if is a reserved word

expr has to be boolean and thus evaluate to either true or false

If expr evaluates to true, statement is executedand otherwise skipped

Page 18: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

18GP4, Martin Lillholm

The if Statement – Examples

int a=5; if (a > 10)

a = a – 5;System.out.println(”a is: ” + a);

if (a>0 && a <=10)System.out.println(”a is in the interval from 1 to 10”);

if (a<1 && a>10)System.out.println(”a isn’t in the interval from 1 to 10”);

if (a == 5)a = 3;

if (a != 5)a = 5;

if (a < 0 || a > 10000)System.out.println(”a is negative or very large”);

Page 19: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

19GP4, Martin Lillholm

The if Statement – Example

Limitations ? Either or ... Several statements in the true branch

Page 20: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

20GP4, Martin Lillholm

The if-else Statement

• Syntax

if (expr)statement1;

elsestatement2;

• Examples:

if (level > MAX)System.out.println(”Level Critical!”);

elseSystem.out.printlnt(”Level ok”);

if (a > b - .01 && a < b + .01)a = b;

elseSystem.out.println(”a is different from b”);

condition

statement1

true false

statement2

Page 21: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

21GP4, Martin Lillholm

The if-else – Example

Page 22: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

22GP4, Martin Lillholm

if-else – Example

Page 23: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

23GP4, Martin Lillholm

Block Statements

if (a > b) // swap a and b tmp = a; a = b; b = tmp;else a = a + 1;

if (a > b) { // swap a and b tmp = a; a = b; b = tmp;}else a = a + 1;

• { ... } is used to combine statements in blocks as we do in classes, constructor, and methods.

• A block statement can always substitute any single statement.

• Indentation doesn’t mean anything – logically speaking – but makes the program a lot more readable

Page 24: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

24GP4, Martin Lillholm

Block Statements - Example

Page 25: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

25GP4, Martin Lillholm

Nested if Statements – Example

Page 26: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

26GP4, Martin Lillholm

Nested if Statements – A Pitfall …

if (b < 0)

if (a < b)

System.out.println(”Både a og b er negative”);

else

System.out.println(”b er negativ”);

if (b < 0) {

if (a < b)

System.out.println(”Både a og b er negative”);

}

else

System.out.println(”b er negativ”);

else always “belongs” to closest unmatched if – bar {}’s

Page 27: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

27GP4, Martin Lillholm

Comparing Data

• Some primitive types (integer, long, short, byte) are compared as we seen so far.

• Decimal type (float, double) are still approximations and always be compared within and certain tolerance and not using e.g ==

final double TOLERANCE = 0.00001;

if (Math.abs(f1-f2) < TOLERANCE)

System.out.println(”f1 and f2 are approx. equal”);

• Characters char is compared using their Unicode:

’0’ .. ’9’ < ’A’ .. ’Z’ < ’a’ ... ’ z’‘A’ != ‘a’

Page 28: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

28GP4, Martin Lillholm

Comparing Data (String)

• Think of strings as a sequence of characters (from left to right). This gives an ordering (lexicographic ordering).“garden” before “gardens” and “Martin” before “anders”

• Vi cannot use e.g. == and <= directly – see next slide

equals and compareTo methods I the String class

if (name1.equals(name2)) System.out.println (”The names are the same”);else System.out.println (”The names are not the same”);

int result = name1.compareTo(name2);if (result < 0) System.out.println(name1 + ” comes before ” + name2);else if (result == 0) System.out.println (”The name are the same”); else System.out.println (name1 + ” follows ” + name2);

Page 29: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

29GP4, Martin Lillholm

Comparing Data (Objects)

• We’ll return to object comparison in general.

• But == applied to objects compares references.

Page 30: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

30GP4, Martin Lillholm

The switch Statement

• Alternative construction to select one or more of several statements

• Based on an integral expression one or more options from a given list is selected – possibly the default option is none matches.

• Program execution continues a the option that matches the expression option

switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break;

default: System.out.println(”No match”);

}

Page 31: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

31GP4, Martin Lillholm

The switch Statement

• The purpose af break

• The purpose of default

• Implicit blocks

• The expression must be an integral type:(int, char or (enumerated type))

• The cases must be literals

• Equivalent to a series of if statements but sometimes more elegant

Page 32: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

32GP4, Martin Lillholm

switch – Example

Page 33: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

33GP4, Martin Lillholm

while Loops

• Repeats a statement or block 0 or more times

while (expr) statement;

• If expr evaluates to true, statement is executed

• expr is evaluated again and if true, statement is executed again …

• Continues until expr evaluates to false

int count = 1;while (count <= 5) { Sytem.out.println (count); count++;}

Boolean expression

statement

true false

condigtion

Page 34: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

34GP4, Martin Lillholm

while loops –Example

Page 35: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

35GP4, Martin Lillholm

while loops – Example

Page 36: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

36GP4, Martin Lillholm

Infinite Loops

int count = 1;

while (count <= 25) {

System.out.println (count);

count = count - 1;

}

while (true) {

System.out.println (”This will take a while ...”);

}

int j = 0;

while (j < 10) {

j = j + 0;

System.out.println(j);

}

double num = 1.0;

while (num != 0.0)

num = num – 0.1;

Page 37: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

37GP4, Martin Lillholm

Nested Loops

int count1, count2;

count1 = 1;

while (count1 <= 10) {

count2 = 1;

while (count2 <= 50) {

System.out.println(”Here again”);

count2++;

}

count1++;

}

Page 38: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

38GP4, Martin Lillholm

Nested Loops

Page 39: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

39GP4, Martin Lillholm

do loops

• Very similar to while loops except that the condition is evaluated after the body – thus do loops always run at least once

do

statement;

while (expr)

int count = 0;

do {

count++;

System.out.println(count);

} while (count < 5);

true

condition

statement

false

Boolean expression

Page 40: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

40GP4, Martin Lillholm

do loops – Example

Page 41: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

41GP4, Martin Lillholm

for loops

for ( initialisation ; condition ; increment ) statement;

InitialisationExcuted once before

the loop body

statement is executed untilcondition evaluates to false

“increment” executed after the loop body at the end of each iteration

Page 42: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

42GP4, Martin Lillholm

for loops

statement

true

conditionevaluated

false

increment

initialization

Page 43: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

43GP4, Martin Lillholm

for loops - Examples

int i;

for (i=0; i<10; i++)

System.out.println(”i:” + i);

for (int j=1; j<10; j++)

System.out.println(”j:” + j);

for (i=9; i >= 0; i--) {

k = i + 2;

System.out.println(”k:” + k);

}

– L&L Multiples.java (side 248)

– L&L Stars.java (side 250)

Page 44: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

44GP4, Martin Lillholm

All Loops Constructs are Equivalent

• while

• do

• for

Page 45: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

45GP4, Martin Lillholm

Advise on Variable Declaration and Initialisation

• Declare variables as local as possible

• Declare variables close to their first use

• Declare loop variables close to or in the loop construct (only possible for for loops)

Page 46: GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to

46GP4, Martin Lillholm

Graphics using Loops

– Bullseye.java og BullseyePanel.java i BlueJ(L&L page 251-254)

– Boxes.java(L&L page 255-257)