2.2 Zoo Learning Activity

Under Construction

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

A platypus
2.2 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 2.1 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 2.1 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.

Receive food from the vending machine
Problem

Currently when the guest feed's the animal they are "creating" food themselves. While that might be okay if they were a wizard, it really should be the vending machine that is handling the food creation and the guest that accepts that food.

Solution

You will be returning a food object with values of its own that can be used in other places.

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.

Return Food from the seller. class VendingMachine { + SellFood(payment : decimal) : Food } class Guest { + FeedAnimal(animal : Animal, animalSnackMachine : VendingMachine) : void } class Food { + Weight : double } Guest --> VendingMachine : calls SellFood VendingMachine ..> Food : creates and returns Guest ..> Food : receives and passes to Eat
Return Food from the seller
  • New
  • Changed
  • Removed
5.In the VendingMachine class, have the SellFood method return a Food object instead of the foodWeight
2.2.4 How to Return an Object from a Method 2.2.5 How to Understand Local References in Methods
A Change the return type of the SellFood method to the type Food instead of double
public Food SellFood(decimal payment)
Review this feature’s design and requirements
B In the SellFood method, after assigning the FoodStock field, instantiate an instance of the Food class and assign it to a variable named food
C Assign the food variable's Weight field to the foodWeight variable
food.Weight = foodWeight;
D Add a return statement that returns the food variable
return food;
6.In the Guest class's FeedAnimal method, have the Visitor accept the food created from the vending machine
Review this feature’s design and requirements
A Assign the return of the SellFood method to a variable of the Food type named food instead of the foodWeight
Food food = animalSnackMachine.SellFood(foodPayment);
B Delete the instantiation of the food object and the assignment of the food's Weight field
C In the logic check, replace the moneyBalance variable with the call to GetMoneyBalance
if (this.GetMoneyBalance() >= foodCost)
D Delete the line of code that assigns the moneyBalance variable to the return of GetMoneyBalance()
7.Check Your Work (Como Zoo):
A Build the solution. Make sure it builds without errors or warnings
B Set a breakpoint in the FeedAnimal method on the logic check
C Start the application. Click the New Como Zoo button. Click the Darla, feed dingo button
D Step over the logic check and ensure it passes
E Step over the line of code that assigns the foodPayment
F Step into the SellFood method
G Step through the SellFood method. Ensure that the food object is instantiated and that food's Weight is assigned 0.3975
H Return to the FeedAnimal method and press F11. Ensure that the food object exists and has a Weight of 0.3975
8.Check Your Work (San Diego):
A Set a breakpoint in the FeedAnimal method on the line that calls the Eat method
B Start the application. Click New SanDiego Zoo. Click Dave, feed platypus
C In the Autos window, ensure that the food object's Weight is 0.096
Withdraw only available money
Problem

Currently if the guest's wallet's money balance is less than the 0, it returns nothing. This is fine except that the guest could return less than the amount that is being requested of them.

Solution

You will add another level of logic that checks if the guest has enough money to pay and if they don't, will return whatever money they currently possess.

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

Withdraw up to the available balance. start if (amount > 0?) then (yes) if (MoneyBalance >= amount?) then (yes) :amountRemoved = amount; else (no) :amountRemoved = MoneyBalance; endif else (no) :amountRemoved = 0; endif :MoneyBalance -= amountRemoved; :Return amountRemoved; stop
Withdraw up to the available balance
  • New
  • Changed
  • Removed
Suggested boundary checks
Request with starting balance 3.75m Amount returned Ending balance
0.00m 0.00m 3.75m
-1.00m 0.00m 3.75m
1.00m 1.00m 2.75m
3.75m 3.75m 0.00m
5.00m 3.75m 0.00m
10.In the Wallet class, in the RemoveMoney method, inside the current 'if' statement, add an additional 'if' statement that checks if the MoneyBalance is greater than or equal to the amount parameter
2.1.7 How to Write a Nested If Statement
A Verify the required change or observation

