Experiments in Java


Problems for Session J5: Control Structures for Repetition

Problem J5-A: daysUntil

Using Java's two primary looping mechanisms, we can investigate a more complicated issue: How do we determine the number of days between two dates? More precisely, how do we write the following method?

  /**
   * Determine the number of days from the current day until
   * the event.  Note that the event must occur after the
   * current day.
   */
  public int daysUntil(Date event) {
    ...
  } // daysUntil(Date)

We might employ a number of strategies to answer this question. We might determine the number of days each date falls from a set date (e.g., January 1, 1970) and then subtract the two. We might also use a strategy like the one we used for determining the number of days until the start of the month. That is, rather than counting up days in each month, we might count up the days in each year, starting with the first year and ending with the final year.

We recommend that you try both.

Add a daysSinceDayZero method to the SimpleDate class. This method should compute the number of days since some designated day zero (you can determine what that date is) until the current date (i.e., the one represented by this).

Write a daysUntil method that uses daysSinceDayZero to compute the number of days until another day.

Problem J5-B: daysSince

Write a daysSince method that computes the number of days since another date. Don't use daysSinceDayZero or daysUntil. You will probably need to count up the days in each year, as described earlier.

Problem J5-C: Reflection

Having done Problems J5-A and J5-B, which one seems the more elegant way to solve the problem?

Problem J5-D: Loop mechanisms

What looping mechanisms did you use in problems J5-A and J5-B? Why?

Problem J5-E: Grade averaging

Write a program that reads in a sequence of grades, terminated by -1 and prints out the average of the grades.

Problem J5-F: Maximum grade

Write a program that reads in a sequence of grades, terminated by -1 and prints out largest of the grades.

Problem J5-G: Revising the rainbow's shape

In Experiment J5.5, we created a simple rainbow from a message. We saw how to make that rainbow appear as a diagonal line, and as a simple curve. Find a few other interesting curves for the rainbow. Make it possible for the HTML page to select what curve to use.

Problem J5-H: Revising the rainbow

One of the difficulties of the rainbow from Experiment J5.5 is that the progression of colors was not particularly sophisticated. This is because it's difficult to switch between our normal sense of a rainbow (which is based on wavelengths) and the RGB color model. Java's java.awt.Color class also supports the hue, saturation, and brightness (HSB) model. Read the documentation on java.awt.Color and the corresponding getHSBColor method, and use this method to improve the appearance of the rainbow.

Problem J5-I: A complete grid

In Experiment J5.6, we created a grid of points whose spacing and number of columns and rows were specified by the corresponding HTML page. However, one might instead prefer to have the number of columns and rows determined by the area given to the applet, along with the spacing. Write a new grid applet that fills the applet's area with a regularly spaced grid of points.

Problem J5-J: Improving the bouncing simulation

You may have observed that there are some problems with the bouncing simulation from Experiment J5.7. In particular, even the the bouncing is supposed to be 100 percent elastic, the ball seems to bounce lower and lower at each step. Why is this? Because our code does not appropriately account for the part of the ``step'' in which the ball bounces. In particular, it effectively treats a ball that bounces early in the step (i.e., if it started quite near the floor) the same as one that bounces later in the step (i.e., that fell most of the way before bouncing). Improve the simulation to handle that issue. While doing so, make sure that the ball does not fall below the bottom of the screen.

After making that improvement, consider how you might take elasticity more explicitly into account.


Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.

Source text last modified Mon Oct 25 16:33:18 1999.

This page generated on Tue Oct 26 15:37:57 1999 by Siteweaver.

Contact our webmaster at rebelsky@math.grin.edu