93
COMP 213 Advanced Object-oriented Programming Lecture 10 Utility Classes.

Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

COMP 213Advanced Object-oriented Programming

Lecture 10

Utility Classes.

Page 2: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

(Re)Using Classes

• One of the chief motivations for the Object Paradigm is code re-use.Code in a method is written once, and can be used many times (each time the method is called).

• A class, such as a class of Lists, can also be re-used (each time a programmer needs to store lists of things).

• Many classes can be useful in many different applications(think of all the GUI component classes: Button, TextField, Frame, etc.)These are often bundled together in packages so that programmers can benefit from the hard work that others have put in to implementing these classes.

Page 3: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

(Re)Using Classes

• One of the chief motivations for the Object Paradigm is code re-use.Code in a method is written once, and can be used many times (each time the method is called).

• A class, such as a class of Lists, can also be re-used (each time a programmer needs to store lists of things).

• Many classes can be useful in many different applications(think of all the GUI component classes: Button, TextField, Frame, etc.)These are often bundled together in packages so that programmers can benefit from the hard work that others have put in to implementing these classes.

Page 4: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

(Re)Using Classes

• One of the chief motivations for the Object Paradigm is code re-use.Code in a method is written once, and can be used many times (each time the method is called).

• A class, such as a class of Lists, can also be re-used (each time a programmer needs to store lists of things).

• Many classes can be useful in many different applications(think of all the GUI component classes: Button, TextField, Frame, etc.)These are often bundled together in packages so that programmers can benefit from the hard work that others have put in to implementing these classes.

Page 5: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

(Re)Using Classes

• One of the chief motivations for the Object Paradigm is code re-use.Code in a method is written once, and can be used many times (each time the method is called).

• A class, such as a class of Lists, can also be re-used (each time a programmer needs to store lists of things).

• Many classes can be useful in many different applications(think of all the GUI component classes: Button, TextField, Frame, etc.)These are often bundled together in packages so that programmers can benefit from the hard work that others have put in to implementing these classes.

Page 6: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Packages

• Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗

classes for building GUIs.

• Package javax.swing (it don’t mean a thing) similarly provides useful lightweight†

classes for GUIs.

• Package java.util contains useful utility classes such as linked lists, queues, vectors, etc.

*Heavyweight means implemented using system calls to the native window manager†Lightweight means implemented in Java (on top of the awt)

Page 7: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Packages

• Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗

classes for building GUIs.

• Package javax.swing (it don’t mean a thing) similarly provides useful lightweight†

classes for GUIs.

• Package java.util contains useful utility classes such as linked lists, queues, vectors, etc.

*Heavyweight means implemented using system calls to the native window manager†Lightweight means implemented in Java (on top of the awt)

Page 8: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Packages

• Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗

classes for building GUIs.

• Package javax.swing (it don’t mean a thing) similarly provides useful lightweight†

classes for GUIs.

• Package java.util contains useful utility classes such as linked lists, queues, vectors, etc.

*Heavyweight means implemented using system calls to the native window manager†Lightweight means implemented in Java (on top of the awt)

Page 9: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Packages

• Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗

classes for building GUIs.

• Package javax.swing (it don’t mean a thing) similarly provides useful lightweight†

classes for GUIs.

• Package java.util contains useful utility classes such as linked lists, queues, vectors, etc.

*Heavyweight means implemented using system calls to the native window manager†Lightweight means implemented in Java (on top of the awt)

Page 10: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Packages

• Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗

classes for building GUIs.

• Package javax.swing (it don’t mean a thing) similarly provides useful lightweight†

classes for GUIs.

• Package java.util contains useful utility classes such as linked lists, queues, vectors, etc.

*Heavyweight means implemented using system calls to the native window manager†Lightweight means implemented in Java (on top of the awt)

Page 11: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Example: Vectors

• A Vector is a growable list.I.e., it can hold any number of objects.

• It has operations to add to the start of the list, access the i𝑡h element, place an element at a given position, and much more.

• It’s implemented using an array (default length 10); if it runs out of space, it creates another, larger array (by default, double the size), and copies the old array into the new one.

• It’s generally very fast for accessing at any point, adding/removing at the ends, but adding can occasionally be (a little bit) slower because of the array-copying. Removing elements from the middle can also be slow because of the need to shuffle elements down the array to fill the gap.

Page 12: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Example: Vectors

• A Vector is a growable list.I.e., it can hold any number of objects.

