Download presentation
Presentation is loading. Please wait.
1
Lecture 23:More on Interfaces
Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
2
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.*;
3
Java collections framework
4
The List Interface public interface List{
public Object get(int index); public Object set(int index, Object element); public Object remove(int index); public void add(int index, Object element); … }
5
The List Interface public class ArrayList implements List{ … }
6
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>();
7
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 < 0 if the other object comes "before" this one, a value > 0 if the other object comes "after" this one, or 0 if the other object is considered "equal" to this
8
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 < 0 if A comes "before" B in the ordering, a value > 0 if A comes "after" B in the ordering, or 0 if A and B are considered "equal" in the ordering.
9
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 Objects 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) { ... if (a > b) { ... if (a.compareTo(b) > 0) { ...
10
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.
11
Comparable template public class name implements Comparable { ...
public int compareTo(Object other) { }
12
Comparable Interface String[] fruits ={"Pineapple","Apple", "Orange", "Banana"}; System.out.print("\n"+Arrays.toString(fruits)); Arrays.sort(fruits); Does this work for the Point class? The Car class?
13
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);
14
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 }}}
15
Comparable Interface List<Point> pts = new ArrayList<Point>(); pts.add(new Point(3,4)); pts.add(new Point(-3,7)); pts.add(new Point(-1,0)); pts.add(new Point(23,42)); Collections.sort(pts); System.out.println(pts);
16
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.)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.