advanced object-oriented programming lecture 3akridel/comp213-fall2016/lecture...

77
COMP 213 Advanced Object-oriented Programming Lecture 3 Fields and Abstract Data Types (ADTs)

Upload: others

Post on 27-May-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

COMP 213Advanced Object-oriented Programming

Lecture 3

Fields and Abstract Data Types (ADTs)

Page 2: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Java fields

• A Java field is a variable inside a class.

• E.g., in a class representing employees, the following fields might be declared:

• name

• position

• salary

• hiredDated

• A Java field is declared using the following syntax:

[access_modifier] [static] [final] type name [= initial value] ;

Page 3: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Java fields

• A Java field is a variable inside a class.

• E.g., in a class representing employees, the following fields might be declared:

• name

• position

• salary

• hiredDated

• A Java field is declared using the following syntax:

[access_modifier] [static] [final] type name [= initial value] ;

Page 4: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Java fields

• A Java field is a variable inside a class.

• E.g., in a class representing employees, the following fields might be declared:

• name

• position

• salary

• hiredDated

• A Java field is declared using the following syntax:

[access_modifier] [static] [final] type name [= initial value] ;

Page 5: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Example class

public class Employee {

public String name;

public String position;

public int salary;

public Date hiredDate;

}

This is a “modifier” and declares the scope of a method – we will

look into modifiers in the forthcoming lectures

Page 6: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Abstract Data Types (ADTs)

• In computer science, an abstract data type (ADT) is a mathematical model for data types that have similar behaviour/semantics.

• An abstract data type is defined indirectly, only by the operations that may be performed on it and by mathematical constraints on the effects (and possibly cost) of those operations.

Page 7: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Abstract Data Types (ADTs)

• In computer science, an abstract data type (ADT) is a mathematical model for data types that have similar behaviour/semantics.

• An abstract data type is defined indirectly, only by the operations that may be performed on it and by mathematical constraints on the effects (and possibly cost) of those operations.

Page 8: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

In other words…

• An ADT is a model of a data structure that specifies:

• the characteristics/values of the collection of data

• the operations that can be performed on the collection

• It’s abstract because it doesn’t specify how the ADT will be implemented.

• A given ADT can have multiple implementations.

Page 9: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• A knapsack is just a container for a group of data items (analogy: a bag of sweets).

• The positions of the data items don’t matter (unlike a list).

• {5, 2, 23, 9} is equivalent to {2, 23, 9, 5}.

• The items do not need to be unique (unlike a set).

• {8, 7, 12, 3, 8} isn’t a set, but it is a knapsack.

Page 10: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• A knapsack is just a container for a group of data items (analogy: a bag of sweets).

• The positions of the data items don’t matter (unlike a list).

• {5, 2, 23, 9} is equivalent to {2, 23, 9, 5}.

• The items do not need to be unique (unlike a set).

• {8, 7, 12, 3, 8} isn’t a set, but it is a knapsack.

Page 11: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• A knapsack is just a container for a group of data items (analogy: a bag of sweets).

• The positions of the data items don’t matter (unlike a list).

• {5, 2, 23, 9} is equivalent to {2, 23, 9, 5}.

• The items do not need to be unique (unlike a set).

• {8, 7, 12, 3, 8} isn’t a set, but it is a knapsack.

Page 12: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• A knapsack is just a container for a group of data items (analogy: a bag of sweets).

• The positions of the data items don’t matter (unlike a list).

• {5, 2, 23, 9} is equivalent to {2, 23, 9, 5}.

• The items do not need to be unique (unlike a set).

• {8, 7, 12, 3, 8} isn’t a set, but it is a knapsack.

Page 13: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• A knapsack is just a container for a group of data items (analogy: a bag of sweets).

• The positions of the data items don’t matter (unlike a list).

• {5, 2, 23, 9} is equivalent to {2, 23, 9, 5}.

• The items do not need to be unique (unlike a set).

• {8, 7, 12, 3, 8} isn’t a set, but it is a knapsack.

Page 14: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• The operations supported by our Knapsack ADT

• add(item): add item to the knapsack

• remove(item): remove one occurrence of item (if any) from the knapsack

• includes(item): check if item is in the knapsack

• numItems(): get the number of items in the knapsack

• grab(): get an item at random, without removing it (reflects the fact that the items don’t have a position – and therefore we can’t get the ith item in the knapsack)

• toArray(): get an array containing the current contents of the bag

