2.1 Zoo Learning Activity

Under Construction

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

A platypus
2.1 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 1.5 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 1.5 ending code, renaming both the folder and the Visual Studio solution file

3.Build your solution. Ensure it compiles without warnings or errors
A Verify the required change or observation

Check the result against this instruction: Build your solution. Ensure it compiles without warnings or errors.

Feed an animal with a food object
Problem

As it is currently, the Animal is "eating" an abstract amount of food.

Solution

You will be passing a food object that can be interacted with instead.

4.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.

Food objects and feeding. class Food { + Weight : double } class Guest { + FeedAnimal(animal : Animal, animalSnackMachine : VendingMachine) : void } class Animal { + Eat(food : Food) : void + StashInPouch(food : Food) : void } Guest ..> Food : creates Guest --> Animal : passes food Animal ..> Food : reads Weight
Food objects and feeding
  • New
  • Changed
  • Removed
Feeding with a shared Food reference. participant MainWindow participant Guest participant VendingMachine participant Animal MainWindow -> Guest : FeedAnimal(animal, animalSnackMachine) activate Guest Guest -> Guest : GetMoneyBalance() Guest -> Animal : GetPortionSize() Animal --> Guest : maxFoodWeight Guest -> VendingMachine : DetermineFoodCost(maxFoodWeight) VendingMachine --> Guest : foodCost alt enough money Guest -> Guest : RemoveMoney(foodCost) Guest -> VendingMachine : SellFood(foodPayment) VendingMachine --> Guest : foodWeight create Food Guest -> Food : new Food() Guest -> Food : Weight = foodWeight Guest -> Animal : Eat(food) end deactivate Guest
Feeding with a shared Food reference
  • New
  • Changed
  • Removed
Fresh feeding checkpoints
Value Como: Dolly San Diego: Patti
Food cost 0.53m 0.08m
Food.Weight 0.3975 0.096
Adult weight gain 0.318 0.0768
Adult weight after feeding Approximately 35.618 3.3468
5.Define a new Food class
Review this feature’s design and requirements
A Define a field named Weight of type double
B Build your solution. Ensure that it compiles without warnings or errors
6.Refactor the code so that the Guest class handles feeding instead of the MainWindow
A Define the FeedAnimal method in the Guest class
2.1.4 How to Define an Object Parameter 2.1.5 How to Define Multiple Parameters Review this feature’s design and requirements
1 Have the FeedAnimal method have a return type of void and take two parameters: one of type Animal named animal and one of type VendingMachine named animalSnackMachine
/// <summary>
/// Feeds the specified animal.
/// </summary>
/// <param name="animal">The animal to be fed.</param>
/// <param name="animalSnackMachine">The machine that sells food.</param>
/// <summary>
/// Feeds the specified animal.
/// </summary>
/// <param name="animal">The animal to be fed.</param>
/// <param name="animalSnackMachine">The machine that sells food.</param>
public void FeedAnimal(Animal animal, VendingMachine animalSnackMachine)
{
}
Review this feature’s design and requirements
B In the darlaFeedDingoButton_Click event handler, cut all of the lines of code from within the event handler and paste them within the FeedAnimal method in the Guest class

This will cause errors. Work through the rest of this step to resolve them.

C In the darlaFeedDingoButton_Click event handler, call the Visitor's FeedAnimal method and pass in the FeaturedAnimal object and the AnimalSnackMachine object as arguments
this.ComoZoo.Visitor.FeedAnimal(this.ComoZoo.FeaturedAnimal, this.ComoZoo.AnimalSnackMachine);
Review this feature’s design and requirements
7.Now that the code for feeding an animal is in the Guest class, it needs to be changed to fix the functionality
Review this feature’s design and requirements
A Change the visitorMoneyBalance variable to moneyBalance and assign it to this.GetMoneyBalance();
decimal moneyBalance = this.GetMoneyBalance();

This functionality is the same as it was in the MainWindow, but is structured to target the current (Guest) class.

B Change the assigning of the maxFoodWeight variable to the call to the animal parameter's GetPortionSize method
2.1.6 How to Pass an Object as an Argument Review this feature’s design and requirements
double maxFoodWeight = animal.GetPortionSize();

