XXXList - The University of New Orleans

[Pages:5] !" " # $ % & & & ' !( $ ( ( ) $ " ( 0

Do not repeat yourself!

1

Implementation of classes StudentList, and PlayingCardList, are virtually identical.

1

Client methods that manipulate StudentList

public void swap (StudentList list, int i, int j) { ...} public void sort (StudentList list) { ...} 1 public int search (Student s, StudentList list) { ... }

Will need to be duplicated to deal with any other XXXList type!

May 2004

? ???????

?

3 4 56 7 8 9 4 @ A 8 B @ 5C 6 @ C B 8 D @ E A F G54 @ H 6 F 4 4

AbstractList

StudentList

PlayingCardList RoomList

I AbstractList specifies and implements all list methods,

I subclasses will inherit the implementations. Items in AbstractList are specified of type Object

public void add (Object object) Add the specified Object to this list.

public Object get (int index) The Object on this list with the specified index.

May 2004

? ???????

2

b E 9 B G8 4 4 5 6 7 c 9 4 @ A 8 B @ d 54 @

e

In subclass StudentList we re-implement the methods dealing with Student instances:

public Student get (int i) { return (Student)super.get(i);

}

public void add (Object student) { assert student instanceof Student; super.add(student);

}

May 2004

Q RSTUVWXY` a

P

b E 9 B G8 4 4 5 6 7 c 9 4 @ A 8 B @ d 54 @ g h i p q r s t u

v The specification of add in AbstractList is

public void add (Object object) Add the specified Object to this list.

v while in StudentList is

public void add (Object student) Add the specified Object to this list.

w xy y x y

require: student instanceof Student

v v StudentList cannot be used where AbstractList is required.

Broken code:

public void addAnObject (AbstractList list) {

list.add(new Object());

}

May 2004

Q RSTUVWXY` a

f

h r ij k k i j i h il r m n l p o m p h i qir m r j i r r

v Sshare specification and implementation s class definition specifies dummy Element type as parameter Element is used in List implementation public class List { public void add (Element element) { ... } public Element get (int index) { ... } ... }

t List clients provide item type at creation to fill for Element List roll = new List();

List hand = new List();

?interface? List

AbstractList

DefaultList

May 2004

def g

May 2004

v wxyz{|}~

u

public interface List A finite list of Elements.

Queries

public int size () Number of elements in this List. ensure: this.size() >= 0

public boolean isEmpty () This List contains no elements. this.isEmpty() == (this.size() == 0)

public Element get (int index) The Element with the specified index. require: 0 = 0)

public int indexOf (Element element) The index of the first occurrence of the specified element, or -1 if this List does not contain the specified element. ensure: if this.indexOf(element) >= 0 this.get(this.indexOf(element)).equals (element) for all j: 0 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download