CS 132
Computer Science II
Lab 12 - Drawing From Memory

David B. Levine
Computer Science Department
St. Bonaventure University
Copyright,  2002

Objective

            Before one can really code using linked structures, one needs to understand how the various Java statements affect dynamically allocated structures.  This lab will give you experience tracing such code.

Rules of the Game

Warning:  Some of the code sequences in this lab do something sensible; others attempt to, but fail; still others aren't even trying to behave sensibly.  Nonetheless, all of them do something.  It is up to you to show the effect of their execution.  Determining the intended behavior is not necessary.

PRINTING:  There is no need to print the various code fragments.  In fact, to save paper you are NOT to print them out in the lab.  Each fragment is sufficiently short that working from the screen should not be a hardship.  

 

Instructions:

  1. Consider the linked list diagram shown above.  Each of the code sequences below manipulates the diagram in some way.  For each sequence, work out how it changes the diagram and then draw a clean copy of the resulting structure (as best you can).  You will be given templates in lab for drawing the resulting structures.  If necessary, here is a template.   In each case, be sure to include your name(s), the code fragment  number, and any problems with its execution.
  2.           In each case, you should assume that we are using the same general ListNode structure as in class, i.e. 

        public class ListNode
        {
           private int value;          //  Note slight change of data type
           private ListNode next;      
           
           public ListNode(int v)
           {
               value = v;
               next = null;
           }
           
           public int getValue()
           {
               return value;
           }
    
           public void setValue(int v)
           {
               value = v;
           }
           
           public ListNode getNext()
           {
               return next;
           }
           
           public void setNext(ListNode n)
           {
               next = n;
           }
       }
    
    
    Each code fragment should be viewed separately, i.e. they each work on the diagram above, NOT on the results of the previous fragment.

    Code Fragment 1

    Code Fragment 2

    Code Fragment 3

    Code Fragment 4

    Code Fragment 5

    Code Fragment 6

    Code Fragment 7

    Code Fragment 8



  3. Use the following class definition for these code sequences and diagram.  As above you will be given templates for drawing your structures.  If necessary, here is a single template for these structures.
        public class TreeNode
        {
           private String animal;           //  Note slight change of data type
           private TreeNode left;      
           private TreeNode right;
           
           public TreeNode(String name)
           {
               animal = name;
               left = null;
               right = null;
           }
           
           public String getAnimal()
           {
               return animal;
           }
    
           public void setAnimal(String n)
           {
               animal = n;
           }
           
           public TreeNode getLeft()
           {
               return left;
           }
           
           public void setLeft(TreeNode t)
           {
               left = t;
           }
           
           public TreeNode getRight()
           {
               return right;
           }
           
           public void setRight(TreeNode t)
           {
               right = t;
           }
       }
    
    

  4.  

    Code Fragment 9

    Code Fragment 10

    Code Fragment 11

    Code Fragment 12

    Code Fragment 13

    Code Fragment 14

    Code Fragment 15

    Code Fragment 16

 

To Hand In

 

            Hand in your collection of diagrams (which should include more than one per page) along with a cover page.

 

Assignment Type (see Academic Practices and Policies Document):

Help Policy in Effect for This Assignment:  Group Project with Limited Collaboration

In particular, you may discuss the assignment and concepts related to the assignment with the following persons, in addition to an instructor in this course: any member of your group; any St. Bonaventure Computer Science instructor; and any student enrolled in CS 132. 

You may use the following materials produced by other students:  materials produced by members of your group.  

You may use the following materials produced by other students:  NONE.