PDE - Object Array (Lesson)

Object Array

So... What About a Varied Object Dance?

Watch the video below to see a dance of objects using an array and some of the special statements for using object arrays, using some each in Together code and count and while to get the effect that you see.

Setting Up the Object Array and First Synchronous Movements

Let's look at the code to see how this happened. First the biped array called TwoFeet was created manually and is shown here with set notation. The class contains 5 objects with index positions 0 to 4.

When using an "each in together", the Biped array TwoFeet had to have a name to use for each of the items. The name chosen could have been anything, but "group" was used. Since all is in at the same time, the group name picks up all of them and does whatever you code to do.  

In the "each in together" statement below, all of the objects in the "group" move together. There is not any conditional statements, exclusions, or other modification in the block of code that was coded for this particular "each in together" statement.

ObjectArray1.jpg

Using Modulus Selection With a "each in together" Statement

The second part of this entertainment show is an option based on the modulus use. INTEGER_REMAINDER in Alice is modulus in mathematics,  Taking any number modulus  2 will allow the computer to tell you whether the number is even or odd.  Even numbers come out with a remainder of 0 and odd numbers always have a remainder of 1.  

So why the index?   Keeping track of each of the objects as they are entered in memory to apply the option of flipping over if the index value of the object has a mod 2 value of zero or turning left or right if odd.  

Thus an index (could be called anything you want) is used to keep count of the index value of the object.   The each in together statement automatically controls not going out of bounds due to running over the array, but does not give you the index number to use, so you must control if you want to create option.  Note we keep track of theindex to make sure is less than the length of the array at all times.

Note:

  • Using innovative options based on the index value, interesting selections can be created.
  • The use of "group1" instead of "group" here?   Does it make a difference.  No, this is just a generic name to reference all of the items in the array with for this particular block of code.  It is not usable elsewhere without creating it for other uses.

ObjectArray2.jpg

Using Modulus for Selection

Running through the loop again requires the index to be reset to zero, the beginning, to be able to read through all of the index positions.  

Note: The count loop is controlled by the array length and always must be less than the length value?  Why?

The objects that turn in this code would be object 0, 2, and 4.  What are the objects in 0, 2, and 4 indices?  Check out the order they were put in the array.

ObjectArray3.jpg

Could the same code above been accomplished using a different code combination?  Yes!!!  There is not necessarily only one way to accomplish a task with code.  Using a while statement instead of a count statement is an alternative.  This is a reminder that there are different ways to approach a problem. One way is not necessarily better than the other.

Taking a Bow

This set of code turns each object one at a time based on their index to bend at the base of the spine (the waist).  Setting the index back to zero is required to read through the array again.  The use of the while allows the bend at the waist to happen to each object one after another.  Note count could have been used instead of the while condition statement.  The index < TwoFeet.length is called a condition of truth as the answer the computer comes determines is a true or false.  A conditional expression always results in a true or false condition to the computer.  Remember the binary 0 and 1 or the on and off of the light switch.  Reverting to only two choices allows the computer clear direction as to how to proceed.  The conditional expression in this case is determined to be true or false as long as the index is less than the length of the array.

ObjectArray4.jpg

Standing Back Upright

Reversing the direction of the forward bend to backward allows each of the objects to return to their standing position.

ObjectArray5.jpg

monkeyKing Catches Back Up

The little monkeyKing got confused on the last turns.  Now he realizes that he must catch up.  As he turns to face backwards from the viewer, this time he catches up by doing another half turn to finish joining up with the rest of the objects, finishing facing the correct way.  

You may have had this feeling of confusion if you were in marching band, a dance troupe, cheerleader, sports, or other group activity that requires a synchronous activity.  What do you do?  You try to fix quickly and catch up!!!!

Again, notice the reset of the index to 0 to reread the complete array.

ObjectARray6.jpg

What could be better in the above code structure? 

  1. Taking big chunks of code like the bow and standing back up and putting these two together in a "bow" procedure.  This unclutters the main code. 
  2. Any addition needing to repeat code already written should always be put in a procedure to
    • reduce complexity by having the code in one place
    • cut down on errors as code is in the program one time
    • reduce error fix time as one set of code is fixed one time for all calls to the method (procedure or function)

This code could easily have had the first three lines in myFirstMethod and the rest setup as procedures with appropriate name and called from myFirstMethod, cleaning up the main grouping of code to only the essential needs and letting each procedure handle all of the details.  Parameters could then also be added to the methods to allow for various differences between calls to the procedure.

IMAGES & VIDEO CREATED BY GAVS.