3.3 Zoo Learning Activity

Under Construction

This page is currently being built. Please check back soon.

A platypus
3.3 Zoo Learning Activity — overview

Work through the user stories in order. Each story states the goal and acceptance criteria; its nested tasks retain the detailed implementation and verification steps.

1.Gather your resources
A Open the assignment resources
Set up the checkpoint
2.Copy your 3.2 ending code, renaming both the folder and the Visual Studio solution file
A Verify the required change or observation

Check the result against this instruction: Copy your 3.2 ending code, renaming both the folder and the Visual Studio solution file

Prevent construction of incomplete animal categories
Problem

Currently if you wanted to instantiate an Animal, Mammal, or Bird you could even though these things are only ideas and not actual things.

Solution

You will make the classes abstract so that none of them can be instantiated.

3.Review the feature design
A Identify the contracts and expected behavior

This focused design shows the members or flow relevant to this feature. Keep unchanged members from your preceding checkpoint; follow the tasks below for the order of intermediate edits.

Abstract categories and concrete species. abstract class Animal abstract class Mammal abstract class Bird Animal <|-- Mammal Animal <|-- Bird Mammal <|-- Dingo Mammal <|-- Platypus Bird <|-- Hummingbird
Abstract categories and concrete species
  • New
  • Changed
  • Removed
4.In the Animal class, make the class abstract by adding the abstract keyword in the class header
3.3.2 How to Define Abstract Classes
public abstract class Animal
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: In the Animal class, make the class abstract by adding the abstract keyword in the class header.

5.Make the Bird and Mammal classes abstract
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: Make the Bird and Mammal classes abstract.

6.Check Your Work (Como Zoo):
A Build your solution and ensure it compiles without errors or warnings
B In the MainWindow, try to instantiate an object using the Animal class
C Observe the error
Check Your Work: Abstract Error; compare object identities, field values, and execution position with your debugger.
Check Your Work: Abstract Error
Keep nursing behavior with mammals
Problem

Currently a hummingbird could feed their newborn milk, which goes against the laws of nature.

Solution

You will move the methods and related fields for feeding a newborn with milk out of the animal class and into the mammal class.

7.Review the feature design
A Identify the contracts and expected behavior

This focused design shows the members or flow relevant to this feature. Keep unchanged members from your preceding checkpoint; follow the tasks below for the order of intermediate edits.

Nursing belongs to Mammal. abstract class Animal { + Weight : double {get; protected set} + Nurse(mother : Animal) : double {virtual} + Reproduce() : Animal {virtual} } abstract class Mammal { - milkWeightPercentage : double = 0.005 {readOnly} - FeedNewborn(newborn : Animal) : void + Nurse(mother : Animal) : double {override} + Reproduce() : Animal {override} } Animal <|-- Mammal
Nursing belongs to Mammal
  • New
  • Changed
  • Removed
8.Cut the FeedNewborn method from the Animal class and paste it inside the Mammal class
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: Cut the FeedNewborn method from the Animal class and paste it inside the Mammal class.

9.Modify the Nurse method
Review this feature’s design and requirements
A Copy the Nurse method from the Animal class and paste it in the Mammal class
B Make the Nurse method in the Mammal class override and public
C In the Animal class, make the Nurse method virtual and public
D In the Animal class, delete the body of the Nurse method and instead return 0.0;
10.Cut the milkWeightPercentage from the Animal class and paste it in the Mammal class
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: Cut the milkWeightPercentage from the Animal class and paste it in the Mammal class.

11.In the Animal class, modify the Reproduce method to make it virtual and add an override Reproduce method in the Mammal class
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: In the Animal class, modify the Reproduce method to make it virtual and add an override Reproduce method in the Mammal class.

