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.
You will write a program that solves this puzzle with two classes. The first is the Family class which has the following design:
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).
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.
The second class should be a class which just has a main method. In this main method, do the following things:
haveChildmethod repeatedly until it returns true. This simulates the family having children until they get a boy.
When you are done, please email your 2 .java files to firstname.lastname@example.org. 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.