Top Banner
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann
24

CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

CS 106Introduction to Computer Science I

04 / 30 / 2010

Instructor: Michael Eckmann

Page 2: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 106 - Spring 2010

Today’s Topics• Comments and/or Questions?• Comparable interface• ArrayList class• linked lists

Page 3: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Interfaces• An interface is a collection of constants and abstract methods.

–An abstract method is one that is defined by a header (signature) but no body. An abstract method, when defined in an interface, does not provide an implementation.

• An interface can never be instantiated. What does that mean again?

• The purpose of interfaces are to formally define the ways in which we can interact with a class (that implements the interface).

Page 4: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Interfaces• We can create our own interfaces (and we will later).

• We can also use interfaces in the Java API.

Page 5: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Interfaces• There are many interfaces in the Java API available for us to

use.

• One of these is the interface Comparable

• It contains one abstract method:

– int compareTo(Object object_to_be_compared )

– It is meant to return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Negative means <, Positive means > and 0 means equal.

• It is up to a class that implements the Comparable interface to determine what it means for its objects to be <, > or = to each other. That is specified in the implementation of the method.

Page 6: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Interfaces• Do we recall using a compareTo method in the past? In what

class?

• Example use of Comparable

• int compareTo(Object object_to_be_compared )

• Let's edit our Card class to implement Comparable.

• Will we have to add a compareTo method?

Page 7: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Interfaces• Example use of Comparable

• int compareTo( object_to_be_compared )

• Let's edit our Card class to implement Comparable.

• Will we have to add a compareTo method?

–Yes! If the class implements Comparable it must provide implementation of ALL the abstract methods of the interface.

• What might our compareTo method do in the Card class?

Page 8: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Interfaces• int compareTo( object_to_be_compared )

• Let's edit our Card class to implement Comparable.

• Will we have to add a compareTo method?

–Yes! If the class implements Comparable it must provide implementation of ALL the abstract methods of the interface.

• What might our compareTo method do in the Card class?

– It should return a negative # if the “calling” Card object is less than the Card object that is passed in as a parameter.

– It should return 0 if the Card objects are equal

–Otherwise return a positive number.

Page 9: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Interfaces• The Card class has rank and suit as instance variables.

• Our compareTo method will look like:

public int compareTo(Object o)

{

// how do we refer to the values

// associated with the “calling” Card object?

// how do we refer to the values

// associated with the parameter object?

}

Page 10: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Interfaces• Let's revisit the bubbleSort method that sorted ints and make it

sort Comparables.

• Why?

Page 11: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Interfaces• Let's revisit the bubbleSort method that sorted an array of ints

and make it sort an array of Comparables.

• Why?– because it will be more flexible --- it will be able to sort an

array of objects of any class that implements the Comparable interface

Page 12: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

ArrayList• This is a good time to bring up a class in Java API named

ArrayList, because it works on general Objects.

• ArrayList is a class in the Java API

• It can store different types of data into an “array”

• An ArrayList can change size throughout the lifetime of the program

– whereas a regular array is a fixed size

• An ArrayList actually stores references to objects, not the objects themselves

• We cannot store values of primitive types directly -- we have to use the wrapper classes if we want to do that (we'll see what this means later).

Page 13: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

ArrayList• Some ArrayList methods

– ArrayList() --- constructor

– boolean add(Object o) – add the object reference to the end of the list

– void add(int index, Object o) – add the object reference to the list at the index

– void clear() -- remove all elements from the list

– Object get(int index) – return the object reference at that index in the list

– int indexOf(Object o) – returns the index of the first occurrence of o

– boolean isEmpty() -- returns true if the list contains no elements, false otherwise

Page 14: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

ArrayList• Some ArrayList methods

– Object remove(int index) – returns the object reference at that index in the list and removes it from the list

– int size() – returns the number of elements in the list

– boolean contains(Object o) -- returns true if the list contains o, false otherwise

– void ensureCapacity(int minCapacity) – tells Java to ensure that the list will be able to contain at least minCapacity references

– int lastIndexOf(Object o) – what do you think this does?

– boolean remove(Object o) -- removes a single instance of o from the list

– void removeRange(int fromIndex, int toIndex) --removes from the list all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.

Page 15: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

ArrayList• Some ArrayList methods

– Object set(int index, Object o) -- replaces the element at the specified index in this list with o.

– What if this method wasn't in the class. How might you achieve that functionality?

Page 16: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

ArrayList• Let's create an ArrayList of different types and add some elements to it

and remove some, etc...

• When we create one that allows different types, we have to cast any returned Objects from methods to be the type we want.

• Because Java implemented ArrayLists generically, and because of the way Java decided to have the class hierarchy such that all classes have Object as their superclass, this usefulness of ArrayLists is dramatically increased over a class that may have only allowed one type for its elements.

• So, that's a large benefit of good object oriented design --- generality --- so that you (or the Java implementers themselves) don't have to implement multiple classes to work on multiple types.

• If you create your class hierarchy well, lots of stuff can be done generically (e.g. Have code that works on all Pets instead of just Dog or Cat objects for instance.)

Page 17: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

ArrayList• If we stored several different types of data in a list, we may not know at

some point what type of object is actually stored in a particular index of the ArrayList.

• How do we find out what type a particular object actually is?

Page 18: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

ArrayList• If we stored several different types of data in a list, we may not know at

some point what type of object is actually stored in a particular index of the ArrayList.

• How do we find out what type a particular object actually is?

• We can use the instanceof operator to check if some reference is an instance of some type.

e.g.

Object o = somearraylist.get(0);

if (o instanceof Contact)

{

//then cast o to be a Contact and use it

}

Page 19: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

ArrayList• Let's call some of the methods and see what they return or how they affect

the list.

ArrayList() , boolean add(Object o) ,

void add(int index, Object o) , void clear() , Object get(int index) ,

int indexOf(Object o) , boolean isEmpty() ,

Object remove(int index) , int size() , boolean contains(Object o) ,

void ensureCapacity(int minCapacity) ,

int lastIndexOf(Object o) , boolean remove(Object o) ,

protected void removeRange(int fromIndex, int toIndex)

Page 20: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Polymorphism via Inheriance• An ArrayList uses polymorphism because it holds

references to objects of Object.

• An Object reference can be used to refer to any object (of any type)!!!

Page 21: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Linked lists• A linked list is a data structure where every node contains data

and reference(s) to other node(s.)

• We, as programmers create linked lists by “attaching” node references to each other.

Page 22: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Linked lists • In a singly linked list every node contains data and one

reference to another node.

– e.g.

class Node

{

public AnyType data; // can have more data than one

public Node next;

}

• A Linked List is maintained by keeping a reference to the head of the list.

• From the head, we are able to get at all the other nodes in the list by following the next reference.

Page 23: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Linked lists class Node

{

public AnyType data; // can have more data than one

public Node next;

// when constructing a new node, we set its data and

// set the next reference to null

public Node(AnyType d)

{

data = d;

next = null;

}

}

Page 24: CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.

Linked lists Let me draw a representation of a node on the board and the

connection of that node to other nodes.

Recall that a reference holds an address and when we want to visualize what is happening, we draw an arrow from the reference to the data at the address stored in the reference.