Due: Monday, November 26
Goal: Explore interfaces
Java has a static method named sort in the Arrays class that can be used to sort an array. Here is an example:
import java.util.Arrays; public class SortDemo { public static void main(String[] args) { int[] nums = new int[4]; nums[0] = 3; nums[1] = 45; nums[2] = 1; nums[3] = 23; Arrays.sort(nums); for (int i : nums) { System.out.println(i); } } }
Compile and run this code. It should sort the array of numbers and output 1,3, 23, 45. What if we want to sort an array of something else? For example, consider the Fruit class and the SortDemo below. Here is an attempt to make an array of fruit and sort them.
public class Fruit { private String fruitName; public Fruit() { fruitName = ""; } public Fruit(String name) { fruitName = name; } public void setName(String name) { fruitName = name; } public String getName() { return fruitName; } } import java.util.Arrays; public class SortDemo { public static void main(String[] args) { Fruit[] fruits = new Fruit[4]; fruits[0] = new Fruit("Orange"); fruits[1] = new Fruit("Apple"); fruits[2] = new Fruit("Kiwi"); fruits[3] = new Fruit("Durian"); Arrays.sort(fruits); // Output the sorted array of fruits for (Fruit f : fruits) { System.out.println(f.getName()); } } }
Compile the Fruit and SortDemo classes. The program should compile but give an error when you run it. This error occurs because Java doesn't know how to compare two instances of the Fruit class to each other to see if one "comes after" the other when attempting to sort the array. More precisely, the Arrays.sort method has been written with the expectation that the objects passed in the array have a compareTo method in accordance with the Comparable interface. The program worked with the array of integers because Java has defined the compareTo method for Integer objects.
The Comparable interface specifies only a single method:
public int compareTo(Object other);
The compareTo method is to be written by the programmer and should return
Here is a modified version of the Fruit class that implements the Comparable interface and defines the compareTo method.
public class Fruit implements Comparable { private String fruitName; public Fruit() { fruitName = ""; } public Fruit(String name) { fruitName = name; } public void setName(String name) { fruitName = name; } public String getName() { return fruitName; } public int compareTo(Object obj) { if ((obj != null) && // This makes sure the object is a fruit (obj instanceof Fruit)) { Fruit otherFruit = (Fruit) obj; // Typecast object to a fruit return (fruitName.compareTo(otherFruit.fruitName)); // Use String compareTo } return -1; // Default if other object is not a Fruit } }
This new version first checks if the object the Fruit is being compared to is another Fruit. If it is, the object is typecast to a Fruit. Then it uses the fruitName and compares it to the other fruit name using the String compareTo method. If the fruit name is alphabetically before the other fruit name then -1 is returned. If they are the same then 0 is returned. if the fruit name is alphabetically after the other fruit name then 1 is returned. This all happens in the fruitname.compareTo method.
Here is an alternate version of the Fruit class with a different compareTo implementation:
public class Fruit implements Comparable { private String fruitName; public Fruit() { fruitName = ""; } public Fruit(String name) { fruitName = name; } public void setName(String name) { fruitName = name; } public String getName() { return fruitName; } public int compareTo(Object o) { if ((o != null) && (o instanceof Fruit)) { Fruit otherFruit = (Fruit) o; if (fruitName.length() > otherFruit.fruitName.length()) return 1; else if (fruitName.length() < otherFruit.fruitName.length()) return -1; else return 0; } return -1; // Default if other object is not a Fruit } }
Run the program with this alternate definition. What does it do to the sort order?
Show or email your two completed programs, the versions with the two different implementations of compareTo and their different behavior, to get credit for this drill.