Object-Oriented Programming 1: 7.2 Zoo Lab

  1. Define and use an interface for things that hatch
  2. Currently, the only way for animals to reproduce involves giving birth to live young, but birds lay eggs and then hatch those eggs. In this task, you will define an interface for things that are hatchable, override the reproduce method for birds, and add methods for laying and hatching eggs. You will check your work by birthing a pregnant hummingbird and ensuring that the hummingbird lays an egg and hatches it and that the hatched baby is added to the zoo's list of animals.

    1. Define an IHatchable interface as shown in the class diagram below.
    2. 7.2 Zoo - class diagram for the ihatchable interface
    3. Update the Bird class to implement the IHatchable interface, as shown in the class diagram above.
    4. Check your work: Build the solution and ensure that the code compiles without errors or warnings.
    5. Define the HatchEgg and LayEgg methods in the Bird class, as shown in the class diagram above.
    6. Override the animal's Reproduce method in the Bird class, as shown in the class diagram above.
    7. Within the Bird class, call methods as specified in the following sequence diagram.
    8. Sequence Diagram - Bird lay egg
      1. In the HatchEgg method, call the passed-in egg's Hatch method.
      2. In the LayEgg method, return the result of calling base on the Reproduce method, as shown in the code below.
      3. private IReproducer LayEgg()
        {
            // Return a baby (in egg form)
            return base.Reproduce();
        }
      4. In the bird's overridden Reproduce method, call the LayEgg method and store the result in a variable called baby. Then, use type-checking to check if the baby implements the IHatchable interface; if so, call the HatchEgg method, as shown in the code below.
      5. // Lay an egg
        IReproducer baby = this.LayEgg();
        
        // If the baby is hatchable...
        if (baby is IHatchable)
        {
            // Hatch the baby out of its egg
            this.HatchEgg(baby as IHatchable);
        }
        
        // Return the hatched baby
        return baby;
    9. Ensure that all code is StyleCop compliant.
    10. Check your work:
      1. Change the newZooButton_Click event handler to instantiate a pregnant female hummingbird.
      2. In the birthDingoButton_Click, change the call to the FindAnimal method to pass in a type of Hummingbird.
      3. Set a breakpoint in the employee's DeliverAnimal method on the line that calls the reproducer's Reproduce method.
      4. Start the application. Click the New zoo button, then click the Birth dingo button.
      5. Step through the method calls, and ensure that the bird overrides the animal's Reproduce method, lays an egg, and then hatches the egg. Also ensure that the hatched baby is added to the zoo's list of animals.
      6. Remove the pregnant hummingbird from the newZooButton_Click event handler code.
      7. In the birthDingoButton_Click, change the call to the FindAnimal method to pass in a type of Dingo.
  3. Have the playtpus implement the IHatchable interface
  4. Even though platypuses are mammals, they also lay eggs rather than giving birth to live young. In this task, you will have the Platypus class implement the IHatchable interface so that platypuses can lay eggs. You will check your work by birthing a pregnant platypus and ensuring that the platypus lays an egg and hatches it and that the hatched baby is added to the zoo's list of animals.

    1. Update the Platypus class to implement the IHatchable interface, as shown in the class diagram below.
    2. Class Diagram - Platypus as IHatchable
    3. Check your work: Build the solution and ensure that the code compiles without errors or warnings.
    4. Define the HatchEgg and LayEgg methods in the Platypus class, as shown in the class diagram above.
    5. Override the mammal's Reproduce method in the Platypus class, as shown in the class diagram above.
    6. Move the baby's movement code from the animal's Reproduce method to the Reproduce method of the Mammal class. Place the code inside of the existing if statement that checks if the animal is not a platypus.
    7. Within the Platypus class, call methods as specified in the following sequence diagram. The sequence of method calls is the same as in the Bird class. Be sure to use type-checking and casting when calling the HatchEgg method (as in the Bird class).
    8. Sequence Diagram - Platypus lay egg
    9. Ensure that all code is StyleCop compliant.
    10. Check your work:
      1. Set a breakpoint in the employee's DeliverAnimal method on the line that calls the reproducer's Reproduce method.
      2. In the birthDingoButton_Click, change the call to the FindAnimal method to pass in a type of Platypus.
      3. Start the application. Click the New zoo button, then click the Birth dingo button.
      4. Step through the method calls and ensure that the platypus overrides the mammal's Reproduce method and lays and hatches an egg.
      5. Stop the application. In the birthDingoButton_Click, change the call to the FindAnimal method to pass in a type of Dingo.
      6. Start the application. Click the New zoo button, then click the Birth dingo button.
      7. Step through the method calls and ensure that the dingo calls the mammal's Reproduce method and feeds and moves the newborn, as shown in the sequence diagram below.
      8. Sequence Diagram - Dingo give birth
  5. Birth a WuvLuv
  6. The zoo's store now sells a toy called a WuvLuv, which can give birth by laying an egg and hatching it but is not an animal. In this task, you will define a class to represent WuvLuvs, have that class implement the necessary interfaces to reproduce, and then have the WuvLuv lay an egg and hatch it. You will check your work by instantiating a WuvLuv, having it give birth, and ensuring that it lays an egg and hatches the egg without adding the baby to the zoo's list of animals.

    WuvLuv commercial (0:30)

    1. Add a class to represent a WuvLuv.
      1. Create a Toys folder in the ZooScenario project.
      2. Define the WuvLuv class in the Toys folder as shown in the class diagram below. Have the class implement the IHatchable and IReproducer interfaces.
      3. Class Diagram - WuvLuv gives birth
      4. Set the readonly price field to 19.99m at the field definition.
      5. In the Weight property, return the hard-coded value of 2.1. The other properties simply return the value of the underlying field.
      6. In the MakePregnant method, set the isPregnant field to true.
      7. In the LayEgg method, simply return a new WuvLuv of the same color as the parent (i.e. the current object).
      8. Call the methods in the WuvLuv's Reproduce and HatchEgg methods as specified in the sequence diagram below. The sequence is similar to that in the bird's and platypus's Reproduce methods.
      9. 7.2 Zoo - sequence diagram for wuv luv reproduction
    2. Check your work: Build the solution and ensure that the code compiles without errors or warnings.
    3. Create the Birth WuvLuv button as shown in the wireframe below. Name the button birthWuvLuvButton. Give the button click event.
    4. Wireframe - Birth WuvLuv button
    5. Write the following code within the birthWuvLuvButton_Click event handler.
      1. Instantiate a WuvLuv and store it in a variable. Pass in "Purple" when calling the WuvLuv's constructor. Make the WuvLuv pregnant.
      2. Birth the pregnant WuvLuv by calling the zoo's BirthAnimal method and passing the WuvLuv in.
    6. Ensure that all code is StyleCop compliant.
    7. Check your work:
      1. Set a breakpoint in the employee's DeliverAnimal method on the line that calls the reproducer's Reproduce method.
      2. Start the application. Click the New zoo button, then click the Birth WuvLuv button.
      3. Ensure that the reproducer is a WuvLuv. Then step through the method calls and ensure that the WuvLuv lays an egg and hatches it. Also ensure that the hatched WuvLuv is not added to the zoo's list of animals (because it is not an animal, but a toy).
  7. Submit a zipped Visual Studio solution after completing the following:
    1. Ensure that all code is StyleCop compliant.
    2. Browse to the project folder and add it to a newly created zip archive.
    3. Submit the zipped project folder to the correct assignment in Blackboard.