10-152-312 - Object-Oriented Programming 2

3.4 Zoo Assignment

Deserializing the animals' move timer

  1. Create a private method in Animal called CreateTimers and move the timer instantiation to it from the constructor
  2. Create a private method called OnDeserialized in Animal
    1. Pass in a StreamingContext called context
    2. Add the OnDeserialized attribute
    3. Call CreateTimers() from it
  3. Build and run the application
    1. Create some guests and animals
    2. Save (or autosave) the zoo
    3. Close the application
    4. Restart the application
    5. Load the saved file (if it is not already)
    6. Ensure that animals move in their cages

Refactoring the AnimalCageWindow to be generic

  1. Create a new class library called DrawKit
  2. In the DrawKit package, create the IDrawable interface as shown in the diagram above
  3. Implement the IDrawable interface in Animal
    1. For the ResourceKey, return the name of the animal's Type and whether it is a baby or an adult (e.g. "DingoBaby")
    2. For the DrawRatio, return 0.07 if the animal is a baby; otherwise, return 0.2
  4. Rename the AnimalCageWindow to DrawWindow
  5. Rename the animalGrid to drawableGrid
  6. Instead of an IEnumerable of Animal, pass an IEnumerable of IDrawable called drawables into the window's constructor
  7. Replace the animals IEnumerable with an IEnumerable of IDrawable called drawables
  8. Rename DrawAnimal to DrawItem
    1. Pass in an IDrawable called drawable instead of an Animal
    2. Rename animalWidth to drawableWidth and animalHeight to drawableHeight
    3. Instead of creating a resource key in the method, simply get the drawable's ResourceKey
    4. Use the drawable's DrawRatio when setting the drawableWidth
  9. Rename DrawAllAnimals to DrawAllItems
    1. Instead of looping through the animals, loop through the drawables and draw each one
  10. Build and run the application
    1. Add references and using directives as necessary
    2. Animals should still move and draw as they did before

Adding guests to the DrawWindow

  1. Unzip the starting zip file and add the code for the Guest canvas in the .txt file to App.xaml
  2. Implement the IDrawable interface in Guest
    1. For the ResourceKey, return "Guest"
    2. For the DrawRatio, return 0.45
    3. Set both XPosition and YPosition to 0 in the constructor
  3. Change the DrawWindow's IEnumerable of IDrawable to a List
  4. Add a method to the DrawWindow called AddDrawnItem
    1. It takes an IDrawable as a parameter and adds it to the list of drawables
  5. Add a Dictionary called drawWindows to the MainWindow
    1. Its key is of type DrawWindow
    2. Its value is of type Type
    3. Initialize it to a new Dictionary where it is defined
    4. Every time a new DrawWindow is shown, add it to the drawWindows Dictionary
  6. Add a button to the MainWindow called drawGuestButton
    1. The user must choose a guest to draw and an animal in whose cage the guest will be drawn
    2. If both a guest and animal were chosen:
      1. Loop through the drawWindows Dictionary
      2. If the Value matches the animal's Type, add the guest to the Key as a drawn item
  7. Build and run the application
    1. Show a cage window
    2. Choose a guest and an animal and click the drawGuestButton
    3. The guest should appear in the cage window

Writing the move behavior that mimics flying squirrels

When an animal's behavior is set to the ClimbFallBehavior, it should move horizontally across the bottom of the cage (or wherever it might randomly start). When it hits a vertical wall, it should climb it by moving vertically. When it hits the ceiling of the cage, it should fall diagonally until it hits the bottom of the cage, at which point it should continue moving horizontally in the same direction until it hits a vertical wall and the process begins again.

  1. Create an enumeration called ClimbFallProcess, which has three items
    1. Scurrying
    2. Climbing
    3. Falling
  2. Create a class called ClimbFallBehavior that implements IMoveBehavior
    1. Add a field of type ClimbFallProcess called process
    2. In the Move method:
      1. Check if the current process is Scurrying
        1. If it is, move the animal horizontally
        2. If the animal's next move will make it hit a vertical wall, switch to the next process
      2. Otherwise, check if the current process is Climbing
        1. If it is, make sure the animal is moving up and move it vertically
        2. If the animal's next move will make it hit the ceiling, make it move down and in the opposite horizontal direction and switch to the next process
      3. Otherwise, the current process is Falling
        1. Make the animal move diagonally (it should fall twice as fast as it moves horizontally)
        2. If the animal's next move will make it hit the floor, switch to the next process

Randomizing the climbing behavior

  1. Add a Random to the ClimbFallBehavior and set its seed to DateTime.Now.Millisecond
  2. Add a field of type int called climbHeight
  3. Just before the animal's process is switched to climbing:
    1. Set the climb height to a random value between 15 percent and 85 percent of the Animal's PositionMax
    2. Some conversion between ints and doubles might be necessary
  4. When the animal is climbing, check to see if the animal will hit the randomly set climb height instead of the cage ceiling

3.4 Zoo Assignment: Package Diagram

Create/revise the classes as indicated by the following UML package (class) diagram.

PNG image of 3.4 Zoo UML detailed package (class)

Notes

3.4 Zoo Assignment: Class Diagram

Create/modify the classes as specified in the following UML class diagram.

PNG image of 3.4 Zoo UML class diagram

Grading and Submission

  1. Build/compile your program
  2. Debug/test your program against the following rubric:
  3. PNG image of 3.4 Zoo rubric
  4. Make your code StyleCop-compliant
  5. Close your Visual Studio solution
  6. Compress your Visual Studio solution to a zip file
  7. Submit the zip file via Blackboard