Check the result against this instruction: In the Wallet class, in the RemoveMoney method, inside the current 'if' statement, add an additional 'if' statement that checks if the MoneyBalance is greater than or equal to the amount parameter.

11.Move the assigning of the amountRemoved to inside the nested if statement
if (amount > 0.00m)
{
     if (this.MoneyBalance >= amount)
     {
          amountRemoved = amount;
     }
}
else
{
     amountRemoved = 0.00m;
}
A Verify the required change or observation

Check the result against this instruction: Move the assigning of the amountRemoved to inside the nested if statement.

12.After the nested if statement add an else statement. Inside the else statement, assign the amountRemoved variable to the wallet's MoneyBalance
if (this.MoneyBalance >= amount)
{
    amountRemoved = amount;
}
else
{
    amountRemoved = this.MoneyBalance;
}
A Verify the required change or observation

Check the result against this instruction: After the nested if statement add an else statement. Inside the else statement, assign the amountRemoved variable to the wallet's MoneyBalance.

13.Check Your Work (ComoZoo):
A Build the solution. Ensure is compiles without errors or warnings
B Add a breakpoint in the Wallet class at the beginning of the RemoveMoney method
C Start the application. Click the New Como Zoo button. Click the Darla, feed Dingo button
D Ensure that the amount parameter is 0.53. Ensure that the MoneyBalance is 5.25
E Step over the first logic check and ensure that it passes
F Step over the second logic check and ensure that it passes
G Step over the line of code that assigns the amountRemoved. Ensure it is assigned 0.53
H Step through the remaining method body. Ensure that the MoneyBalance is assigned 4.72
14.Check Your Work (San Diego):
A Add a breakpoint in the Wallet class at the beginning of the RemoveMoney method
B Start the application. Click the New San Diego Zoo button. Click the Dave, feed platypus button
C Ensure that the amount parameter is 0.08. Ensure that the MoneyBalance is 3.75
D Step over the remaining method body. Ensure that MoneyBalance is assigned 3.67
Refill a snack machine to capacity
Problem

Now that the guest can purchase physical food from the snack machine, there needs to be someone who can fill it back up.

Solution

You will set up a sequence using new methods and event handlers to have employees in the zoo refill the snack machine.

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

Refill until the machine is full. start while (machine.IsFull()?) is (no) :machine.AddFoodBag(); :FoodStock = min(FoodStock + 65, 250); endwhile (yes) stop
Refill until the machine is full
  • New
  • Changed
  • Removed
