CSIT Class 21 Making our own classes Comparable, so we can sort their objects

First, review of last class: see those notes.

We looked at sort visualizations from the web. You can see how the sort involves a succession of comparisons of pairs of values. Also, it is clear that there are multiple sort algorithms, some better than others.

Consider Automobile objects. Suppose we want to sort them by VIN. That’s possible if:

--we make Automobile implement Comparable<Automobile>

--and that means Automobile.java needs a new method int compareTo(Automobile other).

Here it is, available here.

public class Automobile implements Comparable<Automobile>  ßnew “implements” clause

{

    private String make;  // make of *this* Automobile

    private String model;

    private int year;

    private String vin;

 

    public Automobile(String make0, String model0, int year0, String vin0)

    {

        make = make0;

        model = model0;

        year = year0;

        vin = vin0;

    }

    public String getMake()               // "getter" method for make

    {   return make;  }                  // compact way to save space

 

    public String getModel()   { return model; }  // even more compact way

    public int getYear() { return year; }

    public String getVIN() { return vin; }

 

    public int compareTo(Automobile other) {  // ßnew method

      return vin.compareTo(other.vin);  ß just pass the buck to String equals

    }

}

 

Here, when comparing cars with VINs v1 = “1G1” and v2 = “1G2” for example, the compareTo will do v1.compareTo(v2) and get a negative number, which is returned as the compare result of the two Automobile objects.

You might be surprised that this code can access other.vin, since vin is a private field of the “other” object. However, this is allowed when the other object is of exactly the same Java object type. Automobiles don’t hide information from each other.

Now we can sort an array or ArrayList of Automobiles,

Case of ArrayList<Automobile> part of AutomobileMain1.java

import java.util.*;

public class AutomobileMain1 {

  public static void main(String[] args) {

    Automobile car1 = new Automobile("Chevrolet", "Cruze", 2011, "1G1JF27W8GJ178227");

    Automobile car2 = new Automobile("Ford", "Taurus", 2010, "1G1BK13WBB265789");

    int numCars = 2;

    ArrayList<Automobile> cars = new ArrayList<Automobile>();

    cars.add(car1);

    cars.add(car2);

 

    Collections.sort(cars);   ß sort the ArrayList using Collections.sort()

    for (int i=0; i < cars.size(); i++) {

         System.out.println( "car " + i + cars.get(i).getVIN());

    }

  }

}

Case of an array of Automobiles: part of AutomobileMain.java

import java.util.*;

 

public class AutomobileMain {

  public static void main(String[] args) {

    Automobile car1 = new Automobile("Chevrolet", "Cruze", 2011, "1G1JF27W8GJ178227");

    Automobile car2 = new Automobile("Ford", "Taurus", 2010, "1G1BK13WBB265789");

    int numCars = 2;

    Automobile[] cars = new Automobile[numCars];

    cars[0] = car1;

    cars[1] = car2;

 

    Arrays.sort(cars);  ß Sort an array of Automobiles with Arrays.sort()

    for (int i=0; i < cars.length; i++) {

       System.out.println( "car " + i + cars[i].getVIN());

    }

  }

}

 

Similarly, we can sort an array or ArrayList of ITSystem, if we add “implements Comparable<ITSystem>”

And the compareTo method to ITSystem.java.

 

Suppose we want to sort on id of ITSystem. Then we add compareTo based on the ids, here ints.

 

public class ITSystem implements Comparable<ITSystem> {

as before

int compareTo(ITSystem other) {

  if (id < other.id)

    return -1;

  else if (id == other.id)

    return 0;

  else return 1;

}

}

 

There is another way to do this using subtraction:

 

int compareTo(ITSystem other) {

  return id - other.id;   // works out as >0 , == 0 , <0 as needed

}

 

 

Now if we have ITSystems in an array “systems”, we can sort it:

 

Arrays.sort(systems);

 

We also looked at the example in the book, CalendarDate, pg. 656. It is complicated by having more than one field (month and day) involved in the comparison.

 

We’ll cover Sorting and Searching, Sec. 13.1, after we finish Chap. 10.

We looked briefly at binary search visualizations from the web.

 

Next time: the weak-passwords users and the privileged users example redone following the methodology shown in Sec. 10.3.