<Previous    Back to Start  Contents   Next>

Fundamental classes in the java.lang package

Write code using the following methods of the java.lang.Math class: abs(), ceil(), floor(), max(), min(), random(), round(), sin(), cos(), tan(), sqrt().

See the JavaDocs for more detail.

abs() - Returns the absolute value of the argument. Overloaded for ints, longs, floats and doubles.

ceil(double) - Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer.

floor(double) - Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer.

max(value1,value2) - Returns the greater of two values.

min(value1,value2) - Returns the smaller of two values.

random() - Returns a random number between 0.0 and 1.0.

round(double) - Returns the closest long to the argument.

sin(double) - Returns the trigonometric sine of an angle. The angle is in radians.

cos(double) - Returns the trigonometric cosine of an angle. The angle is in radians.

tan(double) - Returns the trigonometric tangent of an angle. The angle is in radians.

sqrt(double) - Returns the square root of a double value.

Describe the significance of the immutability of String objects.

String objects cannot be changed. They are assigned a sequence of characters when they are constructed. Look at the following code:

String message = "Good";
message= message + " morning";

When the first line executes, a string object containing "Good" is assigned to message. A new String object is constructed, unless the literal "Good" was used somewhere else, in which case the existing String is re-used. A consequence of this is the behaviour of the == operator which was describe under Language Fundamentals above, here is that information again:

[Note that if you construct two Strings with the same String literal, without using the new keyword, e.g.

String a = "Hello"
String b = "Hello"

, then Java creates only one String object, so a==b evaluates as true.]

On the second line, " morning" is appended. But the String object containing "Good" cannot be changed, so a new String, containing "Good morning" is created and assigned to message.

Describe the significance of wrapper classes, including making appropriate selections in the wrapper classes to suit specified behavior requirements, stating the result of executing a fragment of code that includes an instance of one of the wrapper classes, and writing code using the following methods of the wrapper classes (e.g., Integer, Double, etc.):

There are two fundamental types of variables in Java: primitives and objects. Sometimes a method or a constructor takes objects, but what what you have is a primitive. To cope with this situation, Java has a set of 'wrapper' classes which exist to hold the value of a primitive. For example, you might want to use a float as a key in a Map e.g. keying some business objects representing a loan by their respective interest rates. Using the Float class will allow you to do this. You would create a Float object for each value, and place that object into the Map.

Primitive Type Corresponding Wrapper class
boolean Boolean
byte Byte
short Short
char Character
int Integer
long Long
float Float
double Double

The names are entirely obvious, except for the two that have been highlighted. Take a look at these classes in the JavaDocs. The pattern tends to be pretty similar: For a primitive type x, there is one constructor taking x and another that takes a String, which is parsed to get the value. There is a static parseX() method, which parses a string and returns the primitive type x. An xValue() method returns the underlying primitive value held by wrapped object. The toString() method from Object is overridden appropriately.

toHexString() is a static method on Integer and Long which creates a hexadecimal string representation of the argument as an unsigned integer in base 16.

<Previous    Back to Start  Contents   Next>

©1999, 2000, 2002 Dylan Walsh.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being the disclaimer, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".