San Diego refill trace
Bag added FoodStock
Start 3.5
1 68.5
2 133.5
3 198.5
4 250.0
16.Define the AddFoodBag method in the VendingMachine class
Review this feature’s design and requirements
A AddFoodBag has a return type of void and takes no parameters
B Assign the FoodStock field to either the current FoodStock + 65 or 250, whichever is smaller
this.FoodStock = Math.Min(this.FoodStock + 65.0, 250.0);
17.Define the IsFull method in the VendingMachine class
Review this feature’s design and requirements
A IsFull has a return type of bool and takes no parameters
B In the method body, return true if the FoodStock is greater than or equal to 250
18.Define the FillVendingMachine method in the Employee class
Review this feature’s design and requirements
A FillVendingMachine has a return type of void and takes a type of VendingMachine named machine as a parameter
19.Create buttons to handle the employees filling the Vending Machine
A In the MainWindow.xaml, under the line of code that creates the birthDingo button, paste the c below
<Button x:Name="newComoZooButton" Content="New Como Zoo" Click="newComoZooButton_Click" />
<Button x:Name="darlaFeedDingoButton" Content="Darla, feed dingo" Click="darlaFeedDingoButton_Click" />
<Button x:Name="birthDingoButton" Content="Birth dingo" Click="birthDingoButton_Click" />
<Button x:Name="floraFillVendingMachineButton" Content="Flora, fill vending machine" Click="floraFillVendingMachineButton_Click" />
B In the MainWindow.xaml, under the line of code that creates the birthPlatypus button, paste the code below
<Button x:Name="newSanDiegoZooButton" Content="New San Diego Zoo" Click="newSanDiegoZooButton_Click" />
<Button x:Name="daveFeedPlatypusButton" Content="Dave, feed platypus" Click="daveFeedPlatypusButton_Click" />
<Button x:Name="birthPlatypusButton" Content="Birth platypus" Click="birthPlatypusButton_Click" />
<Button x:Name="bettyFillVendingMachineButton" Content="Betty, fill vending machine" Click="bettyFillVendingMachineButton_Click" />
C Double click the Flora, fill vending machine button and the Betty, fill vending machine button to create event handers
D In the event handler for Flora filling the vending machine, call the FillVendingMachine method on the ComoZoo's BirthArea's Doctor and pass in the ComoZoo's AnimalSnackMachine as an argument
E In the event handler for Betty filling the vending machine, call the FillVendingMachine method on the SanDiegoZoo's TicketBooth's Attendant and pass in the SanDiegoZoo's AnimalSnackMachine as an argument
20.In the Employee class, modify FillVendingMachine method
2.2.6 How to Write a While Loop
A In the method body, call the AddFoodBag method on the machine parameter
B Surround the call to AddFoodBag in a while loop that checks if the capacity of the machine is full
21.Check Your Work (ComoZoo):
A Build to solution. Make sure it compiles without errors or warnings
B Set a breakpoint in the Flora, fill vending machine event handler on the line that calls FillVendingMachine
C Start the application. Click the New Como Zoo button. Click the Flora, fill vending machine button
D Press F11. In the Autos window, expand the machine object. Ensure that the foodPricePerPound is 0.75
E Step into the IsFull method. Ensure that this.FoodStock is 250
F Return to the FillVendingMachine method and press F11. Ensure the AddFoodBag method call is not hit
22.Check Your Work (SanDiego):
A Set a breakpoint in the Betty, fill vending machine event handler on the line that calls FillVendingMachine
B Start the application. Click New San Diego Zoo. Click Betty, fill vending machine
C Step into the call to FillVendingMachine. Ensure that the machine object's FoodPricePerPound is 1.20
D Step into the call to IsFull. Ensure that this.FoodStock is 3.5
E Return to the FillVendingMachine method and press F11. Ensure that that logic check passes
F Step into the AddFoodBag method and press F11 twice. Ensure that the FoodStock is now 68.5
G Return to the FillVendingMachine method and press F11 to step through the sequence again. Ensure that the logic check passes and that AddFoodBag is called again
H Step though the AddFoodBag method. Ensure that the FoodStock is now 133.5
I Step through the sequence again. Ensure that FoodStock is now 198.5
J Step through the sequence again. Ensure that FoodStock is now 250
K Return to the FillVendingMachine and step through the sequence again. Ensure that this time the logic check fails and the AddFoodBag method is not called
Keep a reference to the newborn
Problem

Currently the newborn is not returned to the caller before the mother clears her Baby reference. Instead the zoo would like the baby animal to continue living.

Solution

You will return the Baby object to other method calls so it can be used.

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.

Return a retained newborn reference. start :baby = Baby; :Baby = null; :Subtract baby weight and afterbirth from mother; :Move mother; :Move baby; if (Mother is not a Platypus?) then (yes) :FeedNewborn(baby); endif :Return baby; stop
Return a retained newborn reference
  • New
  • Changed
  • Removed
24.Modify the Reproduce method in the Animal class
A Define a variable of type Animal named baby and assign the Baby field to it
Animal baby = this.Baby;
B Move the line of code that assigns the Baby field to null to below the code that assigns the baby variable

Since the Baby is born and no longer "inside" the mother there should not be an additional place it is referenced.

