CompSci 94 - Classwork 19- November 7, 2013
Today's classwork gives you practice with both visual and
Part 1: Nonvisual Array of Numbers
- Create a new Alice world. Create a world method called compute and
in the event window, set "when the world starts" to do world.compute.
- Under world properties, create a new variable called "numbers" that is an array of
numbers. Don't put anything in it when you create it. It will look like
- In the compute method using a loop, generate 20 random integers from 1
to 5 and put
them into the array numbers. How? If you set the index position of the array to a
number, it will add it to that slot in the array. Arrays start numbering at
0. So put the first random number in slot 0, the second random number in
slot 1, etc.
- Run your program with the world properties showing and you should see
the numbers being created and placed into the array. You may have to
increase the size of the world properties area so you can see all 20
- Write a world.function called world.numOccurences that has a number
parameter called value. This function returns the number of times value
appears in the numbers array.
- Back in the compute method add code to determine which number occurs
the most often in the array. You will need a loop and you will need to call
the function world.numOccurrences.
- Print out a message in one line with the result such as: The number that occurs the most often is
5 and it appears 6 times. Note, you will need to use the String functions
"a join b" and "what as a string".
For example, once when I ran my code, it generated the following numbers
into my array:
and the output printed was:
Part 2: Visual Arrays
- Add this to the same Alice world and change the "When the world
starts" event to do world.myfirstmethod.
Add in seven frogs (add them in one at a time, don't use the
copy feature). Then add in an ArrayVisualization and put the
seven frogs into the array.
- Write an ArrayVisualization method called DoubleInSize
that uses a loop to go through the array and double the size of
each frog in the array. Call this method in myFirstMethod.
- Write an ArrayVisualization method called changeColor
that has one color parameter. This method goes through each element
in the array, and for each element randomly decides if it should change
the frog's color to the color passed in. Call this method in
myFirstMethod to randomly change some of the frogs in the array to Blue,
otherwise leave the frog's color as is.
- Write an ArrayVisualization function named
NumberOfFrogsThisColor that returns a number. This method has a
color parameter and it returns the number of frogs in the array of this
color. In MyFirstMethod, call this function in a print statement
after the call to "changeColor" to print the number of frogs that are
- Write an ArrayVisualization function named isColorToTheRight
that returns a number. This function has two parameters, a number called
StartPosition and a color named someColor. This function returns the
index position of the first frog with someColor starting at StartPosition
and moving through the Array. If there is no frog of SomeColor
from StartPosition to the end of the array, then this function should
return -1. You will use this function in the next method.
- Write an ArrayVisualization method called MoveColorFrogsToLeft that
has one color parameter. This method should swap frogs in the array,
all those frogs of the parameter color at the low end of the array
(starting with position 0) and all the frogs of the other color at the
high end of the array. Call this method in myFirstMethod with the color
Blue to move the blue frogs to the lower end of the array.
Hint: You will need to add in an objectVisualization for swapping. Suppose the
parameter color is blue. Then start in
0 and check the color of the frog. If the color is not Blue, then call
the function your wrote isColorToTheRight with Blue. It either returns the
position of a blue frog (then swap it with the frog in slot 0) or it
-1 (meaning no more blue frogs to the right in the array). Repeat this
in a loop with position 1, 2, 3, etc. continuing
through the array swapping frogs if you need to.