Saturday 30 November 2013

Overloading and Overriding



OVERRIDINGOverriding a method means that its entire functionality is being replaced. Overriding is something done in a child class to a method defined in a parent class. To override a method a new method is defined in the child class with exactly the same signature as the one in the parent class. This has the effect of shadowing the method in the parent class and the functionality is no longer directly accessible.

Java provides an example of overriding in the case of the equals method that every class inherits from the granddady parent Object. The inherited version of equals simply compares where in memory the instance of the class references. This is often not what is wanted, particularly in the case of a String. For a string you would generally want to do a character by character comparison to see if the two strings are the same. To allow for this the version of equals that comes with String is an overriden version that performs this character by character comparison. when you extend a class and write a method in the derived class which is exactly similar to the one present in the base class, it is termed as overriding.
Example:
public class BaseClass{
public void methodToOverride()
{
//Some code here
}
}
public class DerivedClass extends BaseClass{
public void methodToOverride()
{
//Some new code here
}
}
As you can see, in the class DerivedClass, we have overridden the method present in the BaseClass with a completely new piece of code in the DerivedClass.
What that effectively means is that if you create an object of DerivedClass and call the methodToOverride() method, the code in the derivedClass will be executed. If you hadn’t overridden the method in the DerivedClass then the method in the BaseClass would have been called.
OVERLOADINGOverloading of methods is a compiler trick to allow you to use the same name to perform different actions depending on parameters. when you have more than one method with the same name but different arguments, the methods are said to be overloaded.
Example:
public class OverLoadingExample{
public void add(int i, int j)
{
int k = i + j;
}
public void add(String s, String t)
{
int k = Integer.parseInt(s) + Integer.parseInt(t);
}
}
As you can see in the example above, we have the same method add() taking two parameters but with different data types. Due to overloading we can now call the add method by either passing it a String or int .

Thursday 28 November 2013

Java Lang Math


Introduction
The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
Following is the declaration for java.lang.Math class:
public final class Math
   extends Object
Following are the fields for java.lang.Math class:
static double E -- This is the double value that is closer than any other to e, the base of the natural logarithms.
static double PI -- This is the double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.


This method returns the absolute value of a double value.
static float abs(float a)
This method returns the absolute value of a float value.
static int abs(int a)
This method returns the absolute value of an int value.
static long abs(long a)
This method returns the absolute value of a long value.
static double acos(double a)
This method returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.
static double asin(double a)
This method returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2.
 static double atan(double a)
This method returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.
 static double atan2(double y, double x)
This method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).
static double cbrt(double a)
This method returns the cube root of a double value.
static double ceil(double a)
This method returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
static double copySign(double magnitude, double sign)
This method returns the first floating-point argument with the sign of the second floating-point argument.
 static float copySign(float magnitude, float sign)
This method returns the first floating-point argument with the sign of the second floating-point argument.
static double cos(double a)
This method returns the trigonometric cosine of an angle.
 static double cosh(double x)
This method returns the hyperbolic cosine of a double value.
 static double exp(double a)
This method returns Euler's number e raised to the power of a double value.
static double   expm1(double x)
This method returns ex -1.
 static double floor(double a)
This method returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
 static int getExponent(double d)
This method returns the unbiased exponent used in the representation of a double.
 static int getExponent(float f)
This method returns the unbiased exponent used in the representation of a float.
 static double hypot(double x, double y)
This method returns sqrt(x2 +y2) without intermediate overflow or underflow.
static double IEEEremainder(double f1, double f2)
This method computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.
static double log(double a)
This method returns the natural logarithm (base e) of a double value.
static double log10(double a)
This method returns the base 10 logarithm of a double value.
static double log1p(double x)
This method returns the natural logarithm of the sum of the argument and 1.
 static double max(double a, double b)
This method returns the greater of two double values.
static float max(float a, float b)
This method returns the greater of two float values.
static int max(int a, int b)
This method returns the greater of two int values.
static long max(long a, long b)
This method returns the greater of two long values.
static double min(double a, double b)
This method returns the smaller of two double values.
 static float min(float a, float b)
This method returns the smaller of two float values.
static int min(int a, int b)
This method returns the smaller of two int values.
 static long min(long a, long b)
This method returns the smaller of two long values.
static double nextAfter(double start, double direction)
This method returns the floating-point number adjacent to the first argument in the direction of the second argument.
static float nextAfter(float start, double direction)
This method returns the floating-point number adjacent to the first argument in the direction of the second argument.
 static double nextUp(double d)
This method returns the floating-point value adjacent to d in the direction of positive infinity.
static float nextUp(float f)
This method returns the floating-point value adjacent to f in the direction of positive infinity.
static double pow(double a, double b)
This method returns the value of the first argument raised to the power of the second argument.
static double random()
This method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
 static double rint(double a)
This method returns the double value that is closest in value to the argument and is equal to a mathematical integer.
 static long round(double a)
This method returns the closest long to the argument.
 static int round(float a)
