1.1 Zoo Learning Activity

Overview
1.Project Foundation
A Object Instantiation and Assigning Field Values

In this activity, you will start from the provided Zoo 1.1 LastName project and complete the first version of the Como Zoo object model.

B User Story: Como Zoo Starter Program

The manager of Como Zoo has asked for the first version of a program that stores basic zoo information. This version will create the main Como Zoo object and connect it to the first objects the zoo needs to track: a featured animal, restrooms, a ticket booth, and the employee working at the booth.

This checkpoint gives the rest of the project a foundation. Later checkpoints will build on these objects and add more behavior.

C Acceptance Criteria
Details
  • Create a ComoZoo object.
  • Store the zoo's name and capacity.
  • Store a featured animal with its starting information.
  • Store both a ladies' restroom and a men's restroom.
  • Store a ticket booth with its ticket price.
  • Store the employee assigned to the ticket booth.
  • Run the program and verify the object values with the debugger.
2.Set Up Zoo Starting Code
A Download Zoo 1.1 LastName.zip from this folder
Zoo 1.1 LastName.zip
1 Unzip the folder into your working location
2 Rename the parent folder and solution name by replacing LastName with your own last name, if your instructor requires that naming pattern
3 Open the solution in Visual Studio
4 If Visual Studio prompts you to restore packages, restore the NuGet packages before you start editing
File Explorer showing the extracted Zoo 1.1 LastName folder being renamed.
Rename the extracted project folder by replacing LastName with your own last name.
File Explorer showing the Zoo 1.1 LastName solution file being renamed.
Rename the solution file by replacing LastName with your own last name.
Visual Studio Solution Explorer showing the required solution and project naming pattern.
Open the solution and confirm that the solution and project names use the required naming pattern.
Visual Studio Output window showing NuGet packages restored successfully.
If prompted, restore the NuGet packages and wait for the restore to finish.
3.Explore the New Como Zoo Event Handler
Visual Studio showing MainWindow.xaml and the New Como Zoo button highlighted.
Double-clicking the button opens its event handler in MainWindow.xaml.cs.
A Check Your Work
1 Open ZooScenario/MainWindow.xaml and double-click New Como Zoo.
2 Confirm Visual Studio opens newComoZooButton_Click in MainWindow.xaml.cs.
3 Click the left margin beside the empty handler’s opening brace. Confirm a red breakpoint dot appears.
4 Press F5 to start the application, then click New Como Zoo. Confirm execution pauses in that handler.
5 Press F5 to continue. Stop debugging, then select Debug > Delete All Breakpoints.
6 Run again and click New Como Zoo. Confirm the handler no longer pauses.
4.Instantiate the Como Zoo
Current object state after this section. Only items introduced in section 1 are marked with green squares.
Current object state after this task.
  • New
  • Changed
  • Removed
// Create an instance of the Zoo class.
this.ComoZoo = new Zoo();
newComoZooButton_Click event handler
Show where the student should add the Zoo object instantiation inside MainWindow.xaml.cs.
ComoZoo after instantiation
Show the breakpoint and the Locals or Watch window after F10 (Step Over), with this.ComoZoo no longer null.
A Check Your Work
1 In MainWindow.xaml.cs, set a breakpoint on this.ComoZoo = new Zoo(); inside newComoZooButton_Click.
2 Press F5 and click New Como Zoo. Open Debug > Windows > Watch > Watch 1.
3 Add this.ComoZoo to Watch. Before the highlighted statement runs, confirm null.
4 Press F10 once. Confirm this.ComoZoo now expands as a Zoo object.
5 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
5.Assign the Como Zoo's Capacity
Current object state after this section. Only items introduced in section 2 are marked with green squares.
Current object state after this task.
  • New
  • Changed
  • Removed