12.Modify the Reproduce method in the Mammal class
Review this feature’s design and requirements
A Assign a variable of type Animal to the call to the base's Reproduce method
B In the Reproduce method in the Animal class, cut the code that checks if the animal is a Platypus and feeds the newborn and paste it into the Reproduce method in the Mammal class
if (this.GetType() != typeof(Platypus))
{
    this.FeedNewborn(baby);
}
C Return the baby object
13.Modify the Animal class
3.3.3 How to Use the Protected Visibility Keyword Review this feature’s design and requirements
public double Weight
{
    get
    {
        return this.weight;
    }
    protected set
    {
        this.weight = value;
    }
}
A In the Weight property, define a setter and make it protected
B In the Animal and Mammal classes, change lines of code that reference the private weight field to reference the Weight property
14.Check Your Work (Como Zoo):
A Set a breakpoint in the Employee class in the DeliverAnimal on the line that calls Reproduce
B Start the application. Click New Como Zoo. Click the Birth dingo button
C Press F11. Ensure the debugger steps into the Reproduce method in the Mammal class
D Step into the call to base.Reproduce. Ensure the debugger steps into the Reproduce method in the Animal class
E Use F10 to step through the Reproduce method in the Animal class and back to the Reproduce method in the Mammal class
F Step through the Reproduce method in the Mammal class. Ensure that the debugger steps into the FeedNewborn/Nurse methods in the Mammal class
G Step through the remaining code and return to the DeliverAnimal method. Ensure a baby Dingo is returned
15.Check Your Work (San Diego):
A In the MainWindow, under the line of code that instantiates Harold the Hummingbird, paste the following code:
animal.Weight = 200;
B Observe the error. We cannot make a Hummingbird 200lbs from the MainWindow even when using a property. Whew
C Remove the line of code
Use category-specific newborn weight percentages
Problem

Birds and Mammals have two different baby weight percentages, but are currently the same. However, if we move the readonly fields for the weight gain percentage to the Bird and Mammal classes, the Animal class that uses the percent loses access to it.

Solution

You will move the baby weight percentage fields to their respective classes and define a property in the Animal class that can get and assign those values.

16.Review the feature design
A Identify the contracts and expected behavior

This focused design shows the members or flow relevant to this feature. Keep unchanged members from your preceding checkpoint; follow the tasks below for the order of intermediate edits.

