Experiment 8: Expressions Containing Functions

The idea of a function, such as the square root of a number, or the sine of an angle should be a familiar one. In Java, functions are called methods and are typically stored in libraries (packages or modules): software bundles in which useful methods and classes can be stored for reuse. Some packages (i.e., java.lang) are automatically made available and do not need to be imported. With packages that are more specialized, we use import to make them generally available throughout our program. (The import is not strictly needed. It allows us to use names that do not contain the whole package name. For example: Math.sqrt() instead of java.lang.Math.sqrt(). The import also serves a documentation purpose by informing readers of the code which packages are going to be used.)

Java stores methods in packages (instead of building them into the language) for two reasons:

  1. Doing so allows different programs to share the same method/class; and
  2. A program that does not use such methods/classes may take up less space than a program that does. (This would not necessarily be the case if methods were built-in.)


The Java Math Class

The module that provides methods for mathematical functions like sine, cosine, logarithm, and so on is contained in the class Math which is a part of the package java.lang. Most of its functions should be familiar to students who have had a course in trigonometry. Some of the more common methods there include:




Absolute value of x


Sine of x (in radians)


Cosine of x (in radians)


Tangent of x (in radians)


Inverse sine of x (in radians)


Inverse cosine of x (in radians)


Inverse tangent of x (in radians)


Base e (natural) logarithm of x


x raised to power y


The constant e raised to the power x


Square root of x


Smallest integer not less than x


Largest integer not greater than x


Closest integer to x


Minimum of x and y


Maximum of x and y

In each of these methods, the arguments x and y, are real (double) values. (Any value that can be promoted to a double value can be used as well.)

The definitions of these methods are stored the package java.lang so there is no need to do an import.

To use a method, we need to add the name of the class it belongs to. For example, to compute the sine of the variable x and place the result in y we would need to use

y = Math.sin(x);

Modify Express.java as needed to find the following values (Hint: do all of them simultaneously, to reducing the time spent recompiling):


the log of 1000.0


10.0 raised to the power 3.0


the sine of pi (3.14159) radians


the cosine of pi (3.14159) radians


the logarithm of 1000


10 raised to the power 3


the ceiling of 9.9


the ceiling of 9.1


The Character class

In addition to a module of mathematical methods, Java also provides a module containing a rich assortment of character-manipulating methods contained in the class Character which is also a part of the package java.lang. The methods provided by this module include:




returns the upper-case equivalent of ch, if and only if ch is lower-case


returns the lower-case equivalent of ch, if and only if ch is upper-case


returns true if and only if ch is one of 'A' through 'Z'


returns true if and only if ch is one of 'a' through 'z'


returns true if and only if ch is one of '0' through '9'


returns true if and only if ch is one of 'A' through 'Z' or 'a' through 'z'


returns true if and only if ch is a white-space character (SPC, TAB, '\n', FORMFEED, CR, or VTAB)

In each of these methods, the argument ch is a character (char) value.

For now, just take a moment to read through the description of each method and try to remember the general capabilities the module provides. Then if you encounter a problem in which that operation is needed and you remember that there is a predefined method for that operation, you make the mistake of "reinventing the wheel."


As before, to use these methods, we need to add the name of the class it belongs to. For example, to convert a character in variable c1 to upper case and place the result in y we would use:

y = Character.toUpperCase(c1);


From this exercise, you should now have an intuitive idea of what a module of methods is. You should also know that among the many modules Java provides are a module of mathematical methods, and a module of character-manipulating methods.

Back to the Exercise List

Forward to the Next Experiment

Back to the Table of Contents

Back to the Introduction

Copyright 2000 by Prentice Hall. All rights reserved.