Project 7
Real-world events like counting how many babies are born means collecting lots of data. Sometimes, this can take too long. Creating a simulation helps you to understand an event in less time. A simulation is a pretend version of the real event. In this project, you will create a maternity ward simulation — a model of a hospital room where babies are born and celebrate their “zero birthdays.”
Instead of taking nine months for a baby to arrive, this simulation makes a pretend newborn boy or girl by clicking a button. Then, using the repeat command, you can make hundreds of babies (or more) to represent all the births in a state or country. Using the simulation, you can quickly look at the distribution of births, meaning how many boys and girls are born in a large population of babies. Say “Happy Birthday!”
The Happy Birthday simulation does not have to feature babies being born. Consider simulating the birth of any creature, particularly those that produce only one offspring at a time:
… or fantasy creatures you invent!
Also consider alternatives for where the “baby” is born:
You can even change the entire project to represent any other two-outcome scenario, such as flipping a coin (heads or tails), evaluating the state of a radioactive atom (decayed or not decayed), lighting a lamp (on or off), or monitoring the health of Schrödinger’s cat (dead or alive).
Begin creating Happy Birthday by starting a new project as follows:
A new project opens.
Because babies are often born in a maternity ward, apply a background to your simulation that conveys a hospital theme:
The Painting/Clipart palette opens.
The image is stamped to fill the entire workspace.
This area will be used to feature the buttons of the graphical user interface (refer to the figure at the beginning of this project).
Add a title to the page as follows:
See Project 1 for details on formatting text.
Text boxes can be resized at any time. Ctrl-click (Windows) or Command-click (Mac) an opaque text box. Sizing dots appear — click and drag any of them to resize the text box.
Your can edit text only when the text box is opaque (not transparent). To change a text box from transparent to opaque, click the text; then right-click (Windows) or Ctrl-click (Mac) and select Opaque from the pop-up menu.
The simulation requires only one turtle object. The turtle will represent the newborn baby. At each birth, the baby turtle will wear a shape indicating whether it is a girl baby or a boy baby. Create girl and boy characters for your simulation as follows:
Your workspace now looks similar to Figure 7-2.
The Shape Editor opens.
Feel free to call the shape any name you want; just be sure to keep track of the name you use when coding procedures. It’s easiest to name the shape by its color.
The Shape Editor closes, and the pink shape appears in a spot on the project Shapes tab.
Again, call the shape any name you want; just keep track of the name you use when coding procedures.
The Shape Editor closes, and the blue shape appears in a spot on the project Shapes tab.
The turtle now wears the shape.
If you accidentally click somewhere other than the turtle, the pink or blue shape will appear on the background — simply right-click (Windows) or Ctrl-click (Mac) the shape and select Remove from the pop-up menu to get rid of it.
The Happy Birthday simulation needs a way to show whether a newborn baby is a boy or a girl. It also needs a way to count how many total boys and how many girls are born in a large population. Variables will be used for all three of these.
Create a variable as follows:
Now, instead of showing a title, this text box will be used to show the value of a variable.
This text box will be used to show a discrete variable value — a value that can only be a certain number such as 0 (for a girl) or 1 (for a boy). Like flipping a coin, there is no “in-between” value — it’s either heads or tails, boy or girl.
Text boxes used as variables must remain opaque for the variable values to change.
These will be counting variables or counters that count the total number of girls and the total number of boys. (Counting variable is not a formal mathematical term, just a description of how this variable is functioning in your program.)
To create a new text box with the same size and shape as an existing text box: Right-click (Windows) or Ctrl-click (Mac) inside the text box you want to duplicate and select Copy Box from the pop-up menu. Then right-click (Windows) or Ctrl-click (Mac) in the workspace and select Paste from the pop-up menu. A duplicate text box appears.
Your workspace should look similar to Figure 7-5.
Like any simulation, the maternity ward needs a starting point. A procedure is needed to start, or initialize, the model. The procedure represents starting a new shift at a hospital or starting a new day when the number of babies born is zero.
Follow these steps to write an initialize procedure and create a button to execute it:
to initialize
setgirls 0
setboys 0
end
This procedure sets the value of the girls and boys variables to zero. Remember, each of these variables counts how many girls and how many boys are made with the model. Now that you have written the initialize procedure, MicroWorlds EX recognizes it as a new command that you can use!
The Initialize button is added to the workspace.
A zero should appear in the girls text box, and a zero should appear in the boys text box.
Besides calling a procedure from a button, you can also add it to the rules of a turtle backpack, or embed it another procedure.
My students always ask me, “What does reproduce mean?” In our computer simulation, it means to make a new baby. (In biology, it means that, too, but I won’t explain those details. There may be a stork involved.)
You probably know from simple genetics that if a baby has certain genes, it’s a girl. If it has different genes, it’s a boy. Notice that I used the words if and then when writing those sentences. You will use the same structure to create IF-THEN commands in your program code. These commands let you show the gender of the newly made baby. Gender means boy or girl.
Write a procedure to make a baby and show its gender as follows:
to reproduce
setgender random 2
if gender = 0 [setsh "pink setgirls girls + 1]
if gender = 1 [setsh "blue setboys boys + 1]
end
This procedure sets the value of gender randomly to 0 or 1. The number 0 means that a girl is born, and the number 1 means that a boy is born. Each number has an equal probability of appearing, so there is a 50% chance of creating a girl and a 50% chance of creating a boy.
The procedure then uses two IF-THEN conditional statements. Each statement affects the shape worn by the turtle and a counter variable:
Here, the girls variable and the boys variable serve as counters by keeping track of the total number of girls and the total number of boys.
The Reproduce button is added to the workspace.
A 0 or 1 should appear in the gender text box, and the shape worn by the baby turtle object should change to match the gender. The associated girls or boys variable value should increase by 1.
If needed, you can resize a button (assuming it is not frozen) to better fit the labels it displays. Ctrl-click (Windows) or Command-click (Mac) a button. Sizing dots appear — click and drag any of them to resize the button.
After you have written both the initialize procedure and the reproduce procedure, you can put them together in new ways. For example, you can combine these two procedures with a repeat command to write a procedure to make ten babies.
Follow these steps to write the make_10_babies procedure and make a button to execute it:
to make_10_babies
initialize
repeat 10 [reproduce]
end
This procedure might represent the births at a single hospital in a single day, assuming that ten births occur. It allows you to see each newborn baby appear — really fast — and count the total number of boys born and the total number of girls born.
The Make 10 Babies button is added to the workspace.
Each time you click the button, the make_10_babies procedure is executed. The girls and boys counters allow you to see the frequency distribution of gender in the baby population.
Choose File⇒Save Project from the menu bar to save your simulation. Your finished simulation should look similar to the title figure, and your procedures should look similar to Figure 7-9. Note that this figure includes procedures suggested in the nearby “Enhance your simulation” sidebar.
Test each button to make sure it functions as you intend. Check for error messages in the Command Center to help you determine where any bugs may exist in your code. When you’re finished, run the simulation several times and evaluate how well you believe it models the real world.
3.139.72.78