C Refactor the code to have all references to this.Baby reference the baby variable
D Modify the method header to return a type of Animal
E At the end of the method, return the baby variable
25.In the Employee class, in the DeliverAnimal method, define a variable of type Animal named baby and assign the existing call to Reproduce to it
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, define a variable of type Animal named baby and assign the existing call to Reproduce to it.

26.Check Your Work (Como Zoo):
A Build to solution. Make sure it compiles without errors or warnings
B Set a breakpoint at the beginning of the Reproduce method in the Animal class
C Start the application. Click the New Como Zoo button. Click the Birth dingo button
D Step over the line of code that assigns the baby variable. Expand the baby object in the Autos window. Ensure that the Gender field is Male, the Type field is Dingo and that the Weight field is 3.53
E Step over the line of code that assigns the this.Baby field to null. Ensure that the value is null
F Use F10 to step through the remaining code in the method body and stop on the line of code that returns the baby object. Ensure that the Weight of the baby object is 3.619

Expand 'this' in the Autos window. Ensure that the value of this.Baby is null.

G Press F10 to return to the call to Reproduce. Ensure that the baby variable is null
H Press F10. Ensure that that baby variable is assigned the Animal reference and that the Weight is 3.619
27.Check Your Work (San Diego)
A Restart the application. Click New San Diego Zoo. Click the Birth platypus button. Ensure the breakpoint is not hit. Since the the animal in this zoo is not pregnant, the breakpoint will never be hit
Make the newborn the featured animal
Problem

The baby is now being returned to the Employee class but nothing is happening to it. Additionally, the zoo itself would like the capability of modifying the baby object.

Solution

You will modify the baby object in the Employee class and change the birthing sequence to return the baby to the zoo.

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

Return the same baby through delivery. participant Zoo participant BirthingRoom participant Employee participant Animal Zoo -> BirthingRoom : BirthAnimal(mother) activate BirthingRoom BirthingRoom -> Employee : DeliverAnimal(mother) activate Employee Employee -> Animal : Reproduce() Animal --> Employee : baby Employee -> Employee : baby.Name = "Baby" Employee --> BirthingRoom : baby deactivate Employee BirthingRoom --> Zoo : baby deactivate BirthingRoom Zoo -> Zoo : FeaturedAnimal = baby (if non-null)
Return the same baby through delivery
  • New
  • Changed
  • Removed
29.Modify the DeliverAnimal method in the Employee class
A Assign the baby's Name field to "Baby"
B Modify the method header to return an Animal type
C Return the baby object
30.Modify the BirthAnimal method in the BirthingRoom class
A Define a variable named baby of type Animal and assign it to null
B Assign the baby object to the call to DeliverAnimal
C Modify the method header to return an Animal type
D Return the baby object
31.Modify the BirthAnimal method in the Zoo class
A Define a variable named baby of type Animal and assign it to the call to BirthAnimal
B If the returned baby is not null, assign it to the Zoo's FeaturedAnimal field
32.Check Your Work (Como Zoo):
A Build to solution. Make sure it compiles without errors or warnings
B Set a breakpoint in the Employee class in the DeliverAnimal method on the line that assigns the baby's Name
C Start the application. Click New Como Zoo. Click the Birth Dingo button. In the autos window, ensure that the Name field is " "
D Press F10. Ensure that the Name field is now "Baby"
E Press F10 to return to the call to DeliverAnimal
F Press F10. Ensure that the baby variable is assigned to the object with the Name "Baby"
G Press F10 to return to the call to BirthAnimal
H Press F10. Ensure that the baby variable is assigned to the object with the Name "Baby"
I Step over the remaining code in BirthAnimal and ensure that the FeaturedAnimal field is a reference to the animal with the name "Baby"
Submit your work
33.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.

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

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

Receive food from the vending machine Withdraw only available money Refill a snack machine to capacity Keep a reference to the newborn Make the newborn the featured animal
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.