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
Sign up for Piazza (up to 90% registration!)
piazza.com/buffalo/spring2016/cse116
Recitation
Attendance for full recitation time is expected
ACM meeting
Tonight @ 7:00 PM in Davis Hall, 2nd floor atrium
ROADMAP
Today
Writing tests
Running JUnit
MultiSet
Coming up
Arrays
Defining the MultiSet
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
Exercise
Define a test to verify that a MultiSet<E> can
contain duplicate items.
channel 1
WhatisacorrectorderforthesestatementstomakeaJUnit test?
1)
2)
3)
4)
5)
assertTrue("...",expected == actual);
int expected = 2;
ms.add(s); ms.add(s); int actual = ms.size();
MultiSet<String> ms = new MultiSet<String>();
String s = "River";
A.
B.
C.
D.
4,2,5,3,1
5,3,2,4,1
2,1,4,3,5
5,4,3,1,2
WhatisacorrectorderforthesestatementstomakeaJUnit test?
1)
2)
3)
4)
5)
assertTrue("...",expected == actual);
int expected = 2;
ms.add(s); ms.add(s); int actual = ms.size();
MultiSet<String> ms = new MultiSet<String>();
String s = "River";
A.
B.
C.
D.
4,2,5,3,1
5,3,2,4,1
2,1,4,3,5
5,4,3,1,2
WhatisacorrectorderforthesestatementstomakeaJUnit test?
1)
2)
3)
4)
5)
assertTrue("...",expected == actual);
int expected = 2;
ms.add(s); ms.add(s); int actual = ms.size();
MultiSet<String> ms = new MultiSet<String>();
String s = "River";
A.
B.
C.
D.
4,2,5,3,1
5,3,2,4,1
2,1,4,3,5
5,4,3,1,2
Implementation options
There are two basic approaches:
array-based
linked
We will consider array-based implementation first
Use a TDD approach: first consider what the
essential functionality of a given ADT is.
Collection interface
We’ll set up our MultiSet implementation to
implement the java.util.Collection interface.
Method signatures come from this interface.
We will not look at all the required methods. To
start with we focus on these four:
add, remove, contains and size
Question
What do you know about arrays?
An array is a collection of variables, all of the
same type.
Arrays
An array is created with the new operator.
The size of an array must be specified at
creation time.
The memory for an array is allocated in a
contiguous block.
The size of an array is fixed.
Array indexing
A variable in an array is accessed using
special syntax.
Suppose String[] x = new String[4];
Variables: x[0], x[1], x[2] and x[3]
Valid index ranges from 0 to x.length-1.
Memory organization and usage
A computer’s memory is, at its lowest level,
composed of binary digits (bits).
The memory is organized into bytes, which are
groups of eight bits.
Each byte has a unique address in memory.
A byte is the smallest addressable unit of memory
(i.e. nothing smaller than a byte has its own address)
Memory organization
Process A
Process B
Process C
Memory organization
Process A
STATIC
SEGMENT
RUNTIME
STACK
Process B
FREE/AVAILABLE
MEMORY
dynamically allocated memory
Process C
HEAP
Memory organization
Table at right shows
16 bytes, each
consisting of 8 bits
Each byte has an
address, shown in the
column to the left
21380000
21380001
21380002
21380003
21380004
21380005
21380006
21380007
21380008
21380009
21380010
21380011
21380012
21380013
21380014
21380015
A collection of variables, all of the same type.
(same type, and therefore same size)
Arrays
Each variable in an array is accessed by an index.
An array of size n has indices from 0 to n-1.
An array occupies a contiguous block of memory.
The size of an array is fixed at creation time.
Accessing an array member
t type of elements in array
s size (in bytes) of an element of type t
b base address of array
address of element i is
b+i*s
21380000
21380001
21380002
Array access example
21380003
In Java, an int occupies 4 bytes:
21380004
21380005
21380006
int [] a = new int[5];
21380007
21380008
21380009
The base address of ‘a’ is 21380002.
21380010
21380011
21380012
21380013
Indices are 0, 1, 2, 3 and 4.
21380014
21380015
21380016
Total size needed for array is 20
bytes (5 cells times 4 bytes per cell)
21380017
21380018
21380019
21380020
21380021
21380021
21380022
21380000
21380001
21380002
Array access example
21380003
Where is a[0]?
21380004
21380005
21380006
Address of a[0] is:
b+s*i
where b = 21380002, s = 4 and i = 0:
21380002 + 4 * 0 = 21380002
21380007
21380008
21380009
21380010
21380011
21380012
21380013
21380014
a[0] occupies bytes 21380002,
21380003, 21380004 and 21380005.
21380015
21380016
21380017
21380018
21380019
21380020
21380021
21380021
21380022
a[0]
21380000
21380001
21380002
Array access example
21380003
Where is a[3]?
21380004
21380005
21380006
Address of a[3] is:
b+s*i
where b = 21380002, s = 4 and i = 3:
21380002 + 4 * 3 = 21380014
21380007
21380008
21380009
21380010
21380011
21380012
21380013
21380014
a[3] occupies bytes 21380014,
21380015, 21380016 and 21380017.
21380015
21380016
21380017
21380018
21380019
21380020
21380021
21380021
21380022
a[3]
Defining the MultiSet
On to Eclipse!
Download