• It has operations to add to the start of the list, access the i𝑡h element, place an element at a given position, and much more.

• It’s implemented using an array (default length 10); if it runs out of space, it creates another, larger array (by default, double the size), and copies the old array into the new one.

• It’s generally very fast for accessing at any point, adding/removing at the ends, but adding can occasionally be (a little bit) slower because of the array-copying. Removing elements from the middle can also be slow because of the need to shuffle elements down the array to fill the gap.

Page 13: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Example: Vectors

• A Vector is a growable list.I.e., it can hold any number of objects.

• It has operations to add to the start of the list, access the i𝑡h element, place an element at a given position, and much more.

• It’s implemented using an array (default length 10); if it runs out of space, it creates another, larger array (by default, double the size), and copies the old array into the new one.

• It’s generally very fast for accessing at any point, adding/removing at the ends, but adding can occasionally be (a little bit) slower because of the array-copying. Removing elements from the middle can also be slow because of the need to shuffle elements down the array to fill the gap.

Page 14: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Example: Vectors

• A Vector is a growable list.I.e., it can hold any number of objects.

• It has operations to add to the start of the list, access the i𝑡h element, place an element at a given position, and much more.

• It’s implemented using an array (default length 10); if it runs out of space, it creates another, larger array (by default, double the size), and copies the old array into the new one.

• It’s generally very fast for accessing at any point, adding/removing at the ends, but adding can occasionally be (a little bit) slower because of the array-copying. Removing elements from the middle can also be slow because of the need to shuffle elements down the array to fill the gap.

Page 15: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Vectors

public class Vector<E>...

The Vector class implements a growable array of objects. Like

an array, it contains components that can be accessed using an

integer index. However, the size of a Vector can grow or shrink

as needed to accommodate adding and removing items after the

Vector has been created.

from the API

Page 16: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Vectors

public class Vector<E>...

The Vector class implements a growable array of objects. Like

an array, it contains components that can be accessed using an

integer index. However, the size of a Vector can grow or shrink

as needed to accommodate adding and removing items after the

Vector has been created.

from the API

So, what’s the <E>?

Page 17: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics

• Class Vector belongs to the Java ‘Collections Framework’, a group of interfaces and data structures for storing collections of things.

• Before Java 1.5, none of these were homogeneous : i.e., they could store any instance of any class.

• For example, a Vector could contain a String , an Integer , a Car, a Machine, and a Button in its list.

Page 18: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics

• Class Vector belongs to the Java ‘Collections Framework’, a group of interfaces and data structures for storing collections of things.

• Before Java 1.5, none of these were homogeneous : i.e., they could store any instance of any class.

• For example, a Vector could contain a String , an Integer , a Car, a Machine, and a Button in its list.

Page 19: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics

• Class Vector belongs to the Java ‘Collections Framework’, a group of interfaces and data structures for storing collections of things.

• Before Java 1.5, none of these were homogeneous : i.e., they could store any instance of any class.

• For example, a Vector could contain a String , an Integer , a Car, a Machine, and a Button in its list.

Page 20: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

pre-1.5 Vector methods

public void addElement( Object o) { ...}public Object elementAt(int i) { ...}

Page 21: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

pre-1.5 Vector methods

public void addElement( Object o) { ...}public Object elementAt(int i) { ...}

In Java, all classes are subclasses of Object.

Page 22: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

A Non-homogeneous Vector

Vector stuff = new Vector();stuff.add("hello");stuff.add(new Integer(9));stuff.add(new Car(“Audi”, “R8”, “Red”, 2016));System.out.println("2nd element is: " + stuff.elementAt(1));

Pre-1.5 code

Page 23: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

A Non-homogeneous Vector

Vector stuff = new Vector();stuff.add("hello");stuff.add(new Integer(9));stuff.add(new Car(“Audi”, “R8”, “Red”, 2016));System.out.println("2nd element is: " + stuff.elementAt(1));

Page 24: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

A Non-homogeneous Vector

Vector stuff = new Vector();stuff.add("hello");stuff.add(new Integer(9));stuff.add(new Car(“Audi”, “R8”, “Red”, 2016));System.out.println("2nd element is: " + stuff.elementAt(1));

Page 25: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

A Non-homogeneous Vector

Vector stuff = new Vector();stuff.add("hello");stuff.add(new Integer(9));stuff.add(new Car(“Audi”, “R8”, “Red”, 2016));System.out.println("2nd element is: " + stuff.elementAt(1));

