In Zoo, creating an unborn Baby requires checking the animal’s state in the correct order. A nested decision can check Sex only when the animal does not already have a Baby. The inner decision belongs inside the path where creation is still possible.
if (this.Baby == null)
{
if (this.Sex == "Female")
{
this.Baby = new Animal();
// Assign the new Baby's fields.
}
}When Baby is present, the outer body is skipped, preserving the existing object. When Baby is null and Sex is Female, a new Baby is created. When Baby is null and Sex is not Female, no Baby is created. A later Zoo solution may express the first check as an early return; the dependent decision stays the same.
Keep all statements that initialize the new Baby inside the inner true branch. Moving any of them below the braces could change an existing Baby or try to access a null one. The activity diagram shows both tests before the creation action.
Test a female animal without a Baby, a male animal without a Baby, and an animal with an existing Baby. For each, name the condition that stops creation and inspect Baby afterward.
The outer Baby == null check prevents replacing an existing Baby. The inner Sex == "Female" check limits creation to eligible animals. Both checks must pass for all initialization statements to run. Use braces around each block so the assignment of Sex, Weight, Age, Name, and Type belongs to the same inner branch as the new Animal.