Java2 Certification
|
|
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
Write code using the following methods of the java.lang.Math class: abs ceil floor max min random round sin cos tan sqrt.
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.
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));
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;
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
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
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));
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
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.
returns a double value that is the square root of the parameter.
|
|
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));
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
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
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
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
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 MyCals(); 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
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.
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.
4) -3.0
3) 2.1
Answer 5)
1) -2.0
1) Compile time error
The math class is final and thus cannot be subclassed (MyCalc is defined as
extending Math)
Jyothi Krishnan on this topic at |
Last updated
21 Jan 2000
copyright © Marcus Green 1999
most recent version at www.software.u-net.com