# CompSci 94 - Classwork 17 - October 31, 2013 10 pts

Today's classwork goes along with Chapter 9. We will get more practice with lists.

The following work needs to be checked off when you have completed it.

Today you will sort animals by their height. You are given some animals and some tennisballs already placed in the world. The tennis balls are evenly spaced and represent positions where the animals will stand in sorted order based on their height. The animals are standing behind the tennis balls. In this classwork, you will place the tallest animal to be positioned at the rightmost tennis ball. Then you will declare this animal and tennis ball "taken" so they will not be considered again. Next you will place the tallest animal (of those not taken yet) at the rightmost tennis ball (of those not taken yet). This is really the second tallest animal. Etc. Finally the shortest animal is to be positioned at the leftmost tennis ball.

A player is considered "taken" if it has been placed in sorted position. We will use a boolean class variable isTaken to indicate if a player is taken or not. If a player is taken, then you should not compare it with other players.

Tennis balls will be considered taken if their color is NOT yellow. A tennis ball is taken if a player has been placed in sorted order at that tennis ball. To make a tennis ball taken, you must change its color to something other than yellow, such as red.

• First download the file sortingWork.a2w from the Compsci 94 web page.

• Some things have already been done for you.

• A list of six animals called "players" has been created. Each player has a new Boolean variable called "isTaken", initially set to false. (For example, look at Chicken's properties and you will see the variable isTaken.)
• A list of six tennis balls has been created called positions.
• myfirstmethod does two things, sets up the tennis balls' orientation and then sorts the players.
• The setup method turns all the tennis balls to face the camera.

NOTE: You may want to modify the setup method so the balls then turn their "back" to the camera, turn 180 degrees. Then "their" right is also your right when you look at them.

• A Boolean function called isPlayerTaken with parameter object returns true if the object's "isTaken" variable is set to true. Do not edit this function, but look at it.
• A method called setTakenTrue with parameter object sets the isTaken variable of the object to true. Do not edit it this function, but look at it.

• Write the following methods and functions. You should use "For all in order" or "For all together" where appropriate.

1. Write the function AnyBallToRight with parameter ball. This function returns true if "ball" is active (color is yellow) AND there is any active tennis ball to the right of the "ball". That is, if you are looking at the balls and they are all yellow, then this would be false if you passed in the rightmost ball, and it would be true if you passed in any other ball.

2. Write the function rightMostBall. This function has no parameters. It should return the rightmost tennis balls of those active balls. If there is no active tennis ball, then return the gazebo. This function should call AnyBallToRight.

3. Write method testRightMostBall to make sure the two functions you have written thus far are working correctly. This method should make the rightmost ball do something (such as go up and then go down) and then turn it a non-yellow color, then do the same to the next rightmost active ball, etc. until all the balls have been changed found and changed to another color.

4. Write the function isAnyBodyTaller with parameter player. Return true if player is active (meaning they have not been taken yet, their isTaken value is false) and there is another active player that is taller. Otherwise return false.

NOTE: A world function has been provided for you to use, isPlayerTaken returns True if the player has been taken.

5. Write the function tallestPlayer that returns the tallest player of all active players. If there are no active players, then return the gazebo.

NOTE: A world method "setTakenTrue" has been provided for you to set an animals isTaken value to True.

6. Write the method sort1 that sorts the players in increasing height. Move the tallest player to the rightmost tennisball, then the next tallest player to the next rightmost tennisball, etc.

Hint: Don't use "move to" to move a player to a tennis ball or some of them will move into the ground. Instead make the player face the tennis ball and then use "move" with the distance to the tennis ball.

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