Chapter 8: Packages

The exercises here are taken from my forthcoming book The Java Developer's Resource.


  1. What happens if you try to take the square root of a negative number?
  2. What happens if you use java.lang.Math.pow to raise zero to the zeroth power?
  3. What, if anything, can come before a package statement in a Java source code file?
  4. What, if anything, can come before an import statement in a Java source code file?
  5. Can one source code file import from more than one package?


  1. Although mathematicians prefer to work in radians, most scientists and engineers find it easier to think in degrees. Write sine, cosine and tangent methods that accept their arguments in degrees.
  2. Write the corresponding set of inverse trigonometric methods that return their values in degrees instead of radians.
  3. The math library is missing secant, cosecant and cotangent methods. Write them.
  4. The math library lacks a log10 method for taking the common logarithm. Write one.
  5. Computer scientists often use a log2 (log base 2). java.lang.Math doesn't have one of those either. Write it.
  6. A simple model for the growth of bacteria with an unlimited supply of nutrients says that after t hours an initial population of p0 will have grown to p0e1.4t. Write a Java application that calculates the growth of a colony of bacteria. As usual get the value of p0 and t from the command line.
  7. Modify the bacteria growth program so that the time can be input in minutes. Note that the formula still requires a time in hours.
  8. Carbon dating is used to estimate the age of once living objects according to the following formula:

    Write a Java application that calculates the age of an object given the percentage of carbon 14 remaining.

  9. Put the VeryLong and Rational classes of the Exercises in Chapter 7 into your own package.

[ Exercises | Cafe Au Lait | Books | Trade Shows | Links | FAQ | Tutorial | User Groups ]

Copyright 1996 Elliotte Rusty Harold
Last Modified August 20, 1996