1.2 Zoo Learning Activity

Zoo Instruction
1.Overview
A Classes and Reusable Object Blueprints

Zoo 1.2 shifts from working mainly with individual objects to defining the class blueprints those objects use. You will add BirthingRoom and VendingMachine, create Como Zoo objects from those definitions, and reuse the same class design for a separate San Diego Zoo.

A class defines a reusable kind of object. Multiple objects can be created from the same class, and each object can maintain its own state.

B Getting Started
C Make a copy of the Zoo 1.1 [LastName] folder
D Rename the copied folder to Zoo 1.2 [LastName]
E Rename Zoo 1.1 [LastName].sln to Zoo 1.2 [LastName].sln
F Open the copied Zoo 1.2 solution

Make a copy of your code at the beginning of each week and rename the folder and .sln file to the appropriate week number. Doing so will help you maintain a record of your work and keep a working copy of your code in case you need to revert your changes.

G Starting Point — End of Zoo 1.1

This class structure already exists. Zoo 1.2 extends it.

Zoo 1.1 starting class diagram. MainWindow references Zoo through ComoZoo. Zoo references Animal through FeaturedAnimal, Restroom through LadiesRoom and MensRoom, and Booth through TicketBooth. Booth references Employee through Attendant. Existing scalar members are shown in their owning classes.
Authoritative Zoo 1.1 End class structure.
  • New
  • Changed
  • Removed
H Acceptance Criteria
  • BirthingRoom and VendingMachine use only the current Zoo 1.2 fields.
  • Zoo contains BirthArea : BirthingRoom and AnimalSnackMachine : VendingMachine.
  • Como Zoo has its own configured birthing-room and vending-machine objects.
  • BirthArea.Doctor refers to Flora, number 98.
  • MainWindow contains SanDiegoZoo : Zoo.
  • San Diego Zoo reuses the same class definitions while maintaining separate object state.
  • No later-course design techniques are added.
Use a birthing room for animal care
2.Add the birthing room to the zoo model

Define BirthingRoom and add only the current Zoo 1.2 fields and class relationships required by the scenario.

A Add the BirthingRoom class

Before you write the new class, use Solution Explorer to add the class file to the ZooScenario project.

B Add a class through Solution Explorer
C In Solution Explorer, right-click the ZooScenario project node
D Choose Add
E Choose Class...
F In the Add New Item workflow, create the file as BirthingRoom.cs
Visual Studio Solution Explorer showing the ZooScenario project context menu with Add expanded and Class visible.
Right-click the ZooScenario project, choose Add , then choose Class... .

Now continue with the class structure for BirthingRoom.

Progressive class diagram. A green square marks BirthingRoom as the new class. Existing Zoo, Animal, and Employee classes provide context.
Add the BirthingRoom class.
  • New
  • Changed
  • Removed
G Move the BirthingRoom class into the Business Classes folder

Insert the following suppression pattern. Keep the two attributes together in one horizontally scrollable code frame and keep each attribute on one physical source line:

C#

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Event handlers may begin with lower-case letters.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Encapsulation not yet taught.")]
H Add the temperature fields

BirthingRoom exists, but it does not yet describe the temperature information the current scenario needs.

Add the five current scalar fields shown in the class diagram.

Progressive BirthingRoom class diagram. Green squares mark the new fields InitialTemperature double, MaxTemperature double, MinTemperature double, TemperatureIncrease double, and Temperature double.
The five current temperature fields belong inside BirthingRoom.
  • New
  • Changed
  • Removed

These values belong to one birthing-room object's state. double supports the current fractional values such as 77.0 and 0.5.

I Add the five fields shown by the diagram
J Add comments to the class and each field, following the comment style already used in the project
K Check Your Work
1 Select Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.
2 Open BirthingRoom.cs. Confirm the five numeric fields are named BirthingRoom.InitialTemperature, BirthingRoom.MaxTemperature, BirthingRoom.MinTemperature, BirthingRoom.TemperatureIncrease, BirthingRoom.Temperature.
3 Confirm each of those five field declarations uses double.
L Add the Animal relationship

A birthing-room object must be able to refer to an animal when the scenario requires one.

Add the current Animal-typed field represented as a class relationship.

Progressive class diagram showing BirthingRoom related to Animal. A green square marks the new relationship role Animal.
BirthingRoom can reference an Animal object through the Animal role.
  • New
  • Changed
  • Removed

Animal is the class/type and also the field/role name. The class relationship describes what a BirthingRoom can reference; it does not create an Animal object.