Page 26: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

A Non-homogeneous Vector

Vector stuff = new Vector();stuff.add("hello");stuff.add(new Integer(9));stuff.add(new Car(“Audi”, “R8”, “Red”, 2016));System.out.println("2nd element is: " + stuff.elementAt(1));

Page 27: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics

• However, very often we want to work with homogeneous collections: lists of Cars, for example.

• If we know that everything in a Vector belongs to a particular class (i.e., the Vector is in fact homogeneous), we can use a cast to stop the compiler generating a type-error (we get a run-time error if we’re wrong).

Page 28: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics

• However, very often we want to work with homogeneous collections: lists of Cars, for example.

• If we know that everything in a Vector belongs to a particular class (i.e., the Vector is in fact homogeneous), we can use a cast to stop the compiler generating a type-error (we get a run-time error if we’re wrong).

Page 29: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Example (Pre-1.5 code)

Vector myCars = new Vector();... //fill myCars with Carsint len = myCars.size();for (int i = 0 ; i < len ; i++) {

System.out.println( ((Car)myCars.elementAt(i)).getName() );}

Page 30: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Example (Pre-1.5 code)

Vector myCars = new Vector();... //fill myCars with Carsint len = myCars.size();for (int i = 0 ; i < len ; i++) {

System.out.println( ((Car)myCars.elementAt(i)).getName() );}

Page 31: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Example (Pre-1.5 code)

Vector myCars = new Vector();... //fill myCars with Carsint len = myCars.size();for (int i = 0 ; i < len ; i++) {

System.out.println( ((Car)myCars.elementAt(i)).getName() );}

Page 32: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Example (Pre-1.5 code)

Vector myCars = new Vector();... //fill myCars with Carsint len = myCars.size();for (int i = 0 ; i < len ; i++) {

System.out.println( ((Car)myCars.elementAt(i)).getName() );}

Here, we use a cast to avoid a type-error.

Page 33: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics

• This is not very nice code: if we wrote the code for storing a list of Cars ourselves, our methods such as add or remove would explicitly state that they take a Car as parameter, or return a Car, and we have no need of casts in our code.

• Because homogeneous collections are so common in programming, Java introduced ‘Generics’ in Java 1.5.

• This allows programmers to declare that their collections are homogeneous (e.g., they are working with a Vector of Strings , or a Vector of Cars, etc.)

Page 34: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics

• This is not very nice code: if we wrote the code for storing a list of Cars ourselves, our methods such as add or remove would explicitly state that they take a Car as parameter, or return a Car, and we have no need of casts in our code.

• Because homogeneous collections are so common in programming, Java introduced ‘Generics’ in Java 1.5.

• This allows programmers to declare that their collections are homogeneous (e.g., they are working with a Vector of Strings , or a Vector of Cars, etc.)

Page 35: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics

• This is not very nice code: if we wrote the code for storing a list of Cars ourselves, our methods such as add or remove would explicitly state that they take a Car as parameter, or return a Car, and we have no need of casts in our code.

• Because homogeneous collections are so common in programming, Java introduced ‘Generics’ in Java 1.5.

• This allows programmers to declare that their collections are homogeneous (e.g., they are working with a Vector of Strings , or a Vector of Cars, etc.)

Page 36: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The main mechanism for this is Type Parameters.

Type parameters already exist in the array types.For example:

• Integer[] — arrays of Integers

• String[] — arrays of Strings

• Machine[] — arrays of Machines

• Car[] — arrays of Cars

Page 37: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The main mechanism for this is Type Parameters.

Type parameters already exist in the array types.For example:

• Integer[] — arrays of Integers

• String[] — arrays of Strings

• Machine[] — arrays of Machines

• Car[] — arrays of Cars

Page 38: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The main mechanism for this is Type Parameters.

Type parameters already exist in the array types.For example:

• Integer[] — arrays of Integers

• String[] — arrays of Strings

• Machine[] — arrays of Machines

• Car[] — arrays of Cars

Page 39: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The main mechanism for this is Type Parameters.

Type parameters already exist in the array types.For example:

• Integer[] — arrays of Integers

• String[] — arrays of Strings

• Machine[] — arrays of Machines

• Car[] — arrays of Cars

Page 40: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The main mechanism for this is Type Parameters.

Type parameters already exist in the array types.For example:

• Integer[] — arrays of Integers

