Data Organization in Real Life

A lot of things in real life can be implemented with certain data structures.

Computer Data Organization

Abstract Data Type:

\text{ADT in Java} = \text{Interface} + \text{Class}

Data Structure:

Collection:

Examples: Containers

Design Decisions

What do you do for unusual conditions?

Example: Throwing an exception

throw new Exception("Exception message");

Observations about Vending Machines

Bag Abstract Data Type

Definition:

Operations:

Metaphor: Bag of groceries

CRC (Class-Responsibility-Collaboration) Card

Responsibilities:

Collaboration:

Interface UML

BagInterfacegetCurrentSize() : integerisEmpty() : booleanadd(newEntry: T) : booleanremove() : Tremove(anEntry : T) : booleanclear() : voidgetFrequencyOf(anEntry : T): integercontains(anEntry : T) : booleantoArray() : T[]

Example Interface

Remember: The interface is wholly interface-independent. No constructor, no fields, no comments about any implementation details.

Interface:

// "T" is a generic type parameter
// - (So the bag can store any type of object)
public interface BagInterface<T> {
    int getCurrentSize();
    boolean isEmpty();
    boolean add(T newEntry);
    boolean remove();
    boolean remove(T anEntry);
    void clear();
    integer getFrequencyOf(T anEntry);
    boolean contains(T anEntry);
    T[] toArray();
}

Implementation:

public class Coin implements BagInterface {
    // we'll cover implementation after learning generics
}

Client:

public class PiggyBank {
    private BagInterface<Coin> coins;
    public PiggyBank() {
        this.coins = new Bag<>();
    }
    public boolean add(Coin aCoin) {
        return coins.add(aCoin):
    }
    public Coin remove() {
        return coins.remove();
    }
    public boolean isEmpty() {
        return coins.isEmpty();
    }
}

Example Use:

public class Client {
    public static void main(String[] args) {
        PiggyBank bank = new PiggyBank();
    }
}

Observations about Bag ADT

Set Interface

public interface SetInterface<T> {
    int getCurrentSize();
    boolean isEmpty();
    boolean add (T newEntry);
    boolean remove (T anEntry);
    T remove();
    void clear();
    boolean contains(T anEntry);
    T[] toArray();
    // etc...
}