// Assign field values of Como Zoo.
this.ComoZoo.Capacity = 1000;
Zoo capacity value
Show this.ComoZoo.Capacity in the Locals or Watch window after the assignment executes.
A Check Your Work
1 Set a breakpoint on this.ComoZoo.Capacity = 1000; in newComoZooButton_Click.
2 Start with F5 and click New Como Zoo. Add this.ComoZoo.Capacity to Watch.
3 Confirm this.ComoZoo.Capacity is 0.
4 Press F10 once to execute the assignment.
5 Confirm this.ComoZoo.Capacity is 1000.
6 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
B Try the Error Intentionally
1 Stop debugging and place // at the beginning of this.ComoZoo = new Zoo(); so that line is temporarily commented out
2 Set a breakpoint on this.ComoZoo.Capacity = 1000; in ZooScenario/MainWindow.xaml.cs
3 Start the application with F5 and click the New Como Zoo button
4 When execution reaches the capacity line, inspect this.ComoZoo and confirm that it is null
5 Press F10 (Step Over). Observe the null-reference error because the program tried to use a Zoo object that was never created
6 Stop debugging and remove the // from this.ComoZoo = new Zoo();
7 Run the application again and confirm that the capacity assignment now executes successfully
6.Create the Featured Animal
Current object state after this section. Only items introduced in section 3 are marked with green squares.
Current object state after this task.
  • New
  • Changed
  • Removed
// Create the featured animal.
this.ComoZoo.FeaturedAnimal = new Animal();
A Check Your Work
1 Set a breakpoint on this.ComoZoo.FeaturedAnimal = new Animal();.
2 Start with F5 and click New Como Zoo. Add this.ComoZoo.FeaturedAnimal to Watch.
3 Confirm this.ComoZoo.FeaturedAnimal is null.
4 Press F10 once. Confirm the reference now expands as an Animal object.
5 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
7.Assign the Como Zoo's Remaining Main Fields
Current object state after this section. Only items introduced in section 4 are marked with green squares.
Current object state after this task.
  • New
  • Changed
  • Removed
// Create the restrooms.
this.ComoZoo.LadiesRoom = new Restroom();
this.ComoZoo.MensRoom = new Restroom();
// Assign the remaining field value of Como Zoo.
// You may place this beside Capacity to group Zoo field values.
this.ComoZoo.Name = "Como Zoo";
// Create the ticket booth.
this.ComoZoo.TicketBooth = new Booth();
A Check Your Work
1 Break on the first remaining-object assignment in newComoZooButton_Click. Start and click New Como Zoo.
2 Use F10 to execute each assignment once. Watch the corresponding reference change from null to its object.
3 Confirm this.ComoZoo.LadiesRoom and this.ComoZoo.MensRoom each expand as a Restroom object.
4 Confirm this.ComoZoo.TicketBooth expands as a Booth object.
5 Confirm this.ComoZoo.Name is "Como Zoo".
6 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
8.Assign the Featured Animal's Fields
Current object state after this section. Only items introduced in section 5 are marked with green squares.
Current object state after this task.
  • New
  • Changed
  • Removed
// Assign field values of the featured animal.
this.ComoZoo.FeaturedAnimal.Age = 4;
this.ComoZoo.FeaturedAnimal.Sex = "Female";
this.ComoZoo.FeaturedAnimal.IsPregnant = true;
this.ComoZoo.FeaturedAnimal.Name = "Dolly";
this.ComoZoo.FeaturedAnimal.Type = "Dingo";
this.ComoZoo.FeaturedAnimal.Weight = 35.3;
A Check Your Work
1 Break on the first featured-animal field assignment. Start and click New Como Zoo.
2 Use F10 to execute the featured-animal assignments, then inspect these full paths in Watch.
3 Confirm this.ComoZoo.FeaturedAnimal.Name is "Dolly".
4 Confirm this.ComoZoo.FeaturedAnimal.Type is "Dingo".
5 Confirm this.ComoZoo.FeaturedAnimal.Sex is "Female".
6 Confirm this.ComoZoo.FeaturedAnimal.Age is 4.
7 Confirm this.ComoZoo.FeaturedAnimal.IsPregnant is true.
8 Confirm this.ComoZoo.FeaturedAnimal.Weight is 35.3.
9 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
9.Assign the Restroom and Ticket Booth Fields
Current object state after this section. Only items introduced in section 6 are marked with green squares.
Current object state after this task.
  • New
  • Changed
  • Removed