• Note: we don’t specify how the knapsack will be implemented

Page 15: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• The operations supported by our Knapsack ADT

• add(item): add item to the knapsack

• remove(item): remove one occurrence of item (if any) from the knapsack

• includes(item): check if item is in the knapsack

• numItems(): get the number of items in the knapsack

• grab(): get an item at random, without removing it (reflects the fact that the items don’t have a position – and therefore we can’t get the ith item in the knapsack)

• toArray(): get an array containing the current contents of the bag

• Note: we don’t specify how the knapsack will be implemented

Page 16: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• The operations supported by our Knapsack ADT

• add(item): add item to the knapsack

• remove(item): remove one occurrence of item (if any) from the knapsack

• includes(item): check if item is in the knapsack

• numItems(): get the number of items in the knapsack

• grab(): get an item at random, without removing it (reflects the fact that the items don’t have a position – and therefore we can’t get the ith item in the knapsack)

• toArray(): get an array containing the current contents of the bag

• Note: we don’t specify how the knapsack will be implemented

Page 17: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• The operations supported by our Knapsack ADT

• add(item): add item to the knapsack

• remove(item): remove one occurrence of item (if any) from the knapsack

• includes(item): check if item is in the knapsack

• numItems(): get the number of items in the knapsack

• grab(): get an item at random, without removing it (reflects the fact that the items don’t have a position – and therefore we can’t get the ith item in the knapsack)

• toArray(): get an array containing the current contents of the bag

• Note: we don’t specify how the knapsack will be implemented

Page 18: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• The operations supported by our Knapsack ADT

• add(item): add item to the knapsack

• remove(item): remove one occurrence of item (if any) from the knapsack

• includes(item): check if item is in the knapsack

• numItems(): get the number of items in the knapsack

• grab(): get an item at random, without removing it (reflects the fact that the items don’t have a position – and therefore we can’t get the ith item in the knapsack)

• toArray(): get an array containing the current contents of the bag

• Note: we don’t specify how the knapsack will be implemented

Page 19: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• The operations supported by our Knapsack ADT

• add(item): add item to the knapsack

• remove(item): remove one occurrence of item (if any) from the knapsack

• includes(item): check if item is in the knapsack

• numItems(): get the number of items in the knapsack

• grab(): get an item at random, without removing it (reflects the fact that the items don’t have a position – and therefore we can’t get the ith item in the knapsack)

• toArray(): get an array containing the current contents of the bag

• Note: we don’t specify how the knapsack will be implemented

Page 20: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• The operations supported by our Knapsack ADT

• add(item): add item to the knapsack

• remove(item): remove one occurrence of item (if any) from the knapsack

• includes(item): check if item is in the knapsack

• numItems(): get the number of items in the knapsack

• grab(): get an item at random, without removing it (reflects the fact that the items don’t have a position – and therefore we can’t get the ith item in the knapsack)

• toArray(): get an array containing the current contents of the bag

• Note: we don’t specify how the knapsack will be implemented

Page 21: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Simple example: A knapsack

• The operations supported by our Knapsack ADT

• add(item): add item to the knapsack

• remove(item): remove one occurrence of item (if any) from the knapsack

• includes(item): check if item is in the knapsack

• numItems(): get the number of items in the knapsack

• grab(): get an item at random, without removing it (reflects the fact that the items don’t have a position – and therefore we can’t get the ith item in the knapsack)

• toArray(): get an array containing the current contents of the bag

• Note: we don’t specify how the knapsack will be implemented

Page 22: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Specifying an ADT using an Interface

public interface Knapsack {

boolean add(Object item);

boolean remove(Object item);

boolean includes(Object item);

int numItems();

Object grab();

Object[] toArray();

}

An interface specifies a set of methods and can be used to specify an ADT:• includes only the method headers• does not include the actual method definitions!

Page 23: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Specifying an ADT using an Interface

public interface Knapsack {

boolean add(Object item);

boolean remove(Object item);

boolean includes(Object item);

int numItems();

Object grab();

Object[] toArray();

}

An interface specifies a set of methods and can be used to specify an ADT:• includes only the method headers• does not include the actual method definitions!

Page 24: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Specifying an ADT using an Interface

public interface Knapsack {

boolean add(Object item);

boolean remove(Object item);

boolean includes(Object item);

int numItems();

Object grab();

Object[] toArray();

}

An interface specifies a set of methods and can be used to specify an ADT:• includes only the method headers• does not include the actual method definitions!

