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 !
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 !
Big Java…
Big Java… huge libraries of classes (data structures)
Even Math
add Prof. Williams!
Java structures data
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();
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.
List L;
mySize myHead
List
0
Java structures data
List L
L = new List();
null
int ListNode
List L;
mySize myHead
List
myFirst myRest
"c"null
1
Java structures dataListNode
L.addToFront("c");List L
L = new List();
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();
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();
mySize myHead
List
myFirst myRest
"a"myFirst myRest
"c"null
myFirst myRest
"b"
3
List classListNode ListNode ListNode
ListNode class
!
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:
removeFirst
L.removeFirst( );mySize myHead
List
myFirst myRest
"c"null
myFirst myRest
"b"
2
ListNode ListNode
before
after
L
List
(before)
int ListNode
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: ?
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:
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.
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!
How many?
Why have both the mySize field and a length() method?
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!
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…
this
List
Walking the list…
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
(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??
(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
(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
(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??
(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
(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
(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
(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
(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
(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??
(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…
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!
this
List
Walking the list…
(1) Declare + initialize a "runner" variable
(2) Test!
(4) Update + go back to step 2
(3) Loop body!
Sprinting
(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
(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
(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
(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
(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
(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
(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
(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
(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
(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…
More loops: toString
Racket style!
More loops: get
"Big errors" are handled in Java by
throwing exceptions
loops until k == pos
More loops: equals
loops for the full list
add
L.add( "d" );mySize myHead
List
myFirst myRest
"c"null
myFirst myRest
"b"
2
ListNode ListNode
beforeadds to the end of the List…
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…
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
"Base Case"
L.add( "b" );mySize myHead
List
0before
mySize myHead
List
1
after
myFirst myRest
"b"null
ListNode
null
this
List
this
List
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.
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?
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!
Lists vs. Arrays…
mySize myHead
List L
myFirst myRest
"c"myFirst myRest
"b"
3
ListNode ListNode
List
myFirst myRest
"d"null
ListNode
L
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…
Lists vs. Arrays…
Let's scrapbook it!I cherish my Java!
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" )
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?
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
After break?
Java
Java
Data-structuring details
Applicationsplus, lots more data-structuring details
Prof Lewis!
Pixels!
The week after spring break…
Have a great spring break!
My plans are for maximum Java!