- Getters return information from private fields.
- Only the properties that need outside assignment have setters.
Continue from your completed Zoo 2.6. Use the diagrams and the directions below to make the next set of changes.
MainWindow.xaml, set the window attribute to
Title="Programming Concepts 2: Zoo 2.7 Lastname"
, using your last name. Keep the project target at .NET Framework 4.8.The Animal class still has public fields that can be altered from outside the class.
You will change their visibility and add properties for the information other classes need.
Animal.cs.HappinessLevel, Name, Type and Weight private backing fields named happinessLevel, name, type and weight. Keep the existing private age, sex and baby fields. This task changes the fields; the following tasks add the properties shown in the focused diagram.baby.Weight as a read where only the returned value is needed.Eat, update the unborn baby’s weight through this.baby.weight. Code inside Animal can access a private field on another Animal object.Eat, Move and Reproduce. An animal’s own state changes still happen inside Animal; its public properties will provide the reads.Name property pattern.this.name. The setter assigns this.name = value;.public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}baby.Name = "Baby";: the setter receives "Baby" as value and stores it in the baby’s private name field.Name getter as a pattern.GetSex() with Sex; remove the old GetSex method after moving its return statement into the getter.| Property | Type | Getter returns |
|---|---|---|
HappinessLevel |
int |
this.happinessLevel |
Sex |
string |
this.sex |
Type |
string |
this.type |
Weight |
double |
this.weight |
animal.Weight returns its stored value. An outside assignment to Weight will no longer be valid, so internal changes must use the backing field.IsPregnant as a public getter-only property of type bool. Return this.baby != null.GetIsPregnant and update each call to use IsPregnant without parentheses.PortionSize as a public getter-only property of type double. Return this.weight * this.portionSizePercentage.GetPortionSize and update each call to use PortionSize without parentheses.Booth.cs.Attendant private as attendant and update the constructor assignment.| Property | Type | Getter returns |
|---|---|---|
Attendant |
Employee |
this.attendant |
Food.cs.Weight private as weight and have Food(double weight) assign this.weight = weight;.| Property | Type | Getter returns |
|---|---|---|
Weight |
double |
this.weight |
Wallet.cs.moneyBalance field. Move the existing getter method’s return into the property, then remove the method.| Property | Type | Getter returns |
|---|---|---|
MoneyBalance |
decimal |
this.moneyBalance |
Guest.cs.wallet field. Move the method’s return into the property, reading wallet.MoneyBalance, then remove the method.| Property | Type | Getter returns |
|---|---|---|
MoneyBalance |
decimal |
this.wallet.MoneyBalance |
VendingMachine.cs.GetFoodPricePerPound and GetFoodStock with the corresponding getter-only properties.| Property | Type | Getter returns |
|---|---|---|
FoodPricePerPound |
decimal |
this.foodPricePerPound |
FoodStock |
double |
this.foodStock |
animal.PortionSize, machine.FoodPricePerPound, machine.FoodStock and this.wallet.MoneyBalance in the corresponding existing expressions.| Previous read | Property read |
|---|---|
animal.GetPortionSize() |
animal.PortionSize |
machine.GetFoodPricePerPound() |
machine.FoodPricePerPound |
machine.GetFoodStock() |
machine.FoodStock |
this.wallet.GetMoneyBalance() |
this.wallet.MoneyBalance |
this.wallet.MoneyBalance read during Guest.FeedAnimal. Trace the separate Guest.MoneyBalance forwarding getter from its source.AnimalCount is the population count; CountAnimals remains the display-area count.TotalAnimalWeight and AverageAnimalWeight calculate their values from current animals.Zoo.cs and its constructor.AnimalSnackMachine, BirthArea, Pen, Tank, TicketBooth and Visitor private backing fields with lowercase names. Keep the existing private name field.Zoo’s own behavior methods, use the private fields where the object owns the assignment or action.AnimalSnackMachine, BirthArea, TicketBooth and Visitor. Replace GetName() with the getter-only Name property.Pen and Tank properties with both get and set accessors. MainWindow’s existing area creation still assigns these two references.| Property | Type | Getter returns | Setter |
|---|---|---|---|
AnimalSnackMachine |
VendingMachine |
this.animalSnackMachine |
None |
BirthArea |
BirthingRoom |
this.birthArea |
None |
Name |
string |
this.name |
None |
TicketBooth |
Booth |
this.ticketBooth |
None |
Visitor |
Guest |
this.visitor |
None |
Pen |
Pen |
this.pen |
this.pen = value |
Tank |
Tank |
this.tank |
this.tank = value |
MainWindow still reads the public property names; its Pen and Tank assignments use setters.TotalAnimalWeight stays private because only Zoo uses it.public int AnimalCount
{
get
{
return this.animals.Count;
}
}TotalAnimalWeight stays private because only Zoo uses it.TotalAnimalWeight stays private because only Zoo uses it.CalculateTotalAnimalWeight() with this.TotalAnimalWeight. Keep the existing this.animals.Count reads.Temperature has a getter and a range-checked setter.BirthingRoom.cs.Doctor private as doctor. Update the constructor with this.doctor = doctor;. No stored Mother member is needed; BirthAnimal already receives its mother parameter.maxTemperature is 85.0 and minTemperature is 55.0.Doctor, MaxTemperature and MinTemperature properties returning their corresponding private fields.| Property | Getter returns | Setter |
|---|---|---|
Doctor |
this.doctor |
None |
MaxTemperature |
this.maxTemperature |
None |
MinTemperature |
this.minTemperature |
None |
The existing private helper already enforces 55–85. Move that validation into the Temperature setter so property assignments enforce the same rule.
You will define a property with logic inside it to control the temperature range.
GetTemperature() with a Temperature property.this.temperature. In the setter, perform this.temperature = value; only when value is between minTemperature and maxTemperature, inclusive.GetTemperature after moving its read operation into the getter. Keep invalid assignments from changing the stored temperature.public double Temperature
{
get
{
return this.temperature;
}
set
{
if (value >= this.minTemperature && value <= this.maxTemperature)
{
this.temperature = value;
}
}
}55.0 and 85.0 as accepted. Trace 54.5 and 85.5 as rejected; the existing stored temperature is retained.mother.IsPregnant and the private doctor reference in the existing condition.this.IncreaseTemperature() before calling this.doctor.DeliverAnimal(mother). The existing public action delegates to ChangeTemperature, whose updated body uses the property. Keep the existing returned-baby behavior.85.0, a proposed birth increment of 0.5 is rejected by the setter. The existing delivery call still follows the property operation.ChangeTemperature private and change its body to this.Temperature += temperatureChange;. Keep IncreaseTemperature and DecreaseTemperature as the public fixed-step actions.this.birthArea.Temperature.ToString("0.0") from a getter-only string property, then remove GetTemperatureDisplay.display variable, loops, X/x/empty markers, separators and return statement. Remove only the old method wrapper and replace it with the public string property and get accessor.display variable, loops, X/x/empty markers, separators and return statement. Remove only the old method wrapper and replace it with the public string property and get accessor.zoo.Pen.DisplayLocation for the Como pen and zoo.Tank.DisplayLocation for the San Diego tank. Keep the existing zoo guards and TextBox names.MainWindow; each property returns the map its class already built.Booth’s constructor argument. Preserve the remaining setup, exact scenario values, pregnancy and registration calls, pen/tank references and area refresh.Booth’s constructor argument. Preserve the remaining setup, exact scenario values, pregnancy and registration calls, pen/tank references and area refresh.Zoo.Name property in the existing success message.zoo.GetName() with zoo.Name. Keep the animal search, Guest.FeedAnimal call, switch and messages.Zoo.Name, a different class’s property from Animal.Name.AnimalCount and AverageAnimalWeight. Keep the existing private result fields and messages.| Where | Previous expression | Replacement |
|---|---|---|
newComoZooButton_Click |
this.comoZoo.GetName() |
this.comoZoo.Name |
newSanDiegoZooButton_Click |
this.sanDiegoZoo.GetName() |
this.sanDiegoZoo.Name |
| Both birth handlers | baby.GetSex() |
baby.Sex |
birthPlatypusButton_Click |
this.sanDiegoZoo.GetTemperatureDisplay() |
this.sanDiegoZoo.TemperatureDisplay |
| Increase/decrease and new San Diego handlers | this.sanDiegoZoo.BirthArea.GetTemperature() |
this.sanDiegoZoo.BirthArea.Temperature |
getComoZooAnimalCount_Click |
this.comoZoo.GetAnimalCount() |
this.comoZoo.AnimalCount |
getSanDiegoZooAnimalCount_Click |
this.sanDiegoZoo.GetAnimalCount() |
this.sanDiegoZoo.AnimalCount |
getComoZooAverageAnimalWeight_Click |
this.comoZoo.CalculateAverageAnimalWeight() |
this.comoZoo.AverageAnimalWeight |
getSanDiegoZooAverageAnimalWeight_Click |
this.sanDiegoZoo.CalculateAverageAnimalWeight() |
this.sanDiegoZoo.AverageAnimalWeight |
Eat.private void Bark() and increase happiness with this.happinessLevel += 2;.happinessLevel changing from 0 to 2. Eat calls this helper for type "Dingo".Food reference parameter.private void StashInPouch(Food food). Increase happiness with this.happinessLevel++;.happinessLevel of 0 becoming 1. The current helper does not alter the food object.A feeding action should use the shared weight changes and then choose the response for its type.
You will use the switch statement pattern from the diagrams and call the private helpers.
food.Weight and the private percentage fields for the existing weight updates. Change this animal’s backing field this.weight and, when pregnant, this.baby.weight.this.type. Call Bark for "Dingo", StashInPouch(food) for "Platypus", and leave the default case with no additional state change. Keep break statements.Pen and Tank still have public fields.3 and 2 and initial averages approximately 28.2 and 1.885. Use directions and confirm X/x map behavior remains.Bark’s increment before feeding. Recreate Como and feed Dolly once. When paused, add this.happinessLevel to Watch, then step over the increment and observe 2. Set a breakpoint on StashInPouch’s increment before feeding. Recreate San Diego and feed Patti once; when paused, add this.happinessLevel to Watch, then step over it and observe 1. Press F5 after each observation and remove the breakpoints.this.name = value; in Animal.Name, then click Give birth to dingo.| Expression | Before the assignment |
|---|---|
this.name |
Empty string; the baby constructor used string.Empty |
value |
"Baby" |
this.Type |
"Dingo" |
this |
The newborn Animal, not MainWindow or the mother |
F10 to follow the assignment.this.name is "Baby". The public Name getter now reads that same value.F5 to finish the birth handler, confirm the newborn map marker, then click Get animal count in the Como column to confirm population count 4. Remove the setter breakpoint.this.sanDiegoZoo.BirthArea.IncreaseTemperature() in increaseSanDiegoTemperatureButton_Click. Start, create San Diego, then click +. The caller breakpoint pauses before the action. While paused there, set or enable breakpoints on return this.temperature; in the Temperature getter and on the condition inside its setter. Enabling these after creation avoids pausing during the initial display refresh. Add this.sanDiegoZoo.BirthArea.temperature to Watch: 77.0. this is MainWindow here; value is unavailable.F11 through IncreaseTemperature and the private ChangeTemperature helper. At the getter breakpoint, this is BirthingRoom. Add this.temperature, this.minTemperature, this.maxTemperature and value to Watch before continuing. The first three are 77.0, 55.0 and 85.0; value is unavailable in get. Continue to the setter breakpoint: value is now 77.5. Follow the condition and assignment; inspect this.temperature after the assignment executes. It becomes 77.5. Disable the breakpoints and press F5; the display becomes 77.5.+= invokes get before set. A rejected set leaves the stored field unchanged. If stepping skips properties, use the explicit getter/setter breakpoints above; the relevant bodies still pause there.55.0 and 85.0 and rejects 54.5 and 85.5, retaining the previous value.77.0. Click + sixteen times: 85.0. One more + keeps 85.0. Click - sixty times: 55.0. One more - keeps 55.0. Click + once: 55.5. Reset again and verify + then - produces 77.5 then 77.0. No temporary source edits or Watch assignments are required.getSanDiegoZooAverageAnimalWeight_Click and the first statements inside the AverageAnimalWeight and TotalAnimalWeight getters. Then click Get average animal weight.| Expression | Expected state |
|---|---|
this.zooAverageAnimalWeight |
Last stored result; assignment has not run |
averageWeight |
Unavailable in MainWindow; belongs to the average getter |
totalWeight |
Unavailable in MainWindow; belongs to the total getter |
this.sanDiegoZoo.animals |
Private list; this.sanDiegoZoo.animals.Count is 2 |
F11, F10 and their breakpoints.AverageAnimalWeight, averageWeight is 0. The guard reads this.animals.Count as 2.TotalAnimalWeight: totalWeight becomes approximately 3.27 and then 3.77. Return to the average getter and observe approximately 1.885 after division.F5 to display the result.TotalAnimalWeight and the list’s Count. It does not need to call the old calculation methods.