Lesson 4

Transcription

Lesson 4
261494 Visual and Interac4ve Programming Dr. San4 Phithakkitnukoon (อ.ดร. สันติ พิทักษ์กิจนุกูร) Lesson 4: Arrays 9 Arrays 9.1 Arrays, why do we care? •  Let’s take a moment to revisit the car example from the previous chapter on object-­‐oriented programming. You may remember we spent a great deal of effort on developing a program that contained mul4ple instances of a class, that is, two objects. 9.1 Arrays, why do we care? •  How could we take this further and write a program with 100 car objects? •  With some clever copying and pas4ng, you might write a program with the following beginning: … 9.1 Arrays, why do we care? •  An array will allow us to take these 100 lines of code and put them into one line. •  Instead of having 100 variables, an array is one thing that contains a list of variables. •  Any 4me a program requires mul4ple instances of similar data, it might be 4me to use an array. For example, an array can be used to store the scores of four players in a game, a selec4on of 10 colors in a design program, or a list of fish objects in an aquarium simula4on. 9.1 Arrays, why do we care? 9.2 What is an array? •  From Sec4on 4, you may recall that a variable is a named pointer to a loca4on in memory where data is stored. •  In other words, variables allow programs to keep track of informa4on over a period of 4me. •  An array is exactly the same, only instead of poin4ng to one singular piece of informa4on, an array points to mul4ple pieces. 9.2 What is an array? •  You can think of an array as a list of variables. •  A list, it should be noted, is useful for two important reasons. 1.  The list keeps track of the elements in the list themselves. 2.  The list keeps track of the order of those elements (which element is the first in the list, the second, the third, etc.) 9.2 What is an array? •  In an array, each element of the list has a unique index, an integer value that designates its posi4on in the list (element #1, element #2, etc.). •  In all cases, the name of the array refers to the list as a whole, while each element is accessed via its posi4on. •  For example, the array has a total of 10 elements, but the first element number is 0 and the last element is 9. 9.2 What is an array? 9.3 Declaring and Crea4ng an Array •  We denote the use of an array by placing empty square brackets (“[]”) a`er the type declara4on. •  Let’s start with an array of primi4ve values, for example, integers. 9.3 Declaring and Crea4ng an Array •  One fundamental property of arrays, however, is that they are of fixed size. •  Once we define the size for an array, it can never change. •  We use new operator allows us to specify the array size: how many elements we want the array to hold 9.3 Declaring and Crea4ng an Array 9.3 Declaring and Crea4ng an Array 9.3 Declaring and Crea4ng an Array int[] num = new int[30];!
float[] num = new float[100];!
Zoog[] x = new Zoog[56];!
9.3 Declaring and Crea4ng an Array new float[num];!
Array size must be integer!
What is the size of ‘numbers’?!
5 9.4 Ini4alizing an Array •  One way to fill an array is to hard-­‐code the values stored in each spot of the array. •  We refer to each element of the array individually by specifying an index, star4ng at 0 9.4 Ini4alizing an Array •  A second op4on for ini4alizing an array is to manually type out a list of values enclosed in curly braces and separated by commas. 9.4 Ini4alizing an Array 9.4 Ini4alizing an Array Zoog!
3!
zoogs! 0!
new! Zoog!
zoogs! 1!
new! Zoog! x, y, w, h, eyeSize!
zoogs! 2!
new! Zoog!
9.5 Array Opera4ons •  Consider, for a moment, the following problem: (A)  Create an array of 1,000 floa2ng point numbers. (B)  Ini2alize every element of that array with a random number between 0 and 10. 9.5 Array Opera4ons •  Part A we already know how to do. •  What we want to avoid is having to do this for Part B: 9.5 Array Opera4ons •  We can employ while loop •  A for loop allows us to be even more concise 9.5 Array Opera4ons •  What if there might be some changes in the array size in the future? We can use length!
9.6 Simple Array Example: The Snake •  A seemingly trivial task, programming a trail following the mouse, is not as easy as it might ini4ally appear. •  The solu4on requires an array, which will serve to store the history of mouse loca4ons. •  We will use two arrays, one to store horizontal mouse loca4ons, and one for ver4cal. –  Let’s say, arbitrarily, that we want to store the last 50 mouse loca4ons. 9.6 Simple Array Example: The Snake •  First, we declare the two arrays. •  Second, in setup( ), we must ini4alize the arrays. Since at the start of the program there has not been any mouse movement, we will just fill the arrays with 0’s. 9.6 Simple Array Example: The Snake •  Each 4me through the main draw( ) loop, we want to update the array with the current mouse loca4on. •  Let’s choose to put the current mouse loca4on in the last spot of the array. –  The length of the array is 50, meaning index values range from 0–49. The the last spot is index 49, or the length of the array minus one. 9.6 Simple Array Example: The Snake •  Now comes the hard part. We want to keep only the last 50 mouse loca4ons. •  By storing the current mouse loca4on at the end of the array, we are overwri4ng what was previously stored there. 9.6 Simple Array Example: The Snake •  The full code for performing this array shi` is as follows: 9.6 Simple Array Example: The Snake •  Finally, we can use the history of mouse loca4ons to draw a series of circles. •  For each element of the xpos array and ypos array, draw an ellipse at the corresponding values stored in the array. 9.7 Arrays of Objects •  One of the nicest features of combining object-­‐
oriented programming with arrays is the simplicity of transi4oning a program from one object to 10 objects to 10,000 objects. •  Let’s revisit the main program for one Car object. 9.7 Arrays of Objects •  We need to do some modifica4ons to get 100 Car objects using arrays. 9.7 Arrays of (Car) Objects Example: An array of Car objects 9.7 Arrays of (Car) Objects 9.8 Interac4ve Objects •  When we first learned about variables (Sec4on 4) and condi4onals (Sec4on 5), we programmed a simple rollover effect. –  A rectangle appears in the window and is one color when the mouse is on top and another color when the mouse is not. •  The following is an example that takes this simple idea and puts it into a “Stripe” object. 9.8 Interac4ve Objects •  Even though there are 10 stripes, each one individually responds to the mouse by having its own rollover( ) func4on. •  This func4on checks to see if a point (mx, my) is contained within the ver4cal stripe. 9.8 Interac4ve Objects •  When designing your classes, it is o`en convenient to use a boolean variable to keep track of proper4es of an object that resemble a switch. •  This boolean variable is used in a condi4onal statement inside of the Stripe object’s display( ) func4on to determine the Stripe’s color. 9.8 Interac4ve Objects •  When we call the rollover( ) func4on on that object, we can then pass in mouseX and mouseY as arguments. 9.8 Interac4ve Objects Example: Interac9ve Stripes Handout: Example 9-­‐10 9.8 Interac4ve Objects 9.8 Interac4ve Objects 9.9 Processing’s Array Func4ons •  Processing offers a set of array func4ons that manipulate the size of an array by managing this process for you J •  They are: shorten( ), concat( ), subset( ), append( ), splice( ), and expand( ). •  In addi4on, there are func4ons for changing the order in an array, such as sort( ) and reverse( ). •  Details about all of these func4ons can be found in the reference. 9.9 Processing’s Array Func4ons •  Let’s look at one example that uses append( ) to expand the size of an array. •  This example starts with an array of one object. Each 4me the mouse is pressed, a new object is created and appended to the end of the original array. 9.9 Processing’s Array Func4ons [Handout: Example 9-­‐11] 9.9 Processing’s Array Func4ons 9.10 One Thousand and One Zoogs •  It is 4me to complete Zoog’s journey and look at how we move from one Zoog object to many. •  In the same way that we generated the Car array or Stripe array example, we can simply copy the exact Zoog class created in Example 8-­‐3 and implement an array. 9.10 One Thousand and One Zoogs 9.10 One Thousand and One Zoogs •  Exercise: Can you make Zoogs to react to the mouse? Try using dist() func>on to determine Zoog’s proximity to the mouse. For example, could you make each Zoog jiggle more the closer it is to the mouse? Project #3