1.1 Zoo Learning Activity

Overview

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.

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.

Acceptance Criteria

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

Task List

  1. Set Up Zoo Starting Code
  2. Explore the New Como Zoo Event Handler
  3. Instantiate the Como Zoo
  4. Assign the Como Zoo's Capacity
  5. Create the Featured Animal
  6. Assign the Como Zoo's Remaining Main Fields
  7. Assign the Featured Animal's Fields
  8. Assign the Restroom and Ticket Booth Fields
  9. Assign the Attendant's Fields
  10. Finish and Submit
  11. Review the Checkpoint Summary
Exact Casing Matters

Names in C# must match exactly. A missing capital letter, a renamed event handler that does not match XAML, or a changed class or field name can prevent the project from building or running correctly.

Watch Out For

Most issues in this checkpoint come from one character being different: a missing capital letter, a renamed event handler that does not match XAML, or a class file that was created outside the expected folder.

Before You Begin

Each code block shows only the line or lines you need to add or change. Do not recopy code that is already in the file. Place each snippet inside the correct method and inside the correct set of braces.

For this checkpoint, the code changes belong in ZooScenario/MainWindow.xaml.cs, inside the newComoZooButton_Click event handler.

You have some freedom in how you organize the lines. The key rule is that an object must exist before you assign values to its fields.

  1. Set Up Zoo Starting Code

    Begin with the starter ZIP file supplied by your instructor.

    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.
    Go to top
  2. Explore the New Como Zoo Event Handler

    The Goal

    Before adding any zoo code, confirm that the New Como Zoo button is already connected to code that runs when the button is clicked.

    Your Task

    Open ZooScenario/MainWindow.xaml, locate the New Como Zoo button, and double-click it. Visual Studio should open MainWindow.xaml.cs and place the cursor inside the existing newComoZooButton_Click 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.

    Verify Your Work

    What This Means

    The button is already connected to its event handler. A breakpoint pauses the program so you can inspect what is happening, but the breakpoint is not required for the button or the event handler to work.

    Go to top
  3. Instantiate the Como Zoo

    The Goal

    The MainWindow already has a ComoZoo field, but that field does not point to a real Zoo object yet.

    Your Task

    Inside the newComoZooButton_Click event handler, create one new Zoo object and store it in this.ComoZoo. Place it near the start of the method, before any code that uses this.ComoZoo.

    Current object state after this section. Only items introduced in section 1 are marked with green squares.
    Current object state after this task.
    // Create an instance of the Zoo class.
    this.ComoZoo = new Zoo();
    
    What This Code Does

    This line creates one Zoo object in memory. The this.ComoZoo = part saves a reference to that object so the rest of the method can keep filling in information for the same zoo. The comment above the line labels this part of the event handler so the object-creation code is easy to distinguish from the field assignments that follow.

    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.

    Verify Your Work

    What This Means

    Once this line runs, this.ComoZoo is no longer null. It now points to the zoo object that the rest of this checkpoint will fill in.

    Go to top
  4. Assign the Como Zoo's Capacity

    The Goal

    The zoo object now exists, but its Capacity field still has its default value.

    Your Task

    Give the existing zoo object a capacity value. Add this after this.ComoZoo has been created, near the other zoo field assignments.

    Current object state after this section. Only items introduced in section 2 are marked with green squares.
    Current object state after this task.
    // Assign field values of Como Zoo.
    this.ComoZoo.Capacity = 1000;
    
    Understanding the Change

    This line does not create another object. It follows the reference stored in this.ComoZoo and fills in the Capacity field on the existing zoo object.

    Zoo capacity value
    Show this.ComoZoo.Capacity in the Locals or Watch window after the assignment executes.

    Verify Your Work

    Try the Error Intentionally

    Why You Are Trying This

    Before moving on, intentionally create a common programming mistake so you can see why the order of these statements matters. The line this.ComoZoo = new Zoo(); must run before the capacity assignment. Otherwise, this.ComoZoo is still null, and the program cannot assign a capacity to a zoo object that does not exist.

    You will temporarily comment out the object-creation line to observe the error. Restore the line before continuing to the next task.

    and Diagnosing a ReferenceException.

    Go to top
  5. Assign the Como Zoo's Remaining Main Fields

    The Goal

    The zoo object still needs its main related objects and its name.

    Your Task

    Create the restroom and ticket booth objects, and assign the zoo's name. Add these lines after the zoo object exists. The restroom and booth objects must be created before you assign values to their fields later.

    Current object state after this section. Only items introduced in section 4 are marked with green squares.
    Current object state after this task.
    // 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();
    
    Understanding the Change

    The restroom and ticket booth lines create objects. The name line fills in a string value on the zoo object.

    Main Zoo object references
    Show LadiesRoom, MensRoom, TicketBooth, and Name under this.ComoZoo in the debugger.

    Verify Your Work

    What's Different Now?

    The main zoo object now has references to its restroom objects and ticket booth object. It also has a name stored directly on the zoo.

    Go to top
  6. Assign the Restroom and Ticket Booth Fields

    The Goal

    The restroom and ticket booth objects exist, but they still need values. The ticket booth also needs an employee object before you can fill in employee information.

    Your Task

    Fill in the restroom fields, set the ticket price, and create the attendant object. Add these lines after LadiesRoom, MensRoom, and TicketBooth have been created.

    Current object state after this section. Only items introduced in section 6 are marked with green squares.
    Current object state after this task.
    // Assign field values of the ladies' restroom.
    this.ComoZoo.LadiesRoom.Capacity = 4;
    this.ComoZoo.LadiesRoom.Gender = "Female";
    // Assign field values of the men's restroom.
    this.ComoZoo.MensRoom.Capacity = 4;
    this.ComoZoo.MensRoom.Gender = "Male";
    // Create and assign field values of the ticket booth.
    this.ComoZoo.TicketBooth.Attendant = new Employee();
    this.ComoZoo.TicketBooth.TicketPrice = 15.00m;
    
    What This Code Does

    The ticket booth can store a reference to an employee. The line with new Employee() creates that employee object so the next section can fill in the attendant's name and number.

    Restroom and ticket booth values
    Show the expanded restroom and ticket booth objects with their assigned values.

    Verify Your Work

    Putting It Together

    The zoo now has restroom details and a ticket booth that knows its ticket price. The booth also has an attendant object ready for employee information.

    Go to top
  7. Assign the Attendant's Fields

    The Goal

    The ticket booth now has an attendant object, but the attendant does not yet have values.

    Your Task

    Fill in the fields on the existing attendant object. Add these lines after the attendant object has been created.

    Current object state after this section. Only items introduced in section 7 are marked with green squares.
    Current object state after this task.
    // Assign field values of the ticket booth attendant.
    this.ComoZoo.TicketBooth.Attendant.Name = "Sam";
    this.ComoZoo.TicketBooth.Attendant.Number = 42;
    
    Ticket booth attendant values
    Show the expanded Attendant object with the expected Name and Number.

    Verify Your Work

    Key Takeaway

    The ticket booth attendant now has identifying information. The program can connect the ticket booth to a specific employee instead of leaving the employee object blank.

    Go to top
  8. Finish and Submit

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

    Verify Your Work

    Submit Your Work

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

    Go to top

Checkpoint Summary

You created the first complete Como Zoo object graph. The final diagram shows the objects, references, and field values that should exist when the checkpoint is complete.

Diagram MarkerNew in this checkpoint
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.