15 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank...

Post on 28-Mar-2015

214 views 0 download

Tags:

Transcript of 15 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank...

15 February 2013 Birkbeck College, U. London 1

Introduction to Programming

Lecturer: Steve Maybank

Department of Computer Science and Information Systems

sjmaybank@dcs.bbk.ac.ukSpring 2013

Week 6: if statement

Overview Java Lab 4, Exercises 2 and 3 Example of a class and an object if statement See Java for Everyone, Ch. 3

15 February 2013 Birkbeck College, U. London 2

JavaLab 4 Exercise 2 The data is included in the program

rather than read in from the keyboard.

int a1=28, b1=418, c1=-89, d1=-3007;

Find the width in characters of the field in which the numbers are to be printed.

15 February 2013 Birkbeck College, U. London 3

Exercise 2: first part The format specifier is %5d, where 5 is

the field width and d stands for decimal integer.

Use four print statements, one for each line of the output.

System.out.printf(“a1:%5d\n”, a1);System.out.printf(“b1:%5d\n”, b1); //etc.

15 February 2013 Birkbeck College, U. London 4

Exercise 2: second part

double a2 = 28.467, b2 = -1.2;double c2 = 0.0145, d2 = 587.2;

Find the width of the field:

15 February 2013 Birkbeck College, U. London 5

d 2 : 5 8 7 . 2 0

Format Specifier

Use %9.2f, where f: fixed floating point, 9: field width, 2: number of places to the right of the decimal point.

Note the rightmost 0. Define a format specifier that places

587.20 adjacent to the colon.15 February 2013 Birkbeck College, U. London 6

d 2 : 5 8 7 . 2 0

Print Statements

System.out.printf(“a2:%9.2f\n”, a2);System.out.printf(“b2:%9.2f\n”, b2);System.out.printf(“c2:%9.2f\n”, c2);System.out.printf(“d2:%9.2f\n”, d2);

15 February 2013 Birkbeck College, U. London 7

JavaLab 4, Exercise 3

String a1=“Tom”, b1=“Jerry”;/* Print true if a1 precedes b1 in

lexicographic order, otherwise print false.Recall string1.compareTo(string2). This method returns an integer, but a boolean result is required. */

15 February 2013 Birkbeck College, U. London 8

Exercise 3

System.out.println(a1.compareTo(b1)<=0);

/* And similarly for the other pairs of words. */

15 February 2013 Birkbeck College, U. London 9

Example of a Class public class BankAccount

{

private double balance; // data held in each object

public BankAccount() // constructor to make objects

{

balance = 0; // each new object has balance=0

}

public void payIn(double payment) // pay into the account

{

balance = balance+payment;

}

}

15 February 2013 10JFE Chapter 7.3

Creating an Object

BankAccount ba = new BankAccount();/* Create an object ba in the class

BankAccount. The object ba has its own balance which is 0.*/

double payment = 4.23;ba.payIn(payment);/* Add £4.23 to the balance in ba. */

15 February 2013 Birkbeck College, U. London 11

The if Statementint actualFloor;if (floor >13){

actualFloor = floor-1;}else}

actualFloor = floor;}

15 February 2013 Birkbeck College, U. London 12

Alternative Code

int actualFloor = floor;if (floor > 13){

actualFloor = floor-1;}

15 February 2013 Birkbeck College, U. London 13

Flow Chart for if Statement

15 February 2013 Birkbeck College, U. London 14

floor> 13?

actualFloor =floor-1

actualFloor=floor

true false

Flow Chart for if Statement with no else

Branch

15 February 2013 Birkbeck College, U. London 15

floor> 13?

actualFloor =floor-1

true false

Brackets Note the alignments

{…

} Brackets can be omitted for single

statements (not recommended).if (floor > 13)

actualFloor = floor-1;

15 February 2013 Birkbeck College, U. London 16

Avoid Code Duplicationif (floor > 13){

actualFloor = floor-1;System.out.println(“Actual floor: ”+actualFloor);

}else{

actualFloor = floor;System.out.println(“Actual floor: ”+actualFloor);

}

15 February 2013 Birkbeck College, U. London 17

Duplication Removedif (floor > 13){

actualFloor = floor-1;}else{

actualFloor = floor;}System.out.println(“Actual floor: ”+actualFloor);

15 February 2013 Birkbeck College, U. London 18

Multiple Alternatives

15 February 2013 Birkbeck College, U. London 19

The Richter Scale for Earthquakes

Value

Effect

8 Most structures fall

7 Many buildings destroyed

6 Many buildings considerably damaged. Some collapse.

4.5 Damage to poorly constructed buildings

Multiple if Statementsif (richter>=8.0)

{System.out.println(“Most structures fall”);}else if (richter >= 7.0)

{System.out.println(“Many buildings destroyed”);}else if (richter >= 6.0)

{System.out.println(“Considerable damage”);}else if (richter >= 4.5)

{System.out.println(“Damage to poorly constructed buildings”);}

else{System.out.println(“No destruction of buildings”);}

15 February 2013 Birkbeck College, U. London 20

Result As soon as one of the tests

succeeds the message is printed and no further tests are made.

If no test succeeds then the final else clause applies.

15 February 2013 Birkbeck College, U. London 21

Discussion What happens if the order of the

tests is reversed?

What happens if the all the “else” words (and the final print statement) are removed?

15 February 2013 Birkbeck College, U. London 22

Squares on a Chess Board

15 February 2013 Birkbeck College, U. London 23

8

7

6

5

4

3

2

1

a b c d e f g h

char file =`a`;int row = 3;

/* square a3 */

Nested if Statementsif (file == `a` || file == `c` || file == `e` || file == `g`){

if (row%2 == 1){colour = “black”;}

else{colour = “white”;}

}else{

if (row%2 == 0){colour = “black”;}

else{colour = “white”;}

}

15 February 2013 Birkbeck College, U. London 24

Dangling else Problem

double shippingCharge = 5.00;if (country.equals(“USA”))

if (state.equals(“HI”))shippingCharge = 10.00;

elseshippingCharge=20.00;

15 February 2013 Birkbeck College, U. London 25

Problem Avoided by Using Brackets

double shippingCharge = 5.00;if (country.equals(“USA”)){

if (state.equals(“HI”)){

shippingCharge = 10.00; // Hawaii is more expensive}

}else{

shippingCharge = 20.00; // As are shipments outside the USA

}

15 February 2013 Birkbeck College, U. London 26