- Creation calls pass the existing scenario values as arguments.
- Constructors assign the starting state and connect contained objects.
Continue from your completed Zoo 2.5. 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.6 Lastname"
, using your last name. Keep the project target at .NET Framework 4.8.77.0.Currently MainWindow instantiates employees and assigns their field values, when it should be the responsibility of the class to handle assigning those values.
You will instantiate Employee by passing values into a constructor that assigns the required fields.
Employee.cs based on the class and activity diagrams.Name and Number to private fields named name and number.Employee(string name, int number) and assign both parameters to their corresponding fields.name "Sam" and number 42, trace the assignments to this.name and this.number. This is a source trace until the creation paths are updated.public Employee(string name, int number)
{
this.name = name;
this.number = number;
}Booth.cs based on the class and activity diagrams.TicketPrice private and rename it ticketPrice. Keep Attendant public for now.Booth(Employee attendant, decimal ticketPrice). Assign this.Attendant = attendant; and this.ticketPrice = ticketPrice;.Employee reference and price 15.00m, trace Attendant receiving that same reference and ticketPrice receiving 15.00m.Guest.cs based on the class and activity diagrams.Name and Age private fields named name and age. Remove new Wallet() from the wallet field declaration, leaving the private field.Guest(string name, int age, decimal moneyBalance). Assign the name and age parameters to their matching fields, instantiate this.wallet, then call this.AddMoney(moneyBalance)."Darla", 11 and 5.25m, trace the private name/age assignments, wallet creation and AddMoney call. The wallet ends with balance 5.25m.MainWindow is handling the instantiation of the animal snack machine and assigning its values, when the class should handle its required starting information.
You will instantiate VendingMachine by passing values into a constructor that assigns the required fields.
VendingMachine.cs based on the class and activity diagrams.FoodPricePerPound private readonly as foodPricePerPound; make FoodStock private as foodStock. Update the class’s own references to those fields.VendingMachine(decimal foodPrice). Assign this.foodPricePerPound = foodPrice; and call this.Fill() so the existing loop supplies the initial stock.foodPrice 0.75m, trace price assignment and Fill. The final foodStock is 195 and the machine’s money balance is still zero.BirthingRoom.cs based on the class and activity diagrams.temperature, leaving private double temperature;. Add private readonly double initialTemperature = 77.0;.BirthingRoom(Employee doctor). Assign this.temperature = this.initialTemperature; and this.Doctor = doctor;. Keep the existing private readonly limits at 85.0 and 55.0 and the existing adjustment methods unchanged.Employee reference, trace temperature becoming 77.0 and Doctor receiving Flora’s same reference. The limits retain their declaration values of 85.0 and 55.0.Restroom.cs based on the class and activity diagrams.Capacity private readonly as capacity, and make Sex private as sex.Restroom(int capacity, string sex) and assign the fields from the parameters.capacity 4 and sex "Female", trace the assignments. The readonly capacity receives its value inside construction.MainWindow is handling the instantiation of the animals and assigning their values, when the Animal class should handle assigning those values.
You will instantiate Animal by passing values into a constructor that assigns the required fields.
Animal.cs based on the class and activity diagrams.Age and Sex private as age and sex; update the class’s own references. Keep the other current fields and behavior.Animal(string type, string sex, string name, int age, double weight). Assign Type, sex, Name, age and Weight from the corresponding parameters.Type "Dingo", sex "Female", Name "Dolly", age 4 and Weight 35.3. The baby field is still null until MakePregnant is called.Food is instantiated in different places for different reasons, and its required weight should be assigned in Food itself.
You will instantiate Food by passing values into a constructor that assigns the required fields.
Food.cs based on the class and activity diagrams.Food(double weight).this.Weight = weight;.weight 1.25, trace the constructor assigning Food.Weight to 1.25. This is a source trace, not a new UI action.this.foodPricePerPound using the return type shown in the class diagram.this.foodStock using the return type shown in the class diagram.machine.FoodPricePerPound read with machine.GetFoodPricePerPound().machine.FoodStock read with machine.GetFoodStock(). Keep the wallet, payment, stock, refund and result-code logic.this.sex using the return type shown in the class diagram."Dingo" and "Platypus".this.baby using the Animal constructor: the case’s type, "Male", string.Empty, age 0 and this.Weight multiplied by this.babyWeightPercentage.Sex and Weight assignments; those values now come from the constructor.35.3, the baby constructor receives weight 3.53. Its initial name is string.Empty; the existing birth handler supplies the name after delivery.Food’s constructor at the existing food-creation point.Weight assignment with foodPacket = new Food(amount);. Keep the amount/stock guard and stock subtraction.Food is created only on the successful path.Zoo creates its own list, snack machine, birthing room and restrooms.Zoo without duplicating them.MainWindow is handling the instantiation of the zoo, its snack machine, birth area and restrooms, and assigning their values.
You will move the zoo-owned setup into the Zoo constructor.
Zoo.cs.new List<Animal>() from the private readonly animals field declaration. Make Capacity, LadiesRoom, MensRoom and Name private with lowercase names.animals to a new list and assign name, capacity, TicketBooth and Visitor from the parameters.AnimalSnackMachine using animalFoodPrice, BirthArea using doctor, and the two restrooms using restroomCapacity and the corresponding "Female" or "Male" string.booth, doctor and visitor should not be replaced with duplicates.this.name using the return type shown in the class diagram.MakePregnant calls, AddAnimal calls, the first animal’s pen/tank reference and its array placement. Keep the final area refresh.MakePregnant calls, AddAnimal calls, the first animal’s pen/tank reference and its array placement. Keep the final area refresh.zoo.Name with zoo.GetName(). Keep the search, transaction and result-code switch unchanged.name field is read inside Zoo.GetName; MainWindow uses the returned string.newComoZooButton_Click, replace this.comoZoo.Name with this.comoZoo.GetName(). In newSanDiegoZooButton_Click, replace this.sanDiegoZoo.Name with this.sanDiegoZoo.GetName(). Keep the creation-method calls and area-count messages.baby.Sex with baby.GetSex(). Keep the birth guards, array placement, names, messages and temperature refresh.| Handler | Read expression |
|---|---|
newComoZooButton_Click |
this.comoZoo.GetName() |
newSanDiegoZooButton_Click |
this.sanDiegoZoo.GetName() |
birthDingoButton_Click |
baby.GetSex() |
birthPlatypusButton_Click |
baby.GetSex() |
3 and 2 and the original maps remain.77.0. Test + and - once each.2, then click Get animal count in the Como column to confirm population 4; the newborn occupies the existing starting space.Employee creation in CreateComoZoo, then click New Como Zoo.| Expression | Expected state |
|---|---|
this.comoZoo |
null on this first creation |
attendant |
Not assigned yet; null or unavailable |
name |
Unavailable in MainWindow; constructor parameter |
number |
Unavailable in MainWindow; constructor parameter |
F11 to enter Employee and F10 for assignments.Employee’s constructor body, add name, number, this.name and this.number to Watch before stepping. name is "Sam" and number is 42. this refers to the new Employee. Its name field is initially null and number field is initially 0.MainWindow. attendant now refers to that Employee.Booth’s constructor and confirm its Attendant is the same reference. Press F5 to continue and remove these breakpoints.name and this.name.Zoo constructor’s first statement and click New Como Zoo. Use F11 to follow its contained-object construction.| Expression | Expected state at the first statement |
|---|---|
name |
"Como Zoo" |
animalFoodPrice |
0.75m |
doctor |
Flora’s existing Employee reference |
visitor |
Darla’s existing Guest reference |
this.animals |
null before the list assignment |
this.AnimalSnackMachine |
null before machine creation |
this.BirthArea |
null before room creation |
this.ladiesRoom |
null before restroom creation |
this.mensRoom |
null before restroom creation |
VendingMachine and observe foodPrice become 0.75m. Follow Fill: foodStock starts at 0 and becomes 65, 130 and 195; the next whole bag is rejected by the existing fit check.Zoo and enter BirthingRoom. Its initialTemperature is 77.0, temperature starts at 0 before its body assignment, and Doctor is assigned the same doctor parameter.Zoo, expand this.ladiesRoom and this.mensRoom. Confirm their private capacity values are 4 and their sex values are "Female" and "Male". Press F5 to continue and remove these breakpoints.MainWindow’s earlier FoodStock assignment.foodPacket = new Food(amount) in VendingMachine.RemoveFood, then click Darla, feed dingo.| Expression | Expected state |
|---|---|
amount |
Approximately 0.7066667 |
foodPacket |
null before the assignment |
weight |
Unavailable in this method; Food constructor parameter |
this.foodStock |
Already reduced by amount; subtraction precedes Food creation |
F11 into Food and F10 through its assignment.Food, add weight and this.Weight to Watch before the constructor assignment. weight is approximately 0.7066667 and this.Weight starts at 0. After the assignment it holds the supplied weight.RemoveFood and the existing sale/feeding path. Press F5 to continue, then remove the breakpoint.Food’s required weight is assigned before the caller receives its reference.