This functionality is the same as it was in the MainWindow, but is structured to target the Animal object passed in.

C Change the assigning of the foodCost variable to the call to the animalSnackMachine's DetermineFoodCost method
D In the logic check, change the visitorMoneyBalance to moneyBalance
E Assign foodPayment from the current Guest instance's RemoveMoney(foodCost) call
F Change the assigning of the foodWeight to the call to the animalSnackMachine's SellFood method
G Change the call to the FeaturedAnimal's Eat method to a call to the animal's Eat method
8.Instantiate a new food item for the Animal to eat
2.1.8 How to Instantiate an Object Using a Variable
A In the Guest class's FeedAnimal method, below the line of code that assigns foodWeight, instantiate a new instance of the Food class named food
B Assign the food variable's weight field to the foodWeight variable
C In the call to the animal's Eat method, pass in the food object as an argument instead of the foodWeight variable
D In the daveFeedPlatypusButton_Click event handler, delete the method body and model the FeedAnimal call after the darlaFeedDingo FeedAnimal call
9.Change the Animal's Eat method so it uses the food object
A In the Animal class in the Eat method header, change the parameter to take a Food type instead of a double. Name the parameter food
B Wherever the foodWeight variable had been used, change it to the food variable's Weight field
C Modify the StashInPouch method header by adding a parameter of type Food named food
D Pass the food variable into the StashInPouch method call
10.Check Your Work (ComoZoo):
A Set a breakpoint in the darlaFeedDingoButton_Click on the line that calls the FeedAnimal method
B Start the application. Click the New Como Zoo button. Click the Darla, feed dingo button
C Step into the FeedAnimal method. In the Autos window, expand the animal object. Ensure that the Name of the animal is "Dolly". Expand the animalSnackMachine object. Ensure that the FoodPricePerPound is 0.75
D Step over the call to GetMoneyBalance. Ensure that moneyBalance is 5.25
E Step over the call to GetPortionSize. Ensure that the maxFoodWeight is 0.706
F Step over the call to DetermineFoodCost. Ensure that foodCost is 0.53
G Step over the call to RemoveMoney. Ensure that the foodPayment is 0.53
H Step over the call to SellFood. Ensure that the foodWeight is 0.3975
I Step over the instantiation of the food object
J Step over the line of code that assigns the food's Weight. Expand the food object in the Autos window. Ensure that the Weight field is 0.3975
K Step into the Eat method
L Step over the line of code that assigns the weightGain variable. Ensure that it is 0.318
M Step over the line of code that increases the mother's Weight. Ensure that Weight is 35.6179999
N Step over the line of code that increases the baby's Weight. Ensure that this.Baby.Weight is 3.56975
O Step through the rest of the method. Ensure the debugger steps into the Bark method
11.Check Your Work (SanDiego)
A Set a breakpoint in the Animal class on the line that checks if the Animal is a Type of Dingo
B Start the application. Click the New San Diego Zoo button. Click the Dave, feed platypus button
C In the autos window, expand this. Ensure that the animal is "Patti" and that the Weight is 3.3468. Ensure that this.Baby is null
D Press F11. Ensure the code steps into the StashInPouch method
E In the autos window, ensure that the food's Weight is 0.096
12.Ensure that your code is StyleCop compliant
A Verify the required change or observation

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

Route a birth request to the birthing room
Problem

The zoo would like the ability to support the birthing of animals.

Solution

You will set up a sequence using a button click and method calls that will allow the zoo to enable the birthing of an animal.

13.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.

Birth request contracts. class Zoo { + BirthAnimal() : void } class BirthingRoom { + BirthAnimal(mother : Animal) : void } class Employee { + DeliverAnimal(mother : Animal) : void } Zoo --> BirthingRoom : BirthArea BirthingRoom --> Employee : Doctor Employee --> Animal : mother
Birth request contracts
  • New
  • Changed
  • Removed
