CS1321L Homework 5
Due: Monday, October 3 at 8:00 pm

Introduction

In this HW, you will begin to write programs with iteration, so you will get some practice working with Java's iteration constructs. You will also continue to do more with GUIs and Swing.

On the last few HW assignments, you began to learn about javadoc. More specifically, in the last homework you learned about how to do javadoc for methods. Now you will learn a couple of additional tags for class headers. In the class header you must add two tags after you write the description of the class.

The first tag is @author MyName gtnum. This identifies the person who wrote the class. It may seem unimportant to you now as a comment, but in a real software project, it is important to know who wrote and maintains a specific class in the application.

The second tag is @version versionnum. This identifies which version of the class is present. Again, it may seem unimportant, but in the real world, with many people potentially changing the code, it can become confusing which version of a class someone is using. Each project will have its own numbering systems for versions. In this class, you can just make your initial version of a class 1.0. As you make subsequent changes to the class, the version number may change. Smaller changes modify the decimal place (version 1.1, 1.3) and larger changes modify the integer part (version 2.0, 5.0). If you have multiple versions of a class for 1321, use whatever numbering system seems useful to you. If you have only one version, then just put 1.0 as your version.

For example:

/**
* This is a description of the entire class that may be some length.
*
* @author Mia Hamm gtg987s
* @version 1.0
*/

By convention the first line of a javadoc comment is always a summary sentence. Other sentences can then be included to detail the desired description.

Important javadoc note: From now on, we will relax our requirements that you write javadoc comments for all your methods. While this may be important when producing large class libraries for external use, adding javadoc comments to a one-line accessor or modifier method does seem like overkill on a class assignment. So for future assignments, you only need to add javadoc comments to "important, non-trivial" methods.

Assignment 5.1: Family Planning

How many children must a couple have in order to make sure they have at least one boy and one girl? In this program, you will develop a simulation that will help you estimate the answer to that question. Your objective is to model a couple that continues to have children and only stops once they have at least one boy and one girl. Assume that for any particular childbirth, the chances of having a boy or a girl are even. Model this with a simple coin toss (i.e., use Random()).

More specifically, write a program that prompts the user for the number of couples in the simulation. The program then should simulate each couple's child birth pattern, printing out the order of boys and girls born. Once the requisite number of families has been simulated, print out both the average and maximum number of children necessary to achieve the problem constraints.

After that, prompt the user whether they want to run another simulation and continue in the same fashion until the user decides to quit.

A sample run might look like the following:

c:\java\hw5\> java FamilySimulator
Ready to run a family simulation. Enter the number of families:
4
Simulating Families
1 - BBG
2 - GB
3 - GGGB
4 - BG
The average number of children was 2.75 and maximum was 4.

Would you like to run another simulation? (y/n)
y
Ready to run a family simulation. Enter the number of families:
25
1 - GGB
2 - BG
...
Notes & Hints:
  • You can assume that the user will only enter an integer for the number of families.
  • That said, the user may enter an unallowable integer, and may respond with an inappropriate character to the continue question. Your program should handle these situations well.
  • Do NOT code this entire program in main(). Think about a good program design and the set of classes you might create.
  • Try a big number like 100 and see what your answers are.

    Assignment 5.2: Pick a Color, Any Color

    In this assignment you will build a simple graphical application that helps a person choose a color. Your program will have three text entry boxes, one each for the red, green, and blue components of a color. It will also have a rectangular region that is used to display the currently specified color. A valid value in each box is from 0 to 255 (but remember that users sometimes do nasty things like entering bad values!). When the user types a return in any of the boxes, you should update the color shown in the box. Additionally, your program should have a button that allows the user to make a random choice. When this button is clicked, you should generate random values from 0 to 255 for each color component, change the text boxes, and then display the new color. An example view of what your program might look like is shown below, but you should feel free to design your own user interface.

    Requirement: Call your top-level class (one with main()) ColorChooser.

    Hint: Examine the class Color in AWT. It has useful constructors and methods to help in the assignment. Look at the GUI code examples at the end of Chapters 4 and 5. This program is a step up from the level of the programs we have been doing so far.

    Turn-in Procedures

    After you have finished the above assignments, turn them via Webwork You will be submitting multiple files. Please make sure they are named as shown below:

  • FamilyPlanner.java
  • Any other classes needed in part 5.1.
  • ColorChooser.java
  • Any other classes needed in part 5.2.