Hands on Testing Java: Lab #3

Experiment #9: Method Calls

Do this...
Create a test-case class named Experiment09Test.

Mathematical Functions

You should be familiar with mathematical functions, such as the square root of a number or the sine of an angle. In Java, a mathematical function is implemented as a method. The most common and useful mathemtical functions can be found in the Math class, provided by standard Java.

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) Natural logarithm (base e) 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 integer or real numbers (i.e., doubles).

In addition to methods, the Math class declares constants, one for the value pi, Math.PI, and one for the Euler number, Math.E. We will look at constants in more detail in another experiment.

To invoke a method, use the name of the class it belongs to as well as its name. For example, this expression computes the sine of pi:

Math.sin(Math.PI)

We could then test the result like so:

Do this...
Create a test method in Experiment09Test named testMath(), and add this statement:

assertEquals("sin of pi", 0.0, Math.sin(Math.PI), 1e-3);

Compile, and run for a green bar.

Try some more methods:

Do this...
Write assert statements in testMath() to test these computations:

Compile and run for a green bar. It's easiest if you work on one computation at a time.

The Character Functions

Java also provides a module containing a rich assortment of character-manipulating methods contained in the class Character. The methods provided by this module include:

Method Description
toUpperCase(ch) returns the uppercase equivalent of ch if ch is lowercase, otherwise it just returns ch
toLowerCase(ch) returns the lowercase equivalent of ch if ch is uppercase, otherwise it just returns ch
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'
isWhitespace(ch) returns true if and only if ch is a white-space character (i.e., a space, a tab, a newline, a formfeed, a cariage return, or a vertical tab)

The argument ch in these methods is a character (char) value. Note that ch can be any character, even if the method doesn't quite make sense for some characters. For example, you can compute the lowercase equivalent of the period character, '.'!

Do this...
Create a test method in Experiment09Test named testCharacter(), and add these assertions:

assertEquals("lowercase of 'A'", 'a', Character.toLowerCase('A'));
assertEquals("lowercase of 'a'", 'a', Character.toLowerCase('a'));
assertEquals("lowercase of '.'", '.', Character.toLowerCase('.'));

Compile, and run for a green bar.

Do this...
Add more assertions to prove your understand of any three other methods from the Character class. Be sure to test each method two or three times with a variety of values.

Those Are Long Lists!

The point of these lists is not to give your instructor fodder for a quiz or test. Memorization is not the point. You should, however, be familiar with what's available (that might be fair game for a quiz!). When asked to solve a problem, you should be aware of these libraries so that you can use them to solve your problem. You don't want to waste your time reinventing the wheel.

Terminology

constant, invoke a method, method