M Add the documented current class-typed field represented by the diagram
N Check Your Work
1 Select Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.
2 Open BirthingRoom.cs. Confirm the declaration public Animal Animal; is inside the class.
3 Confirm the type is the existing Animal class and the field name is BirthingRoom.Animal. Declaring this field does not create an animal.
O Add the Doctor relationship

The room also needs a reference to the employee serving as the doctor.

Add the current Employee-typed Doctor field.

Progressive class diagram showing BirthingRoom related to Employee. A green square marks the new Doctor relationship. The established Animal relationship remains unmarked.
Doctor is an Employee role, not a separate Doctor class.
  • New
  • Changed
  • Removed
P Add the documented current Doctor field represented by the diagram
Q Check Your Work
1 Select Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.
2 Confirm BirthingRoom.cs declares public Employee Doctor;.
3 Confirm BirthingRoom.Doctor uses the existing Employee type. Check Solution Explorer: this task should not have added a separate Doctor.cs class.
3.Set up the Como Zoo birthing room

Connect Zoo to BirthingRoom, then create and configure the Como BirthArea.

A Add BirthArea to Zoo
Progressive class diagram showing a green square on the new BirthArea relationship from Zoo to BirthingRoom. BirthingRoom already has its temperature fields and Animal and Doctor relationships.
Zoo can reference its own BirthingRoom through BirthArea.
  • New
  • Changed
  • Removed

Declaring BirthArea creates the class relationship. It does not create the runtime BirthingRoom object.

B Add the documented BirthArea field to Zoo
C Check Your Work
1 Select Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.
2 Open Zoo.cs. Confirm public BirthingRoom BirthArea; is inside the class.
3 Confirm the existing Zoo.FeaturedAnimal, Zoo.LadiesRoom, Zoo.MensRoom and Zoo.TicketBooth declarations remain.
D Create and configure the Como BirthArea
E Create a BirthingRoom object in the existing Como Zoo creation flow and store it in ComoZoo.BirthArea
F Set this.ComoZoo.BirthArea.InitialTemperature = 77.0;, this.ComoZoo.BirthArea.MaxTemperature = 85.0;, this.ComoZoo.BirthArea.MinTemperature = 55.0;, this.ComoZoo.BirthArea.TemperatureIncrease = 0.5;, this.ComoZoo.BirthArea.Temperature = this.ComoZoo.BirthArea.InitialTemperature;
G Create and configure Como Zoo’s doctor
1 Assign this.ComoZoo.BirthArea.Doctor = new Employee();
2 Assign this.ComoZoo.BirthArea.Doctor.Name = "Flora";
3 Assign this.ComoZoo.BirthArea.Doctor.Number = 98;

This focused source line adds separate meaning because it shows that the current temperature is initialized from the initial temperature:

C#

this.ComoZoo.BirthArea.Temperature = this.ComoZoo.BirthArea.InitialTemperature;
Completed Como BirthArea object diagram. Green squares mark the new BirthArea object and reference, the assigned values InitialTemperature 77.0, MaxTemperature 85.0, MinTemperature 55.0, TemperatureIncrease 0.5, and Temperature 77.0, plus the new Flora Employee object with Name Flora and Number 98 and the Doctor reference from BirthArea to Flora. Untouched default or uninitialized members are omitted.
Completed Como BirthArea state. Only intentionally established current state is shown.
  • New
  • Changed
  • Removed
H 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.BirthArea.InitialTemperature is 77.0.
5 Confirm this.ComoZoo.BirthArea.MaxTemperature is 85.0.
6 Confirm this.ComoZoo.BirthArea.MinTemperature is 55.0.
7 Confirm this.ComoZoo.BirthArea.TemperatureIncrease is 0.5.
8 Confirm this.ComoZoo.BirthArea.Temperature is 77.0.
9 Confirm this.ComoZoo.BirthArea.Doctor.Name is "Flora".
10 Confirm this.ComoZoo.BirthArea.Doctor.Number is 98.
11 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
Provide animal food through a vending machine
4.Add the vending machine to the zoo model

Define VendingMachine with the current five fields and connect it to Zoo.

A Add VendingMachine
Progressive class diagram. Green squares mark the new VendingMachine class and its fields BagSize double, FoodCapacity double, FoodPricePerPound decimal, FoodStock double, and MoneyBalance decimal.
Add VendingMachine and its five current fields.
  • New
  • Changed
  • Removed

BagSize, FoodCapacity, and FoodStock use double. FoodPricePerPound and MoneyBalance use decimal.