Initialize the base baby-weight rule. abstract class Animal { - babyWeightPercentage : double # SetBabyWeightPercentage(value : double) : void } abstract class Mammal { - babyWeightPercentage : double = 0.1 {readOnly} } abstract class Bird { - babyWeightPercentage : double = 0.17 {readOnly} } Animal <|-- Mammal Animal <|-- Bird
Initialize the base baby-weight rule
  • New
  • Changed
  • Removed
17.In the Mammal class, define a field named babyWeightPercentage of type double with a readonly value of 0.1
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: In the Mammal class, define a field named babyWeightPercentage of type double with a readonly value of 0.1

18.In the Bird class, define a field named babyWeightPercentage of type double with a readonly value of 0.17
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: In the Bird class, define a field named babyWeightPercentage of type double with a readonly value of 0.17

19.In the Animal class, modify the babyWeightPercentage field so that it's assignable and add a method named SetBabyWeightPercentage that takes a double. In the SetBabyWeightPercentage method, assign the babyWeightPercentage field to the value passed in
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: In the Animal class, modify the babyWeightPercentage field so that it's assignable and add a method named SetBabyWeightPercentage that takes a double. In the SetBabyWeightPercentage method, assign the babyWeightPercentage field to the value passed in.

20.In the Mammal and Bird constructors, call the SetBabyWeightPercentage method and pass in the read only babyWeightPercentage fields
A Verify the required change or observation

Check the result against this instruction: In the Mammal and Bird constructors, call the SetBabyWeightPercentage method and pass in the read only babyWeightPercentage fields.

21.Check Your Work (Como Zoo):
A Set a breakpoint in the newComoZooButton_Click on the line that instantiates Dolly the Dingo
B Start the application. Click the New Como Zoo button
C Press F11. Ensure the debugger steps into the Dingo constructor
D Press F11. Ensure the debugger steps into the Mammal class
E Press F11. Ensure the debugger steps into the Animal class
F Step through the Animal constructor body and back to the Mammal constructor
G Step through the Mammal constructor body. Ensure that the SetBabyWeightPercentage method is called and that the babyWeightPercentage field is assigned to 0.1
Require an eating-percentage implementation
Problem

Currently if a class that inherits from animal doesn't have an EatWeightGainPercentage property, the property in the Animal class returns 0.0, but that is never going to be true for an animal.

Solution

You will make the EatWeightGainPercentage abstract instead of virtual to ensure that the descendant class has that property.

22.Review the feature design
A Identify the contracts and expected behavior

This focused design shows the members or flow relevant to this feature. Keep unchanged members from your preceding checkpoint; follow the tasks below for the order of intermediate edits.

Require an eating-percentage override. abstract class Animal { # {abstract} EatWeightGainPercentage : double {get} } abstract class Mammal { # EatWeightGainPercentage : double {get; override} } abstract class Bird { # EatWeightGainPercentage : double {get; override} } Animal <|-- Mammal Animal <|-- Bird
Require an eating-percentage override
  • New
  • Changed
  • Removed
23.In the Mammal and Bird classes, modify the EatWeightGainPercentage property so that they are protected
A Verify the required change or observation

Check the result against this instruction: In the Mammal and Bird classes, modify the EatWeightGainPercentage property so that they are protected.

24.Modify the EatWeightGainPercentage property in the Animal class
3.3.4 How to Use Abstract Properties Review this feature’s design and requirements
A Change the header from virtual to abstract
B Remove the code that returns a value and make the body a "skeleton"
protected abstract double EatWeightGainPercentage { get; }
25.Check Your Work (Como Zoo):
Review this feature’s design and requirements
A Set a breakpoint in the Eat method in the Animal class that assigns the weightGain
B Start the application. Click the New Como Zoo Button. Click the Darla, feed dingo button
C Press F11. Step through the Weight property and back to the Eat method
D Press F11. Ensure that the debugger steps into the EatWeightGainPercentage in the Mammal class
E Step through the property and back to the Eat method
F Press F11. Ensure that the weightGain is 0.318
26.Check Your Work (San Diego Zoo):
A In the Bird class, delete the property for EatWeightGainPercentage
B Build your solution and observe the error
Check Your Work: Abstract Error; compare object identities, field values, and execution position with your debugger.
Check Your Work: Abstract Error
C Replace the EatWeightGainPercentage in the Bird class
D Build your solution and ensure the error is resolved
Move each animal using its own rule
Problem

Currently all animals lose weight when they move based on the same weight loss percentage, but animals move in different ways and lose weight in different ways.

Solution

You will make the Move method abstract and ensure that all animals lose weight when they move based on what they are.

27.Review the feature design
A Identify the contracts and expected behavior

This focused design shows the members or flow relevant to this feature. Keep unchanged members from your preceding checkpoint; follow the tasks below for the order of intermediate edits.

Species-specific movement rules. abstract class Animal { + {abstract} Move() : void } abstract class Mammal { - paceWeightLossPercentage : double = 0.03 {readOnly} + Move() : void {override} } abstract class Bird { - flyWeightLossPercentage : double = 0.06 {readOnly} + Move() : void {override} } class Platypus { - swimWeightLossPercentage : double = 0.02 {readOnly} + Move() : void {override} } Animal <|-- Mammal Animal <|-- Bird Mammal <|-- Platypus
Species-specific movement rules
  • New
  • Changed
  • Removed
Movement loss
Implementation Percentage Base Move call
Mammal.Move 0.03 None: Animal.Move is abstract
Platypus.Move 0.02 None: do not also apply mammal pacing
Bird.Move 0.06 None: Animal.Move is abstract
28.Remove and define fields using this story’s design reference
3.3.5 How to Define and Use Abstract Methods Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: Remove and define fields using this story’s design reference.

29.To make the Move method abstract, remove the method body and replace the method header with this:
public abstract void Move();
A Verify the required change or observation

Check the result against this instruction: To make the Move method abstract, remove the method body and replace the method header with this:

30.Modify the Mammal class
A Assign the paceWeightLossPercentage to 0.03
B In the Move method, reduce the animal's weight by the animal's weight multiplied by the paceWeightLossPercentage
31.Modify the Platypus class
A Assign the swimWeightLossPercentage to 0.02
B In the Move method, reduce the animal's weight by the animal's weight multiplied by the swimWeightLossPercentage

We do not call base in this method because the Mammal class does not have anything to add to the Platypus swimming. Ensure there is no call to base in this method.

32.Modify the Bird class
A Assign the flyWeightLossPercentage to 0.06
B In the Move method, reduce the animal's weight by the animal's weight multiplied by the flyWeightLossPercentage
33.Check Your Work (Como Zoo):
A Set a breakpoint in the Reproduce method in the Animal class on the line that calls Move
B Start the application. Click the New Como Zoo button. Click the Birth dingo button. Ensure that Weight is 31.417
C Press F11. Ensure the debugger steps into the Move method in the Mammal class
D Step through the Move method and back to the Animal class. Ensure that Weight is now 30.474
34.Check Your Work (San Diego Zoo):
A Set a breakpoint in the Reproduce method in the Animal class on the line that calls Move
B Start the application. Click the New San Diego Zoo button. Click the Birth platypus button

Ensure that Weight is 2.910

C Press F11. Ensure the debugger steps into the Move method in the Platypus class
D Step through the Move method and back to the Animal class. Ensure that Weight is now 2.852
Identify animals and guests in the debugger
Problem

When we debug and look at what an object is, a quick glace won't tell you much.

Solution

We will add an override the ToString method to show us information we want in the Autos window.

35.Review the feature design
A Identify the contracts and expected behavior

This focused design shows the members or flow relevant to this feature. Keep unchanged members from your preceding checkpoint; follow the tasks below for the order of intermediate edits.

Useful debugger descriptions. class Animal { # HappinessLevel : int {get; set} + ToString() : string {override} } class Guest { + ToString() : string {override} }
Useful debugger descriptions
  • New
  • Changed
  • Removed
36.Ensure that only the Animal class and its descendants can use the HappinessLevel property by making it protected
A Verify the required change or observation

Check the result against this instruction: Ensure that only the Animal class and its descendants can use the HappinessLevel property by making it protected.

37.Observe the Value description for objects
A Set a breakpoint at the end of the newComoZooButton_Click
B Start the application. Click the 'New Como Zoo' button
C In the Autos window, note that the Value column doesn't show us very helpful information about what an object is without having to expand it
Snip - Autos Values; compare object identities, field values, and execution position with your debugger.
Snip - Autos Values
38.Add overrides to both the Guest and Animal classes
3.3.6 How to Use ToString Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: Add overrides to both the Guest and Animal classes.

39.In the ToString method in the Animal class, paste the following code:
return this.name + " " + this.GetType().Name + " " + this.HappinessLevel + " " + this.Weight;
A Verify the required change or observation

Check the result against this instruction: In the ToString method in the Animal class, paste the following code:

40.In the ToString method in the Guest class, paste the following code:
return this.name + " " + this.age + " " + this.MoneyBalance;
A Verify the required change or observation

Check the result against this instruction: In the ToString method in the Guest class, paste the following code:

41.Check Your Work (ComoZoo):
A Set a breakpoint at the end of the newComoZooButton_Click
B Start the application. Click the New ComoZoo button
C Observe that now the Value column is more readable
Snip - Autos Values; compare object identities, field values, and execution position with your debugger.
Snip - Autos Values
Submit your work
42.Ensure that all code is StyleCop compliant
A Verify the required change or observation

Check the result against this instruction: Ensure that all code is StyleCop compliant.

43.Zip the solution and submit to the Feedback System
A Verify the required change or observation

Check the result against this instruction: Zip the solution and submit to the Feedback System

44.Submit the Feedback System url to the correct assignment in Canvas
A Verify the required change or observation

Check the result against this instruction: Submit the Feedback System url to the correct assignment in Canvas.

Completion checklist
45.Review the required evidence
A Check every acceptance criterion

Return to each user story and confirm that you can demonstrate its acceptance criteria. Use the task checkboxes to track completion; a checked box is not a substitute for testing or inspecting the required evidence.

Prevent construction of incomplete animal categories Keep nursing behavior with mammals Use category-specific newborn weight percentages Require an eating-percentage implementation Move each animal using its own rule Identify animals and guests in the debugger
B Build and inspect the final solution

Remove temporary lines used only to demonstrate compile-time errors. Build without errors or warnings, check StyleCop compliance, and rerun the required scenario paths.

C Follow this activity’s submission instructions

Use the submission or attendance instructions in this page. For a Feedback System submission, upload the requested ZIP, review and resolve the feedback, and submit the resulting URL to the matching Canvas activity. For a Canvas quiz, enter only the requested answers; for an oral final, complete the scheduling and attendance steps.