14.Add a new button to birth an animal in the zoo
A In the MainWindow.xaml, in the XML editor, under the line that creates the 'darlaFeedDingoBut add the following line of code
<Button x:Name="birthDingoButton" Content="Birth dingo" Click="birthDingoButton_Click" />
B In the MainWindow.xaml, in the XML editor, under the line that creates 'daveFeedPlatypusButto add the following line of code
<Button x:Name="birthPlatypusButton" Content="Birth platypus" Click="birthPlatypusButton_Click" />
C Ensure the window now looks like the following:
Snip: XAML window; compare object identities, field values, and execution position with your debugger.
Snip: XAML window
D Double click the Birth dingo button and Birth platypus button to create the event handlers in the MainWindow
15.In the Zoo class, define a method named BirthAnimal with a return type of void that has no parameters
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: In the Zoo class, define a method named BirthAnimal with a return type of void that has no parameters.

16.In the BirthingRoom class, define a method named BirthAnimal with a return type of void that has a parameter of type Animal named mother
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: In the BirthingRoom class, define a method named BirthAnimal with a return type of void that has a parameter of type Animal named mother.

17.In the Employee class, define a method named DeliverAnimal with a return type of void that has a parameter of type Animal named mother
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: In the Employee class, define a method named DeliverAnimal with a return type of void that has a parameter of type Animal named mother.

18.In the MainWindow, in the birthDingoButton_Click and birthPlatypusButton_Click event handlers, call the BirthAnimal method on the respective Zoo objects
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: In the MainWindow, in the birthDingoButton_Click and birthPlatypusButton_Click event handlers, call the BirthAnimal method on the respective Zoo objects.

19.Modify the BirthAnimal method in the Zoo class
A Call the BirthAnimal method on the BirthArea and pass in the FeaturedAnimal object
B Wrap the method call in a logic check that checks if the FeaturedAnimal object is pregnant using a call to GetIsPregnant
20.Modify BirthAnimal method in the BirthingRoom class
A In the method body, call the DeliverAnimal method on the Employee and pass in the mother parameter
B Increase the Temperature of the BirthingRoom by 0.5
C Wrap the method body in a logic check that checks if the mother object exists AND that the mother is pregnant
D Remove the Mother field in the BirthingRoom class
E In the MainWindow, remove the lines of code that assign the BirthArea's Mother field to the FeaturedAnimal
21.Check Your Work (ComoZoo):
A Set a breakpoint in the birthDingoButton_Click on the line that calls the BirthAnimal method
B Start the application. Click the New ComoZoo button. Click the Birth dingo button
C Press F11. Step over the logic check and ensure it passes
D Step into the BirthArea's BirthAnimal method. Ensure that the mother parameter is assigned to the Dolly the Dingo object
E Step over the logic check. Ensure that it passes
F Step into the Doctor's DeliverAnimal method. Ensure that the mother parameter is assigned to the Dolly the Dingo object
G Step over the line of code that increases the Temperature. Ensure that it changes from 77 to 77.5
22.Check Your Work (SanDiego):
A Set a breakpoint in the birthPlatypusButton_Click on the line that calls the BirthAnimal method
B Start the application. Click the New San Diego Zoo button. Click the Birth platypus button
C Step into the zoo's BirthAnimal method
D Step over the logic check. Ensure that logic check fails and that the call to BirthAnimal is never hit

Since the MakePregnant method is never called on the FeaturedAnimal, the baby will be null.

Deliver and feed a newborn
Problem

The zoo now has support for birthing an animal, but the animal itself doesn't have the ability to birth their baby.

Solution

You will create a sequence that has the Animal birth a baby, move both the mother and the baby, and then feed the newborn baby.

23.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.

Newborn care contracts. class Animal { + Baby : Animal + Weight : double + Reproduce() : void + Move() : void + FeedNewborn(newborn : Animal) : void + Nurse(mother : Animal) : double + Eat(food : Food) : void } Animal --> Animal : Baby
Newborn care contracts
  • New
  • Changed
  • Removed
24.In the Animal class, define methods using this story’s design reference
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: In the Animal class, define methods using this story’s design reference.

25.In the Employee class, in the DeliverAnimal method, call the Reproduce method on the mother object
Review this feature’s design and requirements
A Verify the required change or observation

