Thursday, 4 December 2014

// The "MathMethodExamples3" class.  Page 75.
// (from the Carter book)

// Example 5.  How to use Math.round, Math.ceil
// and Math.floor to show the rounded value of a number,
// and the int or long value of a float or double value.
public class MathMethodExamples3
{
    public static void main (String[] args)
    {
        // Example 5 (parts a,b and c) -- Math.round -- how to round
        // a float or double value to an int or long value:

        // Part (5a):  (returning an int value from a float or double value)
        System.out.println ("Math.round (2.7) = " + Math.round (2.7));

        // Part (5b):  (returning an int value from a float or double value)
        System.out.println ("Math.round (4.5) = " + Math.round (4.5));

        // Part (5c):  (returning a negative int value from a negative float
        // value)
        System.out.println ("Math.round (-2.13) = " + Math.round (-2.13));

        // Example 5 (parts d and e) -- Math.floor -- how to show the
        // a returned double value:
        System.out.println ();

        // Part (5d):  (returning a double value)
        System.out.println ("Math.floor (21.94) = " + Math.floor (21.94));

        // Part (5e):  (returning an a double value)
        System.out.println ("Math.floor (8.8) = " + Math.floor (8.8));

        // Example 5 (parts f, g and h) -- Math.ceil -- how to show the
        // returned double value:
        System.out.println ();

        // Part (5f):  (returning a positive double value)
        System.out.println ("Math.ceil (18.8) = " + Math.ceil (18.8));

        // Part (5g):  (returning a negative double value)
        System.out.println ("Math.ceil (-5.47) = " + Math.ceil (-5.47));

        // Part (5h):  (returning a negative double value -- no change to the value --
        // HINT: when rounding up -5.8, go to -5.0 as the next whole number up)
        System.out.println ("Math.ceil (-5.8) = " + Math.ceil (-5.8));
        System.out.println ();

        // about "Math.random" and how to use it:
        System.out.println ("(int)(12 * Math.random()) + 1 = " 
                                                                              + ((int) (12 * Math.random ()) + 1));

    } // main method
} // MathMethodExamples3 class

No comments:

Post a Comment