go to previous page   go to home page   go to next page

Answer:

It seems reasonable to compare the volume of two Boxes:

// compare this Box to another Box  
public int compareTo( Object other )
{
  Box box = (Box)other;
  
  if ( volume() < box.volume() )
    return -1;
  else if ( volume() > box.volume() )
    return 1;
  
  return 0;
}

Sorting Programmer-defined Objects

Here is how a program might declare an array of references to Box objects. (Often, such an array might be called "an array of Boxes" although this is somewhat misleading.)

Box[] boxArray = 
  { new Box( 1.0, 2.3, 2.7),  new Box( 1.0, 4.9, 3.2), new Box( 3.0, 1.3, 2.7),
    new Box( 3.0, 0.1, 4.67), new Box( 1.3, 1.3, 1.3), new Box( 4.0, 2.3, 1.7),
    new Box( 2.2, 2.1, 1.67), new Box( 2.3, 7.3, 6.3), new Box( 2.0, 3.3, 5.3) 
  };

The references to Boxes in the array are in no particular order. The goal of sorting is to rearrange the references in the array so that the Boxes are in order.


QUESTION 8:

Each Box object is a block of memory. Does the location of a Box in memory change after sorting the array?


go to previous page   go to home page   go to next page