Note: We will take breaks at least once an hour
In-person exam on Oct 19. Relax! We will have a study sheet. Your exam and project are together worth 15%.
Subjects to review:
Midterm project is due on Oct 26 (2 weeks).
We are a little over a third of the way through the semester and have come far in our programming knowledge. We have covered variables and types, conditionals, loops, functions, translations, interaction through keypresses and the mouse, classes and objects. Next week we will add in arrays to complete the foundations. Taking the totality of what we’ve learned so far our midterm project is to code a Tamagotchi, type of digital pet such as a Furby or artificial human companions.
Part A due next week:
Weeks ahead: Part B will be to create your creature’s Class in code. Part C will be to implement timers and interactivity to build the state machine.
What are they? Arrays are lists. An array is a list of data stored under the same name. You can create an array of any type of data. When you create an array, you can access any of the data stored in that array. We call the location of a piece of data its index.
The first location in an array is always 0. We call that position [0]
. The second location is [1]
. Etc.
IMPORTANT: Each array has a variable length
that returns the total number of elements in the array. Keep in mind that arrays start counting at 0 so an array with 10 elements in (a length of 10) will go from location [0]
up to location [9]
!
//from Processing tutorials - Arrays
int[] x = {
50, 61, 83, 69, 71, 50, 29, 31, 17, 39
};
fill(0);
// Read one array element each time through the FOR loop
for (int i = 0; i < x.length; i++) {
rect(0, i*10, x[i], 8);
}
You define an array similar to other data types with the addition of brackets [ and ]. When you declare an array you specify what kind of data it will hold. An array can only hold a single data type.
int[] ages; //declaring an array for BTS members' ages
void setup(){
size(100,100);
ages = new int[7]; //create space in memory for 7 values
ages[0] = 27; //assign
ages[1] = 26;
ages[2] = 25;
ages[3] = 25;
ages[4] = 24;
ages[5] = 24;
ages[6] = 22;
}
OR
int[] ages = new int[7]; //declare and create
void setup(){
size(100,100);
ages[0] = 27; //assign
ages[1] = 26;
ages[2] = 25;
ages[3] = 25;
ages[4] = 24;
ages[5] = 24;
ages[6] = 22;
}
OR
int[] ages_of_BTS_members = {27, 26, 25, 25, 24, 24, 22};
//declare, create and assign all in one
void setup(){
size(100,100);
}
String[] names = {"Owen","Zev","Anthony","Frankie","Alexander","Brock","Jake","Sofia","Sergio","Tiffany","Maya","Markus","Allan","Khalid"};
void setup(){
size(100,100);
println("Total students: "+names.length);
println("The first student: "+names[0]);
println("The last student: "+names[names.length-1]);
//question: why do we specify name.length - 1?
}
How do pick a random student name from the list?
Let’s say we wanted to roll 5 dice.
int[] die = new int[5];
die[0] = random(1,7);
die[1] = random(1,7);
die[2] = random(1,7);
die[3] = random(1,7);
die[4] = random(1,7);
die[5] = random(1,7);
Instead, let’s use a loop:
int [] die = new int[5];
for (int i = 0; i < die.length; i++){
die[i] = int(random(1,7));
//must convert from float to int. random produces floats!
}
Remember .length
tells us how long the array is
Create an array of 1000 floats. Initialize every element with random number between 0 and 10.
Create an array of 10 integers. Add to each number the number that follows in the array. Skip the last number.
Create and initialize an array of 100 integers. Calculate the sum of all elements in the array.
new
to create the array.
data = new int[5];
Watch The Coding Train videos on Arrays.