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 lines!! slam mnaval, 859 lines! further adventures in cs60 !

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

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

(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

(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

(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

(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

(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

(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

(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

(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

(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

(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

(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

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

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

(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

(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

(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

(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

(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

(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

(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

(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

(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

(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

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

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

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

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

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

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

"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

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

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

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

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

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

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

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

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

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

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

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

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