Overview
Passing Information with Method Parameters
In this activity, you will update the Zoo project so methods can receive the values they need instead of relying on fixed values inside each method.
User Story: Make Zoo Actions More Flexible
The zoo manager wants the same actions to work with different amounts. Feeding an animal should be able to change its weight and happiness by different values. Ticket sales should work for more than one ticket. The birthing room and vending machine should also accept the amounts needed for each zoo.
Acceptance Criteria
- Methods receive needed values through parameters.
- Methods use those parameter values instead of fixed numbers.
- Positive-value checks prevent invalid amounts from changing object state.
- Ticket price and ticket count travel through the related objects.
- The Como Zoo and San Diego Zoo event handlers pass their own values.
- The project builds and the updated values can be verified with the debugger.
Task List
Parameter names can differ from field or property names, but every method call must match the method's name, parameter order, and value types.
Each code block shows the complete method or event handler that changes. Replace only that method. Do not replace the entire class file.
-
Prepare Your Working Copy
The Goal
Protect your completed 1.3 checkpoint and begin 1.4 from a separate working copy.
Your Task
Go to topWork in the CopyKeep the completed 1.3 folder unchanged so you can return to it if needed.
-
Understand the Change
The Goal
Make existing methods flexible by passing information into them through parameters.
What This Means
In 1.3, several methods used fixed values inside the method body. For example, an animal always gained the same amount of weight and a ticket sale always represented one ticket. In 1.4, the caller supplies those amounts when it calls the method.
A parameter is a named input in a method declaration. An argument is the actual value supplied in the method call. The method can then use the received value in its calculation.
Follow the ValueSome values pass through several objects. A ticket count begins in an event handler, moves into
Zoo, then continues into the guest, wallet, booth, and employee methods. Each method passes the value to the next object that needs it.Go to topWatch Out ForThe order of arguments matters. A call such as
FeedFeaturedAnimal(0.75, 3, 4.25m)must match the method's parameter order: weight gain, happiness gain, then money amount. -
Pass Values into Animal-Care Methods
The Goal
Replace fixed amounts with parameters in the animal, birthing-room, vending-machine, and wallet behaviors.
Update ZooScenario/Business Classes/Animal.cs
Replace the existing
Eatmethod. The method now receives both changes and applies each one only when it is positive.public void Eat(double weightGain, int happinessGain) { if (weightGain > 0) { this.Weight = this.Weight + weightGain; } if (happinessGain > 0) { this.HappinessLevel = this.HappinessLevel + happinessGain; } }Update ZooScenario/Business Classes/BirthingRoom.cs
Add
WarmRoom, then replaceWakeMotherUp. The temperature increase is passed from one method to the next.public void WarmRoom(double temperatureIncrease) { if (temperatureIncrease > 0) { this.Temperature = this.Temperature + temperatureIncrease; } } public void WakeMotherUp(double temperatureIncrease) { this.Mother.WakeUp(); this.WarmRoom(temperatureIncrease); }Update ZooScenario/Business Classes/VendingMachine.cs
Replace these three methods so the caller controls bag sizes and the money amount.
public void AddFoodBag(double poundsOfFood) { if (poundsOfFood > 0) { this.FoodStock = this.FoodStock + poundsOfFood; } } public void FillVendingMachine(double firstBagPounds, double secondBagPounds) { this.AddFoodBag(firstBagPounds); this.AddFoodBag(secondBagPounds); } public void AddMoney(decimal moneyAmount) { if (moneyAmount > 0) { this.MoneyBalance = this.MoneyBalance + moneyAmount; } }Update ZooScenario/Business Classes/Wallet.cs
Replace these methods so the wallet can add a supplied amount and subtract the price of multiple tickets.
public void AddMoney(decimal moneyAmount) { if (moneyAmount > 0) { this.MoneyBalance = this.MoneyBalance + moneyAmount; } } public void RemoveTicketPrice(decimal ticketPrice, int ticketCount) { if (ticketPrice > 0) { if (ticketCount > 0) { decimal totalTicketPrice = ticketPrice * ticketCount; this.MoneyBalance = this.MoneyBalance - totalTicketPrice; } } }Go to topWhy Check for Positive Values?These checks prevent zero or negative amounts from changing the object's state. The method is responsible for protecting the data it manages.
-
Pass Ticket Information Through the Object Model
The Goal
Let one ticket count move through the guest, wallet, booth, and employee objects so every related object updates consistently.
Update ZooScenario/Business Classes/Guest.cs
Replace
BuyTicketandVisitZoo. The guest passes the ticket price and count to the wallet.public void BuyTicket(decimal ticketPrice, int ticketCount) { this.Wallet.RemoveTicketPrice(ticketPrice, ticketCount); } public void VisitZoo(decimal ticketPrice, int ticketCount) { this.BuyTicket(ticketPrice, ticketCount); }Update ZooScenario/Business Classes/Employee.cs
Replace
SellTicket. The employee now records the number of tickets sold instead of always adding one.public void SellTicket(int ticketCount) { if (ticketCount > 0) { this.TicketsSold = this.TicketsSold + ticketCount; } }Update ZooScenario/Business Classes/Booth.cs
Replace
SellTicket. The booth calculates the total sale, updates its balance, and passes the count to the attendant.public void SellTicket(int ticketCount) { if (ticketCount > 0) { decimal totalTicketPrice = this.TicketPrice * ticketCount; this.MoneyBalance = this.MoneyBalance + totalTicketPrice; this.Attendant.SellTicket(ticketCount); } }Go to topOne Value, Several ResponsibilitiesThe same ticket count affects more than one object, but each object handles only its own responsibility: the wallet pays, the booth receives money, and the employee records tickets sold.
-
Connect the Values in Zoo.cs
The Goal
Update the coordinating methods in
Zooso each value is passed to the object that needs it.Your Task
In ZooScenario/Business Classes/Zoo.cs, replace these methods. Keep the methods near their current locations.
public void OpenForVisitor(int ticketCount) { this.SellTicket(ticketCount); this.FeaturedAnimal.WakeUp(); } public void SellTicket(int ticketCount) { if (ticketCount > 0) { this.Visitor.BuyTicket(this.TicketBooth.TicketPrice, ticketCount); this.TicketBooth.SellTicket(ticketCount); } } public void FeedFeaturedAnimal( double weightGain, int happinessGain, decimal moneyAmount) { this.FeaturedAnimal.Eat(weightGain, happinessGain); this.AnimalSnackMachine.AddMoney(moneyAmount); } public void PrepareBirthingRoom(double temperatureIncrease) { this.BirthArea.WakeMotherUp(temperatureIncrease); } public void FillAnimalSnackMachine( double firstBagPounds, double secondBagPounds) { this.AnimalSnackMachine.FillVendingMachine( firstBagPounds, secondBagPounds); }Go to topDo Not Reorder the ArgumentsThe call in each method must use the same order as the receiving method's parameter list.
-
Supply Values from MainWindow
The Goal
Give each zoo action the values it should use when the user clicks a button.
Your Task
In ZooScenario/MainWindow.xaml.cs, replace the eight event handlers below. The event handlers now provide arguments to the updated
Zoomethods.private void openComoZooButton_Click(object sender, RoutedEventArgs e) { this.ComoZoo.OpenForVisitor(1); this.informationTextBox.Text = "The Como Zoo is open for " + this.ComoZoo.Visitor.Name + "."; } private void feedComoAnimalButton_Click(object sender, RoutedEventArgs e) { this.ComoZoo.FeedFeaturedAnimal(0.75, 3, 4.25m); this.informationTextBox.Text = this.ComoZoo.FeaturedAnimal.Name + " has been fed."; } private void prepareComoBirthingRoomButton_Click( object sender, RoutedEventArgs e) { this.ComoZoo.PrepareBirthingRoom(1.5); this.informationTextBox.Text = "The Como Zoo birthing room is ready."; } private void fillComoVendingMachineButton_Click( object sender, RoutedEventArgs e) { this.ComoZoo.FillAnimalSnackMachine(40.0, 35.0); this.informationTextBox.Text = "The Como Zoo animal snack machine has been filled."; } private void openSanDiegoZooButton_Click(object sender, RoutedEventArgs e) { this.SanDiegoZoo.OpenForVisitor(2); this.informationTextBox.Text = "The San Diego Zoo is open for " + this.SanDiegoZoo.Visitor.Name + "."; } private void feedSanDiegoAnimalButton_Click( object sender, RoutedEventArgs e) { this.SanDiegoZoo.FeedFeaturedAnimal(0.25, 2, 2.50m); this.informationTextBox.Text = this.SanDiegoZoo.FeaturedAnimal.Name + " has been fed."; } private void prepareSanDiegoBirthingRoomButton_Click( object sender, RoutedEventArgs e) { this.SanDiegoZoo.PrepareBirthingRoom(0.75); this.informationTextBox.Text = "The San Diego Zoo birthing room is ready."; } private void fillSanDiegoVendingMachineButton_Click( object sender, RoutedEventArgs e) { this.SanDiegoZoo.FillAnimalSnackMachine(25.0, 25.0); this.informationTextBox.Text = "The San Diego Zoo animal snack machine has been filled."; }Go to topWhy the Values DifferThe Como Zoo and San Diego Zoo use different values to demonstrate that the same methods can perform similar work with different input.
-
Check Your Work
Build First
Trace a Ticket Sale
In ZooScenario/Business Classes/Zoo.cs, place a breakpoint on the first line inside
SellTicket(int ticketCount).Trace an Animal Feeding
In ZooScenario/Business Classes/Animal.cs, place a breakpoint on the first
ifstatement insideEat(double weightGain, int happinessGain).What This Means
The methods are no longer limited to one hard-coded amount. The caller supplies the information, and the receiving method uses that information to update the correct object.
Go to top