Page 25: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Specifying an ADT using an Interface

public interface Knapsack {

boolean add(Object item);

boolean remove(Object item);

boolean includes(Object item);

int numItems();

Object grab();

Object[] toArray();

}

An interface specifies a set of methods and can be used to specify an ADT:• includes only the method headers• does not include the actual method definitions!

Page 26: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Specifying an ADT using an Interface

public interface Knapsack {

boolean add(Object item);

boolean remove(Object item);

boolean includes(Object item);

int numItems();

Object grab();

Object[] toArray();

}

An interface specifies a set of methods and can be used to specify an ADT:• includes only the method headers• does not include the actual method definitions!

Page 27: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Specifying an ADT using an Interface

public interface Knapsack {

boolean add(Object item);

boolean remove(Object item);

boolean includes(Object item);

int numItems();

Object grab();

Object[] toArray();

}

An interface specifies a set of methods and can be used to specify an ADT:• includes only the method headers• does not include the actual method definitions!

Page 28: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Specifying an ADT using an Interface

public interface Knapsack {

boolean add(Object item);

boolean remove(Object item);

boolean includes(Object item);

int numItems();

Object grab();

Object[] toArray();

}

An interface specifies a set of methods and can be used to specify an ADT:• includes only the method headers• does not include the actual method definitions!

Page 29: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Specifying an ADT using an Interface

public interface Knapsack {

boolean add(Object item);

boolean remove(Object item);

boolean includes(Object item);

int numItems();

Object grab();

Object[] toArray();

}

An interface specifies a set of methods and can be used to specify an ADT:• includes only the method headers• does not include the actual method definitions!

Page 30: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Implementing an ADT using a Class

