Download presentation
Presentation is loading. Please wait.
1
Abstract Data Types (ADTs) An ADT consists of: –a set of values –a set of operations on those values Rational numbers –15/7, -3/4, 123/1, 0/1 (but NOT 1/0 !) –addition, multiplication, negation, reciprocal
2
Data Structure (DS) A data structure is an implementation of an ADT In Java Collections framework, the List ADT is implemented by several data structures: –ArrayList (an array-based structure) –LinkedList (a linked structure)
3
ADTs Bag – unordered collection, allowing duplicates Set – unordered collection, no duplicates List – ordered collection – client determines order Sorted list – comparator determines ordering Stack – last-in first-out (insert/remove from front) Queue – first-in first-out (insert back, remove front) Tree – hierarchical organization Binary Search Tree – comparator determines ordering Dictionary – key-value pairs Graph – vertices and arcs
4
Bag ADT values: object references (unordered) basic operations: –insert (client has no control over placement) –remove (based on object identity) –find (membership test) common operations –size (how many values are in a given bag) a simple but often useful ADT
5
insert Given an object reference R and a bag B, after B.insert(R), B.find(R) must return true. Size of B must be one more after an insertion than the size of B immediately prior to insertion. B.insert(R) may fail if memory to store R in B cannot be allocated B.insert(R) should not fail otherwise (duplicates are allowed)
6
Unit tests for ‘insert’? REQUIREMENT: Given an object reference R and a bag B, after B.insert(R), B.find(R) must return true. UNIT TEST: Bag b = new Bag(); SomeType r = new SomeType(); b.insert(r); assertTrue(r+“ should be in ”+b+“ but isn’t!”, b.find(r));
7
Unit tests for ‘insert’? REQUIREMENT: Size of B must be one more after an insertion than the size of B immediately prior to insertion. UNIT TEST: Bag b = new Bag(); SomeType r = new SomeType(); int sizeBefore = b.size(); b.insert(r); int sizeAfter = b.size(); assertTrue(sizeAfter+“ should be ”+sizeBefore+“+1, but isn’t!”, sizeAfter==sizeBefore+1);
8
Unit tests for ‘insert’ / ‘remove’ / ‘find’ / ‘size’ / other operations? Left as exercise for you!
9
Let’s look at some code… Unit tests for Bag Exception handling (continued from Friday)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.