CompSci 4 - Classwork 18 - November 8, 2011
10 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.

When writing a function, note what type of value it returns.

  1. 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.

  2. 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.

  3. In world.myfirstmethod, call the PrintAllHeights method and note which person is the shortest and which is the tallest.

  4. 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.

  5. In myfirstmethod, add code that calls objectWithTallestHeight and makes the person with the tallest height turn around once.

  6. 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).

  7. 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).

  8. 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.

  9. 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.

  10. 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).

  11. 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: 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).

  12. Write a new world method called test and call Shuffle to test it out.

  13. 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.

  14. Add code to world.test to test indexOfSmallestHeight.

  15. 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.
  16. At the end of world.test, call world.SelectionSort to test it out.

If you finish early, feel free to embelish your animations.