Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the...
-
Upload
annette-wheller -
Category
Documents
-
view
217 -
download
1
Transcript of Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the...
![Page 1: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/1.jpg)
Hw #6 due on 3/25…
Java, from the details to the bigger picture
Spamventure! From the details, the big (ASCII) picture
after break…emanning
msheely, 1432 lines!!
slam
mnaval, 859 lines!further adventures
in cs60 !
![Page 2: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/2.jpg)
Hw #6 due on 3/25…
Java, from the details to the bigger picture
after break…emanning
msheely, 1432 lines!!
slam
mnaval, 859 lines!further adventures
in cs60 !
![Page 3: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/3.jpg)
Big Java…
![Page 4: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/4.jpg)
Big Java… huge libraries of classes (data structures)
![Page 5: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/5.jpg)
Even Math
add Prof. Williams!
![Page 6: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/6.jpg)
Java structures data
![Page 7: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/7.jpg)
List L; mySize myHead
List
myFirst myRest
"a"myFirst myRest
"c"null
myFirst myRest
"b"
3
Java structures dataListNode ListNode ListNode
Singly-linked list data structure
L.addToFront("c");L.addToFront("b");L.addToFront("a");
List L
L = new List();
![Page 8: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/8.jpg)
List L;
Java structures data
List Lnull
I guess this reference is a null space…
All objects are handled by reference.
Empty references are null.
![Page 9: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/9.jpg)
List L;
mySize myHead
List
0
Java structures data
List L
L = new List();
null
int ListNode
![Page 10: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/10.jpg)
List L;
mySize myHead
List
myFirst myRest
"c"null
1
Java structures dataListNode
L.addToFront("c");List L
L = new List();
![Page 11: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/11.jpg)
List L;
mySize myHead
List
myFirst myRest
"c"null
myFirst myRest
"b"
2
Java structures dataListNode ListNode
L.addToFront("c");L.addToFront("b");
List L
L = new List();
![Page 12: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/12.jpg)
List L;
mySize myHead
List
myFirst myRest
"a"myFirst myRest
"c"null
myFirst myRest
"b"
3
Java structures dataListNode ListNode ListNode
L.addToFront("c");L.addToFront("b");L.addToFront("a");
List L
L = new List();
![Page 13: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/13.jpg)
mySize myHead
List
myFirst myRest
"a"myFirst myRest
"c"null
myFirst myRest
"b"
3
List classListNode ListNode ListNode
ListNode class
!
![Page 14: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/14.jpg)
addToFront
public void addToFront( String str ) { ListNode LN = new ListNode( str, null ); // 1 LN.myRest = this.myHead; // 2 this.myHead = LN; // 3 this.mySize += 1; // 4}
public void addToFront( String str ) { this.myHead = new ListNode( str, myHead ); // 1-3 this.mySize += 1; // 4
L.addToFront("a");
whoa!
same thing:
![Page 15: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/15.jpg)
removeFirst
L.removeFirst( );mySize myHead
List
myFirst myRest
"c"null
myFirst myRest
"b"
2
ListNode ListNode
before
after
L
List
(before)
int ListNode
![Page 16: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/16.jpg)
removeFirst
L.removeFirst( );mySize myHead
List
myFirst myRest
"c"null
myFirst myRest
"b"
2
ListNode ListNode
before
mySize myHead
List
1
after
myFirst myRest
"c"null
ListNode
L
List
(before)
L
List
(after)
int ListNode
int ListNode
return value: ?
![Page 17: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/17.jpg)
removeFirst
L.removeFirst( );mySize myHead
List
myFirst myRest
"c"null
myFirst myRest
"b"
2
ListNode ListNode
before
mySize myHead
List
1
after
myFirst myRest
"c"null
ListNode
L
List
(before)
L
List
(after)
int ListNode
int ListNode
return value:
![Page 18: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/18.jpg)
Quiz
public ListNode removeFirst( ) {
ListNode result =
if
return result;}
(1) What is the result?
(2) We need an if – why?!
(3) Fix the list up …
Write the removeFirst method… Name(s): ________________________
Step (3) is one to remember!
(4) Return.
![Page 19: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/19.jpg)
Quiz
public ListNode removeFirst( ) {
ListNode result =
return result;}
(1) Cut the node out and give it a name.
(2) "Fix up" the List (this object)
(…) Return the node you cut out.
(3) What have we forgotten?!
Write the removeFirst method…Try this on the back page first…
Step (3) is one to remember!
![Page 20: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/20.jpg)
How many?
Why have both the mySize field and a length() method?
![Page 21: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/21.jpg)
How many?
checks the length by actually walking the list
(1) Declare + initialize a "runner" variable
(2) Test!(4) Update + go back to step 2
(3) Loop body!
for loop!
![Page 22: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/22.jpg)
How many?
checks the length by actually walking the list
(1) Declare + initialize a "runner" variable
(2) Test!(4) Update + go back to step 2
(3) Loop body!
while loops do the same four things…
![Page 23: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/23.jpg)
this
List
Walking the list…
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
![Page 24: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/24.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
0
Heap vs. stack ?
What is node pointing to??
![Page 25: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/25.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
0
![Page 26: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/26.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
1
![Page 27: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/27.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
1
What is node now pointing to??
![Page 28: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/28.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
1
![Page 29: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/29.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
2
![Page 30: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/30.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
2
![Page 31: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/31.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
2
![Page 32: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/32.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
3
![Page 33: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/33.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
3
What will node now point to??
![Page 34: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/34.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
3
Nothing!
null
the loop exits, returning count…
![Page 35: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/35.jpg)
this
List
Walking the list…
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
too slow!
![Page 36: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/36.jpg)
this
List
Walking the list…
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
Sprinting
![Page 37: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/37.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
0
![Page 38: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/38.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
0
![Page 39: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/39.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
1
![Page 40: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/40.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
1
![Page 41: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/41.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
1
![Page 42: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/42.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
2
![Page 43: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/43.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
2
![Page 44: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/44.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
2
![Page 45: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/45.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
3
![Page 46: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/46.jpg)
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
this
node
List
ListNode
count
int
3
Done!
null
the loop exits, returning count…
![Page 47: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/47.jpg)
More loops: toString
Racket style!
![Page 48: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/48.jpg)
More loops: get
"Big errors" are handled in Java by
throwing exceptions
loops until k == pos
![Page 49: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/49.jpg)
More loops: equals
loops for the full list
![Page 50: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/50.jpg)
add
L.add( "d" );mySize myHead
List
myFirst myRest
"c"null
myFirst myRest
"b"
2
ListNode ListNode
beforeadds to the end of the List…
![Page 51: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/51.jpg)
L.add( "d" );mySize myHead
List
myFirst myRest
"c"null
myFirst myRest
"b"
2
ListNode ListNode
before
mySize myHead
List
myFirst myRest
"c"myFirst myRest
"b"
3
ListNode ListNode
after
myFirst myRest
"d"null
ListNode
add adds to the end of the List…
![Page 52: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/52.jpg)
Try it! add
public void add( String str ) {
ListNode LN = new ListNode( , );
}
(1) Handle the empty case
(2) If nonempty, write a loop !
(3) How far to loop?
(4) and when the loop ends…?
(0) Make a new ListNode
![Page 53: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/53.jpg)
"Base Case"
L.add( "b" );mySize myHead
List
0before
mySize myHead
List
1
after
myFirst myRest
"b"null
ListNode
null
this
List
this
List
![Page 54: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/54.jpg)
Try it!
(1) Handle the empty case
(2) If nonempty, write a loop !
(3) How far to loop?
(4) and when the loop ends…?
(0) Make a new ListNode
public void add( String str ) {
ListNode end = new ListNode( , );
if (this.myHead == null) {
} else {
}
Write the add method, which adds a new ListNode at the end.
![Page 55: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/55.jpg)
this
node
List
ListNode
Our example while loop…
(1) You need a different test!
(2) What do you need in the loop and after it?
![Page 56: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/56.jpg)
Try it!
(1) Handle the empty case
(2) If nonempty, write a loop !
(3) Loop until
node.myRest == null
(4) Add end at end of loop.
(0) Make a new ListNode
public void add( String str ) {
ListNode end = new ListNode( str , null );
if (this.myHead == null) {
this.myHead = end;
} else {
ListNode node = this.myHead;
while (node.myRest != null) {
node = node.myRest;
}
node.myRest = end;
}
this.mySize += 1;
}
Write the add method, which adds a new ListNode at the end.
(5) Don't forget!
![Page 57: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/57.jpg)
Lists vs. Arrays…
mySize myHead
List L
myFirst myRest
"c"myFirst myRest
"b"
3
ListNode ListNode
List
myFirst myRest
"d"null
ListNode
L
![Page 58: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/58.jpg)
Lists vs. Arrays…
mySize myHead
List L
myFirst myRest
"c"myFirst myRest
"b"
3
ListNode ListNode
List
myFirst myRest
"d"null
ListNode
• a single, set length• 1 type of element• indexing ~ Python• no built-in slicing• traverse with loops…
LArray
length A[0]
String[] A
3
A[1] A[2]
"c""b" "d"
AWe should end with Arrays' upside…
![Page 59: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/59.jpg)
Lists vs. Arrays…
Let's scrapbook it!I cherish my Java!
![Page 60: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/60.jpg)
other methods in hw6pr2
split
merge
( "d" "b" "e" "c" "a" )
( "d" "b" "e" ) ( "c" "a" )
( "b" "d" "e" ) ( "a" "c" )
( "a" "b" "c" "d" "e" )
![Page 61: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/61.jpg)
Mergesort!
split
merge
( "d" "b" "e" "c" "a" )
( "d" "b" "e" ) ( "c" "a" )
( "b" "d" "e" ) ( "a" "c" )
( "a" "b" "c" "d" "e" )
What has to happen within these pink arrows?
![Page 62: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/62.jpg)
Mergesort!
split
merge
( "d" "b" "e" "c" "a" )
( "d" "b" "e" ) ( "c" "a" )
( "b" "d" "e" ) ( "a" "c" )
( "a" "b" "c" "d" "e" )
mergesort mergesort
![Page 63: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/63.jpg)
After break?
Java
Java
Data-structuring details
Applicationsplus, lots more data-structuring details
![Page 64: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/64.jpg)
Prof Lewis!
Pixels!
The week after spring break…
![Page 65: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/65.jpg)
Have a great spring break!
My plans are for maximum Java!
![Page 66: Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432.](https://reader038.fdocuments.net/reader038/viewer/2022110304/5518d243550346a61f8b5d6a/html5/thumbnails/66.jpg)