B Add ZooScenario/Business Classes/VendingMachine.cs
C Add the five fields shown by the class diagram
D Preserve the current XML documentation and analyzer/suppression pattern
E Do not add purchase or refill behavior
F Check Your Work
1 Select Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.
2 Open VendingMachine.cs. Confirm VendingMachine.BagSize, VendingMachine.FoodCapacity and VendingMachine.FoodStock are declared as double.
3 Confirm VendingMachine.FoodPricePerPound and VendingMachine.MoneyBalance are declared as decimal.
4 Confirm you added fields only; purchase and refill methods belong to later tasks.
G Add AnimalSnackMachine to Zoo
Progressive class diagram showing a green square on the new AnimalSnackMachine relationship from Zoo to VendingMachine. The established BirthArea relationship remains unmarked.
Each Zoo object can reference its own VendingMachine through AnimalSnackMachine.
  • New
  • Changed
  • Removed
H Add the documented current AnimalSnackMachine field to Zoo
I Check Your Work
1 Select Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.
2 Open Zoo.cs. Confirm public VendingMachine AnimalSnackMachine; is declared inside the class.
3 Confirm Zoo.BirthArea still has type BirthingRoom.
5.Set up the Como Zoo vending machine

Create the Como AnimalSnackMachine and configure the authoritative current state.

A Create and configure the Como vending-machine object
B Create a VendingMachine object in the existing Como Zoo setup flow and store it in ComoZoo.AnimalSnackMachine
C Set this.ComoZoo.AnimalSnackMachine.BagSize = 65.0;, this.ComoZoo.AnimalSnackMachine.FoodCapacity = 250.0;, this.ComoZoo.AnimalSnackMachine.FoodPricePerPound = 0.75m;
Completed Como vending-machine object diagram. Green squares mark the new AnimalSnackMachine object and reference plus BagSize 65.0, FoodCapacity 250.0, and FoodPricePerPound 0.75. Untouched default-valued members are omitted.
Completed Como vending-machine state. Untouched defaults are intentionally left out.
  • New
  • Changed
  • Removed
D 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.AnimalSnackMachine.BagSize is 65.0.
5 Confirm this.ComoZoo.AnimalSnackMachine.FoodCapacity is 250.0.
6 Confirm this.ComoZoo.AnimalSnackMachine.FoodPricePerPound is 0.75m.
7 Confirm this.ComoZoo.AnimalSnackMachine.FoodStock is 0.0.
8 Confirm this.ComoZoo.AnimalSnackMachine.MoneyBalance is 0.00m.
9 Press F5 to continue. Stop debugging and remove the breakpoint before the next check.
Add the San Diego Zoo to the application
6.Reuse the same class design for the San Diego Zoo

Use the existing Zoo, Animal, Employee, Restroom, Booth, BirthingRoom, and VendingMachine classes to create a separate San Diego object graph.

A Add SanDiegoZoo
Class diagram showing MainWindow related to Zoo through the existing ComoZoo role and a new SanDiegoZoo role. A green square marks only SanDiegoZoo.
Two fields can refer to two separate objects created from the same Zoo class.
  • New
  • Changed
  • Removed
B Add the documented current SanDiegoZoo field/reference
C Check Your Work
1 Select Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.
2 Open MainWindow.xaml.cs. Confirm both MainWindow.ComoZoo and MainWindow.SanDiegoZoo are fields of type Zoo.
3 Confirm these are two fields in the existing window class, rather than two new zoo class definitions.
D Use the supplied San Diego button path

The UI markup is not represented by the UML, so use the exact current button source:

XML

<Button x:Name="newSanDiegoZooButton" Content="New San Diego Zoo" Click="newSanDiegoZooButton_Click"/>
E Use/preserve the supplied San Diego button path and exact current control/event names
F Check Your Work
1 Open MainWindow.xaml. Find the button whose Content is New San Diego Zoo.
2 Confirm its name is newSanDiegoZooButton and its Click value is newSanDiegoZooButton_Click.
3 Confirm MainWindow.xaml.cs contains a method named newSanDiegoZooButton_Click with the signature shown in this task.
G Use the supplied San Diego event-handler path

The current one-line handler signature is:

C#

private void newSanDiegoZooButton_Click(object sender, RoutedEventArgs e)

The event path coordinates creation of a separate object graph. Do not copy a complete finished handler body from this walkthrough.

H Create the San Diego Zoo and supporting objects

Create the separate San Diego objects using the same class definitions and the current source values.

