CompSci 4 - Classwork - Nov 7, 2006
20 pts
Today's classwork goes along with Chapter 10, section 2.
You will be manipulating an array, shuffling and sorting the items in
the array. This is the last Alice classwork and will most likely need
to be completed for homework. Note that is is worth more points than
other classworks.
When writing a function, note what type of value it returns.
- In a new Alice world, add 6 different people or animals to the world.
Then add in an ArrayVisualization and initialize it with the six people
from
your world.
- Write a world
method called PrintAllHeights that prints the
heights of all the people in the array. You should use loop (complicated
version)
and print the height of the item in the index position of the array.
That is, there should be just one print statement in the method.
- In world.myfirstmethod, call the PrintAllHeights method and
note which person is the shortest and which is the tallest.
- Write the world function objectWithTallestHeight that returns
the object in the array that has the tallest height. You should use a loop
(complicated version) to go through the array and determine which one is
the tallest.
NOTE: You will need to scan through the list of objects
trying to determine which one is the tallest. Assume the object at position
0 is the "tallest so far", then compare the "tallest so far" to each item
in
the array. If you find another object that is taller than the tallest, then
it should become your "tallest so far".
You can do this by creating a local variable that keeps track of the index
of the tallest object so far.
Initialize it to 0. Each time you find a "taller object" than the current
tallest object, rest tallest index to the taller object.
Return only after you have examined all the objects in the array and are
confident that you have found the tallest object.
- In myfirstmethod, add code that calls objectWithTallestHeight
and makes the person with the tallest height turn around once.
- In myfirstmethod, add code that makes one of the people who is not the
tallest person in the array to
resize to be twice as big. Follow that with code
that calls objectWithTallestHeight
and makes the person with the tallest height turn around once (it should be
a different person this time).
- Write the world function indexOfSmallestHeight that returns
the index position of the person in the array with the smallest height. (note this
returns
the index, not the object).
- Add code to the beginning of world.myfirstmethod to make the person
with the smallest height move up one meter and then move down one meter.
You should call indexOfSmallestHeight to determine where the
smallest
person is.
- In world.myfirstmethod, right after the code that resizes someone to
twice as big, add code to resize a different person who is not the
smallest, to resize to 1/2 their size.
- At the end of world.myfirstmethod, add code to make the smallest
person
go up one meter and then go down one meter (it should be a different person
this time).
- Write a world method called Shuffle that swaps the object at
each
position in the array with a random object in another position.
You should use loop (complicated
version). This method has no parameters.
NOTE: When you swap two objects, use a
"DoTogether" so they will both move to the other's position at the same
time.
- Write a new world method called test and call
Shuffle to test it
out.
- Modify the world function indexOfSmallestHeight that you
have already written.
That function returns
the index position of the person in the array with the smallest
height. (note this
returns
the index, not the object). Modify it to add an integer parameter called
startPos. The function should now return the index position of the
person
in the array with the smallest height, considering only those objects in
position startPos to the end of the array.
- Add code to world.test to test indexOfSmallestHeight.
- Write the method world.SelectionSort to sort the people in
the array
by their heights. Use the SelectionSort algorithm discussed in class. You
should call
indexOfSmallestHeight inside a loop to find the position of the
next
smallest,
and then swap it with the object that is in the position where the next
smallest belongs in sorted
order.
- At the end of world.test, call world.SelectionSort
to test it out.
If you finish early, feel free to embelish your animations.