Wrapper classes and the Math class  Peter Komisar

                                                                                                                                                    latest revision Oct 24 / 2000
references:
'Just Java', P.V.Linden, 'The Java Certification Study Guide' Heller&Roberts, JDK 1.2.2 documentation


Wrapper Classes

Sometime you will need to treat a primitive type, ( i.e. int ), as an object. For instance, when
adding a number to a Vector. Wrapper classes encapsulate primitive types so they can be
treated like an object type. For instance, Integer is the wrapper class for integers. A wrapper
class exists for each of the primitive types.

The Wrapper class name for each primitive can be derived by creating the unabreviated version
of the primitive type name and capitalizing it. For example, the int type's wrapper class is
the Integer class, while the char type's wrapper isthe Character class. All the primitives and
their corresponding wrapper classes are listed in the following table.
 

 Wrapper Class for each Primitive Type
 
Wrapper  Primitive
Boolean boolean
Character char
Byte byte
Short short
Integer int
Long long
Float float
Double double

Besides providing objects types for primitives, wrapper classes store useful constants and
methods to convert to and from String and other types.
 
 More on Wrappers

 Wrappers also make it possible to dointrospectionon primitive types. Introspection is 
  important to the process of how java  beans work as well as to the process of serialization

 Introspection is a java tool used  to extract information about object types. It is required for 
 debugging, storing java beans, and using certain networking API's like RMI where objects 
 have to be serialized, sent to remote machines and brought back to an active object form.

 Serialization is the process of adding type information to a streamed object so that the state 
 of the object can be reconstituted after being sent serially over a wire (or other) transmission 
 medium.
.

Example
 
/* The number converts to exponential floating point form  at 10 million. A NumberFormatException 
     is thrown when the range of an int is exceeded, ~ +/- 2/1 billion. */ 

     class wrap{
          public static void main(String[] args){
            if (args.length==0){
                System.out.println("Enter a number at the command line");
                System.exit(0);
               }
   // parseInt() is a static method called here on the class name which converts the string value
   // taken from the command line to an int value (could have usede parseDouble to be more direct)
          int i=Integer.parseInt(args[0]);
          Integer theWrapperClass= new Integer(i); 
          float f = theWrapperClass.floatValue( ); 
          // floatValue( ) returns the encapsulated primitive value as a float
          System.out.println("Here's your number as a float: " + f);
         }
      }


The Math Class

Math contains a collection of math methods and two constants. The constants are Math.PI
and Math.E. Both are public, static, final and double. The math class itself is final so cannot
be extended. It's constructor is private so it cannot be instantiated. It's methods and constants
are static so can be accessed through the class name.

A Brief look at Math class Methods.

abs( )
There are four versions of abs( ), each which returns an absolute value for a value
provided which may be int, long, float, or double.

ceil( ) & floor( )
These methods take and return doubles corresponding to the nearest int value above
or below the double provided

max( ) & min( )
There are four versions of each max( ) and min( ) for int, long, float and double types.
These methods take two values and return the greater or the lesser of two.

double random( )
random( ) returns a random number between 0.0 and just less than 1.0. This method
acts as a wrapper for the creation of an instance of the Random class which is one of
the utility methods supplied in the java.util package.
//  Random # formula:  random number =  (int) ( Math.random( ) * number_range) + 1

round( )
Two forms of this method are available to round a float (to an int) or a double (to a long)

sin( ), cos( ) & tan( )             // there are also asin acos atan & atan2
These methods take doubles and return the sine, cosine, and tangent as doubles.

double sqrt( double d )
sqrt( ) returns the square root of a double provided as an argument

double pow(double number, double power)
pow( ) returns a number evaluated to a given power

log( )
Returns the natural logarithm (base e) of a double value.

exp( )
Returns the exponential number e (i.e., 2.718...) raised to the power of a double value.

rint( )
takes a double, rounds to an int then returns this value as a double

IEEEremainder( )
returns an IEEE formulated remainder of a division

toDegrees( )     // new with JDK1.2
Converts an angle measured in radians to the equivalent angle measured in degrees.

toRadians( )     // new with JDK1.2
Converts an angle measured in degrees to the equivalent angle measured in radians.



 
Using Math methods


 /* not catching number format exception (Try entering something like 5y5) */

   class mathMethods{
      public static void main(String[] args){
         if(args.length <6){
             System.out.println("Enter 6 numbers at the command line");
             System.exit(0);
             } 
      int a=Integer.parseInt(args[0]);
      int b=Integer.parseInt(args[1]);
      int c=Integer.parseInt(args[2]);
      int d=Integer.parseInt(args[3]);
      int e=Integer.parseInt(args[4]);
       int f=Integer.parseInt(args[5]);
      double t;
      int[] intArray=new int[]{a,b,c,d,e,f};

          for ( int i=0; i < intArray.length - 1; i++ ){
              int z=Math.min(intArray[i],intArray[i+1] );
              intArray[i+1]=z;
              }
      int small=intArray[intArray.length-1];
      System.out.println("The smallest number in this set is "+small);
      System.out.println
      (" The square root of this number is " + Math.sqrt(small ) +
      "\n which when rounded is " +Math.round( Math.sqrt(small) )  );
      }
   }