Continue from your completed Zoo 2.2. Como keeps its four-space pen. San Diego replaces its pen with a three-row, four-column tank. Both areas will hold a mother and, after birth, a stationary baby.
Keep the Programming Concepts 2 window title.
Tank to Business ClassesUse the focused Tank class diagram below to declare Animal, StartingColumn, StartingRow, and TankSpaces. Copy the existing public-field StyleCop suppression attribute from Pen. Add using System; above the namespace if the class template has not already supplied it; GetDisplayLocation uses Environment.NewLine. Keep the class inside namespace ZooScenario. Add the Tank field to Zoo; keep its Pen field for Como. Use the existing XML-documentation style for new fields and methods.
Expression for new Animal[3, 4] |
Meaning |
|---|---|
TankSpaces.GetLength(0) |
3 rows; valid indexes 0–2 |
TankSpaces.GetLength(1) |
4 columns; valid indexes 0–3 |
TankSpaces.Length |
12 total spaces |
TankSpaces[1, 1] |
Second row, second column |
IsValidLocation(row, column)Use the diagram to return true only when both indexes are valid. GetLength arguments identify dimensions, not particular rows or columns.
ContainsAnimal(animal)Use foreach (Animal tankAnimal in this.TankSpaces). Start with false, skip the search when animal is null, and inspect each tankAnimal reference. Set the result to true and break when the references match. Return the result after the search.
FindAnimalRow(animal) from its activity diagramInitialize foundRow to -1. A match exits the inner loop; the following decision exits the outer loop. Preserve the first match.
FindAnimalColumn(animal) using its separate diagramReturn the column index, not the row index. Null and absent animals return -1.
GetAnimalLocation(animal)Call ContainsAnimal before searching for coordinates. Keep array indexes zero-based; add one only when constructing the displayed row and column numbers.
PlaceAnimalValidate the stored mother and starting coordinates, then assign the existing reference. The creation method must allocate TankSpaces before this method runs.
GetDisplayLocationCheck every array element with nested for loops. Append X for the mother, x for another non-null occupant, and - for an empty entry. Add spaces between columns and Environment.NewLine between rows, without trailing separators. Environment.NewLine supplies the line break between the rows of the displayed string.
rowChange and int columnChange parametersUse its activity diagram to find the mother, calculate the destination, check the current location and destination, and update the two array entries only when the destination is empty. These parameters are changes such as 0 and 1; they are not absolute destination indexes.
MoveAnimalInTank, add public void MoveAnimal(string direction) to Tank.csKeep the numeric movement logic in MoveAnimalInTank. Follow the switch activity: each recognized direction calls MoveAnimalInTank with the two integer changes shown, then breaks. This method only chooses the movement request. Leave an unrecognized direction without a movement call.
Replace the occupied-space decision with the mother/baby/empty decisions in the diagram. Do not identify the baby by name or position; the mother may visit index 0 before birth.
Pen.cs, edit only the existing method declaration: change public void MoveAnimal(int positionChange) to public void MoveAnimalInPen(int positionChange)Do not use Rename refactoring or Replace All, and do not change the existing callers during this step. Old calls temporarily refer to the removed integer signature; complete the wrapper, window helper, and arrow-handler steps before expecting the project to build. Keep the integer parameter and movement calculations. Add the empty-destination check after IsValidPosition, as shown in the activity diagram. This edit preserves the mother reference and protects the baby’s occupied space.
MoveAnimal(string direction) methodThe original movement code now belongs to MoveAnimalInPen; do not duplicate that code in the new method. The existing window helper will be updated below to pass a string; finish those caller edits before building. Follow the switch diagram to call MoveAnimalInPen(-1) for "Left" and MoveAnimalInPen(1) for "Right", with break after each case. Keep the two method names distinct so each method has one clear job.
Zoo.cs, following the class and activity diagramsStart an int variable named count at zero. For each occupied entry, count++ increases that number by one. If Pen exists, foreach through Pen.PenSpaces and increment only for non-null entries. Independently check Tank and do the same for Tank.TankSpaces. Return count. Do not add counting methods to Pen, Tank, or Animal. The existing informationTextBox will announce the count after zoo creation, successful birth, and each movement attempt. Put Environment.NewLine before the count so it appears on its own line.
Zoo.cs, add public void MoveFeaturedAnimal(string direction)Follow the diagram to forward direction to Pen.MoveAnimal when Pen exists, otherwise to Tank.MoveAnimal when Tank exists. Keep control updates in MainWindow. Como has a pen; San Diego has a tank.
MainWindow.xaml, replace the Animal pen label and its horizontal display/button StackPanel inside sanDiegoZooStackPanel with this complete blockKeep the temperature controls below it and keep Como’s controls unchanged. Removing sanDiegoPenDisplayTextBox temporarily leaves an outdated C# reference; replace that reference when updating DisplayAnimalArea below.
<TextBlock Text="Fish tank:" Margin="3,10,3,0"/> <StackPanel Orientation="Horizontal"> <TextBox x:Name="sanDiegoTankDisplayTextBox" Margin="5,5,10,5" Padding="5" FontFamily="Consolas" FontSize="24" Height="99" Width="109" HorizontalAlignment="Left" IsReadOnly="True" TextWrapping="NoWrap" AcceptsReturn="True" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled"/> <Grid Margin="10,5,5,5"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Button x:Name="moveSanDiegoZooAnimalUpButton" Width="25" FontSize="16" Grid.Row="0" Grid.Column="1" Content="^" Click="moveSanDiegoZooAnimalUpButton_Click"/> <Button x:Name="moveSanDiegoZooAnimalDownButton" Width="25" FontSize="16" Grid.Row="2" Grid.Column="1" Content="v" Click="moveSanDiegoZooAnimalDownButton_Click"/> <Button x:Name="moveSanDiegoZooAnimalLeftButton" Width="25" FontSize="16" FontWeight="Bold" Grid.Row="1" Grid.Column="0" Content="<" Click="moveSanDiegoZooAnimalLeftButton_Click"/> <Button x:Name="moveSanDiegoZooAnimalRightButton" Width="25" FontSize="16" FontWeight="Bold" Grid.Row="1" Grid.Column="2" Content=">" Click="moveSanDiegoZooAnimalRightButton_Click"/> </Grid> </StackPanel>
DisplayPen in MainWindow.xaml.cs and press Ctrl+R, Ctrl+RRename it to DisplayAnimalArea and apply the rename to its references. This updates existing calls from both zoos. You are starting from 2.2, so do not add a separate DisplayTank method.
Remove the common zoo.Pen map lookup. Compare zoo with this.ComoZoo to obtain the pen map and assign penDisplayTextBox.Text. Otherwise compare zoo with this.SanDiegoZoo to obtain the tank map and assign sanDiegoTankDisplayTextBox.Text. Use the diagram’s exact control names.
BirthArea.Mother is assignedFollow the diagram to allocate the tank, set starting row and column to 1, share FeaturedAnimal, place it, and call DisplayAnimalArea. Keep all earlier setup. Como keeps this.ComoZoo.Pen.PenSpaces = new Animal[4]; and this.ComoZoo.Pen.StartingPosition = 1;, and the renamed display call.
| 0 | 1 | 2 | 3 | |
|---|---|---|---|---|
| 0 | null | null | null | null |
| 1 | null | Patti | null | null |
| 2 | null | null | null | null |
MainWindow.xaml.cs, use Ctrl+R, Ctrl+R to rename the existing MoveAnimalInPen helper to MoveFeaturedAnimalKeep zoo and zooName, but change its final parameter to string direction. Follow the activity diagram to guard a missing zoo, call zoo.MoveFeaturedAnimal(direction), and refresh DisplayAnimalArea. Then compare zoo to this.ComoZoo or this.SanDiegoZoo to display that area’s location and CountAnimals result. Keep the exact messages shown in the diagram. Do not create separate pen and tank movement helpers in MainWindow.
Add the down handler and update all four existing left/right handlers using the table. If Visual Studio has already created a handler stub, edit that existing method instead of adding a second method with the same name. Every handler calls MainWindow.MoveFeaturedAnimal with the correct zoo, zooName, and direction string. Use RoutedEventArgs in each signature.
private void moveSanDiegoZooAnimalUpButton_Click(object sender, RoutedEventArgs e)
{
this.MoveFeaturedAnimal(this.SanDiegoZoo, "San Diego Zoo", "Up");
}| Handler name | Helper | Zoo | zooName |
direction |
|---|---|---|---|---|
moveComoZooAnimalLeftButton_Click |
MoveFeaturedAnimal |
this.ComoZoo |
"Como Zoo" |
"Left" |
moveComoZooAnimalRightButton_Click |
MoveFeaturedAnimal |
this.ComoZoo |
"Como Zoo" |
"Right" |
moveSanDiegoZooAnimalDownButton_Click |
MoveFeaturedAnimal |
this.SanDiegoZoo |
"San Diego Zoo" |
"Down" |
moveSanDiegoZooAnimalLeftButton_Click |
MoveFeaturedAnimal |
this.SanDiegoZoo |
"San Diego Zoo" |
"Left" |
moveSanDiegoZooAnimalRightButton_Click |
MoveFeaturedAnimal |
this.SanDiegoZoo |
"San Diego Zoo" |
"Right" |
Preserve San Diego’s existing temperature update.
// newComoZooButton_Click
this.DisplayMessage("Created " + this.ComoZoo.Name + "." + Environment.NewLine + "Animals in pen: " + this.ComoZoo.CountAnimals() + ".");
// newSanDiegoZooButton_Click
this.DisplayMessage("Created " + this.SanDiegoZoo.Name + "." + Environment.NewLine + "Animals in tank: " + this.SanDiegoZoo.CountAnimals() + ".");Keep the existing birth workflow and its returned baby reference. Add the space check before BirthAnimal is called so a blocked birth leaves the unborn baby and the mother unchanged.
Keep the missing-zoo guard. Check the baby’s destination before calling BirthAnimal, retain the existing failure message, and only place a non-null returned baby. Name it Baby, assign it to the specified array entry, refresh the area, and append CountAnimals to the success message.
Keep the missing-zoo guard. Check the baby’s destination before calling BirthAnimal, retain the existing failure message, and only place a non-null returned baby. Name it Baby, assign it to the specified array entry, refresh the area, and append CountAnimals to the success message. Preserve the temperature update in the platypus handler.
Search for DisplayPen, DisplayTank, and sanDiegoPenDisplayTextBox; the completed code should use DisplayAnimalArea and sanDiegoTankDisplayTextBox instead. Resolve build errors before testing. Search within MainWindow.xaml.cs and MainWindow.xaml, rather than in the diagrams showing the old names. Confirm TankSpaces is allocated before placement, display, search, or movement runs.
| Action | Expected result |
|---|---|
| Click any movement arrow before creating its zoo | Create-zoo message; no exception |
| Create both zoos | Como: - X - -; San Diego: three rows with X at [1, 1]; each creation message reports 1 |
| Birth both babies without moving the mothers | Como: x X - -; San Diego: x at [0, 0] and X at [1, 1]; each success reports 2 |
| Move Como left after birth | Array and map stay x X - -; count remains 2 |
| Move Como right twice, then right again | Mother reaches index 3; the extra move is blocked; baby stays at index 0 |
Click New San Diego Zoo, move up and left once each, then click Birth platypus |
Mother occupies [0, 0]; birth is blocked before reproduction |
| Move that mother right, then birth | Baby occupies [0, 0]; mother stays [0, 1]; count becomes 2 |
| Click San Diego left now | Baby blocks the destination; both references and count are unchanged |
| Move to each tank boundary and attempt to cross it | No invalid index access; mother stays in bounds |
| Create one zoo again | Its map resets to one mother and the creation message reports 1; the other zoo is unchanged |
Confirm earlier behavior is preserved. Try an unsupported direction in a temporary edit: the area should return without moving; restore the handler afterward. No averages or species-letter markers are required.
Use F11 (Step Into), Watch, and Autos to follow a San Diego right-arrow click. A highlighted statement normally has not executed yet. Use F11 through method calls and returns; no Immediate Window commands are needed.
Click New San Diego Zoo again if you previously moved Patti or gave birth; the upcoming tables assume a fresh tank with only Patti at [1, 1]. Set a breakpoint on the MoveFeaturedAnimal call inside moveSanDiegoZooAnimalRightButton_Click, then click that arrow. Open Debug > Windows > Watch > Watch 1 and add the expressions in the table before stepping into the helper.
| Expression | In the button handler | Inside MainWindow.MoveFeaturedAnimal |
|---|---|---|
this.SanDiegoZoo.Tank.TankSpaces |
3 rows × 4 columns; Patti at [1, 1] | Same array |
zoo.Name |
Unavailable in this scope | "San Diego Zoo" |
| direction | Unavailable in this scope | "Right" |
MainWindow.MoveFeaturedAnimal, then step into zoo.MoveFeaturedAnimal(direction)| Current method | this refers to | Watch expressions and expected values | Next F11 action |
|---|---|---|---|
Zoo.MoveFeaturedAnimal |
The San Diego Zoo object | this.Pen: nullthis.Tank: the tank referencedirection: "Right" |
Step into this.Tank.MoveAnimal(direction). |
Tank.MoveAnimal |
The tank object | direction is "Right"; rowChange and columnChange are unavailable because they are parameters of MoveAnimalInTank |
Follow the "Right" case and step into this.MoveAnimalInTank(0, 1). |
Tank.MoveAnimalInTank |
The same tank object | rowChange is 0; columnChange is 1; direction is unavailable because it belongs to the calling method |
Continue into the coordinate searches. |
Tank.MoveAnimalInTank, add this.TankSpaces, this.Animal.Name, row, column, newRow, and newColumn to WatchUse F11 to follow the searches. Observe each expected value after the indicated statement executes. In each Watch-expression cell, add each displayed line as a separate Watch entry; the expected values appear in the same order.
| Current method | Pause after | Watch expression | Expected value or observation |
|---|---|---|---|
Tank.FindAnimalRow |
The result initializer | foundRow |
-1 |
Tank.FindAnimalRow |
The loop initializers | row column |
0 0; row 0 has no mother reference. |
Tank.FindAnimalRow |
The matching assignment | row column foundRow |
1 1 1 |
Tank.FindAnimalRow |
The matching assignment | this.TankSpaces[row, column] |
The same Patti reference held by this.Animal. |
Tank.MoveAnimalInTank |
FindAnimalRow returns and its assignment executes |
row | 1 |
Tank.FindAnimalColumn |
The result initializer | foundColumn |
-1 |
Tank.FindAnimalColumn |
The matching assignment | row column foundColumn |
1 1 1 |
Tank.MoveAnimalInTank |
FindAnimalColumn returns and its assignment executes |
column | 1 |
The search loop variables and the movement helper’s locals belong to different methods. Follow both breaks in each search before returning to the movement helper.
Tank.MoveAnimalInTank, row and column receive 1 and 1Use F11 to inspect the destination and follow the two array assignments.
| Current method | Pause after | Watch expression | Expected value or observation |
|---|---|---|---|
Tank.MoveAnimalInTank |
The newRow assignment |
newRow |
1 |
Tank.MoveAnimalInTank |
The newColumn assignment |
newColumn |
2 |
Tank.IsValidLocation |
Entering the method | row column |
1 2 |
Tank.MoveAnimalInTank |
The validity call returns | this.TankSpaces[newRow, newColumn] |
null; the destination is empty. |
Tank.MoveAnimalInTank |
The old-entry assignment | this.TankSpaces[1, 1] |
null |
Tank.MoveAnimalInTank |
The old-entry assignment | this.Animal.Name |
"Patti"; clearing the entry has not lost the mother reference. |
Tank.MoveAnimalInTank |
The destination assignment | this.TankSpaces[1, 2] |
Patti |
The window map still shows its previous value. It refreshes after movement returns to MainWindow.
Tank.MoveAnimalInTank, Tank.MoveAnimal, and Zoo.MoveFeaturedAnimal until MainWindow resumesContinue with F11 through the map refresh, location description, and count.
| Current method | F11 action or statement to execute | Watch expression | Expected value or observation |
|---|---|---|---|
MainWindow.DisplayAnimalArea |
Pause before entering GetDisplayLocation. |
this.sanDiegoTankDisplayTextBox.Text |
Still shows the previous map. While inside Tank, this window expression is unavailable. |
MainWindow.DisplayAnimalArea |
Execute the Text assignment. | this.sanDiegoTankDisplayTextBox.Text |
Three rows; the mother marker is now at row index 1, column index 2. |
Tank.ContainsAnimal |
Follow the foreach called by GetAnimalLocation. |
tankAnimalcontainsAnimal |
Patti when the matching entry is reached. true after the assignment; break then ends the search. |
Tank.GetAnimalLocation |
Follow both coordinate searches and the display-coordinate assignments. | row column displayRowdisplayColumn |
1 2 2 3 |
Tank.GetAnimalLocation |
Execute the description assignment. | location | "Patti is in tank row 2, column 3." |
Zoo.CountAnimals |
Follow the area checks. | this.Penthis.Tank |
null The San Diego tank reference. |
Zoo.CountAnimals |
Follow the foreach and matching increment. | animal count |
Patti at the occupied entry. 1 after incrementing for Patti. |
MainWindow.DisplayMessage |
Execute the Text assignment. | this.informationTextBox.Text |
"Patti is in tank row 2, column 3.""Animals in tank: 1." |
New San Diego Zoo, click Birth platypus, and move up once so Patti is at [0, 1]Break on the left-arrow attempt. Use F11 to follow MainWindow, Zoo, and Tank.MoveAnimal into Tank.MoveAnimalInTank(0, -1).
| Current method | Watch expression or condition | Expected value or observation |
|---|---|---|
Tank.MoveAnimalInTank |
rowChangecolumnChange |
0 -1 |
Tank.MoveAnimalInTank |
row column |
0 1, after the search assignments execute. |
Tank.MoveAnimalInTank |
newRownewColumn |
0 0, after the destination assignments execute. |
Tank.MoveAnimalInTank |
this.IsValidLocation(newRow, newColumn) |
true; the baby’s position is inside the tank. |
Tank.MoveAnimalInTank |
this.TankSpaces[newRow, newColumn] |
The Baby reference. |
Tank.MoveAnimalInTank |
this.TankSpaces[newRow, newColumn] == null |
false; neither movement assignment runs. |
MainWindow.DisplayAnimalArea |
this.sanDiegoTankDisplayTextBox.Text |
The baby remains x at [0, 0]; Patti remains X at [0, 1]. |
Zoo.CountAnimals |
count | 2 before returning. |
Remove breakpoints after the check.
Compare your finished classes with the diagram. Green identifies new members, blue identifies changed members, and red identifies renamed old members.
Confirm Como uses four pen spaces, San Diego uses three tank rows and four columns, both babies remain fixed, and the count changes only when occupancy changes. Submit the completed Zoo 2.3 project using your course procedure.