CSCI 201 Fall 2003

Short Homework Assignment 4: Memory Diagrams

In this homework, you are asked to depict the contents of memory (RAM) at a specific point during the execution of the Pets program. Download Pets.jar which contains a version of the Pets program that is very similar to the one developed in class. Feel free to run the program; you can also observe the execution of the program using the netBeans debugger.

Your diagram is to depict memory immediately before the execution of the line of code indicated below.

public class Pets
{
  public static void main (String [] args)
  {
    String dogName = new String("spike");
    String catName = new String("tom");
    Cat tom = new Cat(catName);     
    Dog spike = new Dog(dogName);   
 
    spike.bite(tom); ***THIS LINE***
  }
}

For 5 points extra credit (15 points total for this assignment), you can also diagram memory immediately before the execution of this line of code:


public class Dog
{
   ...

   public void bite(Cat catmeat)
   {
      growl(); ***THIS LINE***
      if (beenScratched < 3) {
         beenScratched++;
         System.out.println(name + " is bitting " + catmeat.getName());
         catmeat.scratch(this);
      }
      else {
         System.out.println(name + " is running away!");
         yelp();
      }
   }   

   ...
}

Memory Diagrams

Your memory diagram should depict the three partitions of memory that we have discussed in class: (1) the stack, (2) the heap, and (3) the static or code section. Points to remember in creating your memory diagram:
  1. variables are drawn as rectangles
  2. objects are drawn as circles
  3. Public methods and instance variables are shown as extending outside of the circle representing an object; private methods and variables are shown enclosed in that circle.
  4. Each method gets its own partition of the stack when it is running. Be sure to show these partitions.
  5. The representation of String objects can be simplified as shown in the example below. You do not need to show the methods and instance variables in that class. Instead, depict the String character sequence in quotes inside the object.

What to Turn In

Turn in a hard copy of your memory diagram(s) at the start of the next class.