I Create the zoo with this.SanDiegoZoo = new Zoo();. Then assign this.SanDiegoZoo.Name = "San Diego Zoo"; and this.SanDiegoZoo.Capacity = 3000;
J Create this.SanDiegoZoo.TicketBooth = new Booth();. Assign this.SanDiegoZoo.TicketBooth.TicketPrice = 25.50m;. Create this.SanDiegoZoo.TicketBooth.Attendant = new Employee();, then assign this.SanDiegoZoo.TicketBooth.Attendant.Name = "Betty"; and this.SanDiegoZoo.TicketBooth.Attendant.Number = 84;
K Create this.SanDiegoZoo.LadiesRoom = new Restroom(); and this.SanDiegoZoo.MensRoom = new Restroom();. Assign this.SanDiegoZoo.LadiesRoom.Capacity = 12; and this.SanDiegoZoo.LadiesRoom.Sex = "Female";. Assign this.SanDiegoZoo.MensRoom.Capacity = 12; and this.SanDiegoZoo.MensRoom.Sex = "Male";
L Create this.SanDiegoZoo.FeaturedAnimal = new Animal();. Assign this.SanDiegoZoo.FeaturedAnimal.Name = "Patti";, this.SanDiegoZoo.FeaturedAnimal.Sex = "Female";, this.SanDiegoZoo.FeaturedAnimal.Age = 5;, this.SanDiegoZoo.FeaturedAnimal.Weight = 3.27;, this.SanDiegoZoo.FeaturedAnimal.HappinessLevel = 0;, this.SanDiegoZoo.FeaturedAnimal.IsPregnant = true;, this.SanDiegoZoo.FeaturedAnimal.Type = "Platypus";
M Create this.SanDiegoZoo.AnimalSnackMachine = new VendingMachine();. Assign this.SanDiegoZoo.AnimalSnackMachine.BagSize = 65.0;, this.SanDiegoZoo.AnimalSnackMachine.FoodCapacity = 250.0;, this.SanDiegoZoo.AnimalSnackMachine.FoodPricePerPound = 1.20m;
N Create this.SanDiegoZoo.BirthArea = new BirthingRoom();. Assign this.SanDiegoZoo.BirthArea.InitialTemperature = 77.0;, this.SanDiegoZoo.BirthArea.MaxTemperature = 85.0;, this.SanDiegoZoo.BirthArea.MinTemperature = 55.0;, this.SanDiegoZoo.BirthArea.TemperatureIncrease = 0.5;, this.SanDiegoZoo.BirthArea.Temperature = this.SanDiegoZoo.BirthArea.InitialTemperature;
O Create and configure San Diego Zoo’s doctor
1 Assign this.SanDiegoZoo.BirthArea.Doctor = new Employee();
2 Assign this.SanDiegoZoo.BirthArea.Doctor.Name = "Steve";
3 Assign this.SanDiegoZoo.BirthArea.Doctor.Number = 24;
Object diagram rooted at MainWindow. ComoZoo is folded existing context. A green-square-marked SanDiegoZoo object has Name San Diego Zoo and Capacity 3000. Its separate supporting objects are TicketBooth with Betty as Attendant, LadiesRoom, MensRoom, Patti as FeaturedAnimal, AnimalSnackMachine, BirthArea, and Steve as Doctor. Only intentionally initialized current values are shown; untouched default or uninitialized members are omitted.
Como and San Diego are separate Zoo objects. The San Diego supporting objects are grouped under the San Diego object to make the relationships easier to follow.
  • New
  • Changed
  • Removed

ComoZoo and SanDiegoZoo are different Zoo objects that use the same class. Their supporting objects hold separate state, so a second zoo does not require duplicate domain classes.

