|
|
|
We have previously studied the Selection Sort algorithm that the use < 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 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] 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 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] 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
In order to sort BankAcoount object, we must write the following selectionSort( ) method:
public static void selectionSort(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 ) // 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] BankAccount help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
DEMO: demo/05-interfaces/20-sort-bankAccount/Demo3.java
|
Idea for writing a selection sort algorithm for the superclass of BankAccount and Circle:
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 ) // 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] SuperClass help = list[minIndex]; // Standard exchange alg list[minIndex] = list[i]; list[i] = help; } } } |
|
|
|
|
|
DEMO:
05-interfaces/21-interface
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);
}
}
|
We 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);
}
}
|
We 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); } } |
|
The selectionSort() method will work for any class that implements the ComparableThing interface !!
|
|
|
|
|
|
DEMO: demo/05-interfaces/30-default-impl/Demo.java + Demo2.java
|
Abstract class | Interface |
---|---|
|
|
|
|
|