• String[] — arrays of Strings

• Machine[] — arrays of Machines

• Car[] — arrays of Cars

Page 41: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The main mechanism for this is Type Parameters.

Type parameters already exist in the array types.For example:

• Integer[] — arrays of Integers

• String[] — arrays of Strings

• Machine[] — arrays of Machines

• Car[] — arrays of Cars

Page 42: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The main mechanism for this is Type Parameters.

Type parameters already exist in the array types.For example:

• Integer[] — arrays of Integers

• String[] — arrays of Strings

• Machine[] — arrays of Machines

• Car[] — arrays of Cars

Page 43: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The new (from Java 1.5) notation for type parameters uses angle brackets.

For example:

• Vector<Integer> — Vectors of Integers

• Vector<String> — Vectors of Strings

• Vector<Machine> — Vectors of Machines

• Vector<Car> — Vectors of Cars

Similarly: java.util.ArrayList<Car>, etc.

Page 44: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The new (from Java 1.5) notation for type parameters uses angle brackets.

For example:

• Vector<Integer> — Vectors of Integers

• Vector<String> — Vectors of Strings

• Vector<Machine> — Vectors of Machines

• Vector<Car> — Vectors of Cars

Similarly: java.util.ArrayList<Car>, etc.

Page 45: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The new (from Java 1.5) notation for type parameters uses angle brackets.

For example:

• Vector<Integer> — Vectors of Integers

• Vector<String> — Vectors of Strings

• Vector<Machine> — Vectors of Machines

• Vector<Car> — Vectors of Cars

Similarly: java.util.ArrayList<Car>, etc.

Page 46: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The new (from Java 1.5) notation for type parameters uses angle brackets.

For example:

• Vector<Integer> — Vectors of Integers

• Vector<String> — Vectors of Strings

• Vector<Machine> — Vectors of Machines

• Vector<Car> — Vectors of Cars

Similarly: java.util.ArrayList<Car>, etc.

Page 47: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The new (from Java 1.5) notation for type parameters uses angle brackets.

For example:

• Vector<Integer> — Vectors of Integers

• Vector<String> — Vectors of Strings

• Vector<Machine> — Vectors of Machines

• Vector<Car> — Vectors of Cars

Similarly: java.util.ArrayList<Car>, etc.

Page 48: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The new (from Java 1.5) notation for type parameters uses angle brackets.

For example:

• Vector<Integer> — Vectors of Integers

• Vector<String> — Vectors of Strings

• Vector<Machine> — Vectors of Machines

• Vector<Car> — Vectors of Cars

Similarly: java.util.ArrayList<Car>, etc.

Page 49: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

The new (from Java 1.5) notation for type parameters uses angle brackets.

For example:

• Vector<Integer> — Vectors of Integers

• Vector<String> — Vectors of Strings

• Vector<Machine> — Vectors of Machines

• Vector<Car> — Vectors of Cars

Similarly: java.util.ArrayList<Car>, etc.

Page 50: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

Vector<Car> myCars = new Vector<Car>();myCars.add(new Car(“Audi”, “R8”, 2016));myCars.add(new Car(“Mercedes”, “SL”, 2014));System.out.print( myCars.elementAt(0).getName() );myCars.add( new Integer(8) ); // compiler type error

// not a Car

post-1.5 code

Page 51: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

Vector<Car> myCars = new Vector<Car>();myCars.add(new Car(“Audi”, “R8”, 2016));myCars.add(new Car(“Mercedes”, “SL”, 2014));System.out.print( myCars.elementAt(0).getName() );myCars.add( new Integer(8) ); // compiler type error

// not a Car

Page 52: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

Vector<Car> myCars = new Vector<Car>();myCars.add(new Car(“Audi”, “R8”, 2016));myCars.add(new Car(“Mercedes”, “SL”, 2014));System.out.print( myCars.elementAt(0).getName() );myCars.add( new Integer(8) ); // compiler type error

// not a Car

Page 53: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Generics: Type Parameters

Vector<Car> myCars = new Vector<Car>();myCars.add(new Car(“Audi”, “R8”, 2016));myCars.add(new Car(“Mercedes”, “SL”, 2014));System.out.print( myCars.elementAt(0).getName() );myCars.add( new Integer(8) ); // compiler type error

// not a Car

Page 54: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

E is a Type Parameter

