- 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.
Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.77.5 then 77.0.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.this.baby.weight. Code inside Animal can access a private field on another Animal object.Animal.cs. Confirm the private fields are named this.happinessLevel, this.name, this.type and this.weight.this.weight. In Eat, confirm the baby update uses this.baby.weight.this.baby.Name property pattern.this.name. The setter assigns this.name from value.public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}Animal.Name in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.Name assignment into the setter. Confirm value is Baby and the setter stores it in this.name.Baby without changing the 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 getters. Confirm HappinessLevel returns this.happinessLevel, Sex returns this.sex, Type returns this.type and Weight returns this.weight.35.3 for this.weight. Confirm Weight returns 35.3.IsPregnant as a public getter-only property of type bool. Return this.baby != null.GetIsPregnant and update each call to use IsPregnant without parentheses.Animal.IsPregnant in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.baby equal to null: expect false.true. Confirm the getter does not create or remove a baby.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.Animal.PortionSize in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.35.3 and portion percentage 0.02. Confirm approximately 0.706.3.27: expect approximately 0.0654. Confirm neither read changes this.weight.Food weight, balances, machine price and stock can be read through properties.Booth.cs.| Property | Type | Getter returns |
|---|---|---|
| Attendant | Employee |
this.attendant |
Booth.Attendant in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.attendant. Use Sam’s Employee reference as its stored value.this.attendant.Food.cs.Weight private as weight and have Food(double weight) assign this.weight.| Property | Type | Getter returns |
|---|---|---|
Weight |
double | this.weight |
Food.Weight in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.weight. Use 1.25 as its stored value.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 |
Wallet.MoneyBalance in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.moneyBalance. Use 5.25m as its stored value.this.moneyBalance.Guest.cs.wallet.MoneyBalance, then remove the method.| Property | Type | Getter returns |
|---|---|---|
MoneyBalance |
decimal | this.wallet.MoneyBalance |
Guest.MoneyBalance in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.wallet.MoneyBalance into Wallet. With this.moneyBalance of 5.25m, confirm returned 5.25m.VendingMachine.cs.GetFoodPricePerPound and GetFoodStock with the corresponding getter-only properties.| Property | Type | Getter returns |
|---|---|---|
FoodPricePerPound |
decimal | this.foodPricePerPound |
FoodStock |
double | this.foodStock |
VendingMachine FoodPricePerPound and FoodStock getters. Confirm they return this.foodPricePerPound and this.foodStock.0.75m and 195.0. Confirm those are returned, with no sale, refill or money change.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 inside Guest.FeedAnimal. Follow that read directly into Wallet’s getter.Guest.MoneyBalance in the source. Confirm it forwards the same wallet property; FeedAnimal need not call that Guest getter.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.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 |
Zoo’s constructor. Confirm each object assignment stores the supplied or constructed reference in its lowercase backing field.AnimalSnackMachine, BirthArea, TicketBooth and Visitor getters: each returns its matching field.MainWindow’s Pen and Tank assignments into their setters. Confirm each setter stores value in its matching field.public int AnimalCount
{
get
{
return this.animals.Count;
}
}Zoo.AnimalCount in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.animals.Count is 3 and the getter returns 3.0. This count measures the population list, not map occupancy.Zoo.TotalAnimalWeight in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.35.3, 33.8 and 15.5. Confirm the accumulated total is approximately 84.6.0.0. Confirm no animal weight changes.CalculateTotalAnimalWeight() with this.TotalAnimalWeight. Keep the existing this.animals.Count reads.Zoo.AverageAnimalWeight in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.TotalAnimalWeight approximately 84.6 and this.animals.Count of 3. Confirm returned average approximately 28.2.3.77 and count 2: expect approximately 1.885.Temperature has a getter and a range-checked setter.BirthingRoom.cs.maxTemperature and minTemperature private readonly, initialized to 95.0 and 35.0. Remove the constructor’s old MaxTemperature/MinTemperature assignments.MaxTemperature and MinTemperature properties. Add Mother with a getter and setter because MainWindow assigns that relationship.| Property | Getter returns | Setter |
|---|---|---|
| Doctor | this.doctor |
None |
MaxTemperature |
this.maxTemperature |
None |
MinTemperature |
this.minTemperature |
None |
| Mother | this.mother |
this.mother = value |
BirthingRoom.cs. Confirm this.minTemperature is private readonly 35.0 and this.maxTemperature is private readonly 95.0.MinTemperature and MaxTemperature return those fields. Confirm Doctor returns this.doctor and Mother reads and writes this.mother.Currently, repeated temperature changes could push the room outside an allowed range.
You will define a property with logic inside it to control the temperature range.
GetTemperature() with a Temperature property.this.temperature. In the setter, assign this.temperature from 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;
}
}
}BirthingRoom.Temperature in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.temperature at 77.0. Trace setter value 35.0: confirm accepted and stored.77.0. Trace value 95.0: confirm accepted and stored.77.0. Trace value 34.5: confirm rejected and stored temperature still 77.0.95.5: confirm rejected and stored temperature still 77.0.mother.IsPregnant and the private doctor reference in the existing condition.this.Temperature += this.temperatureIncrease before calling this.doctor.DeliverAnimal(mother). Keep the existing returned-baby behavior.BirthingRoom.BirthAnimal in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.temperature of 95.0.0.5. Confirm proposed setter value 95.5 is rejected and stored temperature stays 95.0.this.Temperature += temperatureChange.BirthingRoom.ChangeTemperature in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.77.0 with change 0.5. Follow the getter, addition and setter: confirm proposed and stored value 77.5.95.0 with change 0.5. Confirm proposed 95.5 is rejected and stored value remains 95.0.this.birthArea.Temperature.ToString("0.0") from a getter-only string property, then remove GetTemperatureDisplay.Zoo.TemperatureDisplay in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.birthArea.Temperature and retains ToString("0.0").77.0 and 77.5 separately. Confirm one decimal digit in each display. The separator follows the current culture.Pen.DisplayLocation in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.PenSpaces entries with the mother at index 1 and the others empty. Confirm - X - -.0. Confirm lowercase x marks the newborn and uppercase X marks the mother.Tank.DisplayLocation in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.TankSpaces[1, 1]. Confirm uppercase X marks her cell and - marks empty cells.this.TankSpaces[0, 0]. Confirm lowercase x.Environment.NewLine separates rows, with no trailing separators.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’s map helper. Confirm it reads zoo.Pen.DisplayLocation or zoo.Tank.DisplayLocation for the matching area.MainWindow only displays its returned string.Booth needs its argument.Employee("Sam", 42) inside the Booth constructor call, then delete the old attendant declaration.Zoo, Animal and area setup. Pen/Tank and BirthArea.Mother now use their public setters.Booth booth = new Booth(new Employee("Sam", 42), 15.00m);MainWindow.CreateComoZoo. Identify the Employee constructor call for Sam.Booth constructor. Confirm no second attendant Employee is created.Booth into the Zoo constructor. Confirm this.comoZoo.TicketBooth.Attendant returns the original attendant.Booth needs its argument.Employee("Betty", 84) inside the Booth constructor call, then delete the old attendant declaration.Zoo, Animal and area setup. Pen/Tank and BirthArea.Mother now use their public setters.Booth booth = new Booth(new Employee("Betty", 84), 25.50m);MainWindow.CreateSanDiegoZoo. Identify the Employee constructor call for Betty.Booth constructor. Confirm no second attendant Employee is created.Booth into the Zoo constructor. Confirm this.sanDiegoZoo.TicketBooth.Attendant returns the original attendant.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 in the feeding message. Confirm it reads Zoo.Name.Como Zoo separately from the found animal’s Name Dolly. Confirm the message uses both in their appropriate places.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 |
Bark and increase happinessLevel by 2.Animal.Bark() in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.happinessLevel at 0. Confirm the increment produces 2.Bark.Food reference parameter.StashInPouch method with a Food parameter named food. Increase happinessLevel by 1.Animal.StashInPouch in the source. Follow its statements from top to bottom using the sample values below; this is a paper/source trace, so do not run or edit the application for this check.this.happinessLevel at 0. Confirm the increment produces 1.Food object’s weight.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 old guide 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.Animal.Eat from its first statement. Confirm the common adult weight update occurs before the switch.Bark.StashInPouch.Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.Bark’s increment. Recreate Como and feed once. Inspect this.happinessLevel as 2.StashInPouch’s increment. Recreate San Diego and feed once. Inspect this.happinessLevel as 1.F5 to continue. Stop debugging and remove the breakpoint before the next check.this.name = value; inside the Animal.Name setter, 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 |
this.name is "Baby". The public Name getter now reads that same value.Name setter. Confirm it stores Baby in the newborn’s name field.Name Baby and hold the returned newborn reference.F5 to continue. Stop debugging and remove the breakpoint before the next check.MainWindow.ChangeSanDiegoTemperature, on return this.temperature in the room getter, and on the condition inside its setter. Click + once.| Expression | Expected state |
|---|---|
temperatureChange |
0.5 |
this.sanDiegoZoo.BirthArea.temperature |
77.0 |
this.temperature |
Unavailable in MainWindow |
| value | Unavailable until inside the property setter |
this.minTemperature |
Unavailable in MainWindow; 35.0 inside BirthingRoom |
this.maxTemperature |
Unavailable in MainWindow; 95.0 inside BirthingRoom |
BirthingRoom.ChangeTemperature and follow the property-body breakpoints.BirthingRoom and this.temperature is 77.0. value is unavailable in the getter.this.minTemperature is 35.0 and this.maxTemperature is 95.0.this.temperature becomes 77.5. Remove the property/helper breakpoints while paused, then press F5 and confirm the display shows 77.5.this.temperature before the preceding property increment.+= performs both a read and a write.F5 to continue. Stop debugging and remove the breakpoint before the next check.| Proposed value | Decision | Stored result |
|---|---|---|
| 35.0 | Accept | 35.0 |
| 95.0 | Accept | 95.0 |
| 34.5 | Reject | Previous temperature |
| 95.5 | Reject | Previous temperature |
increaseSanDiegoTemperatureButton_Click with 18.0. Build, run, create San Diego and click + twice. The display becomes 95.0 and stays 95.0. Restore 0.5.decreaseSanDiegoTemperatureButton_Click with -42.0. Build, run, create San Diego and click - twice. The display becomes 35.0 and stays 35.0. Restore -0.5.0.5 for increase and -0.5 for decrease.Build > Build Solution. Open View > Error List and resolve any errors introduced by this task.New San Diego Zoo to reset that zoo. Do not feed, refill, move or give birth before this test.77.0. Increase once: expect 77.5. Decrease once: expect 77.0.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 with Count 2 |
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.this.TotalAnimalWeight into its getter during the preceding average trace. Confirm it returns the list’s total.AverageAnimalWeight. For fresh Como, confirm total approximately 84.6 divided by this.animals.Count of 3 gives approximately 28.2.F5 to continue. Stop debugging and remove the breakpoint before the next check.