Presentation is loading. Please wait.

Presentation is loading. Please wait.

Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Arrays COMP 102.

Similar presentations


Presentation on theme: "Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Arrays COMP 102."— Presentation transcript:

1 Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Arrays COMP 102 #22 2014T2

2 © Xiaoying Gao, Peter Andreae COMP102 21:2 Outline Arrays for : shorthand version of while Administration Assign 7 is handed out

3 © Xiaoying Gao, Peter Andreae COMP102 21:3 Data Type Int, double, long, char, boolean String Flower, Scanner, Cartoonfigure, Student, bankAccount, … Array is a new data type holds a lot of data of the same type int [ ] String[ ] Flower[ ]

4 © Xiaoying Gao, Peter Andreae COMP102 21:4 Reading data from a file into an array Suppose grades.txt contains some student grades with the number of grades first String [ ] grades; try { Scanner sc = new Scanner (new File(“grades.txt”)); grades = new String [ sc.nextInt() ]; int count = 0; while (count < grades.length && sc.hasNext() ) { grades[count] = sc.next(); count++; } sc.close(); } catch (IOException e) { UI.printf( “File failure %s\n”, e); } 86 A+ C B+ B- A A- : 1234567850 length: 86 null “C”“C” “ A+ ” ⋯ null grades.txt number of grades

5 © Xiaoying Gao, Peter Andreae COMP102 21:5 Using an Array : for loops Standard pattern for processing each element of an array: int i = 0 ; while (i < data.length ){ … data[ i ] … ; i = i+1 ; } The for loop is a shorthand: for (int i = 0 ; i < data.length ; i = i+1 ) { … data[ i ] … ; } or for (int i = 0 ; i < data.length ; i++ ) { … data[ i ] … ; } Shorthand: same effect as i = i + 1; but the value is the value of i before incrementing it

6 © Xiaoying Gao, Peter Andreae COMP102 21:6 For loop For loop puts the initialisation  once, before the loop body is run at all condition  tested each time, before loop body run increment  run each time, after loop body run together, at the front of the loop But the meaning is (almost) exactly the same as the while loop (scope of loop variable is different) for(statement) ;;expressionstatement InitialisationConditionIncrement

7 © Xiaoying Gao, Peter Andreae COMP102 21:7 Printing out the number of A's String [ ] grades; try { … …  read grades from the file  … /n", e); } int numAs = 0; for (int i = 0; i<grades.length; i++ ) { if ( grades[ i ].startsWith("A") { numAs++; } UIprintf( "%d A's out of %d grades\n", numAs, grades.length); 86 A+ C B+ B- A A- 12345850 length: 86 CB+AA- ⋯ A A+B- ⋯ grades: grades.txt

8 © Xiaoying Gao, Peter Andreae COMP102 21:8 Using an Array: using for loops Same as before, but with for loop: ⇒ easier to read Print out all the marks: for (int num = 0 ; num < marks.length ; num++) { UI.printf( “Student %3d got %4.1f”, num, marks[ num ]); } Compute final marks from essay marks and exam marks: for (int stNum = 0 ; stNum < marks.length ; stNum++) { marks[stNum] = essay[stNum]*0.6 + exam[stNum]*0.4; }

9 © Xiaoying Gao, Peter Andreae COMP102 21:9 Garden program: Flower class public class Flower{ private double baseX; private double baseY; private double height; private String stage; public Flower(double x, double y){ this.baseX = x; this.baseY = y; this.stage = "Bud"; this.height = 20; this.draw(); } public Flower(Scanner sc){ this.baseX = sc.nextInt(); this.baseY = sc.nextInt(); this.height = sc.nextInt(); this.stage = sc.next(); this.draw(); } public void grow(int amt){ if ( this.stage.equals("Bud") || this.stage.equals("Bloom")){ this.erase(); this.height = this.height + Math.max(amt, 0); this.draw(); } public void bloom(){ if (stage.equals("Bud")){ this.erase(); this.stage = "Bloom"; this.draw(); } public void pick(){…} public void draw(){…} public void erase(){…}

10 © Xiaoying Gao, Peter Andreae COMP102 21:10 Garden Program: Garden class public class Garden implements UIButtonListener{ private Flower[ ] = new Flower[12]; 12345678910110 length: 12 flowers: null

11 © Xiaoying Gao, Peter Andreae COMP102 21:11 Garden Program public class Garden implements UIButtonListener{ private Flower[ ] flowers = new Flower[12]; public Garden(){ int i = 0; while (i < this.flowers.length) { this.flowers[ i ] = new Flower(70+i*50, 400); i = i + 1; } this.redraw(); } public void redraw(){ int i = 0; while (i < this.flowers.length) { this.flowers[ i ].draw(); i = i + 1; } 12345678910110 length: 12 flowers:

12 © Xiaoying Gao, Peter Andreae COMP102 21:12 Garden Program public void bloomAll(){ for(int i = 0; i < this.flowers.length; i++) { this.flowers[ i ].bloom(); } 12345678910110 length: 12 flowers:

13 © Xiaoying Gao, Peter Andreae COMP102 21:13 Reading data from a file into an array Suppose garden.txt contains locations of 12 flowers try { Scanner sc = new Scanner (new File(“garden.txt”)); this.flowers = new Flower[12]; for (int i = 0; i<12; i++) { this.flowers[ i ] = new Flower(sc.nextInt(), sc.nextInt()); count++; } sc.close(); } catch (IOException e) { UI.printf( “File failure %s\n”, e); } 48 96 150 300 400 130 380 172 89 364 158 230 : 480 310 1234567850 length: 12 null “C”“C” “ A+ ” ⋯ null garden.txt


Download ppt "Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Arrays COMP 102."

Similar presentations


Ads by Google