- Como Zoo starts with Dolly, Dixie and Pammy; San Diego Zoo starts with Patti and Harold.
- The animals shown in the display arrays are the same objects held by the population list.
The zoo would like to be able to have more animals. In this assignment, you will define a list of animals, add multiple animals to it, and use loops to work with the animals in the list. The existing pen and tank arrays will continue to describe their display spaces.
MainWindow.xaml, change only the window Title to identify Zoo 2.4 and your last name. Keep the project target at .NET Framework 4.8.Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.1, column 1.Currently, the zoo has one featured animal that can be acted upon. The zoo would like to be able to have more animals.
You will define a list of animals and add the featured animal to it.
Zoo and MainWindow classes, add the fields shown in the class diagram below. The methods shown are introduced in later tasks.Zoo.cs, add using System.Collections.Generic; alongside the existing using directives so List<Animal> can be used.Zoo class, define a public field of type List<Animal> named Animals. Instantiate the list using field initializer syntax.public List<Animal> Animals = new List<Animal>();MainWindow class, define ZooAnimalCount as int and ZooAverageAnimalWeight as double.Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.Zoo.Animals declaration. Confirm it creates a List<Animal> for each Zoo instance.this.Animals is not null and this.Animals.Count is 0.The zoo has a list now but no animals have been added to it.
You will instantiate multiple animals and add them to the zoo’s list of animals.
CreateComoZoo in MainWindow.xaml.cs, replace the existing block from this.ComoZoo.FeaturedAnimal = new Animal() through the BirthArea.Mother assignment with the following code. Keep the existing zoo, service, visitor and restroom setup before this block.Animal animal = new Animal();
animal.Sex = "Female";
animal.Name = "Dolly";
animal.Age = 4;
animal.Weight = 35.3;
animal.HappinessLevel = 0;
animal.Type = "Dingo";
animal.MakePregnant();
this.ComoZoo.BirthArea.Mother = animal;
this.ComoZoo.Animals.Add(animal);ComoZoo.FeaturedAnimal when assigning Pen.Animal. Keep the existing array, starting position, PlaceAnimal and DisplayAnimalArea calls.CreateComoZoo, after the existing pen placement and display calls, instantiate another Animal using the previously defined animal variable. Assign its fields, call MakePregnant, and add it to Animals. Repeat for both animals in the table.Name |
Type |
Sex |
Age |
Weight | Call MakePregnant |
|---|---|---|---|---|---|
| Dixie | Dingo | Female | 3 | 33.8 | Yes |
| Pammy | Platypus | Female | 2 | 15.5 | Yes |
Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.MainWindow.xaml.cs, set a breakpoint on the closing brace of CreateComoZoo by clicking its left margin.F5 to start or continue the application. Click New Como Zoo.Debug > Windows > Watch > Watch 1. Enter each full expression below in a separate Watch row.this.ComoZoo.Animals.Count is 3.this.ComoZoo.Animals[0].Name is "Dolly".this.ComoZoo.Animals[1].Name is "Dixie".this.ComoZoo.Animals[2].Name is "Pammy".this.ComoZoo.Pen.PenSpaces[1] refers to Dolly.F5 to continue. Stop debugging and remove the breakpoint before the next check.CreateSanDiegoZoo, replace Patti’s FeaturedAnimal creation and field assignments with a local Animal variable named animal, using the same pattern as Dolly. Declare animal once in this method; CreateComoZoo’s local variable is not available here. Keep the existing service and tank setup.Name |
Type |
Sex |
Age |
Weight | HappinessLevel |
Call MakePregnant |
|---|---|---|---|---|---|---|
| Patti | Platypus | Female | 5 | 3.27 | 0 | Yes |
| Harold | Hummingbird | Male | 1 | 0.5 | 0 by default | No |
FeaturedAnimal creation and field assignments with the local animal reference, using Patti’s existing values. Keep BirthArea.Mother connected to Patti and add Patti to Animals.Tank.Animal and keep the current tank placement and display calls.Animal to animal, use Harold’s table values and add Harold to Animals.Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.MainWindow.xaml.cs, set a breakpoint on the closing brace of CreateSanDiegoZoo by clicking its left margin.F5 to start or continue the application. Click New San Diego Zoo.Debug > Windows > Watch > Watch 1. Enter each full expression below in a separate Watch row.this.SanDiegoZoo.Animals.Count is 2.this.SanDiegoZoo.Animals[0].Name is "Patti".this.SanDiegoZoo.Animals[1].Name is "Harold".this.SanDiegoZoo.Tank.TankSpaces[1, 1] refers to Patti.F5 to continue. Stop debugging and remove the breakpoint before the next check.Count.Animal class, modify the Move method based on the activity diagram below.double named weightLossAmount and assign it one percent of the current instance’s Weight.weightLossAmount from the current instance’s Weight.Animal.Move() in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.Weight of 35.3. Confirm weightLossAmount is approximately 0.353.this.Weight is approximately 34.947.The employees of the zoo would like a way to wake up all the animals in the zoo.
You will write a method that loops through the list of Animals and calls each animal’s Move method.
Zoo class, define the WakeAllAnimals method based on the class diagram. Use the activity diagram below to write its body with a foreach loop.Animal variable named animal for the current list item.Move method inside the foreach loop.Zoo.WakeAllAnimals() in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.Animals[0].Name is Dolly, this.Animals[1].Name is Dixie and this.Animals[2].Name is Pammy.foreach loop. Confirm each of those three references receives exactly one Move() call.Now that the zoo has a list of animals, they would like to get some information based on the animals.
You will add methods that determine the total and average weight of the animals in the zoo.
Zoo class, define a method named CalculateTotalAnimalWeight with a return type of double. Use the activity diagram below to write its body.double named totalWeight and assign it zero.totalWeight. Return totalWeight after the loop.Zoo.CalculateTotalAnimalWeight() in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.totalWeight at 0.0. Use Como’s animal weights 35.3, 33.8 and 15.5.totalWeight is approximately 35.3, then 69.1, then 84.6.this.Animals list. Confirm the loop body does not execute and the method returns 0.0.Zoo class, define a method named CalculateAverageAnimalWeight with a return type of double. Use the activity diagram below, including its check for an empty list.averageWeight as double and initialize it to zero. Use Animals.Count to decide whether division is possible.averageWeight to the return of CalculateTotalAnimalWeight divided by Animals.Count. Return averageWeight.Zoo.CalculateAverageAnimalWeight() in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.Animals.Count is 3 and the total weight is approximately 84.6. Confirm the returned average is approximately 28.2.2, total approximately 3.77 and average approximately 1.885.0.0 before attempting division by the count.MainWindow.xaml, add these complete elements directly after newComoZooButton inside comoZooStackPanel.<Button x:Name="comoZooWakeAllAnimals" Content="Wake all animals" Click="comoZooWakeAllAnimals_Click"/> <Button x:Name="getComoZooAnimalCount" Content="Get animal count" Click="getComoZooAnimalCount_Click"/> <Button x:Name="getComoZooAverageAnimalWeight" Content="Get average animal weight" Click="getComoZooAverageAnimalWeight_Click"/>
newSanDiegoZooButton inside sanDiegoZooStackPanel.<Button x:Name="sanDiegoZooWakeAllAnimals" Content="Wake all animals" Click="sanDiegoZooWakeAllAnimals_Click"/> <Button x:Name="getSanDiegoZooAnimalCount" Content="Get animal count" Click="getSanDiegoZooAnimalCount_Click"/> <Button x:Name="getSanDiegoZooAverageAnimalWeight" Content="Get average animal weight" Click="getSanDiegoZooAverageAnimalWeight_Click"/>
MainWindow.xaml.cs with the exact names shown in the XAML. Use the next task to complete their bodies before building.private void comoZooWakeAllAnimals_Click(object sender, RoutedEventArgs e)
{
if (this.ComoZoo == null)
{
this.DisplayMessage("Create Como Zoo first.");
return;
}
this.ComoZoo.WakeAllAnimals();
this.DisplayMessage("All animals were moved.");
}private void getComoZooAnimalCount_Click(object sender, RoutedEventArgs e)
{
if (this.ComoZoo == null)
{
this.DisplayMessage("Create Como Zoo first.");
return;
}
this.ZooAnimalCount = this.ComoZoo.Animals.Count;
this.DisplayMessage("Animal count: " + this.ZooAnimalCount);
}private void getComoZooAverageAnimalWeight_Click(object sender, RoutedEventArgs e)
{
if (this.ComoZoo == null)
{
this.DisplayMessage("Create Como Zoo first.");
return;
}
this.ZooAverageAnimalWeight = this.ComoZoo.CalculateAverageAnimalWeight();
this.DisplayMessage("Average animal weight: " + this.ZooAverageAnimalWeight);
}MainWindow result fields unchanged.| San Diego handler | Use this Como example | Zoo reference |
Create-first message |
|---|---|---|---|
sanDiegoZooWakeAllAnimals_Click |
comoZooWakeAllAnimals_Click |
this.SanDiegoZoo |
Create San Diego Zoo first. |
getSanDiegoZooAnimalCount_Click |
getComoZooAnimalCount_Click |
this.SanDiegoZoo |
Create San Diego Zoo first. |
getSanDiegoZooAverageAnimalWeight_Click |
getComoZooAverageAnimalWeight_Click |
this.SanDiegoZoo |
Create San Diego Zoo first. |
Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.Create Como Zoo first..Create San Diego Zoo first..The feeding sequence still uses FeaturedAnimal. Additionally, there is currently no way for that sequence to find a particular type of animal in the zoo’s list.
You will set up a loop that finds a particular animal and uses it in the feeding sequence.
Zoo class, define FindAnimal with a return type of Animal and a parameter of type string named type. Follow the activity diagram below.Animal variable named foundAnimal and assign it null. Define a foreach loop that loops through Animals.Type is the same as type. For a match, store the current animal reference in foundAnimal and break out of the loop. Return foundAnimal after the loop.Zoo.FindAnimal("Dingo") in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.foundAnimal begins as null.this.Animals[0].Type with Dingo. Confirm Dolly matches, foundAnimal receives her reference and break stops before Dixie.Hummingbird. Confirm none of the three Type values match and the method returns null.MainWindow.xaml.cs, change the animal argument passed into the existing Visitor.FeedAnimal call. Use the search result from zoo.FindAnimal(animalName). Keep the shared helper’s signature, zoo-null check, result-code switch and messages.FeedAnimal call with the corresponding statement from this table.| Handler | Complete replacement statement |
|---|---|
darlaFeedDingoButton_Click |
this.FeedAnimal(this.ComoZoo, "Como Zoo", "Dingo", "Darla"); |
daveFeedPlatypusButton_Click |
this.FeedAnimal(this.SanDiegoZoo, "San Diego Zoo", "Platypus", "Dave"); |
Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.Zoo.FindAnimal.New Como Zoo to reset that zoo. Do not feed, refill, move or give birth before this test.Darla, feed dingo. Confirm returned foundAnimal.Name is Dolly.MainWindow.FeedAnimal. Confirm the same reference is passed to Guest.FeedAnimal. Continue and confirm feeding succeeds.New San Diego Zoo to reset that zoo. Do not feed, refill, move or give birth before this test.Dave, feed platypus. Confirm the search returns Patti and the completed feeding reports success.F5 to continue. Stop debugging and remove the breakpoint before the next check.Currently the birthing of an animal is only happening to the featured animal, but now that there are multiple animals that are pregnant the zoo needs the capability to find the particular animal that needs to give birth.
You will use a foreach loop to find a pregnant animal and refactor the BirthAnimal method to handle that particular animal.
Zoo class, define FindPregnantAnimal with a return type of Animal and a parameter of type string named type. Use the activity diagram below.Animal variable named foundAnimal and assign it null. Define a foreach loop that loops through Animals.Type matches type and the current animal is pregnant. Store the first matching animal reference in foundAnimal and break out of the loop. Return foundAnimal after the loop.Zoo.FindPregnantAnimal("Dingo") in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.Animals[0].Name is Dolly and her pregnancy test is true. Confirm Dolly is returned.this.Animals[1] is returned.Zoo.cs, keep the existing BirthAnimal(Animal mother) signature and replace its body using the activity diagram below. The mother parameter already exists in this version.this.BirthArea.BirthAnimal(mother) and store the returned reference in a local Animal variable named baby.Animals only when it is not null. Return baby after that check so both the success and no-birth paths return a result.Zoo.BirthAnimal in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.baby returned from this.BirthArea.BirthAnimal(mother). Confirm this.Animals.Add(baby) runs once and the method returns that same reference.null. Confirm the Add call is skipped and this.Animals.Count stays unchanged.new Animal() to replace the returned baby.MainWindow.xaml.cs, replace each existing baby-assignment statement with the corresponding statement below. Keep the rest of each handler, including the occupied-space guard, baby name, array placement, map refresh, temperature update and messages.| Handler | Complete replacement statement |
|---|---|
birthDingoButton_Click |
Animal baby = this.ComoZoo.BirthAnimal(this.ComoZoo.FindPregnantAnimal("Dingo")); |
birthPlatypusButton_Click |
Animal baby = this.SanDiegoZoo.BirthAnimal(this.SanDiegoZoo.FindPregnantAnimal("Platypus")); |
birthDingoButton_Click.New Como Zoo to reset that zoo. Do not feed, refill, move or give birth before this test.Give birth to dingo once. Do not feed, wake or move first. At the breakpoint open Watch.this.ComoZoo.Animals.Count is 4.this.ComoZoo.Pen.PenSpaces[0].Name is "Baby".this.ComoZoo.Pen.PenSpaces[1].Name is "Dolly".this.ComoZoo.Animals[3] and this.ComoZoo.Pen.PenSpaces[0] contain the same newborn reference.2.Give birth to dingo again. Confirm the occupied-space message appears. Continue past the breakpoint if reached, then click the population-count button: confirm 4.F5 to continue. Stop debugging and remove the breakpoint before the next check.FeaturedAnimal field from Zoo.cs. Keep Pen.Animal, Tank.Animal and BirthArea.Mother; these references still have different jobs.Zoo.cs. Confirm the FeaturedAnimal field declaration has been removed.Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.FeaturedAnimal reference, open that caller and compare it with this page’s creation, feeding or birth update.Pen.Animal, Tank.Animal and BirthArea.Mother remain; these still identify animals used by the display and birth workflows.Zoo |
List count | Total used by the average | Average | Displayed-area occupants |
|---|---|---|---|---|
| Como Zoo | 3 | Approximately 84.6 | Approximately 28.2 | 1 |
| San Diego Zoo | 2 | Approximately 3.77 | Approximately 1.885 | 1 |
WakeAllAnimals, click Como’s Wake all animals once, and inspect the weights. Use the following values only after recreating Como and before any feeding or births.Animal |
Watch expression inside Zoo.WakeAllAnimals |
Approximate Weight |
|---|---|---|
| Dolly | this.Animals[0].Weight |
34.947 |
| Dixie | this.Animals[1].Weight |
33.462 |
| Pammy | this.Animals[2].Weight |
15.345 |
Get animal count button. Confirm the pen map and population count stay the same. Animal.Move changes weight; it does not assign a new array location.WakeAllAnimals, click San Diego’s Wake all animals button once.Zoo.WakeAllAnimals, inspect this.Animals[0].Weight as approximately 3.2373 for Patti and this.Animals[1].Weight as approximately 0.495 for Harold.WakeAllAnimals breakpoint before the remaining checks."Dingo" argument in darlaFeedDingoButton_Click to "Digno". Build, recreate Como and click Darla, feed dingo. Confirm the existing no-animal message appears; FindAnimal returns null."Dingo", build and start the application, then create Como again. Confirm feeding succeeds before continuing.CreateComoZoo, set a breakpoint on Animal animal = new Animal(). With the application running, click New Como Zoo. The highlighted statement has not executed yet.| Expression | Expected state |
|---|---|
this.ComoZoo.Animals |
An existing empty list; this.ComoZoo.Animals.Count is 0 |
| animal | Not assigned yet; the debugger may show null or unavailable at this point |
this.ComoZoo.Pen |
null; the pen has not been created yet |
this.ComoZoo.BirthArea.Mother |
null; Dolly has not been assigned yet |
MakePregnant. Before Animals.Add executes, its Count is still 0; after that statement executes, Count is 1.Animal, PenSpaces[1], the list’s first item and BirthArea.Mother refer to Dolly.new Animal() statement. The local now refers to a different object, but the list’s first item and Pen.Animal still refer to Dolly.Count change to 2 and then 3.this.ComoZoo.Animals[0] in Watch. Confirm its Name is still Dolly.new Animal(). Confirm the local now refers to the later animal while list index 0 still refers to Dolly.Add call: the list stored the earlier reference, not a permanent link to the local variable.F5 to continue. Stop debugging and remove the breakpoint before the next check.MainWindow.FeedAnimal, set a breakpoint on the feedResult assignment that contains zoo.FindAnimal(animalName). Click Darla, feed dingo.| Expression | Expected state before stepping into FindAnimal |
|---|---|
zoo.Name |
"Como Zoo" |
animalName |
"Dingo" |
zoo.Animals |
zoo.Animals.Count is 3 |
| type | Unavailable in this method; it is the next method’s parameter |
foundAnimal |
Unavailable in this method |
| animal | Unavailable in this method; a foreach variable belongs to FindAnimal |
this.Animals |
Unavailable here because this refers to MainWindow |
Zoo.FindAnimal, then F10 to follow its loop.Zoo and type is "Dingo". After the declaration executes, foundAnimal is null.foundAnimal also refers to Dolly.Guest.FeedAnimal and confirm its animal parameter refers to Dolly. This parameter and the earlier foreach variable share the name animal but belong to different methods.foundAnimal.Name and confirm Dolly.MainWindow.FeedAnimal. Confirm the search result becomes the animal argument to Guest.FeedAnimal.F5 to continue. Stop debugging and remove the breakpoint before the next check.ZooAverageAnimalWeight assignment in getSanDiegoZooAverageAnimalWeight_Click and click Get average animal weight.| Expression | Expected state |
|---|---|
this.SanDiegoZoo.Animals |
this.SanDiegoZoo.Animals.Count is 2 |
this.ZooAverageAnimalWeight |
Its last stored result from either zoo; the highlighted assignment has not run |
averageWeight |
Unavailable in this handler; inspect it after its declaration inside CalculateAverageAnimalWeight has run |
totalWeight |
Unavailable in this handler; inspect it after its declaration inside CalculateTotalAnimalWeight has run |
| animal | Unavailable in this handler; inspect it inside the total method after foreach selects a list item |
CalculateAverageAnimalWeight. After its declaration executes, ensure averageWeight is 0. Step into CalculateTotalAnimalWeight, then use F10 to follow the foreach loop.totalWeight becomes approximately 3.77. The list Count remains 2.CalculateAverageAnimalWeight. After the division statement executes, averageWeight is approximately 1.885.this.ZooAverageAnimalWeight holds the returned value. Step over DisplayMessage and confirm the result is displayed.CalculateTotalAnimalWeight, inspect local totalWeight as the loop adds each animal’s weight.CalculateAverageAnimalWeight. Confirm the returned total is used to compute local averageWeight.this.ZooAverageAnimalWeight.F5 to continue. Stop debugging and remove the breakpoint before the next check.List<Animal> while the pen and tank keep their arrays. Use a fresh Como population and the shared-Dolly diagram as your example.Count before dividing.The ending diagram includes the existing framework List<Animal> class and its Count property for context.