# Lab 6: Simulation

### Objective

To get more practice programming with Java classes.

In this lab, you will write some code to solve an old Google interview question that tests candidates reasoning skills. The questions goes like this:

In a country in which people want boys, every family continues to have children until they have a boy. If they have a girl, they have another child. If they have a boy, they stop. What is the proportion of boys to girls in the country?

Before beginning to solve this problem, make a guess as to what the answer is. Put this guess in the comments of your program before writing it.

### Family Class

You will write a program that solves this puzzle with two classes. The first is the Family class which has the following design:

The rng variable should be marked static (so the objects all share one random number generator. You should assign it equal to a new Random object where it is declared (not the constructor).

The haveChild method should pick a random number between 0 and 1. It should use that to randomly increment either the boys or girls variable. If it has a boy, it should return true. If it's a girl, false.

### Main Class

The second class should be a class which just has a main method. In this main method, do the following things:

• Make an ArrayList of families.
• Add 1 million families to it.
• Go through each family, and call the haveChild method repeatedly until it returns true. This simulates the family having children until they get a boy.
• Count all of the girls and boys across all million families.
• Print the numbers out, along with the percentages.
• Was the ratio of girls to boys what you expected? Put the outcome in the email message when you turn it in.

### Submitting

When you are done, please email your 2 .java files to ifinlay@umw.edu. Be sure to include the outcome and whether you were surprised or not!