COMP 103 Interfaces, Classes, Collections Thomas Kuehne, Marcus Frean, 2015-T2 Lecture 3 Thomas Kuehne School of Engineering and Computer Science, Victoria University of Wellington RECAP - TODAY 2 RECAP Libraries, Collections: the Java Collections Library Interfaces, Abstract Data Types TODAY More on interface versus class, and the Collections library Parameterized Types ADMINISTRIVIA Assignment#1 out today Videos available from “Blackboard”, may need IE Tutorials will start next week. QUICK NOTE: Coding style 3 I will drop the “this.” except when needed I Instead of just Instead of just this.loadFromFile(...) loadFromFile(...) this.shapes.addShape(shape) shapes.addShape(shape) will leave out { } when surrounding just one statement Instead of while (i<name.length) { name[i] = null; } just while (i<name.length) name[i] = null; Java Interfaces 4 Interface Classes Oval Rect Shape public interface Shape { Triangle public class Rect implements Shape { public void redraw() { public void redraw(); UI.drawRect(x,y,…,…). : } } enables us to say “all shapes must be able to draw themselves” enables us to declare a List of Shapes, without saying exactly what each shape is going to be. Java Interfaces (for collections) 5 Interfaces Classes extends Collection <E> List <E> ArrayList <E> LinkedList <E> Interfaces can extend other interfaces (the “sub” interface has all the methods of the “super” interface, plus its own methods) interface what it is an implementation (= a class) how it is implemented Interfaces are organised in a hierarchy; they “extend” one another Parameterised Types 7 The structure and access discipline of a collection is the same, regardless of the type of elements in it: A set of Strings, a set of Persons, a set of Shapes, a set of integers all behave the same way. ⇒ we only want one Interface for each kind of collection. (there is only one Set interface) Need to specify kind of elements in a particular collection ⇒ The collection Interfaces (and classes) are parameterised: A generic interface has a “generic” type parameter When declaring a variable of a collection type, you specify the type of the collection and the actual type of the elements of the collection 8 Parameterised Types In the Interface, we use a placeholder: public interface Set <E> extends Collection <E> { public void add(E item); public void remove(E item); public boolean contains(E item); /*…description…*/ /*…description…*/ /*…description…*/ … // (lots more methods in the Java Set interface) When declaring a variable, specify the element type private Set <Thing> mystuff; private List <Shape> drawing; Collection Type Element Type 9 Using Java Collection Interfaces Your program can.... 1. declare a variable, parameter, or field of the interface type private List<Thing> myThings; // a list of Thing objects 2. instantiate a collection: myThings = new ArrayList<Thing> (); Why can’t this be “List”? 3. call methods on that variable, parameter, or field myThings.add(new Thing(“discombobulator”, “yellow”, “huge”)); myThings.get(3).toString(); Aside: this won’t work. Why not? Another Example 10 Polymorphism applies, as usual. So you can do this… 1. declare a variable, parameter, or field of the interface type, as in private 2. List <Shape> drawing; instantiate an object, as in drawing= 3. // a list of Shape objects new ArrayList <Shape> (); call methods on that variable, parameter, or field, as in drawing.add(new Rect(100, 100, 20, 30)); ...so long as the Rect class implements the Shape interface: public class Rect implements Shape {..... 11 Methods on Collection and List Collection <E> isEmpty() size() contains(E elem) add(E elem) remove(E elem) iterator() → boolean Methods on all types → int of collections → boolean → boolean (whether it succeeded) → boolean (whether it removed an item) → iterator <E> … List <E> add(int index, E elem) remove(int index) get(int index) set(int index, E elem) indexOf(E elem) subList(int from, int to) … Additional methods on all Lists → E (returns the item removed) →E → E (returns the item replaced) → int → List<E> Example 12 Wanted: a collection of tasks, in order they should be done. Requirements of TasksOrganiser: read list of tasks from a file display all the tasks add task, at end, or at specified position remove task move task to a different position. Collection type: List Element type: Task ... public class TasksOrganiser { private List<Task> tasks; ... 13 Example (TasksOrganiser program) /* read a list of tasks from a file, */ public void readTasks(String fname) { try { Scanner taskScanner= new Scanner(new File(fname)); tasks = new ArrayList<Task>(); while ( taskScanner.hasNext() ) tasks.add(new Task(taskScanner.next())); taskScanner.close(); } catch(IOException e) {…} displayTasks(); } 14 Iterating through List public void displayTasks(){ UI.print(tasks.size() + “ tasks to be done:\n”); // You could do it this way (uses the standard “for” loop) for (int i=0; i<tasks.size(); i++) UI.print(tasks.get(i) + "\n"); // or this way instead (uses the “for each” syntax) for (Task task : tasks) UI.print(task + "\n"); // There is even a third way (use an iterator) Iterator <Task> iter = tasks.iterator(); while (iter.hasNext()){ UI.print(iter.next() + "\n"); } Actually, the “for each” syntax is just a shortcut for using an iterator. More on this later. 15 Lists are nicer than arrays No size limit! Lists grow bigger as necessary More functionality, such as “addAll(anotherCollection)” List taskList.set(ind, value) taskList.get(ind) taskList.size() taskList.add(value) taskList.add(ind, value) taskList.remove(ind) taskList.remove(value) for (Task t : tasks) vs Array taskArray[ind] = value taskArray[ind] ? Not same as length of the array! ? Where is the last value? What happens if it’s full? ? Have to shift everything up!! ? Have to shift everything down!! ? Have to find value, then shift things down!! for( int i = 0; i< ???; i++) Task t = taskArray[ i ];