// Assign field values of the ladies' restroom.
this.ComoZoo.LadiesRoom.Capacity = 4;
this.ComoZoo.LadiesRoom.Sex = "Female";
// Assign field values of the men's restroom.
this.ComoZoo.MensRoom.Capacity = 4;
this.ComoZoo.MensRoom.Sex = "Male";
// Create and assign field values of the ticket booth.
this.ComoZoo.TicketBooth.Attendant = new Employee();
this.ComoZoo.TicketBooth.TicketPrice = 15.00m;
A Check Your Work
1 Break on the first restroom assignment. Start and click New Como Zoo.
2 Step over each restroom and ticket-booth assignment with F10.
3 Confirm this.ComoZoo.LadiesRoom.Capacity is 4.
4 Confirm this.ComoZoo.LadiesRoom.Sex is "Female".
5 Confirm this.ComoZoo.MensRoom.Capacity is 4.
6 Confirm this.ComoZoo.MensRoom.Sex is "Male".
7 Confirm this.ComoZoo.TicketBooth.TicketPrice is 15.00m.
8 Confirm this.ComoZoo.TicketBooth.Attendant expands as an Employee object.
9 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
10.Assign the Attendant's Fields
Current object state after this section. Only items introduced in section 7 are marked with green squares.
Current object state after this task.
  • New
  • Changed
  • Removed
// Assign field values of the ticket booth attendant.
this.ComoZoo.TicketBooth.Attendant.Name = "Sam";
this.ComoZoo.TicketBooth.Attendant.Number = 42;
A Check Your Work
1 Set a breakpoint on the attendant Name assignment in newComoZooButton_Click.
2 Start and click New Como Zoo. Step over the Name and Number assignments with F10.
3 Confirm this.ComoZoo.TicketBooth.Attendant.Name is "Sam".
4 Confirm this.ComoZoo.TicketBooth.Attendant.Number is 42.
5 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
11.Finish and Submit

Before submitting, make sure the project runs and uses the expected object values.

A Check Your Work
1 In MainWindow.xaml.cs, set a breakpoint on the closing brace of newComoZooButton_Click by clicking its left margin.
2 Press F5 to start or continue the application. Click New Como Zoo.
3 When Visual Studio pauses, open Debug > Windows > Watch > Watch 1. Enter each full expression below in a separate Watch row.
4 Confirm this.ComoZoo.Name is "Como Zoo".
5 Confirm this.ComoZoo.Capacity is 1000.
6 Confirm this.ComoZoo.FeaturedAnimal.Name is "Dolly".
7 Confirm this.ComoZoo.FeaturedAnimal.Weight is 35.3.
8 Confirm this.ComoZoo.LadiesRoom.Capacity is 4.
9 Confirm this.ComoZoo.MensRoom.Capacity is 4.
10 Confirm this.ComoZoo.TicketBooth.TicketPrice is 15.00m.
11 Confirm this.ComoZoo.TicketBooth.Attendant.Name is "Sam".
12 Confirm this.ComoZoo.TicketBooth.Attendant.Number is 42.
13 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
14 Select Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.
15 Review warnings in the Error List and resolve StyleCop warnings caused by your changes. Save all files before submission.
B Submit Your Work

Stop debugging, save your work, and submit according to your instructor's directions.

12.Checkpoint Summary
Details
  • A Zoo object is assigned to MainWindow.ComoZoo.
  • The Zoo stores its name and capacity.
  • The Zoo references a featured animal, two restrooms, and a ticket booth.
  • The ticket booth references its attendant.
Ending object diagram. All objects, reference names, and field values added during Zoo 1.1 are marked with green squares.
Ending object diagram for Zoo 1.1.
  • New
  • Changed
  • Removed