Math Class and Methods
This class supports large set of mathematical functions,
Exposing through Static methods, so it can be accessed without creating object
- This class is final class, so it cannot be extended
- The constructor it has is private so it cannot be instantiated
It provides two constants
Math.PI
and
Math.E
The Math class includes methods for retrieving absolute
value, ceiling, floor, maximum, minimum, random, round and square root of a
number
Methods
round() – Acts on the argument and change the
floating point number to nearest integer value. It never affected by an
instance variable state.
ceil() – Returns the smallest double value that is greater
than or equal to the argument and is equal to a mathematical integer
floor() - Returns the largest double value that is less than or equal to the
argument and is equal to a mathematical integer
random() – Returns a double between 0.0 and 1.0
min() – Takes two numerical value and returns the smaller of
the two
max() – Takes two numerical value and returns the maximum of
the two
abs() – Returns the absolute value of a number [This method
is overloaded, if you pass it integer it returns an integer . If you pass it double it returns
double].
Sample Program
package com.crazyprogramming;
public class MathClassSample
{
public static void main(String[]
args) {
double r1 = Math.random();
int r2 = (int)(Math.random()*2);
System.out.println("Double
value: "+r1);
System.out.println("Integer
Value: "+r2);
int x = Math.abs(-240);
double y = Math.abs(240.45);
System.out.println("Double
value: "+y);
System.out.println("Integer
Value: "+x);
int x1 = Math.round(-20.47f);
int y1 = Math.round(23.78f);
System.out.println("Integer
Value: "+x1+" and "+y1);
int x2 = Math.min(24,
30);
double y2 = Math.min(22.56f,
24.90f);
System.out.println("Double
value: "+x2);
System.out.println("Integer
Value: "+y2);
int x3 = Math.max(24,
30);
double y3 = Math.max(22.56f,
24.90f);
System.out.println("Double
value: "+x3);
System.out.println("Integer
Value: "+y3);
System.out.println("Floor
value: "+Math.floor(12.2));
System.out.println("Ceil
Value: "+ Math.ceil(12.6));
System.out.println("Square
root value: "+Math.sqrt(64.95));
System.out.println("Exponential
Value: "+Math.exp(0));
}
}
Output
Double value: 0.5774235073425874
Integer Value: 0
Double value: 240.45
Integer Value: 240
Integer Value: -20 and 24
Double value: 24
Integer Value: 22.559999465942383
Double value: 30
Integer Value: 24.899999618530273
Floor value: 12.0
Ceil Value: 13.0
Square root value:
8.059156283383516
Exponential Value: 1.0
Math Class provides built-in methods in the following areas
Trigonometric, Logarithmic, Euler's Number, Roots, Signum Function, Unit of
Least Precision and the above functions
System Class and Methods
It contains several useful class fields and methods; this
class is final class, so it cannot be extended
Fields
static PrintStream err
- standard error output stream[stream is already open and ready to accept
output data - this stream corresponds to display output or another output
destination specified by the host environment or user]
public static final PrintStream err
static InputStream in
- standard input stream [stream is already open and ready to supply input data –
This stream corresponds to keyboard input or another input source specified by
the host environment or user]
public
static final InputStream in
static PrintStream out
- The standard output stream[stream is already open and ready to accept output
data- this stream corresponds to display output or another output destination
specified by the host environment or user]
public static final PrintStream out
Methods
setIn() - Reassigns the "standard" input stream.
setOut() and setErr() - Reassigns the "standard"
output stream.
console() - Returns the unique Console object associated with
the current Java virtual machine
setSecurityManager() and getSecurityManager() - setter and
getter for System security
currentTimeMillis() - Returns the current time in
milliseconds
getProperties() and setProperties() - getter and setter for
current system properties
arraycopy() - Copies an array from the specified source
array, beginning at the specified position, to the specified position of the
destination array
public static void arraycopy(Object src, int
srcPos, Object
dest, int
destPos, int
length)
lineSeparator() - Returns the system-dependent line
separator string[\r\n or \n]
getProperty() and setProperty() - getterand setter to get
and set the system property based on the key value
clearProperty() - Removes the system property indicated by
the specified key
getenv() - Gets the value of the specified environment
variable. An environment variable is a system-dependent external named value
exit() - Terminates the currently running Java Virtual
Machine. The argument serves as a status code; by convention, a nonzero status
code indicates abnormal termination
gc() - Runs the garbage collector
load() - Loads a code file with the specified filename from
the local file system as a dynamic library. The filename argument must be a
complete path name
This class provides methods to load our own library and map
into native library
Sample Program
package
com.crazyprogramming;
public class
SystemClassSample {
public static void main(String[]
args) {
//Time
System.out.println("Time
method Invocation = "+System.currentTimeMillis());
//Properties
System.out.println("Getting
System properties:\n"+System.getProperties());
System.setProperty("system.userName", "crazy");
System.out.println("Getting
Single Property: system.userName = "+ System.getProperty("system.userName"));
System.clearProperty("system.userName");
System.out.println("After
Clear: system.userName = "+ System.getProperty("system.userName"));
//Exit
Function
for(int
i=0;i<=5;i++){
if(i>2){
System.exit(0);
}
System.out.println("i value
= "+i);
}
}
}
Output
Time method Invocation = 1390041066864
Getting System properties: System property will be displayed
here
Getting Single Property: system.userName = crazy
After Clear: system.userName = null
i value = 0
i value = 1
i value = 2