Thursday, 4 December 2014

// The "MathMethodExamples2" class. Pages 74-75.
// (from the Carter book)

// Example 4.  How to use Math.max and Math.min
// to find the larger or smaller of 2 given values.
// Find the return value of each calculation.

public class MathMethodExamples2
{
    public static void main (String[] args)
    {
        // Example 4 (parts a, b, c and d)-- Math.max --
        // returns the larger of any 2 types of values except boolean:

        // Part (4a):  (returning an int value)
        System.out.println ("Math.max (-3, 2) = " + Math.max (-3, 2));

        // Part (4b):  (returning a double value)
        System.out.println ("Math.max (3.4, 5.3) = " + Math.max (3.4, 5.3));

        // Part (4c):  (returning a long value)
        System.out.println ("Math.max (7, 3) = " + Math.max (7, 3));

        // Part (4d):  (returning an exponent form of the value)
        System.out.println ("Math.max (-9e-4, -1.1e-6) = " +
                Math.max (-9e-4, -1.1e-6));
        System.out.println ();

        // Example 4 (parts e, f, g and h) -- Math.min --
        // returns the smaller of any 2 types of values except boolean:
        // Part (4e):  (returning a long value)
        System.out.println ("Math.min (8, 7) = " + Math.min (8, 7));

        // Part (4f):  (returning a double value)
        System.out.println ("Math.min (1.5, 2.0) = " + Math.min (1.5, 2.0));

        // Part (4g):  (returning a negative double value)
        System.out.println ("Math.min (-4.7, -9.3) = " + Math.min (-4.7, -9.3));

        // Part (4h):  (returning a float when the types are int and float)
        System.out.println ("Math.min (18, 6.7f) = " + Math.min (18, 6.7f));

    } // main method
} // MathMethodExamples2 class

No comments:

Post a Comment