CS 367 – Lecture 2:  Abstract Data Types, Lists

Announcements: 

mailing list – will be active tomorrow morning; I’ll send test message

pictures – keep sending

Unix orientations – last one today

log on to instructional machines, try to copy files to handin directories,

handouts on jdb (Quick Ref, tutorial); try it out together after class today

homework #1 is now available on the course web page; due 1/27

(bookmark www.cs.wisc.edu/~cs367-3)

for Tuesday:  print and read notes on exceptions, read text chapter 5

 

What makes a program good?

1.    It works (as specified)

2.    It’s easy to understand & modify & reuse

3.    It’s reasonably efficient

4.    Good user interface

To help achieve #2:  use abstraction, modularity, information hiding

          i.e., Abstract Data Type 

- separate conceptual object and operations

          from actual implementation

          benefits: 

1.    program is easier to understand (“high level” steps performed are not obscured by “low-level” code)

2.    implementation of ADT can be changed (e.g., for efficiency) without requiring changes to the program that uses the ADT operations

3.    ADT is reusable

Two parts to each ADT:

1.    public or external part:  precise + explanation

conceptual picture, user’s view of what the object is, & operations

in Java:  public methods  (sometimes public fields)

2.    private or internal part

actual representation (how the data is actually stored)

implementation of the operations (actual algorithms used)

in Java:  private fields, private methods, actual code

ADT operations often fall into the following categories:

1.    initialize

2.    add data

3.    access data

4.    remove data

 

 

Read Announcements HERE

 

Our first ADT:  the list – an ordered collection of items

External view: 

picture of a list: item0, item2, …, itemN-1;   //NOTE: 1st item is #0  

          Operations:  initialize – create an empty list

                   add data – add a given item to the end of the list

                   add data #2 – add a given item at a given position to the list

                   get data – get item at a given position

(error if no such position)

                   remove data – remove the item at a given position

ASK:  What other operations might be useful???

          sort

          contains – is a given item in the list?

          isEmpty

          size – return the number of items in the list

 

Note:  can define other operations (in addition or instead)

e.g., “location: returns position of given object)

          Goal:  provide a set of operations for ADT to be useful in many

             contexts, not so many that it’s hard to remember the operations

-         it’s not always easy to find the right balance

How to implement lists?

·       Java class is a good match with ADT  (ask why)

Public method headers = external part

Method bodies, private methods & fields = internal part

·       Could define one class for each kind of list item

class IntList {}

class StringList {}

etc.

-         leads to too much duplication of code

-         instead, define one class List {} for items of type Object

& use it to store any non-primitive type

·       We will consider to ways to implement Lists

- use and array to store the list

- use a linked list to store the list

Note:  we are essentially talking about how to implement Java’s ArrayList and LinkedList classes

ArrayList   

Class List {

// fields – for ADT, almost always want the fields to be private

private Object [] items;    // “items” is a pointer to an array of pointers

private int numItems;       // “numItems” holds the length of the list

 

·       length of “items” is usually larger than numItems; draw picture

(when do we want public fields? 

     e.g.,  public class Point {

                       public int x, y;

              }

work in pairs to write the headers – parameters, return types – for

          two versions of add

get

          remove

          size

          contains

if you finish early, write the code for the first version of add

 

results:

     public void add (Object ob)

     public void add(int pos, Object ob) //pos = position, starting from 0

     public Object get (int pos)

     public void remove (int pos)

     public int size()

     public boolean contains (Object ob)

 

How to implement the Add #1 method???

-         By hand: changes to picture of items & numItems

 

// basic code

Public void add(Object ob) {

items[numItems] = ob;

          numItems++;

 

// unusual cases:  what if array is full? 

// must get a new larger array, copy items to the new array, then add ob

if (numItems == items.length)

          expandArray();   // double the size, implementation next time

 

 

 

“One minute paper”:

          most interesting thing we talked about this week

          things you especially liked about the lectures

          things you didn’t like as much, would want to be different

          don’t put your name on the paper, turn it in on your way out