Sei sulla pagina 1di 4

Function

Description
IEEEremainder(double, double)
Returns the remainder of f1 divided by f2 as
defined by IEEE 754
abs(int a)
Returns the absolute integer value of a
abs(long a)
Returns the absolute long value of a
abs(float a) Returns the absolute float value of a
abs(double a) Returns the absolute double value of a
acos(double a) Returns the arc cosine of a, in the range of 0.0 through pi
asin(double a) Returns the arc sine of a, in the range of -pi/2 through pi/2
atan(double a) Returns the arc tangent of a, in the range of -pi/2 through pi/2
atan2(double a, double b)
Converts rectangular coordinates (a, b) to polar (r, theta)
ceil(double a) Returns the "ceiling," or smallest whole number greater than or equal
to a
cos(double) Returns the trigonometric cosine of an angle
exp(double a) Returns the exponential number e(2.718...) raised to the power of a
floor(double a)
Returns the "floor," or largest whole number less than or equal
to a
log(double a) Returns the natural logarithm (base e) of a
max(int a, int b)
Takes two int values, a and b, and returns the greater of the two
max(long a, long b) Takes two long values, a and b, and returns the greater of the
two
max(float a, float b) Takes two float values, a and b, and returns the greater of the
two
max(double a, double b)
Takes two double values, a and b, and returns the
greater of the two
min(int a, int b)
Takes two integer values, a and b, and returns the smaller of the
two
min(long a, long b) Takes two long values, a and b, and returns the smaller of the
two
min(float a, float b) Takes two float values, a and b, and returns the smaller of the
two
min(double a, double b)
Takes two double values, a and b, and returns the
smaller of the two
pow(double a, double b)
Returns the number a raised to the power of b
random()
Generates a random number between 0.0 and 1.0
rint(double) Returns the closest integer to the argument, but as a floating-point
number
round(float) Rounds off a float value by first adding 0.5 to it and then returning the
largest integer that is less than or equal to this new value
round(double) Rounds off a double value by first adding 0.5 to it and then returning
the largest integer that is less than or equal to this new value
sin(double) Returns the trigonometric sine of an angle
sqrt(double) Returns the square root of a
tan(double) Returns the trigonometric tangent of an angle
toDegrees(double)
Translates radians to degrees
toRadians(double)
Translates degrees to radians
public class MainClass {
public static void main(String args[]) {

double loanAmount = 0;
double top = 2 * 5 / 1200;
double bot = 1 - Math.exp(5 * (-12) * Math.log(1 + 7 / 1200));
System.out.println(loanAmount);
System.out.println(top);
System.out.println(bot);
}

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--max and min take two parameters
random returns value between 0 and 1
abs chops of the sign component
round rounds to the nearest integer but leaves the sign

Potrebbero piacerti anche