Home CPSC 340

Lab 1: Java Setup and Practice

 

Due: January 29


 

Objective

To ensure that you have a working Java programming environment, and get back into the swing of coding.


 

On Java Editors and IDE's

In this class you are free to use whichever Java programming environment you are most comfortable with. If you have something that works for you, then you should probably keep on using it.

In the examples I use throughout this class, I will use the Vim text editor, and the command-line Java compiler. You can use this if you wish, but I don't require you to.

If you need help installing Java or an IDE, please let me know and I will be happy to assist you!


 

Task

To get warmed up for programming, and to make sure you can compile, run, and turn in programs, you will write a simple program for this lab.

Google used to use tricky interview questions that tested candidate's statistics and reasoning skills. One such question goes like this:

In a country in which people only 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.

We could solve this problem using statistics, but we can also simulate this hypothetical country and empirically see what will happen. You will write a short program to empirically find the answer to this riddle by simulating 1,000,000 families having children this way.


 

Details

  1. Create a class called "Family" which contains two private variables: one for the number of girls, and one for the number of boys.
  2. Create a constructor which sets both to 0.
  3. Create "getter" methods which return the number of girls and boys.
  4. Create a method called "step" which will simulate one cycle of the family. If the family already has a girl, the method returns right away. If the family does not have a girl, then they have a child. Use a random number generator to decide if they have a girl or boy (it should be 50/50 of course). Increment either the number of girls or boys based on the outcome. Return a boolean to indicate whether the family is done having children or not.
  5. In the main method, create and initialize an array of one million family objects.
  6. Call the "step" method on each repeatedly until every family has a girl.
  7. Print the number of girls, the number of boys, and the percentage of the population which is female.

 

Submitting

When you are finished, please upload your code to the Canvas page for this assignment. Please send the .java file(s) only.

Copyright © 2022 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.