- Creating either zoo automatically displays
- X - -in that zoo’s pen box. - The left and right buttons update only their own zoo’s pen.
- An animal cannot move outside indexes 0 through 3.
- The array stores the same animal already referenced by the zoo.
Continue from your completed Zoo 2.1. Use the diagrams to implement a four-space pen for each zoo. Each pen stores the existing featured animal reference and displays its position automatically.
The existing window title already identifies Programming Concepts 2; keep it unchanged.
- X - - in that zoo’s pen box.Pen and four-space array.Pen.cs to Business Classes and declare its fieldsPen class in Business Classes.Animal, PenSpaces, and StartingPosition fields with their specified types.Zoo.cs, add a field named Pen whose type is Pen, as shown in the ending class diagram.Pen.cs is included in the project.Pen.Animal has type Animal.Pen.PenSpaces has type Animal[] and Pen.StartingPosition has type int.ClearLocation in Pen.csClearLocation to Pen.cs.PenSpaces.this.Animal.The loop continues checking the other positions automatically. Emptying an array position does not change Pen.Animal; the pen still knows which animal it manages.
Trace the ClearLocation activity diagram alongside your method. Confirm each matching array position is cleared while the Animal field is left unchanged. The Watch task later shows the same reference behavior during movement.
FindAnimalPosition in Pen.csFindAnimalPosition to Pen.cs.PenSpaces for the animal parameter. Use the diagram to determine when to search and when to stop.Pen.FindAnimalPosition 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.PenSpaces array with Dolly at index 1 and the other entries null. Search using Dolly’s reference.-1, index 0 does not match and index 1 matches. Confirm the method returns 1.null. Confirm the result remains -1.Animal reference that is absent from the array. Confirm every comparison fails and the result is -1.GetAnimalLocation in Pen.csGetAnimalLocation to Pen.cs.FindAnimalPosition and decide which description to build.Pen.GetAnimalLocation 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.PenSpaces[1], follow the successful search and confirm the returned text is Dolly is in pen position 2..This animal does not have a pen location..0. Confirm the text uses position 1, while array access still uses index 0.GetDisplayLocation in Pen.csGetDisplayLocation to Pen.cs.Pen.GetDisplayLocation 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.PenSpaces[1]. Follow each loop iteration and confirm returned text - X - -.null entries. Confirm - - - -.2 with the other entries empty. Confirm - - X -.StartingPosition.IsValidPosition in Pen.csIsValidPosition to Pen.cs.Pen.IsValidPosition 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.PenSpaces.Length equal to 4. Trace requested indexes 0 and 3 separately: both must return true.-1: confirm false.4: confirm false because the final valid index is 3.MoveAnimal in Pen.csMoveAnimal to Pen.cs.Pen.MoveAnimal 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.PenSpaces[1] and this.Animal referring to Dolly. Trace one right move.2. After the move, this.PenSpaces[1] is null and this.PenSpaces[2] refers to Dolly.this.Animal still refers to Dolly; the move must not create another Animal.0. Confirm the invalid destination -1 leaves every entry unchanged.3. Confirm destination 4 leaves every entry unchanged.-1 for an absent animal. Confirm no array entry changes.PlaceAnimal in Pen.csPlaceAnimal to Pen.cs.PenSpaces.Pen.PlaceAnimal 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.PenSpaces, this.StartingPosition of 1 and this.Animal referring to Dolly.this.PenSpaces[1] receives the existing Dolly reference; indexes 0, 2 and 3 remain null.this.Animal equal to null. Confirm no entry changes.-1 and 4 separately. Confirm neither changes an entry.TextBox and left and right buttons.MainWindow.xaml, insert this complete block after birthDingoButton and before comoZooStackPanel.Resources<TextBlock Text="Animal pen:" Margin="3,10,3,0"/> <StackPanel Orientation="Horizontal"> <TextBox x:Name="penDisplayTextBox" Margin="5,5,10,5" Padding="5" FontFamily="Consolas" FontSize="24" Height="42" Width="109" IsReadOnly="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Disabled"/> <Button x:Name="moveComoZooAnimalLeftButton" Margin="5" Width="25" Height="25" FontSize="16" FontWeight="Bold" Content="<" Click="moveComoZooAnimalLeftButton_Click"/> <Button x:Name="moveComoZooAnimalRightButton" Margin="5" Width="25" Height="25" FontSize="16" FontWeight="Bold" Content=">" Click="moveComoZooAnimalRightButton_Click"/> </StackPanel>
sanDiegoZooStackPanel, insert this complete block after birthPlatypusButton and before the birthing-room temperature controls<TextBlock Text="Animal pen:" Margin="3,10,3,0"/> <StackPanel Orientation="Horizontal"> <TextBox x:Name="sanDiegoPenDisplayTextBox" Margin="5,5,10,5" Padding="5" FontFamily="Consolas" FontSize="24" Height="42" Width="109" IsReadOnly="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Disabled"/> <Button x:Name="moveSanDiegoZooAnimalLeftButton" Margin="5" Width="25" Height="25" FontSize="16" FontWeight="Bold" Content="<" Click="moveSanDiegoZooAnimalLeftButton_Click"/> <Button x:Name="moveSanDiegoZooAnimalRightButton" Margin="5" Width="25" Height="25" FontSize="16" FontWeight="Bold" Content=">" Click="moveSanDiegoZooAnimalRightButton_Click"/> </StackPanel>
Name values, and Click names exactly as supplied.TextBox. Continue to use informationTextBox for general messages.MainWindow.xaml. Confirm penDisplayTextBox is inside Como Zoo’s StackPanel and sanDiegoPenDisplayTextBox is inside San Diego Zoo’s StackPanel.Content.x:Name is unique and each arrow’s Click value names its corresponding handler.DisplayPen in MainWindow.xaml.csDisplayPen to MainWindow.xaml.cs.TextBox.MainWindow.DisplayPen 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.penDisplayTextBox.Text and not to this.sanDiegoPenDisplayTextBox.Text.this.sanDiegoPenDisplayTextBox.Text is updated.zoo equal to null with zooName of Como Zoo. Confirm Create Como Zoo first. is displayed without accessing zoo.Pen.MoveAnimalInPen in MainWindow.xaml.csMoveAnimalInPen to MainWindow.xaml.cs.MainWindow.MoveAnimalInPen 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.zoo == null guard runs before any zoo.Pen access.MoveAnimal runs before DisplayPen and both use the same zoo reference.DisplayPen still runs after the movement attempt.CreateComoZoo in MainWindow.xaml.csCreateComoZoo. Add the pen setup immediately after the BirthArea.Mother assignment.FeaturedAnimal to the pen as shown in the diagram; this shares the reference rather than creating another animal.this.ComoZoo and the exact name Como Zoo for the display call.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.Pen.PenSpaces.Length is 4.this.ComoZoo.Pen.StartingPosition is 1.this.ComoZoo.Pen.PenSpaces[0] is null.this.ComoZoo.Pen.PenSpaces[2] is null.this.ComoZoo.Pen.PenSpaces[3] is null.this.ComoZoo.Pen.PenSpaces[1], this.ComoZoo.Pen.Animal and this.ComoZoo.FeaturedAnimal. Confirm all reach Dolly.- X - -.- X - - while the other zoo’s map stays unchanged.F5 to continue. Stop debugging and remove the breakpoint before the next check.CreateSanDiegoZoo in MainWindow.xaml.csCreateSanDiegoZoo. Add the pen setup immediately after the BirthArea.Mother assignment.FeaturedAnimal to the pen as shown in the diagram; this shares the reference rather than creating another animal.this.SanDiegoZoo and the exact name San Diego Zoo for the display call.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.Pen.PenSpaces.Length is 4.this.SanDiegoZoo.Pen.StartingPosition is 1.this.SanDiegoZoo.Pen.PenSpaces[0] is null.this.SanDiegoZoo.Pen.PenSpaces[2] is null.this.SanDiegoZoo.Pen.PenSpaces[3] is null.this.SanDiegoZoo.Pen.PenSpaces[1], this.SanDiegoZoo.Pen.Animal and this.SanDiegoZoo.FeaturedAnimal. Confirm all reach Patti.- X - -.- X - - while the other zoo’s map stays unchanged.F5 to continue. Stop debugging and remove the breakpoint before the next check.MainWindowprivate void moveComoZooAnimalLeftButton_Click(object sender, RoutedEventArgs e)
{
this.MoveAnimalInPen(this.ComoZoo, "Como Zoo", -1);
}Use the first handler’s signature pattern and the following exact names and arguments. Each handler body contains one call to MoveAnimalInPen.
| Handler name | Zoo argument | zooName argument |
positionChange argument |
|---|---|---|---|
moveComoZooAnimalRightButton_Click |
this.ComoZoo |
"Como Zoo" |
1 |
moveSanDiegoZooAnimalLeftButton_Click |
this.SanDiegoZoo |
"San Diego Zoo" |
-1 |
moveSanDiegoZooAnimalRightButton_Click |
this.SanDiegoZoo |
"San Diego Zoo" |
1 |
Keep all older event handlers unchanged.
Click values in MainWindow.xaml with the handler table above. Confirm each has one method with exactly the same spelling and capitalization.- - X - - becomes - - X -.- X - -.X - - -.- X - -.Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.this.penDisplayTextBox.Text is - - X - and this.sanDiegoPenDisplayTextBox.Text is - X - -.X - - - while Como stays - - X -.Pen.PenSpaces in this checkpoint.77.5 then 77.0.GetDisplayLocation and inspect position and PenSpaces in Autos and Watch while stepping through the loopPenSpaces.Length and determine whether the loop body will run.Use Watch if an expression is not currently shown in Autos.
GetDisplayLocation loop condition to <=, observe the invalid index, and restore < before submissionCreateComoZoo and confirm this.ComoZoo.Pen.PenSpaces is allocated with four entries and this.ComoZoo.Pen.StartingPosition is 1.CreateSanDiegoZoo and confirm the equivalent San Diego array has four entries and starting position 1.Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.Follow a right-arrow click through the methods it calls. Use Step Into (F11) to enter a method and Step Over (F10) when you want to run a statement without entering its method calls. Watch shows values at the currently highlighted statement, before that statement runs.
MainWindow.xaml.cs, set a breakpoint on the MoveAnimalInPen call inside moveComoZooAnimalRightButton_Click.- X - -.| Expression | Expected value |
|---|---|
this.ComoZoo.Pen.PenSpaces |
Length 4; index 1 contains Dolly and the other positions are null |
this.ComoZoo.Pen.Animal.Name |
"Dolly" |
zoo.Name |
Unavailable in the button handler; becomes "Como Zoo" inside MoveAnimalInPen |
positionChange |
Unavailable in the button handler; becomes 1 inside MoveAnimalInPen |
MoveAnimalInPenzoo.Name and positionChange become available after entering MoveAnimalInPen. Confirm their values are "Como Zoo" and 1.zoo.Pen.MoveAnimal(positionChange) is highlighted.Pen.MoveAnimal.this.PenSpaces, this.Animal.Name, and positionChangethis.Animal.Name is "Dolly", and positionChange is 1.FindAnimalPosition(this.Animal) is highlighted, press F11 to enter the search methodanimal.Name, foundPosition, and position to Watch. Step to the foundPosition assignment, then execute it and confirm foundPosition becomes -1. A local may be unavailable until its declaration runs.this.PenSpaces[position], which is null.this.PenSpaces[position] refers to Dolly.foundPosition changes from -1 to 1. Follow break to the return statement.MoveAnimal has completed. Confirm its local position now contains the returned value 1.newPosition to Watch. Execute its assignment and confirm its value becomes 2 while position remains 1.IsValidPosition(newPosition) when the condition is evaluated. In that method, inspect position and this.PenSpaces.Length; their values are 2 and 4.MoveAnimal. The valid destination allows execution to enter the if statement.this.PenSpaces in Watch and confirm index 1 becomes null while this.Animal.Name remains "Dolly".MoveAnimalInPen and use F11 to enter DisplayPen(zoo, zooName)DisplayPen. Step Over the GetDisplayLocation call and confirm message becomes "- - X -".this.penDisplayTextBox.Text to Watch. Step through the zoo comparison and the Text assignment; confirm the Text value changes to "- - X -".-1 and becomes 1 when it finds Dolly.2. After the assignments, inspect this.ComoZoo.Pen.PenSpaces[1] as null and this.ComoZoo.Pen.PenSpaces[2].Name as Dolly.F5. Confirm Como’s map is - - X - and San Diego’s map remains - X - -.New Como Zoo. Confirm Como resets to - X - -.