Sequence 7 January 2019 OSU CSE 1
Sequence • The Sequence component family allows you to manipulate strings of entries of any (arbitrary) type through direct access by position, similar to an array – Another generic type like Queue and Set – One possible best practice alternative to the built-in Java array, from the OSU CSE components 7 January 2019 OSU CSE 2
Interfaces and Classes Standard extends Sequence- Kernel extends Sequence implements implements Sequence1L Sequence3 Sequence2L 7 January 2019 OSU CSE 3
Interfaces and Classes Standard extends Standard has contracts Sequence- for three methods: Kernel clear newInstance extends transferFrom Sequence implements implements Sequence1L Sequence3 Sequence2L 7 January 2019 OSU CSE 4
Interfaces and Classes Standard extends Sequence- Kernel SequenceKernel has extends contracts for three Sequence methods: implements implements add remove length Sequence1L Sequence3 Sequence2L 7 January 2019 OSU CSE 5
Interfaces and Classes Sequence has contracts for six other methods: Standard entry replaceEntry extends append Sequence- flip Kernel insert extract extends Sequence implements implements Sequence1L Sequence3 Sequence2L 7 January 2019 OSU CSE 6
Mathematical Model • The value of a Sequence variable is modeled as a string of entries of type T • Formally: type Sequence is modeled by string of T 7 January 2019 OSU CSE 7
No-argument Constructor • Ensures: this = < > 7 January 2019 OSU CSE 8
Example Code State Sequence<Integer> si = new Sequence1L<>(); 7 January 2019 OSU CSE 9
Example Code State Sequence<Integer> si = new Sequence1L<>(); si = < > 7 January 2019 OSU CSE 10
add void add( int pos, T x) • Adds x at position pos of this . • Aliases: reference x • Updates: this • Requires: 0 <= pos and pos <= | this | • Ensures: this = # this [0, pos) * <x> * # this [pos, |# this |) 7 January 2019 OSU CSE 11
Example Code State si = < 49, 3 > z = 70 si.add(1, z); 7 January 2019 OSU CSE 12
Example Code State si = < 49, 3 > z = 70 si.add(1, z); si = < 49, 70, 3 > z = 70 7 January 2019 OSU CSE 13
Example Note the alias created Code State here, which you cannot see in the tracing table; si = < 49, 3 > you should be able to z = 70 draw the appropriate diagram showing it. si.add(1, z); si = < 49, 70, 3 > z = 70 7 January 2019 OSU CSE 14
remove T remove( int pos) • Removes and returns the entry at position pos of this . • Updates: this • Requires: 0 <= pos and pos < | this | • Ensures: this = # this [0, pos) * # this [pos+1, |# this |) and <remove> = # this [pos, pos+1) 7 January 2019 OSU CSE 15
Example Code State si = < 49, 3, 70 > z = –584 z = si.remove(1); 7 January 2019 OSU CSE 16
Example Code State si = < 49, 3, 70 > z = –584 z = si.remove(1); si = < 49, 70 > z = 3 7 January 2019 OSU CSE 17
length int length() • Reports the length of this . • Ensures: length = | this | 7 January 2019 OSU CSE 18
entry T entry( int pos) • Reports the entry at position pos of this . • Aliases: reference returned by entry • Requires: 0 <= pos and pos < | this | • Ensures: <entry> = this [pos, pos+1) 7 January 2019 OSU CSE 19
Example Code State si = < 49, 3, 70 > z = –584 z = si.entry(1); 7 January 2019 OSU CSE 20
Example Code State si = < 49, 3, 70 > z = –584 z = si.entry(1); si = < 49, 3, 70 > z = 3 7 January 2019 OSU CSE 21
Example Note the alias created Code State here, which you cannot see in the tracing table; si = < 49, 3, 70 > you should be able to z = –584 draw the appropriate diagram showing it. z = si.entryAt(1); si = < 49, 3, 70 > z = 3 7 January 2019 OSU CSE 22
replaceEntry T replaceEntry( int pos, T x) • Replaces the entry at position pos of this with x , and returns the old entry at that position. • Aliases: reference x • Updates: this • Requires: 0 <= pos and pos < | this | • Ensures: this = # this [0, pos) * <x> * # this [pos+1, |# this |) and <replaceEntry> = # this [pos, pos+1) 7 January 2019 OSU CSE 23
Example Code State si = < 49, 70 > z = –8 w = -584 w = si.replaceEntry(1, z); 7 January 2019 OSU CSE 24
Example Code State si = < 49, 70 > z = –8 w = -584 w = si.replaceEntry(1, z); si = < 49, -8 > z = –8 w = 70 7 January 2019 OSU CSE 25
Example Note the alias created Code State here, which you cannot see in the tracing table; si = < 49, 70 > you should be able to z = –8 w = -584 draw the appropriate diagram showing it. w = si.replaceEntryAt(1, z); si = < 49, -8 > z = –8 w = 70 7 January 2019 OSU CSE 26
Another Example Code State si = < 49, 70 > z = –8 z = si.replaceEntry(1, z); 7 January 2019 OSU CSE 27
Another Example Code State si = < 49, 70 > z = –8 z = si.replaceEntry(1, z); si = < 49, -8 > z = 70 7 January 2019 OSU CSE 28
Another Example This use of the method Code State avoids creating an alias: it swaps z with the entry si = < 49, 70 > previously at position 1. z = –8 z = si.replaceEntry(1, z); si = < 49, -8 > z = 70 7 January 2019 OSU CSE 29
append void append(Sequence<T> s) • Concatenates (“appends”) s to the end of this . • Updates: this • Clears: s • Ensures: this = # this * #s 7 January 2019 OSU CSE 30
flip void flip() • Reverses (“flips”) this . • Updates: this • Ensures: this = rev (# this ) 7 January 2019 OSU CSE 31
insert void insert( int pos, Sequence<T> s) • Inserts s at position pos of this , and clears s . • Updates: this • Clears: s • Requires: 0 <= pos and pos <= | this | • Ensures: this = # this [0, pos) * #s * # this [pos, |# this |) 7 January 2019 OSU CSE 32
Example Code State si1 = < 8, 6, 92 > si2 = < 1, -7 > si1.insert(2, si2); 7 January 2019 OSU CSE 33
Example Code State si1 = < 8, 6, 92 > si2 = < 1, -7 > si1.insert(2, si2); si1 = < 8, 6, 1, -7, 92 > si2 = < > 7 January 2019 OSU CSE 34
extract void extract( int pos1, int pos2, Sequence<T> s) • Removes the substring of this starting at position pos1 and ending at position pos2-1 , and puts it in s . • Updates: this • Replaces: s • Requires: 0 <= pos1 and pos1 <= pos2 and pos2 <= | this | • Ensures: this = # this [0, pos1) * # this [pos2, |# this |) and s = # this [pos1, pos2) 7 January 2019 OSU CSE 35
Example Code State si1 = < 8, 6, 92, 27, 0 > si2 = < 1, -7, 562 > si1.extract(1, 3, si2); 7 January 2019 OSU CSE 36
Example Code State si1 = < 8, 6, 92, 27, 0 > si2 = < 1, -7, 562 > si1.extract(1, 3, si2); si1 = < 8, 27, 0 > si2 = < 6, 92 > 7 January 2019 OSU CSE 37
Resources • OSU CSE Components API: Sequence – http://cse.osu.edu/software/common/doc/ 7 January 2019 OSU CSE 38
Recommend
More recommend