/*
 * @topic T01005 Integer array and Rectangle object array
 * @brief Main driver
*/

package pckg;
/*
 Demo program creates an integer array and then displays it,
 sorts it, displays it again (sorted) and then searches for a
 paticular element.
     
 Next, the program creates an array of rectangle objects and repeats the
 same processing as it did with the integer array.

 The Rectangle class implements Comparable interface with compareTo method
 to meet the requirements of the Comparable interface.
*/


import java.util.Arrays;


public class DemoArrays
{

    //=======================================================================


    public static void main(String args[])
    {
        
        System.out.print( "This will sort and search an int array ");
        System.out.println("and an object array \n" );
        
        //======================INT ARRAY SORT AND SEARCH===================
        
        System.out.println( "// Create an array of int " );
        int[] intArray = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 6 };
        
        for( int x : intArray )
            {
                 System.out.print( x + " " );
            }
        
        Arrays.sort( intArray );
        
        System.out.println( "\n // Display the sorted array" );
        
        for( int x : intArray )
            {
                System.out.print( x + " " );
            } 
        System.out.println();
        System.out.println( "\n// Search for 4" );
        System.out.println
        ( 
            "Integer 4 is at element " + 
             Arrays.binarySearch( intArray, 4 ) 
        );
        
        System.out.println();
        

        //==================RECTANGLE ARRAY SORT AND SEARCH===============
        
        System.out.println( "// Create an array of Rectangle objects");
        Rectangle[] rectArray = {
                                     new Rectangle(15,120), 
                                     new Rectangle(50,50),                                     
                                     new Rectangle(5,65), 
                                };
        for( Rectangle x : rectArray ) 
            {
             System.out.println( x.getLength() + " by " + x.getWidth());            
            }
            
        Arrays.sort(rectArray);
        
        System.out.println( "// Display the array sorted by length" );
        
        for( Rectangle x : rectArray ) 
            {
             System.out.println( x.getLength() + " by " + x.getWidth());            
            }
        System.out.println();
        System.out.println( "// Search for a rectangle 50 by 50" );
        System.out.println
        ( 
            "Rectangel 50 by 50 is at element " + 
             Arrays.binarySearch( rectArray, new Rectangle(50, 50) ) 
        );
        
        // demo code for using a staic method called from the main to display
        // the contents of each array object

        for( Rectangle x : rectArray ) 
            {
             displayRectangle(x);            
            }
        
        System.out.println();
        System.out.println( "Have a nice day." );
        
    } // public static void main(String args[])

    //******************************************************************    

    private static void displayRectangle(Rectangle rect)
    {
        System.out.println();
        System.out.println("Contents of the Rectangle Object");
        System.out.println("Length = " + rect.getLength());
        System.out.println("Width = " + rect.getWidth());
        System.out.println("Area = " + rect.calculateArea());

    } // end of displayRectangle
    //******************************************************************    


} // public class DemoArrays

/*   Test Results
 *   This will sort and search an int array and an object array

// Create an array of int
3 1 4 1 5 9 2 6 5 3 6
 // Display the sorted array
1 1 2 3 3 4 5 5 6 6 9

// Search for 4
Integer 4 is at element 5

// Create an array of Rectangle objects
15.0 by 120.0
50.0 by 50.0
5.0 by 65.0
// Display the array sorted by length
5.0 by 65.0
15.0 by 120.0
50.0 by 50.0

// Search for a rectangle 50 by 50
Rectangel 50 by 50 is at element 2

Contents of the Rectangle Object
Length = 5.0
Width = 65.0
Area = 325.0

Contents of the Rectangle Object
Length = 15.0
Width = 120.0
Area = 1800.0

Contents of the Rectangle Object
Length = 50.0
Width = 50.0
Area = 2500.0

Have a nice day.
Press any key to continue...


*/