Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science I Arrays. Parallel structures. Classwork/Homework: Build your own bouncing things.

Similar presentations


Presentation on theme: "Computer Science I Arrays. Parallel structures. Classwork/Homework: Build your own bouncing things."— Presentation transcript:

1 Computer Science I Arrays. Parallel structures. Classwork/Homework: Build your own bouncing things.

2 Arrays … are ordered collections of things. Specific datatype. Variables need to be declared: int[] scores; This declares scores to be an array variable, each element an int. Or int[] scores = new int[5]; or int[] scores = {78,80,7};

3 Arrays Elements in the array are accessed or set using an index. The indexing starts from 0 and goes to one less than the number. scores[0] = 100;... = scores[2];

4 Typical example Calculate average. int sum = 0; float average; for (int i=0;i<scores.length;i++) { sum = sum + scores[i]; } average = sum/scores.length;

5 Example: Bouncing balls Build on bouncing ball (it could be bouncing thing). Want each ball to have distinct – Diameter – Starting x and y positions – Starting dx and dy (that is, speeds) Use technique called parallel structures. Use for-loop in draw function.

6 Global variables float[] bxs = {width/2,width/4, width *.75}; float[] bys = {width/2, width/3, width/6}; float[] dxs = { 3,2,1}; float[] dys = {3,1,2}; int[] balldiams = {20,40,60};

7 The setup function void setup() { size(900,600); }

8 The draw function void draw(){ background(0); //erase window for (int i=0;i<bxs.length;i++) // loop over items { ellipse(bxs[i],bys[i],balldiams[i], balldiams[i]); bxs[i] = bxs[i]+dxs[i]; if ((bxs[i]>=width)||(bxs[i]<=0)) {dxs[i] = -dxs[i];} bys[i] = bys[i] + dys[i]; if ((bys[i]>=height)||(bys[i]<=0)) {dys[i] = -dys[i];} } //end for loop }

9 Classwork Make bouncing things or some other application making use of arrays. For a bouncing balls sketch, – How about adding color? – Make ball slow down or speed up when hitting wall Change specific dxs or dyx element – Incorporate user input

10 Preview: Making and traveling path Show http://faculty.purchase.edu/jeanine.meyer/html 5/drawingrev.html http://faculty.purchase.edu/jeanine.meyer/html 5/drawingrev.html And http://faculty.purchase.edu/jeanine.meyer/html 5/movingpictures.html http://faculty.purchase.edu/jeanine.meyer/html 5/movingpictures.html Will explain simple version with circles.

11 Homework Start planning midterm project. NEXT WEEK: classes


Download ppt "Computer Science I Arrays. Parallel structures. Classwork/Homework: Build your own bouncing things."

Similar presentations


Ads by Google