15 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 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 February 2013Birkbeck College, U. London2
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 2013Birkbeck College, U. London3
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 2013Birkbeck College, U. London4
Exercise 2: second part double a2 = , b2 = -1.2; double c2 = , d2 = 587.2; Find the width of the field: 15 February 2013Birkbeck College, U. London5 d2:587.20
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 adjacent to the colon. 15 February 2013Birkbeck College, U. London6 d2:587.20
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 2013Birkbeck College, U. London7
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 2013Birkbeck College, U. London8
Exercise 3 System.out.println(a1.compareTo(b1)<=0); /* And similarly for the other pairs of words. */ 15 February 2013Birkbeck College, U. London9
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 JFE 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 2013Birkbeck College, U. London11
The if Statement int actualFloor; if (floor >13) { actualFloor = floor-1; } else } actualFloor = floor; } 15 February 2013Birkbeck College, U. London12
Alternative Code int actualFloor = floor; if (floor > 13) { actualFloor = floor-1; } 15 February 2013Birkbeck College, U. London13
Flow Chart for if Statement 15 February 2013Birkbeck College, U. London14 floor > 13? actualFloor = floor-1 actualFloor= floor truefalse
Flow Chart for if Statement with no else Branch 15 February 2013Birkbeck College, U. London15 floor > 13? actualFloor = floor-1 truefalse
Brackets Note the alignments { … } Brackets can be omitted for single statements (not recommended). if (floor > 13) actualFloor = floor-1; 15 February 2013Birkbeck College, U. London16
Avoid Code Duplication if (floor > 13) { actualFloor = floor-1; System.out.println(Actual floor: +actualFloor); } else { actualFloor = floor; System.out.println(Actual floor: +actualFloor); } 15 February 2013Birkbeck College, U. London17
Duplication Removed if (floor > 13) { actualFloor = floor-1; } else { actualFloor = floor; } System.out.println(Actual floor: +actualFloor); 15 February 2013Birkbeck College, U. London18
Multiple Alternatives 15 February 2013Birkbeck College, U. London19 The Richter Scale for Earthquakes ValueEffect 8Most structures fall 7Many buildings destroyed 6Many buildings considerably damaged. Some collapse. 4.5Damage to poorly constructed buildings
Multiple if Statements if (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 2013Birkbeck College, U. London20
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 2013Birkbeck College, U. London21
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 2013Birkbeck College, U. London22
Squares on a Chess Board 15 February 2013Birkbeck College, U. London abcdefgh char file =`a`; int row = 3; /* square a3 */
Nested if Statements if (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 2013Birkbeck College, U. London24
Dangling else Problem double shippingCharge = 5.00; if (country.equals(USA)) if (state.equals(HI)) shippingCharge = 10.00; else shippingCharge=20.00; 15 February 2013Birkbeck College, U. London25
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 2013Birkbeck College, U. London26