Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP 103 2015-T2 Lecture 3 Thomas Kuehne.

Similar presentations


Presentation on theme: "Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP 103 2015-T2 Lecture 3 Thomas Kuehne."— Presentation transcript:

1 Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP 103 2015-T2 Lecture 3 Thomas Kuehne  Thomas Kuehne, Marcus Frean,

2 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.

3 3 QUICK NOTE: Coding style  I will drop the “ this. ” except when needed  Instead of this.loadFromFile(...) just loadFromFile(...) Instead of this.shapes.addShape(shape) just shapes.addShape(shape)  I 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;

4 Interface Classes  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. 4 Java Interfaces Rect Shape Triange implements Oval implements public interface Shape { public void redraw(); : } public interface Shape { public void redraw(); : } public class Rect implements Shape { public void redraw() { UI.drawRect(x,y,…,…). } public class Rect implements Shape { public void redraw() { UI.drawRect(x,y,…,…). }

5 InterfacesClasses Interfaces can extend other interfaces (the “sub” interface has all the methods of the “super” interface, plus its own methods) 5 Java Interfaces (for collections) ArrayList Collection List LinkedList extends implements

6 extends implements  interface  what it is  an implementation (= a class)  how it is implemented Interfaces are organised in a hierarchy; they “extend” one another

7 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  In the Interface, we use a placeholder: public interface Set extends Collection { public void add(E item); /*…description…*/ public void remove(E item); /*…description…*/ public boolean contains(E item);/*…description…*/ … // (lots more methods in the Java Set interface)  When declaring a variable, specify the element type private Set mystuff; private List drawing; Parameterised Types 8 Collection Type Element Type

9  Your program can.... 1.declare a variable, parameter, or field of the interface type private List myThings; // a list of Thing objects 2.instantiate a collection: myThings = new ArrayList (); 3.call methods on that variable, parameter, or field myThings.add(new Thing( “discombobulator”, “yellow”, “huge”)); myThings.get(3).toString(); Using Java Collection Interfaces 9 Aside: this won ’ t work. Why not? Why can’t this be “List”?

10 Polymorphism applies, as usual. So you can do this… 1. declare a variable, parameter, or field of the interface type, as in private List drawing; // a list of Shape objects 2. instantiate an object, as in drawing= new ArrayList (); 3. 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 {..... Another Example 10

11 Collection isEmpty() → boolean size() → int contains(E elem)→ boolean add(E elem) → boolean (whether it succeeded) remove(E elem) → boolean (whether it removed an item) iterator() → iterator … List add(int index, E elem) remove(int index) → E (returns the item removed) get(int index) → E set(int index, E elem) → E (returns the item replaced) indexOf(E elem)→ int subList(int from, int to)→ List … 11 Methods on Collection and List Methods on all types of collections Additional methods on all Lists

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.  List  Task  Wanted: a collection of tasks, in order they should be done.  Requirements of TasksOrganiser:  Collection type:  Element type: 12 Example

13 13 Example (TasksOrganiser program) public class TasksOrganiser { private List tasks; /* read a list of tasks from a file, */ public void readTasks(String fname) { try { Scanner taskScanner= new Scanner(new File(fname)); tasks = new ArrayList (); while ( taskScanner.hasNext() ) tasks.add(new Task(taskScanner.next())); taskScanner.close(); } catch(IOException e) {…} displayTasks(); }...

14 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 iter = tasks.iterator(); while (iter.hasNext()){ UI.print(iter.next() + "\n"); } 14 Iterating through List Actually, the “for each” syntax is just a shortcut for using an iterator. More on this later.

15  No size limit! Lists grow bigger as necessary  More functionality, such as “addAll(anotherCollection)” List vs Array taskList.set(ind, value) taskArray[ind] = value taskList.get(ind)taskArray[ind] taskList.size()? Not same as length of the array! taskList.add(value)? Where is the last value? What happens if it’s full? taskList.add(ind, value)? Have to shift everything up!! taskList.remove(ind)? Have to shift everything down!! taskList.remove(value)? Have to find value, then shift things down!! for (Task t : tasks) for( int i = 0; i< ???; i++) Task t = taskArray[ i ]; 15 Lists are nicer than arrays


Download ppt "Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP 103 2015-T2 Lecture 3 Thomas Kuehne."

Similar presentations


Ads by Google