48
Lect 08-09 Projects 2: partially-filled array: building, traversing and editing Project 3: singly-linked list: building and traversing. Project 4: add editing to Project 3. Reading List. Live Coding with principles illustrated.

Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

Lect 08-09

Projects 2: partially-filled array: building, traversing and editing

Project 3: singly-linked list: building and traversing.

Project 4: add editing to Project 3.

Reading List.Live Coding with principles illustrated.

Page 2: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

Reading List (Arrays)

● Book Ch. 5: Inserting & deleting done to a

sequence (adt)

implemented by a

partially-filled array (data str.)

● Albany supplement: sdc's notes on arrays● Gaddis&Mugada ?

Page 3: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

Reading List (Linked List)● Basics: Mad PhD w/ neighbors; thief lab.

● Book 7.2 building [manually! :( ] & traversing of

a sequence (adt)

implemented by a

linked list (data str.)

(introduces editing)

● 7.3 editing continues

● LECTURE NOTES! and LIVE CODE examples!

Page 4: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

● Book gets better at Ch. 8: scene graphs as really used in graphics SW.

both 2d and 3d

Page 5: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

   FileChooser.pickMediaPath( );Put that in your program to make

the FileChooser “remember” a folder.(It's just a G&E java-source thing.)

When the going gets slow,invest a little time

to make tedious activitygo FASTER!!!

(less distracting..)

Page 6: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

Please invent solid principles for how you usesome of the variables!

Page 7: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage{  private Picture myArray[];   //...  // the length is available   // with myArray.length.    private int nPictures;   //nPictures will ALWAYS   // equal the nr. of images  //the customer thinks is   //CURRENTLY in his collage.

Page 8: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage {  private int nPictures;   //nPictures will ALWAYS   // equal the nr. of images  //the customer thinks is   //CURRENTLY in his collage.

You can absolutely trust this...and baseyour code on it....because..you make sure EVERY LINE OF CODEkeeps it absolutely TRUE!

Page 9: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

● starts at 0● is NEVER CHANGED except: add 1 when a Picture is added or pasted. subtract 1 when a Picture is cut.

Changing nPictures

Page 10: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

● it indexes next available element of myArray● tell if pasting is impossible because the array is filled to capacity myArray.length==nPictures ●tell if cutting is impossible because the array is empty nPictures==0● calculate middle position nPictures / 2 roughly

Using nPictures

Page 11: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

Linked List

Page 12: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

One of the many(excessively many!!)

things Java classes aregood foris

breaking softwareinto

modules

Page 13: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

The job of my LinearCollage objectis to build & edit onelinear collage.

How?That's the private businessof the LinearCollage class.

Page 14: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage//Linked List Version!{   private class Node   {       Picture data;      Node pnext;   }   //Node called, in Java,   //an “inner class”

Page 15: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage {  private class Node

    {        Picture data;       Node pnext;    }

  private nPictures;  private Node pFirst;  private Node pLast;

Page 16: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage {  private class Node

    {        Picture data;       Node pnext;    }

  private nPictures;  private Node pFirst;  private Node pLast;

Page 17: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence
Page 18: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence
Page 19: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence
Page 20: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

(there ain't any) Dumb Questions

Doesn't Node need accessors and mutators?NO—not by Java language rules.

– Shouldn't Node have them? Info. hiding? Node (name and class def) is ALREADY

HIDDEN!

How LinearCollage implements its sequenc is private business of LinearCollage. Let it define

a cool private class for linked list nodes,COMPLETELY INTERNAL to LinearCollage.

Nobody can see or touch Nodes because they'reprivate insides of LinearCollage

Page 21: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage {

  private class Node {        private Picture data;        private Node pnext;       public Node(Picture p)       { data=p; pnext=null; }/*acc*/public Node getNext()       { return pnext; }/*mut*/public void putNext(Node n)       { pnext=n; }    }  

Do you really want accessors & mutators?? Chaiken thinks they're a waste..

Page 22: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage {  private class Node

    { Picture data; Node pnext; }    private nPictures;    private Node pFirst;    private Node pLast;

    public LinearCollage()  {   nPictures = 0;   pFirst = null;   pLast = null;  }

Page 23: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence
Page 24: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage {  private class Node

    { Picture data; Node pnext; }    private nPictures;    private Node pFirst;    private Node pLast;    public LinearCollage() { ... }

    public void addPAtEnd(Picture p)    { Node tmp = new Node();    tmp.pnext = null;    tmp.data = p;

  

Page 25: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

DONE!

3 MORE THINGSTODO.

tmp

Page 26: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage {    private class Node      { Picture data; Node pnext; }    private nPictures;    private Node pFirst;    private Node pLast;    public LinearCollage() { ... }    public void addPAtEnd(Picture p)    { Node tmp = new Node();      tmp.pnext = null;      tmp.data = p;

     if( pFirst == null )   { pFirst = tmp;     pLast = tmp; }   else { }   nPictures++;  

Page 27: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence
Page 28: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage {    private class Node      { Picture data; Node pnext; }    private nPictures;    private Node pFirst;    private Node pLast;    public LinearCollage() { ... }    public void addPAtEnd(Picture p)

    { Node tmp = new Node();      tmp.pnext = null;      tmp.data = p;     if( pFirst == null )       { pFirst = tmp;         pLast = tmp; }

   else { /* ??? */ }   nPictures++;  

Page 29: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence
Page 30: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence
Page 31: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage {    private class Node      { Picture data; Node pnext; }    private nPictures;    private Node pFirst;    private Node pLast;    public LinearCollage() { ... }    public void addPAtEnd(Picture p)

    { Node tmp = new Node();      tmp.pnext = null;      tmp.data = p;     if( pFirst == null )       { pFirst = tmp;         pLast = tmp; }

   else { /* ??? */ }   nPictures++;  

Page 32: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

Lecture 09

● Adding the FIRST Node.● Adding the SECOND, THIRD, ... and other

Nodes.

Page 33: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence
Page 34: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence
Page 35: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage {  private class Node

    { Picture data; Node pnext; }    private nPictures;    private Node pFirst;    private Node pLast;    public LinearCollage() { ... }

    public void addPAtEnd(Picture p)    { Node tmp = new Node();    tmp.pnext = null;    tmp.data = p;

  

Page 36: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

tmp

What was changed?(A) LinearCollage (C) Both(B) Node (and tmp) (D) Neither

Node tmp =  new Node();tmp.pnext= null;tmp.data  = p;

Page 37: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

tmp

Node (and tmp) changed!Let us move on ... write code to change the LinearCollage object

Node tmp =  new Node();tmp.pnext= null;tmp.data  = p;

Page 38: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

Before

Page 39: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

tmp

After

Page 40: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

if( pFirst == null )   { pFirst = tmp;     pLast = tmp; }   else { }   nPictures++;  

tmp

After

the pointeror address thatis copied 2 times

tmp

Page 41: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

The FirstNode has beenAdded.

Page 42: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

class LinearCollage {    private class Node      { Picture data; Node pnext; }    private nPictures;    private Node pFirst;    private Node pLast;    public LinearCollage() { ... }    public void addPAtEnd(Picture p)

    { Node tmp = new Node();      tmp.pnext = null;      tmp.data = p;     if( pFirst == null )       { pFirst = tmp;         pLast = tmp; }

   else { /* Let's figure     out how to add 2nd, 3rd,     and other Nodes??? */ }   nPictures++;  

Page 43: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

Just before we addNode 2---to the end of the list.

Page 44: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

Just before Node 4

is added.

Page 45: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

iClicker QuizLook on the paper,ONLY LOOK ATtheDiagramNOT THE CODE (B)!

Page 46: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

 in {/* ??? */} which??(A) { pLast.pNext = tmp;      pLast = tmp; }    nPictures++;

(B) { pLast = tmp;      pLast.pNext = tmp;}    nPictures++;

(C) Both A and B are OK code.

Page 47: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

Paper Quiz: Student's program (B)has a bug and he can't figure it out.

MARK the DIAGRAMto show EXACTLYwhat's in the computer AFTER thebuggy code has been run.

Page 48: Lect 08-09sdc/CSI310/Spr12/Classes/C08/ProgNotes.pdfReading List (Linked List) Basics: Mad PhD w/ neighbors; thief lab. Book 7.2 building [manually! :( ] & traversing of a sequence

Before(B) { pLast = tmp;      pLast.pNext = tmp;}    nrPictures++;

Name______________________NetID_______ iClicker MAC addr________

Cross out and change 3 spots below:

tmp