CompSci 4 - Classwork - Nov 8, 2005
5 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 the animals by height, placing the tallest animal
to the right (positioned at the rightmost tennis ball), then the next
tallest, etc. and finally the shortest
animal to the left (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.
- First download the file sortingWork.a2w from the CPS 4 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.
- A list of six tennis balls has been created called positions.
- my first method 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.
- A Boolean function called isPlayerTaken with parameter object
returns true if the object's "isTaken" variable is set to true.
- A method called setTakenTrue with parameter object sets the
isTaken variable of the object to true.
- Write the following methods and functions. You should use "For all in order"
or "For all together" where appropriate.
- 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.
- 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.
- Write method testRightMostBall to make sure the two functions
you have written thus far are working correctly.
This method should turn the rightmost active ball a non-yellow color,
then the next rightmost active ball, etc. until all the balls
have been changed to another color.
- Write the function isAnyBodyTaller with parameter player. Return
true if player is active and there is another active player that is
taller. Otherwise return false.
- Write the function tallestPlayer that returns the tallest player
of all active players. If there are no active players, then return the
gazebo.
- 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.
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.