Lecture 23:More on Interfaces

Lecture 23:More on
Interfaces
Building Java Programs: A Back to Basics Approach
by Stuart Reges and Marty Stepp
Copyright (c) Pearson 2013.
All rights reserved.
Collections
• collection: an object that stores data; a.k.a. "data structure"
– the objects stored are called elements
– some collections maintain an ordering; some allow duplicates
– typical operations: add, remove, clear, contains (search), size
– examples found in the Java class libraries:
• ArrayList, LinkedList, HashMap, TreeSet, PriorityQueue
– all collections are in the java.util package
import java.util.*;
2
Java collections framework
3
The List Interface
public interface List{
public
public
public
public
…
Object get(int index);
Object set(int index, Object element);
Object remove(int index);
void add(int index, Object element);
}
4
The List Interface
public class ArrayList implements List{
…
}
5
The List Interface
ArrayList<String> list1=new ArrayList<String>();
List<String> list2=new ArrayList<String>();
// an arrayList is a list. This is the recommended way to create
// an arraylist.
List<Employee> list3=new ArrayList<Employee>();
6
Comparable
public interface Comparable {
public int compareTo(Object other);
}
• A class can implement the Comparable interface to define a
natural ordering function for its objects.
• A call to your compareTo method should return:
a value <
a value >
or 0
0
if the other object comes "before" this one,
0
if the other object comes "after" this one,
if the other object is considered "equal" to this
7
The compareTo method (10.2)
The String class implements the Comparable interface.
-Example: in the String class, there is a method:
public int compareTo(String other);
• A call of A.compareTo(B) will return:
a value <
a value >
or 0
0
if A comes "before" B in the ordering,
0
if A comes "after" B in the ordering,
if A and B are considered "equal" in the ordering.
8
Using compareTo
• compareTo can be used as a test in an if statement.
String a = "alice";
String b = "bob";
if (a.compareTo(b) < 0) {
...
}
// true
Primitives
if (a < b) { ...
Objects
if (a.compareTo(b) < 0) { ...
if (a <= b) { ...
if (a.compareTo(b) <= 0) { ...
if (a == b) { ...
if (a.compareTo(b) == 0) { ...
if (a != b) { ...
if (a.compareTo(b) != 0) { ...
if (a >= b) { ...
if (a.compareTo(b) >= 0) { ...
if (a > b) { ...
if (a.compareTo(b) > 0) { ...
9
compareTo and collections
• You can use an array or list of strings with Java's included sort
method because it calls compareTo internally.
String[] a = {"bob","mike","cari","dan","alice"};
Arrays.sort(a);
//a={“alice”,”bob”,”cari”,”dan”,”mike”}
Note that if the String class does not implement the Comparable
interface, this will not work. Knowing that a class implement a certain
interface guarantees that the class has certain behavior or methods.
The Arrays class has a static sort method for arrays and the Collections
has a static sort method for arraylists.
10
Comparable template
public class name implements Comparable {
...
public int compareTo(Object other) {
...
}
}
11
Comparable Interface
String[] fruits ={"Pineapple","Apple", "Orange",
"Banana"};
System.out.print("\n"+Arrays.toString(fruits));
Arrays.sort(fruits);
System.out.print("\n"+Arrays.toString(fruits));
Does this work for the Point class? The Car class?
12
Comparable Interface
List<String> fruits = new ArrayList<String>();
fruits.add("Pineapple");
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");
Collections.sort(fruits);
System.out.println(fruits);
13
Point class
public class Point implements Comparable {
private int x;
private int y;
...
// sort by x and break ties by y
public int compareTo(Object other) {
if (x < ((Point)other).x) {
return -1;
} else if (x > ((Point)other).x) {
return 1;
} else if (y < ((Point)other).y) {
return -1;
// same x, smaller y
} else if (y > ((Point)other).y) {
return 1;
// same x, larger y
} else {
return 0;
// same x and same y
}}}
14
Comparable Interface
List<Point>
pts.add(new
pts.add(new
pts.add(new
pts.add(new
pts = new ArrayList<Point>();
Point(3,4));
Point(-3,7));
Point(-1,0));
Point(23,42));
Collections.sort(pts);
System.out.println(pts);
15
Modify randomShape
Modify your randomShape project to include an abstract class Shape
and an interface Movable.
The abstract class Shape now has an abstract method display() and a
concrete method bounce(). The bounce method() checks for collisions
with the boundary and changes the direction of the objects. Part of
this method used to be in the update() method.
The interface Movable only has one abstract method. The move()
method is implemented differently in Rectangle and Circle. The Circle
class move the same way but the Rectangle class only moves either in
the x direction or the y direction but not both. (you can have a new
instance variable initialized in the constructor to move a certain
direction.)
16