// this code creates several objects of type Circ3 and Rect3, sorts them
// (according to the radius and the area, respectively, and searches for a
// specific Circ/Rect in this array by using the searchObj method of 
// class Arrays from the Java API
// The search method of that class can handle any objects that implement
// the Comparable interface. 

import java.util.Arrays;

public class SearchObj3
{
  public static void main(String[] args)
  {
     Circ3[] circs = {new Circ3(1, 1, 2), new Circ3(3, 8, 5),
        new Circ3(1, 1, 8), new Circ3(3, 8, 3), 
        new Circ3(1, 1, 4), new Circ3(3, 8, 9)};

     Rect3[] rects = {new Rect3(1, 1, 2, 6), new Rect3(3, 8, 5,6),
        new Rect3(1, 1, 8, 6), new Rect3(3, 8, 3, 6),
        new Rect3(1, 1, 4, 6), new Rect3(3, 8, 9, 6)};

     // sort the sircles and rects according to the diameter
     Arrays.sort(circs);
     Arrays.sort(rects);

   // Control output of sorted objects
     System.out.println("\nSorted Circles:");
     for (int i=0; i<circs.length; i++)
       System.out.println(circs[i]);

     System.out.println("\nSorted Rects:");
     for (int i=0; i<rects.length; i++)
       System.out.println(rects[i]);

     Circ3 qCirc = new Circ3(0, 0, 5);       // querry circle
     Rect3 qRect = new Rect3(0, 0, 5, 5);    // querry rectangle

     // search for the querry circle
     System.out.print("\nCircle: ");
     int i = Arrays.binarySearch(circs, qCirc);
     if (i >= 0)
       System.out.println("found at position " + i);
     else
       System.out.println("not found");

     // search for querry rectangle
     System.out.print("Rectangle: ");
     i = Arrays.binarySearch(rects, qRect);
     if (i >= 0)
       System.out.println("found at position " + i);
     else
       System.out.println("not found");
  }
}

