We can use the following selectionSort() method to sort arrays of any subclass of GeometricObject:
public static void selectionSort(GeometricObject[] list) // Can sort any subclass { for (int i = 0; i < list.length-1; i++) { /* ----------------------------------------------- Find the minimum in the list[i..list.length-1] ----------------------------------------------- */ GeometricObject min = list[i]; // Assume first element is min int minIndex = i; // Index where min is found for ( int k = minIndex+1; k < list.length; k++ ) if ( list[k].getArea() < min.getArea() ) // Sort objects based by area { min = list[k]; // Update min value minIndex = k; // Update its index } /* ------------------------------------------------------ Swap list[i] with list[minIndex] if necessary ------------------------------------------------------ */ if ( minIndex != i ) { // Swap list[minIndex] and list[i] GeometricObject help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
|
|
|
We have previously studied the Selection Sort algorithm that uses the < operator to compare objects:
public static void selectionSort(Circle[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
Circle min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k].getArea() < min.getArea() ) // <--- Compare the 2 circles
{
min = list[k]; // Update min value
minIndex = k; // Update its index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
Circle help = list[minIndex]; // Standard exchange alg
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
We can re-write the Selection Sort algorithm to use the compareTo( ) method to compare objects:
public static void selectionSort(Circle[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
Circle min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k].compareTo(min) < 0 ) // <--- Compare the 2 circles
{
min = list[k]; // Update min value
minIndex = k; // Update its index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
Circle help = list[minIndex]; // Standard exchange alg
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
|
DEMO: demo/05-interfaces/20-sort-bankAccount/Demo.java
|
DEMO: demo/05-interfaces/20-sort-bankAccount/Demo2.java -- Compile error
The Selection Sort method that sort Circle objects must use parameter type Circle[] or a superclass type:
public static void selectionSort(Circle[] or superclass-of-Circle[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
Circle min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k].compareTo(min) < 0 ) // <--- Compares the 2 Circles
{
min = list[k]; // Update min value
minIndex = k; // Update its index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
Circle help = list[minIndex]; // Standard exchange alg
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
On the other hand: the Selection Sort method that sort BankAccount objects must use:
public static void selectionSort(BankAccount[] or superclass-of-BankAccount[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
BankAccount min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k].compareTo(min) < 0 ) // <--- Compares the 2 BankAccounts
{
min = list[k]; // Update min value
minIndex = k; // Update its index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
BankAccount help = list[minIndex]; // Standard exchange alg
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
DEMO: demo/05-interfaces/20-sort-bankAccount/Demo3.java
We could write a selection sort method for both of BankAccount and Circle using their SuperClass:
public static void selectionSort(SuperClass[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
SuperClass min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k].compareTo(min) < 0 ) // <-- Compares the 2 SuperClass objects
{
min = list[k]; // Update min value
minIndex = k; // Update its index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
SuperClass help = list[minIndex]; // Standard exchange alg
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
|
|
|
|
|
|
DEMO:
05-interfaces/21-interface/Demo.java
Next:
we will
define the
ComparableThing interface
for the BankAccount amd
Circle
classes
|
We can now write the selectionSort( ) for ComparableThing "objects"
We can write the selection sort algorithm to sort an array of ComparableThing as follows:
public static void selectionSort(ComparableThing[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
ComparableThing min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k].compareTo(min) < 0 ) // compare list[k] and min
{
min = list[k]; // Update min value
minIndex = k; // Update its index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
ComparableThing help = list[minIndex]; // Standard exchange alg
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
|
The original BankAccount class that do not implement the ComparableThing interface was:
public class BankAccount
{
private double balance;
public BankAccount(double x)
{
balance = x;
}
public double getBalance()
{
return balance;
}
// compareTo( ) used to compare 2 BankAccount objects
public int compareTo( BankAccount other )
{
double diff = this.getBalance() − other.getBalance();
return (int) Math.signum(diff);
}
}
|
Therefore: we cannot pass an array of BankAccount to selectionSort(ComparableThing[] list),
We must change the BankAccount class to implement the ComparableThing interface:
public class BankAccount implements ComparableThing { private double balance; public BankAccount(double x) { balance = x; } public double getBalance() { return balance; } // We must override compareTo( ) using same signature in ComparableThing public int compareTo( ComparableThing other ) { double diff = this.getBalance() − other.getBalance(); // <---- Error ! return (int) Math.signum(diff); } } |
We have an error because the ComparableThing type does not have a getBalance( ) method...
Solution: we downcast the reference to BankAccount type so we can use the getBalance( ) method:
public class BankAccount implements ComparableThing { private double balance; public BankAccount(double x) { balance = x; } public double getBalance() { return balance; } // We must override compareTo( ) using same signature in ComparableThing public int compareTo( ComparableThing other ) { BankAccount help = (BankAccount) other; double diff = this.getBalance() − help.getBalance(); // No error ! return (int) Math.signum(diff); } } |
|
DEMO:
demo/05-interfaces/22-interface/Demo.java
The
selectionSort() method
will
also work
for Circle objects
if
Circle
implements
ComparableThing
(next)
The original Circle class that do not implement the ComparableThing interface:
public class Circle extends GeometricObject
{
private double radius;
Circle(double r)
{
radius = r;
}
public double getArea()
{
return 3.14159*radius*radius;
}
// compareTo( ) used to compare 2 Circle objects
public int compareTo( Circle other )
{
double diff = this.getArea() − other.getArea();
return (int) Math.signum(diff);
}
}
|
Therefore: we cannot pass an array of Circle to selectionSort(ComparableThing[] list),
We must change the Circle class to implement the ComparableThing interface:
public class Circle extends GeometricObject implements ComparableThing { private double radius; Circle(double r) { radius = r; } public double getArea() { return 3.14159*radius*radius; } // We must override compareTo( ) using same signature in ComparableThing public int compareTo( ComparableThing other ) { double diff = this.getArea() − other.getArea(); // <---- Error ! return (int) Math.signum(diff); } } |
We have an error because the ComparableThing type does not have a getArea( ) method...
Solution: we downcast the reference to Circle type so we can use the getArea( ) method:
public class Circle extends GeometricObject implements ComparableThing { private double radius; Circle(double r) { radius = r; } public double getArea() { return 3.14159*radius*radius; } // We must override compareTo( ) using same signature in ComparableThing public int compareTo( ComparableThing other ) { Circle help = (Circle) other; double diff = this.getArea() − help.getArea(); // No error ! return (int) Math.signum(diff); } } |
|
DEMO:
demo/05-interfaces/22-interface/Demo.java
The
selectionSort() method
will
work
for
any class
that
implements
the
ComparableThing
interface !!