• The E in Vector<E> is a formal Type Parameter, i.e., it stands as a ‘placeholder’ for whatever class is given as an actual Type Parameter.

• Formal and actual parameters are already familiar from methods ...

Page 55: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

E is a Type Parameter

• The E in Vector<E> is a formal Type Parameter, i.e., it stands as a ‘placeholder’ for whatever class is given as an actual Type Parameter.

• Formal and actual parameters are already familiar from methods ...

Page 56: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal/actual method parameters

• Formal parameters are the ‘placeholders’ declared in method signatures (in the parameter list).

• Actual parameters are the values used in method calls (‘invocations’).

public void push(int v) {v = ;values[++pointer] = v;

}

Stack st =new Stack();int i = 2;st.push(12);st.push();

Page 57: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal/actual method parameters

• Formal parameters are the ‘placeholders’ declared in method signatures (in the parameter list).

• Actual parameters are the values used in method calls (‘invocations’).

public void push(int v) {v = ;values[++pointer] = v;

}

Stack st =new Stack();int i = 2;st.push(12);st.push();

Page 58: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal/actual method parameters

• Formal parameters are the ‘placeholders’ declared in method signatures (in the parameter list).

• Actual parameters are the values used in method calls (‘invocations’).

public void push(int v) {v = ;values[++pointer] = v;

}

Stack st =new Stack();int i = 2;st.push(12);st.push();

In class Stack

Page 59: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal/actual method parameters

• Formal parameters are the ‘placeholders’ declared in method signatures (in the parameter list).

• Actual parameters are the values used in method calls (‘invocations’).

Stack st =new Stack();int i = 2;st.push(12);st.push();

public void push(int v) {v = ;values[++pointer] = v;

}

Some other program

Page 60: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal/actual method parameters

• Formal parameters are the ‘placeholders’ declared in method signatures (in the parameter list).

• Actual parameters are the values used in method calls (‘invocations’).

Stack st =new Stack();int i = 2;st.push(12);st.push();

public void push(int v) {v = ;values[++pointer] = v;

}

Page 61: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal/actual method parameters

• Formal parameters are the ‘placeholders’ declared in method signatures (in the parameter list).

• Actual parameters are the values used in method calls (‘invocations’).

Stack st =new Stack();int i = 2;st.push(12);st.push();

public void push(int v) {v = 12;values[++pointer] = v;

}

This is how parameter-passing is implemented

Page 62: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal/actual method parameters

• Formal parameters are the ‘placeholders’ declared in method signatures (in the parameter list).

• Actual parameters are the values used in method calls (‘invocations’).

Stack st =new Stack();int i = 2;st.push(12);st.push();

public void push(int v) {v = 12;values[++pointer] = 12;

}

Page 63: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal/actual method parameters

• Formal parameters are the ‘placeholders’ declared in method signatures (in the parameter list).

• Actual parameters are the values used in method calls (‘invocations’).

Stack st =new Stack();int i = 2;st.push(12);st.push(24+i);

public void push(int v) {v = ;values[++pointer] = v;

}

Page 64: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal/actual method parameters

• Formal parameters are the ‘placeholders’ declared in method signatures (in the parameter list).

• Actual parameters are the values used in method calls (‘invocations’).

Stack st =new Stack();int i = 2;st.push(12);st.push(24+i);

public void push(int v) {v = ;values[++pointer] = v;

}

Page 65: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal/actual method parameters

• Formal parameters are the ‘placeholders’ declared in method signatures (in the parameter list).

• Actual parameters are the values used in method calls (‘invocations’).

Stack st =new Stack();int i = 2;st.push(12);st.push(26);

public void push(int v) {v = 26;values[++pointer] = 26;

}

Page 66: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal parameters

• In methods, formal parameters always have a type.

• Each formal parameter is a variable of its declared type......anything you can do with actual values of that type, you can do with the formal parameter

// some methods we have seenpublic void push( int v ) { ...}public void add( Car car1 ) { ...}

Page 67: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal parameters

• In methods, formal parameters always have a type.

• Each formal parameter is a variable of its declared type......anything you can do with actual values of that type, you can do with the formal parameter

// some methods we have seenpublic void push( int v ) { ...}public void add( Car car1 ) { ...}

Page 68: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal parameters

• In methods, formal parameters always have a type.

• Each formal parameter is a variable of its declared type......anything you can do with actual values of that type, you can do with the formal parameter

// some methods we have seenpublic void push( int v ) { ...}public void add( Car car1 ) { ...}

