Monday, 12 January 2015

// The "Chapter3_Example3" class.  Chapter 3, Example 3.
// (from the Carter book)

// The IF statement shown below efficiently determines the largest
// value among 3 values, x, y and z, and assigns this value to 'largest'.
// There are many other ways to determine the largest of the 3 values,
// but this one (below) involves the least work.  Test this using different
// values for x, y and z.

import java.awt.*;
import hsa.Console;

public class Chapter3_Example3
{
    static Console c;           // The output console
   
    public static void main (String[] args)
    {
        c = new Console ();
        
        int x = 4, y = 5, z = 3, largest;
        
        if (x >= y)
            // y is eliminated - largest must be x or z
            if (x >= z)
                largest = x;
            else
                largest = z;
        else
            // x is eliminated - largest must be y or z
            if (y >= z)
                largest = y;
            else
                largest = z;
        c.println ("largest = " + largest);      
       
    } // main method
} // Chapter3_Example3 class

No comments:

Post a Comment