Minimum and Maximum Methods


Minimum and Maximum Methods

The Java math libraries contain two overloaded methods for computing the maximum or minimum of two input arguments. The return type of each method version is the same as the data type of the input arguments.


   
   public  static  double  max(double  a,  double  b)
   
   public  static  float  max(float  a,  float  b)
   
   public  static  int  max(int  a,  int  b)
   
   public  static  long  max(long  a,  long  b)

max() returns the greater of the input argument values. The method is overloaded to take double , float , int , and long arguments. If either value is NaN , the result is NaN .


   public  static  double  min(double  a,  double  b)
   
   public  static  float  min(float  a,  float  b)
   
   public  static  int  min(int  a,  int  b)
   
   public  static  long  min(long  a,  long  b)

min() returns the smaller of the input argument values. The method is overloaded to take double , float , int , and long arguments. If either value is NaN , the result is NaN .

Example: Finding the Maximum of a Collection of Data

In this example, an array of type double contains a collection of width values. The max() method is used to determine the maximum width in the collection.

 public class MaxDemo {   public static void main(String args[]) {     double width[] = { 1.2, 2.3, 0.8, 1.7 };     double maxWidth;     maxWidth = 0.0;     for(int i=0; i<width.length; ++i) {       maxWidth = Math.max(maxWidth, width[i]);     }     System.out.println("maximum width = "+maxWidth);   } } 

Output ”

 maximum width = 2.3 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net