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:

Method Description abs(x) Absolute value of x sin(x) Sine of x (in radians) cos(x) Cosine of x (in radians) tan(x) Tangent of x (in radians) asin(x) Inverse sine of x (in radians) acos(x) Inverse cosine of x (in radians) atan(x) Inverse tangent of x (in radians) log(x) Base e (natural) logarithm of x pow(x,y) x raised to power y exp(x) The constant e raised to the power x sqrt(x) Square root of x ceil(x) Smallest integer not less than x floor(x) Largest integer not greater than x round(x) Closest integer to x min(x,y) Minimum of x and y max(x,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:

Method

Description

toUpperCase(ch)

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

toLowerCase(ch)

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

isUpperCase(ch)

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

isLowerCase(ch)

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

isDigit(ch)

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

isLetter(ch)

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

isSpace(ch)

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