Home CPSC 240

Lab 7: 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 girls, every family continues to have children until they have a girl. If they have a boy, they have another child. If they have a girl, they stop. What is the proportion of girls to boys 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 a class called "Family", which should have the following variables:

The class should have the following methods:


 

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 submit the two Java files under the assignment in Canvas. Also as a comment, include the outcome and whether you were surprised or not.


 

Solution

A solution to this lab can be found in Family.java, and Main.java.

Copyright © 2024 Ian Finlayson | Licensed under a Creative Commons BY-NC-SA 4.0 License.