The Practice and Philosophy of Object-Oriented Programming in Java

New Practice Problems for Chapter 7

Maximum Load.  If some balls are tossed at random into a series of bins, the maximum load is defined to be the greatest number of balls that land in a single bin. Write a program that prompts the user for the number of balls and the number of bins, and performs a Monte Carlo simulation to calculate the expected maximum load.

    Enter number of balls and number of bins: 8 5
    Expected maximum load: 3
    Enter number of balls and number of bins: 20 10
    Expected maximum load: 4
    Enter number of balls and number of bins: 100 12
    Expected maximum load: 13

Target Load. Write a program that prompts the user to enter a number of bins and a target load, and outputs the expected number of balls that must be tossed at random into the bins until one of them attains the target load. The idea is illustrated by the following execution snapshot.

    Enter number of bins and target load: 20 8
    Expected number of balls needed to reach target load: 74

This output tells us that if you begin tossing balls at random into 20 bins and stop as soon as one of the bins has 8 balls, the expected total number of balls that you will toss is 74.

    Enter number of bins and target load: 10 6
    Expected number of balls needed to reach target load: 29

    Enter number of bins and target load: 100 5
    Expected number of balls needed to reach target load: 117

Rainbow Coloring.  This problem is motivated by the concept of a rainbow coloring described in the article Rainbow Proof Shows Graphs Have Uniform Parts in Quanta Magazine.

Write a JavaFX application that displays 16 dots evenly spaced around a circle. Coordinates for the dots are stored in an array. Each pair of dots is connected by a line whose color depends on the distance between them. To illustrate the idea of distance, consider the following screenshot, which shows the 16 dots and a red line between each pair of dots at distance 1 from each other.


The next two screenshots show a line between each pair of dots at distance 2 and 3 from each other respectively.


 

The following screenshot shows the application in its completed form — that is, all pairs of dots at each fixed distance from each other are joined by line of the same color. The colors for different distances are randomly generated with a red bias.


Rainbow Coloring Extended.  Extend your solution to the previous problem to present a dialog box that prompts the user for the number of dots. Use the TextInputDialog class for this purpose. Consult the API documentation for details.