Page 69: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal parameters

• In methods, formal parameters always have a type.

• Each formal parameter is a variable of its declared type......anything you can do with actual values of that type, you can do with the formal parameter

// some methods we have seenpublic void push( int v ) { ...}public void add( Car car1 ) { ...}

Page 70: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal parameter types

For example, here’s a not very useful method using a Point:

public static void pointless(Point p) {int x = p.getX(); // x == 2p.move(0,x)

}

Point myPoint = new Point(2,4);Point.pointless(myPoint);System.out.print("yCoord of my Point is: " + myPoint.yCoord);

Add this method to the class Point (that we have seen in previous lectures).

Page 71: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal parameter types

For example, here’s a not very useful method using a Point:

public static void pointless(Point p) {int x = p.getX(); // x == 2p.move(0,x)

}

Point myPoint = new Point(2,4);Point.pointless(myPoint);System.out.print("yCoord of my Point is: " + myPoint.yCoord);

Page 72: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal parameter types

For example, here’s a not very useful method using a Point:

public static void pointless(Point p) {int x = p.getX(); // x == 2p.move(0,x)

}

Point myPoint = new Point(2,4);Point.pointless(myPoint);System.out.print("yCoord of my Point is: " + myPoint.yCoord);

Add this to your main method.

Page 73: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal parameter types

For example, here’s a not very useful method using a Point:

public static void pointless(Point p) {int x = p.getX(); // x == 2p.move(0,x)

}

Point myPoint = new Point(2,4);Point.pointless(myPoint);System.out.print("yCoord of my Point is: " + myPoint.yCoord);

Page 74: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal parameter types

For example, here’s a not very useful method using a Point:

public static void pointless(Point p) {int x = p.getX(); // x == 2p.move(0,x)

}

Point myPoint = new Point(2,4);Point.pointless(myPoint);System.out.print("yCoord of my Point is: " + myPoint.yCoord);

Page 75: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal parameter types

For example, here’s a not very useful method using a Point:

public static void pointless(Point p) {int x = p.getX(); // x == 2p.move(0,x)

}

Point myPoint = new Point(2,4);Point.pointless(myPoint);System.out.print("yCoord of my Point is: " + myPoint.yCoord);

Page 76: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal parameter types

For example, here’s a not very useful method using a Point:

public static void pointless(Point p) {int x = p.getX(); // x == 2p.move(0,x)

}

Point myPoint = new Point(2,4);Point.pointless(myPoint);System.out.print("yCoord of my Point is: " + myPoint.yCoord);

We’d expect to see: “yCoord of my Point is: 6”

Page 77: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal parameter types

For example, here’s a not very useful method using a Point:

public static void pointless(Point p) {int x = p.getX(); // x == 2p.move(0,x)

}

Point myPoint = new Point(2,4);Point.pointless(myPoint);System.out.print("yCoord of my Point is: " + myPoint.yCoord);

We’d expect to see: “yCoord of my Point is: 6”

Page 78: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal/actual method parameters

E is a formal type parameter when the class is declared.

public class Vector<E> {...public void addElement(E o) {

...}public E elementAt(int i) {

...}...

}

Page 79: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

formal/actual method parameters

When given an actual parameter, e.g., Vector<Car>, the actual parameter replaces the formal parameter, so the class looks like this:

public class Vector<Car> {...public void addElement(Car o) {

...}public Car elementAt(int i) {

...}...

}Note that this replacement continues even

within the method bodies.

Page 80: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Class Vector<E>

public class Vector<E> {public Vector() {

E[] elementData = ...;}public E elementAt(int i) {

E theElement = elementData[i];...

}...

}

Page 81: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Class Vector<Car>

public class Vector<Car> {public Vector() {

Car[] elementData = ...;}public Car elementAt(int i) {

Car theElement = elementData[i];...

}...

}

Page 82: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Notes

• The constructor does not take a type parameter when declared:

public Vector() {...

}

• The constructor does take an actual parameter when it is called:

Vector<Car> myCars = new Vector<Car>();

Page 83: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Notes

• The constructor does not take a type parameter when declared:

public Vector() {...

}

• The constructor does take an actual parameter when it is called:

In class Vector.

Vector<Car> myCars = new Vector<Car>();

Page 84: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Notes

• The constructor does not take a type parameter when declared:

public Vector() {...

}

• The constructor does take an actual parameter when it is called:

