- What happens if you try to take the square root of a negative number?
Java returns NaN, an IEEE invention that means "Not a Number."

- What happens if you use
`java.lang.Math.pow()`

to raise zero to the zeroth power?Java returns 1. Mathematically, this is incorrect. It should return

`NaN`

. - What, if anything, can come before a
`package`

statement in a Java source code file?Only comments.

- What, if anything, can come before an
`import`

statement in a Java source code file?Comments and package statements.

- Can one source code file import from more than one package?
Absolutely.

- 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.
`public double sine(double degrees) { double radians = degrees * Math.PI / 180.0; return Math.sin(radians); } public double cosine(double degrees) { double radians = degrees * Math.PI / 180.0; return Math.cos(radians); } public double tangent(double degrees) { double radians = degrees * Math.PI / 180.0; return Math.tan(radians); }`

- Write the corresponding set of inverse trigonometric methods that return their values in degrees instead of radians.
`public double arcsine(double sine) { return Math.asin(sine) * 180.0 / Math.PI; } public double arccosine(double cosine) { return Math.acos(cosine) * 180.0 / Math.PI; } public double arctangent(double tangent) { return Math.atan(tangent) * 180.0 / Math.PI; }`

- The math library is missing secant, cosecant and cotangent methods. Write them.
`public double sec(double radians) { return 1.0/Math.cos(radians); } public double csc(double radians) { return 1.0/Math.sin(radians); } public double ctn(double radians) { return 1.0/Math.tan(radians); }`

- The math library lacks a log10 method for taking the common logarithm. Write one.
`public double log10(double x) { return Math.log(x)/Math.log(10.0); }`

- Computer scientists often use a log2 (log base 2). java.lang.Math doesn't have one of those either. Write it.
`public double log2(double x) { return Math.log(x)/Math.log(2.0); }`

- 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 p0e
1.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.`public class bacteria { public static void main(String args[]) { try { double p0 = Double.valueOf(args[0]).doubleValue(); double t = Double.valueOf(args[1]).doubleValue(); System.out.println(p0 * Math.exp(1.4 * t)); } catch (Exception e) { System.out.println("Usage: java bacteria initial_population num_hours"); } } }`

- Modify the bacteria growth program so that the time can be input in minutes. Note that the formula still requires a time in hours.
`public class bacteria { public static void main(String args[]) { try { double p0 = Double.valueOf(args[0]).doubleValue(); double t = Double.valueOf(args[1]).doubleValue()/60.0; System.out.println(p0 * Math.exp(1.4 * t)); } catch (Exception e) { System.out.println("Usage: java bacteria initial_population num_hours"); } } }`

- 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.

`public class agefromC14 { public static void main(String args[]) { try { double p0 = Double.valueOf(args[0]).doubleValue(); double age = -Math.log(p0/1.22E-4); System.out.println(age); } catch (Exception e) { System.out.println("Usage: java agefromC14 %_C14"); } } }`

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

Copyright 1996

elharo@sunsite.unc.edu

Last Modified March 3, 1997