Check the result against this instruction: In the Employee class, in the DeliverAnimal method, call the Reproduce method on the mother object.

26.Modify the Reproduce method
2.1.9 How to Assign Null and Use Local References
A In the method body, define a variable of type double named weightLossAmount and assign it to 110% of the Baby's Weight
B Subtract the weightLossAmount from the current instance's Weight
C Call the Move method on the current instance
D Call the Move method on the Baby
E Call FeedNewborn on the current instance and pass in the Baby object as an argument
F Wrap the call to FeedNewborn in a logic check that determines if the current instance's Type is not a Platypus
G Assign the Baby field to null
27.Modify the FeedNewborn method
2.1.10 How to Use an Inline Method Return Review this feature’s design and requirements
A Subtract the return of the call to Nurse from the FeaturedAnimal's Weight field. Pass in the current instance as the argument

This will give you an error. We will fix it in the next steps.

this.Weight -= newborn.Nurse(this);
28.Modify the Move method
A In the method body, define a variable of type double named weightLossAmount and assign it 1% of the current instance's Weight
B Subtract the weightLossAmount from the current instance's Weight
29.Modify the Nurse method
A In the method body, define a variable of type double named milkWeight and assign it to .5% of the mother object's Weight
double milkWeight = mother.Weight * 0.005;
B Define a variable of type Food named milk and assign it to an instance of the Food class
C Assign the milk's Weight field to the milkWeight variable
D Call the Eat method on the current (baby) object and pass in the milk object as an argument
E Return the milkWeight variable
30.Check Your Work (Como Zoo):
A Set a breakpoint in the Employee class on the line that calls Reproduce
B Start the application. Click the New Como Zoo button. Click the Birth Dingo button
C Step into the Reproduce method
D Step over the line of code that assigns the weightLossAmount. Ensure that it is assigned the value 3.883. Ensure that this.Weight is 35.3
E Step over the line that subtracts the weightLossAmount from the Weight field. Ensure that the Weight field is now 31.42
F Step into the call to the mother's Move method. Ensure that the Weight field is 31.416999
G Step over the line that assigns the weightLossAmount. Ensure that it is 0.31417
H Step over the line that subtracts the weightLossAmount from the Weight field. Ensure that it it is now 31.10282999
I Step into the call to the Baby's Move method. Ensure that the Weight field is 3.53
J Step over the line that assigns the weightLossAmount. Ensure that it is 0.0353
K Step over the line that subtracts the weightLossAmount from the Weight field. Ensure that it is now 3.4947
L Step over the logic check. Ensure that it passes
M Step into the FeedNewborn method
N Step into the call to Nurse. Expand the mother parameter in the Autos window or hover over it to ensure that it is an instance of Dolly the Dingo
O Step over the line of code that assigns the milkWeight variable. Ensure that it is assigned the value 0.15551415
P Step over the line of code that assigns the milk variable to an new instance of the Food class
Q Step over the line of code that assigns the milk object's Weight field to the milkWeight variable. Ensure that the milk object's Weight changes to 0.15551415
R Step into the Eat method. Expand 'this' to ensure that it is the instance of the baby
S Step over the line of code that assigns the weightGain variable. Ensure that it is assigned to the value 0.12441131999
T Step over the conditional that checks if the Baby field is null. Ensure that it fails
U Step over the conditional that checks if the current instance has a Type field of Dingo. Ensure it passes
V Step over the line of code that calls Bark. Ensure the baby's HappinessLevel increases by 2
W Step back to the Nurse method. Step over the return statement and back to the FeedNewborn method
X Press F11. Ensure that the mother's Weight field decreases to 30.94731585
Y Step back to the Reproduce method. Step through the remaining code. Ensure that the Baby field is null
31.Check Your Work (San Diego Zoo):

The BirthAnimal sequence for the San Diego Zoo will not run because there is not a pregnant animal.

A Verify the required change or observation

Check the result against this instruction: Check Your Work (San Diego Zoo):

Submit your work
32.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.

33.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.

34.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
35.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.

Feed an animal with a food object Route a birth request to the birthing room Deliver and feed a newborn
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.