- The room starts at
77.0. - Births and the existing temperature buttons still change the temperature.
Continue from your completed Zoo 2.4. 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.5 Lastname"
, using your last name. Keep the project target at .NET Framework 4.8.77.0.Currently the Temperature field of the BirthingRoom can be manipulated from MainWindow when it should be the BirthingRoom itself that handles the temperature and any changes.
You will make the temperature field private and give other classes public methods for reading and changing it.
BirthingRoom fields.Temperature to temperature and make it private. Initialize the field with private double temperature = 77.0;.InitialTemperature. The temperature field now receives its starting value directly. Keep Doctor. Replace the public limit fields with private readonly double maxTemperature = 85.0; and private readonly double minTemperature = 55.0;. Remove the unused Mother field: BirthAnimal receives the mother as a parameter.TemperatureIncrease with private readonly double temperatureIncrease = 0.5;.temperature stores a changing value; temperatureIncrease stores the fixed increment. Outside callers will be repaired after the public methods are in place.this.IncreaseTemperature(). Keep the mother-null, pregnancy and doctor checks and the returned baby reference. The public adjustment methods are added below before the build check.this.temperature using the return type shown in the class diagram.private void ChangeTemperature(double temperatureChange). Compute double proposedTemperature = this.temperature + temperatureChange;. Follow the activity diagram to store it only when it is within the private minTemperature and maxTemperature limits, inclusive.77.0: a change of 0.5 gives 77.5; a change of -0.5 gives 76.5.public void IncreaseTemperature() method. Use the activity diagram to pass the positive room-owned increment to the private helper.public void DecreaseTemperature() method. Use the activity diagram to pass the negative room-owned increment to the private helper.this.BirthArea.GetTemperature().ToString("0.0") so Zoo reads through the room’s public method and retains one decimal place.Currently the VendingMachine money balance can be manipulated outside the class through both the public MoneyBalance field and the public AddMoney method.
You will make the money field and helper method private so that other classes do not have access to them.
VendingMachine.MoneyBalance to moneyBalance, make it private and update its uses inside the class.BagSize to bagSize and FoodCapacity to foodCapacity. Make both private readonly and initialize them to 65.0 and 250.0.AddMoney from public to private. Keep the amount parameter and add it to this.moneyBalance. Keep SellFood calling this helper after successful dispensing.SellFood into AddMoney in the source. An outside class uses SellFood; VendingMachine itself can still call its private helper.AddFoodBag private. Replace the old BagSize and FoodCapacity references with bagSize and foodCapacity; retain both return paths.12 accepts one 65-pound bag, becomes 77 and returns true. Stock 212 rejects a bag, stays 212 and returns false.IsFull private. Compare FoodStock with the private foodCapacity field.12 makes IsFull return false; stock 250 makes it return true.12, Fill adds three bags, reaches 207 and returns 3. A second Fill adds no bag, leaves stock 207 and returns 0.RemoveFood’s access modifier to private. Keep its null default, stock guard, subtraction, Food creation and return. SellFood remains the public transaction method and can call this helper inside VendingMachine.Wallet.cs.MoneyBalance to moneyBalance, make it private and update AddMoney and RemoveMoney to use that field. Keep the existing positive-deposit and limited-withdrawal checks.2.00 and a withdrawal request of 3.00. The existing method still removes only 2.00.this.moneyBalance using the return type shown in the class diagram.Currently the guest’s wallet can be replaced from outside the Guest class.
You will make the wallet reference private and let the guest provide public operations for using it.
Guest.cs.Wallet to wallet and make it private. Instantiate the wallet directly in its field declaration, as shown below.Guest’s own references from this.Wallet to this.wallet.private Wallet wallet = new Wallet();Guest gets a wallet, so the creation methods will no longer create a Wallet separately.this.wallet.AddMoney(amount).Guest.AddMoney into Wallet.AddMoney. The wallet still rejects a nonpositive deposit.this.wallet.GetMoneyBalance() using the return type shown in the class diagram.RemoveMoney to private and use this.wallet.RemoveMoney(amount). Keep its decimal return value.Guest.FeedAnimal can still call this private method from inside Guest.this.wallet. Replace the direct balance read with this.wallet.GetMoneyBalance().Animal.cs.Baby to baby and make it private. Update all uses of that field inside Animal; keep a local variable named baby in Reproduce.private readonly double moveWeightLossPercentage = 0.03;. Use those field names in Eat, MakePregnant, GetPortionSize and Reproduce.| Field | Value |
|---|---|
afterbirthPercentage |
0.1 |
babyWeightPercentage |
0.1 |
eatBabyWeightGainPercentage |
0.1 |
eatWeightGainPercentage |
0.8 |
portionSizePercentage |
0.02 |
moveWeightLossPercentage |
0.03 |
GetIsPregnant still reports whether this.baby is non-null. Outside classes use that method instead of accessing baby directly.double weightLossAmount = this.Weight * this.moveWeightLossPercentage;, then subtract it with this.Weight -= weightLossAmount;.35.3. A single Move now produces approximately 34.241; the configured rate is three percent.AddAnimal and read the population size through GetAnimalCount.The Zoo class should be the only class with direct access to the list of animals. Additionally, it is possible to add null to the list, which is not something we want to happen.
You will make the list private, refactor the code to use it and make sure an animal is not null before adding it.
Zoo.cs.Animals to animals and make it private readonly. Keep new List<Animal>() in the field declaration.Zoo, including wake, weight and search methods, to use this.animals.this.animals. Their loops and return behavior remain the same.this.animals only if animal != null.null through the diagram. Only the real animal reaches Add. This method does not check for duplicate references.this.animals.Count using the return type shown in the class diagram.this.AddAnimal(baby) instead of adding to the list directly.MainWindow.xaml.cs.ComoZoo to comoZoo and SanDiegoZoo to sanDiegoZoo and make both private.ZooAnimalCount to zooAnimalCount and ZooAverageAnimalWeight to zooAverageAnimalWeight and make both private.MainWindow. Keep method names, XAML names, strings and Click handler names unchanged.MoveAnimal, PlaceAnimal, GetAnimalLocation and CalculateAverageAnimalWeight accessible.| Class | Methods to make private |
|---|---|
MainWindow |
CreateComoZoo(), CreateSanDiegoZoo(), DisplayMessage(string message), DisplayAnimalArea(Zoo zoo, string zooName), FeedAnimal(Zoo zoo, string zooName, string animalName, string guestName), MoveFeaturedAnimal(Zoo zoo, string zooName, string direction) |
Pen |
ClearLocation(), FindAnimalPosition(Animal animal), IsValidPosition(int position), MoveAnimalInPen(int positionChange) |
Tank |
ContainsAnimal(Animal animal), FindAnimalColumn(Animal animal), FindAnimalRow(Animal animal), IsValidLocation(int row, int column), MoveAnimalInTank(int rowChange, int columnChange) |
Zoo |
CalculateTotalAnimalWeight() |
BirthArea.Mother assignment.BirthArea.Mother assignment.ChangeSanDiegoTemperature(double temperatureChange). The existing + and - handlers will call the room’s focused public actions directly. Their complete replacement bodies appear in the next task.this.comoZoo.Animals.Count with this.comoZoo.GetAnimalCount(). In the San Diego handler, replace this.sanDiegoZoo.Animals.Count with this.sanDiegoZoo.GetAnimalCount(). The table below gives the complete replacement assignments.MainWindow reads the result for display.Keep the existing
Click="decreaseSanDiegoTemperatureButton_Click"
and
Click="increaseSanDiegoTemperatureButton_Click"
attribute values on their corresponding buttons.
private void decreaseSanDiegoTemperatureButton_Click(object sender, RoutedEventArgs e)
{
if (this.sanDiegoZoo == null)
{
this.DisplayMessage("Create San Diego Zoo first.");
return;
}
this.sanDiegoZoo.BirthArea.DecreaseTemperature();
this.temperatureTextBlock.Text = this.sanDiegoZoo.BirthArea.GetTemperature().ToString("0.0");
}
private void increaseSanDiegoTemperatureButton_Click(object sender, RoutedEventArgs e)
{
if (this.sanDiegoZoo == null)
{
this.DisplayMessage("Create San Diego Zoo first.");
return;
}
this.sanDiegoZoo.BirthArea.IncreaseTemperature();
this.temperatureTextBlock.Text = this.sanDiegoZoo.BirthArea.GetTemperature().ToString("0.0");
}newSanDiegoZooButton_Click, read and format the initial display with this.sanDiegoZoo.BirthArea.GetTemperature().ToString("0.0").| Handler | Replacement statement |
|---|---|
getComoZooAnimalCount_Click |
this.zooAnimalCount = this.comoZoo.GetAnimalCount(); |
getSanDiegoZooAnimalCount_Click |
this.zooAnimalCount = this.sanDiegoZoo.GetAnimalCount(); |
3 and 2, the original maps and successful feeding.VendingMachine.Fill. Recreate Como and fill once; while paused inside VendingMachine inspect this.FoodStock as 207. Press F5 to continue. Recreate San Diego and fill once; inspect this.FoodStock as 212, then continue and remove the breakpoint. A further whole bag would exceed capacity.77.0. Use + then - to see 77.5 then 77.0.77.0. Click + sixteen times to reach 85.0; another + leaves it at 85.0. From 85.0, click - sixty times to reach 55.0; another - leaves it at 55.0. Reset before the later birth checks. A birth at the maximum still proceeds while its temperature stays at 85.0.CreateComoZoo, temporarily add this.comoZoo.animals.Add(animal);. Build and observe the private-access error, then remove that test line.this.comoZoo.AnimalSnackMachine.AddMoney(3000.00m);. Build and observe the private-method access error, then remove that test line.Guest.FeedAnimal and click Darla, feed dingo. Add the expressions in the table before stepping.| Expression | Expected state |
|---|---|
this.wallet |
Existing Wallet reference |
this.wallet.moneyBalance |
5.25 before payment |
foodCost |
0.53 |
this.moneyBalance |
Unavailable; that field belongs to Wallet |
amount |
Unavailable here; it is a parameter of later wallet methods |
F11 and F10 to follow the actual calls.Wallet.GetMoneyBalance. this now refers to Wallet, where this.moneyBalance is 5.25. Return to the funds check.Guest.RemoveMoney into Wallet.RemoveMoney. amount is 0.53; after subtraction the private balance is 4.72.VendingMachine.AddMoney runs, this refers to the machine and amount is 0.53. After addition its private moneyBalance is 0.53. Press F5 to continue.AddAnimal(animal) call in CreateComoZoo and click New Como Zoo.| Expression | In MainWindow before the call |
|---|---|
animal.Name |
"Dolly" |
this.comoZoo.animals |
Empty list; the debugger can inspect private members |
this.animals |
Unavailable until inside Zoo.AddAnimal |
animal refers to Dolly. The zoo exists, but its private animals list still has Count 0.F11 into Zoo.AddAnimal. The parameter animal becomes available in Zoo; observe this.animals.Count become 1 after Add executes.Get animal count in the Como column and confirm the displayed population count is 3. The creation message itself reports pen occupancy, not population. Remove the breakpoint.null through AddAnimal’s condition. No list mutation occurs. Use the existing application calls for the runtime walkthrough.Zoo.WakeAllAnimals. Click Wake all animals once. While paused, add this.animals[0].Weight, this.animals[1].Weight and this.animals[2].Weight to Watch. Step over the loop iterations until all Move calls finish, then inspect the weights as approximately 34.241, 32.786 and 15.035. Press F5 to continue and remove the breakpoint.2, then click Get animal count in the Como column to confirm population count 4. The existing occupied-space guard still blocks a second birth into the same space.Wallet and MainWindow, whose fields are now private.