P Check Your Work
1 In MainWindow.xaml.cs, set a breakpoint on the closing brace of newSanDiegoZooButton_Click by clicking its left margin.
2 Press F5 to start or continue the application. Click New San Diego 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.SanDiegoZoo.Name is "San Diego Zoo".
5 Confirm this.SanDiegoZoo.Capacity is 3000.
6 Confirm this.SanDiegoZoo.TicketBooth.TicketPrice is 25.50m.
7 Confirm this.SanDiegoZoo.TicketBooth.Attendant.Name is "Betty".
8 Confirm this.SanDiegoZoo.TicketBooth.Attendant.Number is 84.
9 Confirm this.SanDiegoZoo.LadiesRoom.Capacity is 12.
10 Confirm this.SanDiegoZoo.LadiesRoom.Sex is "Female".
11 Confirm this.SanDiegoZoo.MensRoom.Capacity is 12.
12 Confirm this.SanDiegoZoo.MensRoom.Sex is "Male".
13 Confirm this.SanDiegoZoo.FeaturedAnimal.Name is "Patti".
14 Confirm this.SanDiegoZoo.FeaturedAnimal.Sex is "Female".
15 Confirm this.SanDiegoZoo.FeaturedAnimal.Age is 5.
16 Confirm this.SanDiegoZoo.FeaturedAnimal.Weight is 3.27.
17 Confirm this.SanDiegoZoo.FeaturedAnimal.HappinessLevel is 0.
18 Confirm this.SanDiegoZoo.FeaturedAnimal.IsPregnant is true.
19 Confirm this.SanDiegoZoo.FeaturedAnimal.Type is "Platypus".
20 Confirm this.SanDiegoZoo.AnimalSnackMachine.BagSize is 65.0.
21 Confirm this.SanDiegoZoo.AnimalSnackMachine.FoodCapacity is 250.0.
22 Confirm this.SanDiegoZoo.AnimalSnackMachine.FoodPricePerPound is 1.20m.
23 Confirm this.SanDiegoZoo.BirthArea.InitialTemperature is 77.0.
24 Confirm this.SanDiegoZoo.BirthArea.Temperature is 77.0.
25 Confirm this.SanDiegoZoo.BirthArea.MaxTemperature is 85.0.
26 Confirm this.SanDiegoZoo.BirthArea.MinTemperature is 55.0.
27 Confirm this.SanDiegoZoo.BirthArea.TemperatureIncrease is 0.5.
28 Confirm this.SanDiegoZoo.BirthArea.Doctor.Name is "Steve".
29 Confirm this.SanDiegoZoo.BirthArea.Doctor.Number is 24.
30 Press F5 and remove this breakpoint. Click New Como Zoo and check its values at the end of its creation handler: this.ComoZoo.Name is Como Zoo, this.ComoZoo.Capacity is 1000 and this.ComoZoo.FeaturedAnimal.Name is Dolly.
31 Confirm the San Diego creation code uses new Zoo() and creates its own related objects instead of assigning this.ComoZoo to this.SanDiegoZoo.
7.Final Check
A Class structure
  • BirthingRoom has InitialTemperature, MaxTemperature, MinTemperature, TemperatureIncrease, Animal, Doctor, and Temperature with the current types.
  • VendingMachine has BagSize, FoodCapacity, FoodPricePerPound, FoodStock, and MoneyBalance with the current types.
  • Zoo includes BirthArea : BirthingRoom and AnimalSnackMachine : VendingMachine.
  • MainWindow includes SanDiegoZoo : Zoo.
  • No later-course design techniques were added.
B Como Zoo state
  • Original Zoo 1.1 Como state remains intact.
  • BirthArea has 77.0 / 85.0 / 55.0 / 0.5 / 77.0.
  • BirthArea.Doctor is Flora #98.
  • AnimalSnackMachine has 65.0 / 250.0 / 0.75.
C San Diego Zoo state
  • Name = San Diego Zoo; Capacity = 3000.
  • Ticket = 25.50; attendant = Betty #84.
  • Both restroom capacities = 12 with the supplied Female/Male values.
  • Featured animal = Patti, Female, age 5, weight 3.27, happiness 0, pregnant true, type Platypus.
  • San Diego machine: this.SanDiegoZoo.AnimalSnackMachine.BagSize is 65.0; this.SanDiegoZoo.AnimalSnackMachine.FoodCapacity is 250.0; this.SanDiegoZoo.AnimalSnackMachine.FoodPricePerPound is 1.20m.
  • San Diego room: this.SanDiegoZoo.BirthArea.InitialTemperature is 77.0; this.SanDiegoZoo.BirthArea.MaxTemperature is 85.0; this.SanDiegoZoo.BirthArea.MinTemperature is 55.0; this.SanDiegoZoo.BirthArea.TemperatureIncrease is 0.5; this.SanDiegoZoo.BirthArea.Temperature is 77.0; this.SanDiegoZoo.BirthArea.Doctor.Name is "Steve"; this.SanDiegoZoo.BirthArea.Doctor.Number is 24.
  • Como Zoo state remains independent.

Use the normal build/error-reading and already-taught inspection paths. This page does not claim your project built or ran successfully; verify that in your own project.

8.Zoo 1.2 End-Structure Class Diagram
Final Zoo 1.2 class diagram. Green squares mark BirthingRoom and its five scalar fields, its Animal and Doctor relationships, Zoo BirthArea, VendingMachine and its five scalar fields, Zoo AnimalSnackMachine, and MainWindow SanDiegoZoo. Existing Zoo 1.1 classes, members, and relationships are unmarked. There are no Changed or Removed structural items.
Final structural delta: New items only; Changed none; Removed none.
  • New
  • Changed
  • Removed
9.Submit

Open the Feedback System to continue with the current submission/readiness process.