CompSci 94 - Classwork 18 - November 5, 2013
Today's classwork goes along with Chapter 10, section 2.
You will be manipulating an array, shuffling and sorting the items in
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
- Write a world
method called PrintAllHeights that prints the
heights of all the people in the array. You should use loop (complicated
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
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
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
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
- 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
go up one meter and then go down one meter (it should be a different person
- Write a world method called Shuffle that swaps the object at
position in the array with a random object in another position.
You should use loop (complicated
version). This method has no parameters.
NOTE: You will need to add in an objectVisualization to
your world. When you swap two objects,
have one of the two objects move to the objectVisualization first
to clear a free space in the array (see the lecture notes).
- Write a new world method called test and call
Shuffle to test it
- 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
the index, not the object). Modify it to add an integer parameter called
startPos. The function should now return the index position of the
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
by their heights. Use the SelectionSort algorithm discussed in class. You
indexOfSmallestHeight inside a loop to find the position of the
and then swap it with the object that is in the position where the next
smallest belongs in sorted
- At the end of world.test, call world.SelectionSort
to test it out.
If you finish early, feel free to embelish your animations.