Home CPSC 240

Lab 6: Simulation

 

Objective

To get more practice programming with Java classes.


 

Task

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:


 

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!

Copyright © 2019 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.