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

Slides:



Advertisements
Similar presentations
MiniDraw Testing COMP 102 # T1
Advertisements

© Xiaoying Gao, Peter Andreae COMP :1 Term test Grade Marks No of students “A” range 36 ~45 53 “B” range29.25~ “C” range 22.5~2941 “D” range
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
Recognizing Patterns Counting: continually updating a value by a fixed amount Counting raindrops int dropCount = 0; //Raindrop counter while (dropCount.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Create Objects,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Patterns with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
2011-T1 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Working with arrays (we will use an array of double as example)
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Files COMP 102.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington “For each” & Patterns with.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input TextFields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
2011-T1 Lecture 12 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Dealing with Files COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input COMP.
Fields, Constructors COMP 102 # T2
Program with two classes COMP 102 # T2
What’s cheating and what is not?
Writing to a File Open a File Wrap it in a new PrintStream object.
The Matrix A b a d e a a a a a a a A b a d e a a a a a a a
Presentation transcript:

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

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

© 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[ ]

© 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- : length: 86 null “C”“C” “ A+ ” ⋯ null grades.txt number of grades

© 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

© 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

© 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 length: 86 CB+AA- ⋯ A A+B- ⋯ grades: grades.txt

© 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; }

© 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(){…}

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

© 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; } length: 12 flowers:

© 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(); } length: 12 flowers:

© 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); } : length: 12 null “C”“C” “ A+ ” ⋯ null garden.txt