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

Post on 27-May-2020

7 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

COMP 213Advanced Object-oriented Programming

Lecture 3

Fields and Abstract Data Types (ADTs)

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] ;

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] ;

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] ;

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

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.

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.

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.

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.

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.

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.

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.

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.

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

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

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

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

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

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

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

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

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!

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!

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!

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!

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!

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!

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!

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!

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.

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.

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.

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.

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.

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.

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

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

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

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

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

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

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

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.

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.

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.

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.

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.

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");

}

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:

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:

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:

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");

}

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.

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();

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();

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();

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();

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();

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();

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();

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();

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();

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();

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();

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();

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();

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

Summary

• Introduction to Abstract Data Types

• Next: Implementing Abstract Data Types.

top related