CSE116 / CSE504 Introduction to Computer Science II Dr. Carl Alphonce

advertisement
CSE116 / CSE504
Introduction to Computer Science II
Dr. Carl Alphonce
343 Davis Hall
alphonce@buffalo.edu
Office hours:
Thursday 12:00 PM – 2:00 PM
Friday 8:30 AM – 10:30 AM
OR request appointment via e-mail
PROFESSIONALISM
Turn off and put away electronics:
cell phones
pagers
laptops
tablets
etc.
© Dr. Carl Alphonce
ANNOUNCEMENTS
Recitations
Start this week!
Clicker questions
They count this week!
Sign up for Piazza (up to 85% registration!)
piazza.com/buffalo/spring2016/cse116
Practice problems
Posted on website over weekend.
ROADMAP
Today
ACM student club visit
Test-Driven Development (TDD)
Writing tests
Coming up
Running JUnit
Writing implementation code
MultiSet
ACM MEETING
Association for Computing Machinery
http://ubacm.org
next meeting
Wednesday, Feb 3
7:00 PM
Davis Hall, 2nd floor atrium
FREE PIZZA!
© Dr. Carl Alphonce
exercise
Define the method to reverse a String.
Work with your neighbors to solve this
problem.
Let’s run
our JUnit
tests!
HINTS
String objects are immutable
charAt(int) à char
length() à int
Abstract Data Types (ADTs)
An ADT consists of:
a set of values, and
a set of operations on those values.
Example: rational numbers
some values: 15/7, -3/4, 123/1, 0/1 (but NOT 1/0 !)
some operations: addition, multiplication, negation
An ADT therefore specifies:
what the members are, and
what operations are supported.
What an ADT isn’t:
An ADT does not specify:
how the data is stored/represented, or
how the operations are implemented.
These details are abstracted away.
An ADT is implementation independent
An ADT is language independent.
In Java, an ADT is typically specified in an interface.
Data Structure (DS)
A data structure is an implementation of an ADT.
In Java, data structures are classes.
In the Java Collections framework, the List interface
specifies an ADT that is implemented by several
data structures:
ArrayList (an array-based structure)
LinkedList (a linked structure)
MultiSet<E>, a.k.a. Bag<E>
Different Collection<E> classes
HashSet<E>
Allows
duplicates
Exposes order
MultiSet<E> ArrayList<E>
No
Yes
Yes
No
No
Yes
MultiSet<E> implements Collection<E>
methods of interest
boolean add(E item)
boolean remove(Object item)
boolean contains(Object item)
int size()
MultiSet Operations
Basic operations:
add (client has no control over placement; returns a
boolean, multiple references to same object are
permitted)
remove (based on .equals(…) method; returns a boolean)
contains (membership test, based on .equals(…) method;
returns a boolean)
size (how many values are in a given bag)
MultiSet ADT
Values: objects (unordered).
We use generics to define a parameterized type.
The type of the members of the ADT is specified by
a type parameter in the definition of the interface.
Exercise
Define a test to verify that a MultiSet<E> can
contain duplicate items.
Download