This method returns the closest int to the argument.
static double scalb(double d, int scaleFactor)
This method returns d × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set.
 static float scalb(float f, int scaleFactor)
This method return f × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set.
static double signum(double d)
This method returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
static float signum(float f)
This method returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.
static double sin(double a)
This method returns the hyperbolic sine of a double value.
 static double sinh(double x)
This method Returns the hyperbolic sine of a double value.
 static double sqrt(double a)
This method returns the correctly rounded positive square root of a double value.
static double tan(double a)
This method returns the trigonometric tangent of an angle.r
 static double tanh(double x)
This method returns the hyperbolic tangent of a double value.
static double toDegrees(double angrad)
This method converts an angle measured in radians to an approximately equivalent angle measured in degrees.
static double toRadians(double angdeg)
This method converts an angle measured in degrees to an approximately equivalent angle measured in radians.
static double ulp(double d)
This method returns the size of an ulp of the argument.
static double ulp(float f)
This method returns the size of an ulp of the argument.
Class java.lang.Math

java.lang.Object
   |
   +----java.lang.Math
public final class Math
extends Object
The standard Math library. For the methods in this Class, error handling for out-of-range or immeasurable results are platform dependent. This class cannot be subclassed or instantiated because all methods and variables are static.
Variable Index

 o  E
The float representation of the value E.
 o  PI
The float representation of the value Pi.
Method Index
 o  IEEEremainder(double, double)
Returns the remainder of f1 divided by f2 as defined by IEEE 754.
 o  abs(int)
Returns the absolute integer value of a.
 o  abs(long)
Returns the absolute long value of a.
 o  abs(float)
Returns the absolute float value of a.
 o  abs(double)
Returns the absolute double value of a.
 o  acos(double)
Returns the arc cosine of a, in the range of 0.0 through Pi.
 o  asin(double)
Returns the arc sine of a, in the range of -Pi/2 through Pi/2.
 o  atan(double)
Returns the arc tangent of a, in the range of -Pi/2 through Pi/2.
 o  atan2(double, double)
Converts rectangular coordinates (a, b) to polar (r, theta).
 o  ceil(double)
Returns the "ceiling" or smallest whole number greater than or equal to a.
 o  cos(double)
Returns the trigonometric cosine of an angle.
 o  exp(double)
Returns the exponential number e(2.718...) raised to the power of a.
 o  floor(double)
Returns the "floor" or largest whole number less than or equal to a.
 o  log(double)
Returns the natural logarithm (base e) of a.
 o  max(int, int)
Takes two int values, a and b, and returns the greater number of the two.
 o  max(long, long)
Takes two long values, a and b, and returns the greater number of the two.
 o  max(float, float)
Takes two float values, a and b, and returns the greater number of the two.
 o  max(double, double)
Takes two double values, a and b, and returns the greater number of the two.
 o  min(int, int)
Takes two integer values, a and b, and returns the smallest number of the two.
 o  min(long, long)
Takes two long values, a and b, and returns the smallest number of the two.
 o  min(float, float)
Takes two float values, a and b, and returns the smallest number of the two.
 o  min(double, double)
Takes two double values, a and b, and returns the smallest number of the two.
 o  pow(double, double)
Returns the number a raised to the power of b.
 o  random()
Generates a random number between 0.0 and 1.0.
Random number generators are often referred to as pseudorandom number generators because the numbers produced tend to repeat themselves after a period of time.

 o  rint(double)
Converts a double value into an integral value in double format.
 o  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.
 o  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.
 o  sin(double)
Returns the trigonometric sine of an angle.
 o  sqrt(double)
Returns the square root of a.
 o  tan(double)
Returns the trigonometric tangent of an angle.
 Variables

  o  E
  public final static double E
The float representation of the value E. E is equivalent to 2.7182818284590452354f in Java.
  o  PI
  public final static double PI
The float representation of the value Pi. Pi is equivalent to 3.14159265358979323846f in Java.
 Methods

  o  sin
  public static double sin(double a)
Returns the trigonometric sine of an angle.
Parameters:
a - an assigned angle that is measured in radians
  o  cos

  public static double cos(double a)
Returns the trigonometric cosine of an angle.
Parameters:
a - an assigned angle that is measured in radians
  o  tan

  public static double tan(double a)
Returns the trigonometric tangent of an angle.
Parameters:
a - an assigned angle that is measured in radians
  o  asin

  public static double asin(double a)
Returns the arc sine of a, in the range of -Pi/2 through Pi/2.
Parameters:
a - (-1.0) <= a <= 1.0
  o  acos

  public static double acos(double a)
Returns the arc cosine of a, in the range of 0.0 through Pi.
Parameters:
a - (-1.0) <= a <= 1.0
  o  atan

  public static double atan(double a)
Returns the arc tangent of a, in the range of -Pi/2 through Pi/2.
Parameters:
a - an assigned value
Returns:
the arc tangent of a.
  o  exp

  public static double exp(double a)
Returns the exponential number e(2.718...) raised to the power of a.
Parameters:
a - an assigned value
  o  log

  public static double log(double a) throws Arithmetic Exception