Vector<Car> myCars = new Vector<Car>();

In class Vector.

Page 85: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Notes

• The constructor does not take a type parameter when declared:

public Vector() {...

}

• The constructor does take an actual parameter when it is called:

Vector<Car> myCars = new Vector<Car>();

In class Vector.

Using class Vector.

Page 86: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Back to our List implementation

• Our implementations of lists could have been written more generally. E.g., for the unsorted list:

public interface ListInterface<E> {int size();void add(E element);boolean contains(E element);boolean remove(E element);E get(E element);String toString();void reset();E getNext();

}

Page 87: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Back to our List implementation• Our implementations of lists could have been written more generally. E.g., for the unsorted list:

public class ArrayUnsortedList<E> implements ListInterface<E> {protected final int DEFCAP = 100; // default capacityprotected E[] list; // array that holds list's elementsprotected int origiCap; // original capacityprotected int numElements = 0; // number of elements in this list...public ArrayUnsortedList() {

list = (E[]) new Object[DEFCAP];origiCap = DEFCAP;

}public ArrayUnsortedList(int origiCap) {

list = (E[]) new Object[origiCap];this.origiCap = origiCap;

}public int size() {

return numElements;}...

Page 88: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Back to our List implementation• Our implementations of lists could have been written more generally. E.g., for the unsorted list:

public class ArrayUnsortedList<E> implements ListInterface<E> {protected final int DEFCAP = 100; // default capacityprotected E[] list; // array that holds list's elementsprotected int origiCap; // original capacityprotected int numElements = 0; // number of elements in this list...public ArrayUnsortedList() {

list = (E[]) new Object[DEFCAP];origiCap = DEFCAP;

}public ArrayUnsortedList(int origiCap) {

list = (E[]) new Object[origiCap];this.origiCap = origiCap;

}public int size() {

return numElements;}...

Page 89: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Back to our List implementation• Our implementations of lists could have been written more generally. E.g., for the unsorted list:

public class ArrayUnsortedList<E> implements ListInterface<E> {protected final int DEFCAP = 100; // default capacityprotected E[] list; // array that holds list's elementsprotected int origiCap; // original capacityprotected int numElements = 0; // number of elements in this list...public ArrayUnsortedList() {

list = (E[]) new Object[DEFCAP];origiCap = DEFCAP;

}public ArrayUnsortedList(int origiCap) {

list = (E[]) new Object[origiCap];this.origiCap = origiCap;

}public int size() {

return numElements;}...

An array contains, at runtime, information about its component type. So you must know the component type when you create the array.

Since you don't know what E is at runtime, you can't create the array. So:

E[] someArray = new E[n];

is replaced by:

E[] someArray = (E[]) new Object[n];

Page 90: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Back to our List implementation• Our implementations of lists could have been written more generally. E.g., for the unsorted list:

public class Main {public static void main(String[] args) {

ArrayUnsortedList<String> list = new ArrayUnsortedList<String>(3);list.add("Wirth");list.add("Dykstra");

// Instead of doing:// =================================// String line;// line = (String) list.get("Wirth");// =================================// we no longer need to use "cast":String line;line = list.get("Wirth");System.out.println(line);

}}

Page 91: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Back to our List implementation• Our implementations of lists could have been written more generally. E.g., for the unsorted list:

public class Main {public static void main(String[] args) {

ArrayUnsortedList<String> list = new ArrayUnsortedList<String>(3);list.add("Wirth");list.add("Dykstra");

// Instead of doing:// =================================// String line;// line = (String) list.get("Wirth");// =================================// we no longer need to use "cast":String line;line = list.get("Wirth");System.out.println(line);

}}

Page 92: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Back to our List implementation• Our implementations of lists could have been written more generally. E.g., for the unsorted list:

public class Main {public static void main(String[] args) {

ArrayUnsortedList<String> list = new ArrayUnsortedList<String>(3);list.add("Wirth");list.add("Dykstra");

// Instead of doing:// =================================// String line;// line = (String) list.get("Wirth");// =================================// we no longer need to use "cast":String line;line = list.get("Wirth");System.out.println(line);

}}

Page 93: Advanced Object-oriented Programming Lecture 10akridel/COMP213-Fall2016... · Packages • Package java.awt (Abstract Windowing Toolkit) provides many useful heavyweight∗ classes

Summary

• Class Vector

• Type parameters

• Formal/actual parameters

• Next: Documentation