public class ArrayKnapsack implements Knapsack {

private Object[] items;

private int numItems;

...

public boolean add(Object item) {

...

}

When a class header includes an implements clause, the class must define all of the methods in the interface.

Page 31: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Implementing an ADT using a Class

public class ArrayKnapsack implements Knapsack {

private Object[] items;

private int numItems;

...

public boolean add(Object item) {

...

}

When a class header includes an implements clause, the class must define all of the methods in the interface.

Page 32: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Implementing an ADT using a Class

public class ArrayKnapsack implements Knapsack {

private Object[] items;

private int numItems;

...

public boolean add(Object item) {

...

}

When a class header includes an implements clause, the class must define all of the methods in the interface.

Page 33: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Implementing an ADT using a Class

public class ArrayKnapsack implements Knapsack {

private Object[] items;

private int numItems;

...

public boolean add(Object item) {

...

}

When a class header includes an implements clause, the class must define all of the methods in the interface.

Page 34: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Implementing an ADT using a Class

public class ArrayKnapsack implements Knapsack {

private Object[] items;

private int numItems;

...

public boolean add(Object item) {

...

}

When a class header includes an implements clause, the class must define all of the methods in the interface.

Page 35: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Encapsulation

• Our implementation provides proper encapsulation

• a key principle of object-oriented programming (we will see more of it in the forthcoming lectures)

• also known as information hiding

• We prevent direct access to the internals of an object by making its fields private.

• We provide limited indirect access through methods that are labelled public.

Page 36: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

All Interface Methods Are Public

• Methods specified in an interface must be public, so we don’t need to use the keyword public in the interface definition.

• However, when we actually implement one of these methods in a class, we do need to explicitly use the keyword public

Page 37: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Other example ADTs

• A List is a sequence of values with operations to:

• add a value to the start of the list

• get the value at the start of the list

• get the “tail” of the list

• A Stack is a sequence of values with operations to:

• push a value onto the top of the stack

• get the value at the top of the stack

• pop the first value off of the stack

Page 38: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Other example ADTs

• A List is a sequence of values with operations to:

• add a value to the start of the list

• get the value at the start of the list

• get the “tail” of the list

• A Stack is a sequence of values with operations to:

• push a value onto the top of the stack

• get the value at the top of the stack

• pop the first value off of the stack

Page 39: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Other example ADTs

• A List is a sequence of values with operations to:

• add a value to the start of the list

• get the value at the start of the list

• get the “tail” of the list

• A Stack is a sequence of values with operations to:

• push a value onto the top of the stack

• get the value at the top of the stack

• pop the first value off of the stack

Page 40: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

The List ADT

• Think of it as, e.g., a list of important dates, a list of addresses or a grocery list.

• Main features:

• The items appear in a sequence.

• The list has one first element (the head) and one last element (the tail).

• Each element (except for the head & tail) has one unique predecessor and one unique successor.

• The items are of the same type (some element type E).

Page 41: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

The List ADT in Java Collections

• The List ADT is one of the data structures implemented in the Java Collections API (Application programming interface).

• Java contains two general-purpose List implementations:

• ArrayList (usually the better-performing implementation), and

• LinkedList (offers better performance under certain circumstances).

Page 42: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

The List ADT in Java Collections

• The List ADT is one of the data structures implemented in the Java Collections API (Application programming interface).

• Java contains two general-purpose List implementations:

• ArrayList (usually the better-performing implementation), and

• LinkedList (offers better performance under certain circumstances).

Page 43: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Lists vs. Arrays

• Both Lists & Arrays are ordered collections of objects.

• In both cases you can add or access items at a particular position (first position is position zero in both).

• You can find out how many items are in a List (using its size method), and how large an array is (using its length field).

• The main advantage of a List compared to an array is that the size of a List is dynamic.

Page 44: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Lists vs. Arrays

• Both Lists & Arrays are ordered collections of objects.

• In both cases you can add or access items at a particular position (first position is position zero in both).

• You can find out how many items are in a List (using its size method), and how large an array is (using its length field).

• The main advantage of a List compared to an array is that the size of a List is dynamic.

Page 45: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Lists vs. Arrays

• Both Lists & Arrays are ordered collections of objects.

• In both cases you can add or access items at a particular position (first position is position zero in both).

• You can find out how many items are in a List (using its size method), and how large an array is (using its length field).

• The main advantage of a List compared to an array is that the size of a List is dynamic.

Page 46: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Lists vs. Arrays

• Both Lists & Arrays are ordered collections of objects.

• In both cases you can add or access items at a particular position (first position is position zero in both).

• You can find out how many items are in a List (using its size method), and how large an array is (using its length field).

• The main advantage of a List compared to an array is that the size of a List is dynamic.

Page 47: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Lists: example

• Consider the list: milk, bread, butter, eggs, sugar, flour

• We construct it:

• Create an empty list & use series of add operations, or

• Create an array & add the items of the array to the list

• We insert new item:

• Use add operation.

• We delete an item:

• Use remove operation.

Page 48: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Lists: example

public static void main(String[] args) {

String[] myGroceries = {"milk", "bread", "butter", "eggs",

"sugar", "flour"};

List<String> myList = new ArrayList<String>();

for(String x: myGroceries) {

myList.add(x);

}

myList.add("steak");

myList.remove("steak");

}

Page 49: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Lists: example

public static void main(String[] args) {

String[] myGroceries = {"milk", "bread", "butter", "eggs",

"sugar", "flour"};

List<String> myList = new ArrayList<String>();

for(String x: myGroceries) {

myList.add(x);

}

myList.add("steak");

myList.remove("steak");

}

Construct list:

Page 50: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Lists: example

public static void main(String[] args) {

String[] myGroceries = {"milk", "bread", "butter", "eggs",

"sugar", "flour"};

List<String> myList = new ArrayList<String>();

for(String x: myGroceries) {

myList.add(x);

}

myList.add("steak");

myList.remove("steak");

}

Construct list:

Page 51: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Lists: example

public static void main(String[] args) {

String[] myGroceries = {"milk", "bread", "butter", "eggs",

"sugar", "flour"};

List<String> myList = new ArrayList<String>();

for(String x: myGroceries) {

myList.add(x);

}

myList.add("steak");

myList.remove("steak");

}

Construct list:

Page 52: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Lists: example

Add item:

public static void main(String[] args) {

String[] myGroceries = {"milk", "bread", "butter", "eggs",

"sugar", "flour"};

List<String> myList = new ArrayList<String>();

for(String x: myGroceries) {

myList.add(x);

}

myList.add("steak");

myList.remove("steak");

}

Page 53: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Lists: example

Remove item:

public static void main(String[] args) {

String[] myGroceries = {"milk", "bread", "butter", "eggs",

"sugar", "flour"};

List<String> myList = new ArrayList<String>();

for(String x: myGroceries) {

myList.add(x);

}

myList.add("steak");

myList.remove("steak");

}Only removes the 1st instanceof “steak”, if there are morethan one.

Page 54: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

More methods from the List ADT

//from Collection interface

int size();

boolean isEmpty();

void clear();

boolean contains(AnyType x);

boolean add(AnyType x);

boolean remove(AnyType x);

//from List interface

AnyType get(int idx);

AnyType set(int idx, AnyType newVal);

void add(int idx, AnyType x);

void remove(int idx);

java.util.Iterator<AnyType> iterator();

Page 55: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

More methods from the List ADT

//from Collection interface

int size();

boolean isEmpty();

void clear();

boolean contains(AnyType x);

boolean add(AnyType x);

boolean remove(AnyType x);

//from List interface

AnyType get(int idx);

AnyType set(int idx, AnyType newVal);

void add(int idx, AnyType x);

void remove(int idx);

java.util.Iterator<AnyType> iterator();

Page 56: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

More methods from the List ADT

//from Collection interface

int size();

boolean isEmpty();

void clear();

boolean contains(AnyType x);

boolean add(AnyType x);

boolean remove(AnyType x);

//from List interface

AnyType get(int idx);

AnyType set(int idx, AnyType newVal);

void add(int idx, AnyType x);

void remove(int idx);

java.util.Iterator<AnyType> iterator();

Page 57: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

More methods from the List ADT

//from Collection interface

int size();

boolean isEmpty();

void clear();

boolean contains(AnyType x);

boolean add(AnyType x);

boolean remove(AnyType x);

//from List interface

AnyType get(int idx);

AnyType set(int idx, AnyType newVal);

void add(int idx, AnyType x);

void remove(int idx);

java.util.Iterator<AnyType> iterator();

Page 58: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

More methods from the List ADT

//from Collection interface

int size();

boolean isEmpty();

void clear();

boolean contains(AnyType x);

boolean add(AnyType x);

boolean remove(AnyType x);

//from List interface

AnyType get(int idx);

AnyType set(int idx, AnyType newVal);

void add(int idx, AnyType x);

void remove(int idx);

java.util.Iterator<AnyType> iterator();

Page 59: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

More methods from the List ADT

//from Collection interface

int size();

boolean isEmpty();

void clear();

boolean contains(AnyType x);

boolean add(AnyType x);

boolean remove(AnyType x);

//from List interface

AnyType get(int idx);

AnyType set(int idx, AnyType newVal);

void add(int idx, AnyType x);

void remove(int idx);

java.util.Iterator<AnyType> iterator();

Page 60: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

More methods from the List ADT

//from Collection interface

int size();

boolean isEmpty();

void clear();

boolean contains(AnyType x);

boolean add(AnyType x);

boolean remove(AnyType x);

//from List interface

AnyType get(int idx);

AnyType set(int idx, AnyType newVal);

void add(int idx, AnyType x);

void remove(int idx);

java.util.Iterator<AnyType> iterator();

Page 61: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

More methods from the List ADT

//from Collection interface

int size();

boolean isEmpty();

void clear();

boolean contains(AnyType x);

boolean add(AnyType x);

boolean remove(AnyType x);

//from List interface

AnyType get(int idx);

AnyType set(int idx, AnyType newVal);

void add(int idx, AnyType x);

void remove(int idx);

java.util.Iterator<AnyType> iterator();

Page 62: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

More methods from the List ADT

//from Collection interface

int size();

boolean isEmpty();

void clear();

boolean contains(AnyType x);

boolean add(AnyType x);

boolean remove(AnyType x);

//from List interface

AnyType get(int idx);

AnyType set(int idx, AnyType newVal);

void add(int idx, AnyType x);

void remove(int idx);

java.util.Iterator<AnyType> iterator();

Page 63: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

More methods from the List ADT

//from Collection interface

int size();

boolean isEmpty();

void clear();

boolean contains(AnyType x);

boolean add(AnyType x);

boolean remove(AnyType x);

//from List interface

AnyType get(int idx);

AnyType set(int idx, AnyType newVal);

void add(int idx, AnyType x);

void remove(int idx);

java.util.Iterator<AnyType> iterator();

Page 64: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

More methods from the List ADT

//from Collection interface

int size();

boolean isEmpty();

void clear();

boolean contains(AnyType x);

boolean add(AnyType x);

boolean remove(AnyType x);

//from List interface

AnyType get(int idx);

AnyType set(int idx, AnyType newVal);

void add(int idx, AnyType x);

void remove(int idx);

java.util.Iterator<AnyType> iterator();

Page 65: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

More methods from the List ADT

//from Collection interface

int size();

boolean isEmpty();

void clear();

boolean contains(AnyType x);

boolean add(AnyType x);

boolean remove(AnyType x);

//from List interface

AnyType get(int idx);

AnyType set(int idx, AnyType newVal);

void add(int idx, AnyType x);

void remove(int idx);

java.util.Iterator<AnyType> iterator();

Page 66: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

The Iterator<E> Interface

• The Iterator<E> interface is a very useful interface provided by the Collections framework for traversing a Collection.

• When the iterator method is called on a Collection, it returns an Iterator object which has the following methods for traversing the Collection:

boolean hasNext();

AnyType next();

void remove();

Page 67: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Using an Iterator to Traverse a Collection

public static <AnyType> void print( Collection<AnyType> coll ){

Iterator<AnyType> itr = coll.iterator( );

while( itr.hasNext( ) ){

AnyType item = itr.next( );

System.out.println( item );

}

}

General form:

Page 68: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Using an Iterator to Traverse a Collection

public static <AnyType> void print( Collection<AnyType> coll ){

Iterator<AnyType> itr = coll.iterator( );

while( itr.hasNext( ) ){

AnyType item = itr.next( );

System.out.println( item );

}

}

General form:

Page 69: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Using an Iterator to Traverse a Collection

public static <AnyType> void print( Collection<AnyType> coll ){

Iterator<AnyType> itr = coll.iterator( );

while( itr.hasNext( ) ){

AnyType item = itr.next( );

System.out.println( item );

}

}

General form:

Page 70: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Using an Iterator to Traverse a Collection

public static <AnyType> void print( Collection<AnyType> coll ){

Iterator<AnyType> itr = coll.iterator( );

while( itr.hasNext( ) ){

AnyType item = itr.next( );

System.out.println( item );

}

}

General form:

Page 71: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Using an Iterator to Traverse a Collection

public static <AnyType> void print( Collection<AnyType> coll ){

Iterator<AnyType> itr = coll.iterator( );

while( itr.hasNext( ) ){

AnyType item = itr.next( );

System.out.println( item );

}

}

General form:

Page 72: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Using an Iterator to Traverse a Collection

public static void main(String[] args) {

String[] myGroceries = {"milk", "bread", "butter", "eggs",

"sugar", "flour"};

List<String> myList = new ArrayList<String>();

for(String x: myGroceries) {

myList.add(x);

}

Iterator<String> itr = myList.iterator();

while(itr.hasNext()){

String item = itr.next();

System.out.println(item);

}

}

Our example:

Page 73: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Using an Iterator to Traverse a Collection

public static void main(String[] args) {

String[] myGroceries = {"milk", "bread", "butter", "eggs",

"sugar", "flour"};

List<String> myList = new ArrayList<String>();

for(String x: myGroceries) {

myList.add(x);

}

Iterator<String> itr = myList.iterator();

while(itr.hasNext()){

String item = itr.next();

System.out.println(item);

}

}

Our example:

Page 74: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Using an Iterator to Traverse a Collection

public static void main(String[] args) {

String[] myGroceries = {"milk", "bread", "butter", "eggs",

"sugar", "flour"};

List<String> myList = new ArrayList<String>();

for(String x: myGroceries) {

myList.add(x);

}

Iterator<String> itr = myList.iterator();

while(itr.hasNext()){

String item = itr.next();

System.out.println(item);

}

}

Our example:

Page 75: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Using an Iterator to Traverse a Collection

public static void main(String[] args) {

String[] myGroceries = {"milk", "bread", "butter", "eggs",

"sugar", "flour"};

List<String> myList = new ArrayList<String>();

for(String x: myGroceries) {

myList.add(x);

}

Iterator<String> itr = myList.iterator();

while(itr.hasNext()){

String item = itr.next();

System.out.println(item);

}

}

Our example:

Page 76: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Using an Iterator to Traverse a Collection

public static void main(String[] args) {

String[] myGroceries = {"milk", "bread", "butter", "eggs",

"sugar", "flour"};

List<String> myList = new ArrayList<String>();

for(String x: myGroceries) {

myList.add(x);

}

Iterator<String> itr = myList.iterator();

while(itr.hasNext()){

String item = itr.next();

System.out.println(item);

}

}

Our example:

Page 77: Advanced Object-oriented Programming Lecture 3akridel/COMP213-Fall2016/Lecture Slides/Lecture3.pdfJava fields • A Java field is a variable inside a class. E.g., in a class representing

Summary

• Introduction to Abstract Data Types

• Next: Implementing Abstract Data Types.