Fork me on GitHub
ICM Quizzes

Quiz 5: Objects and Arrays


1. Write a program that implements the above sketch. First, draw the first word from the words array on the canvas. And everytime when the mouse is pressed, draw the next word from the array words. We use a variable called index to represent the index of each word in the array. And at last, if the index equals to the length of the array, make index 0 again.


2. Assuming an array of 10 integers, that is, var nums = [ 5, 4, 2, 7, 6, 8, 5, 2, 8, 14 ]; Write code to perform the following array operations. The sketch above shows the final results.

(a) Square each number (i.e., multiply each by itself).
(b) Add a random number between zero and 10 to each number.
(c) Add to each number the number that follows in the array. Skip the last value in the array.
(d) Calculate the sum of all the numbers.

(Note that the number of clues vary, just because a [____] is not explicitly written in does not mean there should not be brackets).


3. Write a program that implements the above sketch. Step 1: create a Bubble class, and make a single Bubble object.

Step 2: Use a for loop to create an array of bubble objects with the class you created. Then try to create 100 bubble objects.


4. Write a program that implements the above sketch. When the mouse is dragged, create a new bubble object at the mouseX, mouseY positon, and add it to the bubbles array. If there are more than 20 bubbles, delete the first bubble object in the bubbles array. Use push() and splice() function to add and remove objects from an array.
Learn more about push() and splice() function.


5. Write a program that implements the above sketch. Create a function called clicked for bubble objects to check if the mouse is on the bubble. If yes, change the color of this bubble. Don't forget to call the clicked function in the mousePressed() function.