Return to Tutorial Index

Back to the home page of this site

morterboard

Java2 Certification
Tutorial

 

You can discuss this topic with others at http://www.jchq.net/discus
Read reviews and buy a Java Certification book at http://www.jchq.net/bookreviews/jcertbooks.htm


9) The java.lang package

Objective 1)

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

Note on this objective

The Math class is final and these methods are static. This means you cannot subclass Math and create modified versions of these methods. This is probably a good thing, as it reduces the possibility of ambiguity. You will almost certainly get questions on these methods and it would be a real pity to get any of them wrong just because you overlooked them.

abs

Due to my shaky Maths background I had no idea what abs might do until I studied for the Java Programmer Certification Exam. It strips off the sign of a number and returns it simply as a number. Thus the following will simply print out 99. If the number is not negative you just get back the same number.

System.out.println(Math.abs(-99));

ceil

This method returns the next whole number up that is an integer. Thus if you pass

ceil(1.1)

it will return a value of 2.0

If you change that to

ceil(-1.1) 

the result will be -1.0;

floor

According to the JDK documentation this method returns

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

If that is not entirely clear, here is a short program and its output

public class MyMat{
public static void main(String[] argv){
        System.out.println(Math.floor(-99.1));
        System.out.println(Math.floor(-99));
        System.out.println(Math.floor(99));
        System.out.println(Math.floor(-.01));
        System.out.println(Math.floor(0.1));
        }
}

And the output is

-100.0
-99.0
99.0
-1.0
0.0

max and min

Take note of the following two methods as they take two parameters. You may get questions with faulty examples that pass them only one parameter. As you might expect these methods are the equivalent of

"which is the largest THIS parameter or THIS parameter"

The following code illustrates how these methods work

public class MaxMin{
public static void main(String argv[]){
        System.out.println(Math.max(-1,-10));
        System.out.println(Math.max(1,2));
        System.out.println(Math.min(1,1));
        System.out.println(Math.min(-1,-10));
        System.out.println(Math.min(1,2));
        }
}

Here is the output

-1
2
1
-10
1

random

Returns a random number between 0.0 and 1.0.

Unlike some random number system Java does not appear to offer the ability to pass a seed number to increase the randomness. This method can be used to produce a random number between 0 and 100 as follows.

For the purpose of the exam one of the important aspects of this method is that the value returned is between 0.0 and 1.0. Thus a typical sequence of output might be


0.9151633320773057
0.25135231957619386
0.10070205341831895

Often a program will want to produce a random number between say 0 and 10 or 0 and 100. The following code combines math code to produce a random number between 0 and 100.

        System.out.println(Math.round(Math.random()*100));

round

Rounds to the nearest integer. So, if the value is more than half way towards the higher integer, the value is rounded up to the next ingeter. If the number is less than this the next lowest integer is returned. So for example if the input to round is x then :


2.0 <=x < 2.5. then Math.round(x)==2.0
2.5 <=x < 3.0 the Math.round(x)==3.0


Here are some samples with output

System.out.println(Math.round(1.01));
System.out.println(Math.round(-2.1));
System.out.println(Math.round(20));
1
-2
20

sin cos tan

These trig methods take a parameter of type double and do just about what trig functions do in every other language you have used. In my case that is 12 years of programming and I have never used a trig function. So perhaps the thing to remember is that the parameter is a double.

sqrt

returns a double value that is the square root of the parameter.

Summary


 

Questions

Question 1)

Which of the following will compile correctly?

1) System.out.println(Math.max(x));
2) System.out.println(Math.random(10,3));
3) System.out.println(Math.round(20));
4) System.out.println(Math.sqrt(10));


Question 2)

Which of the following will output a random with values only from 1 to 10?

1) System.out.println(Math.round(Math.random()* 10));
2) System.out.println(Math.round(Math.random() % 10));
3) System.out.println(Math.random() *10);
4) None of the above


Question 3)

What will be output by the following line?

System.out.println(Math.floor(-2.1));

1) -2
2) 2.0
3) -3
4) -3.0


Question 4)

What will be output by the following line?

System.out.println(Math.abs(-2.1));

1) -2.0
2) -2.1
3) 2.1
4) 1.0


Question 5)

What will be output by the following line?

System.out.println(Math.ceil(-2.1));

1) -2.0
2) -2.1
3) 2.1
3) 1.0


Question 6)

What will happen when you attempt to compile and run the following code?

class MyCalc extends Math{
public int random(){
        double iTemp;
        iTemp=super();
        return super.round(iTemp);
        }
}

public class MyRand{
public static void main(String argv[]){
        MyCalc m = new MyCalc();
        System.out.println(m.random());
        }
}        

1) Compile time error
2) Run time error
3) Output of a random number between 0 and 1
4) Output of a random number between 1 and 10

Answers

Answer 1)


3) System.out.println(Math.round(20));
4) System.out.println(Math.sqrt(10));

Option one is incorrect as max takes two parameters and option two is incorrect because random takes no parameters.


Answer 2)


4) None of the above
The closest is option 1 but the detail to remember is that random will include the value zero and the question asks for values between 1 and 10.


Answer 3)


4) -3.0



Answer 4)

3) 2.1

Answer 5)


1) -2.0


Answer 6)


1) Compile time error

The math class is final and thus cannot be subclassed (MyCalc is defined as extending Math). This code is a mess of errors, you can only use super in a constructor but this code uses it in the random method.



Other sources on this topic

Jyothi Krishnan on this topic at
http://www.geocities.com/SiliconValley/Network/3693/obj_sec9.html#obj28

Last updated
25 Dec 2000
copyright © Marcus Green 2000
most recent version at www.software.u-net.com