T-Th 9:05
or
T-Th 11:15
in Phillips 101

CS 1110: Introduction to Computing Using Java

Spring 2012

Diagraming Objects and Frames

Due in class on Tuesday, February 21

There is a PDF version of these instructions, if you would prefer to have that instead.

This is a simple written assignment where you are going to diagram a few objects. It should not take more than 15 minutes do to this assignment. So even if you are busy revising assignment 1, you should be able to do this assignment as well.

"Diagraming" is what we do in the lecture notes where we draw the folders to represent the objects and associated variables. It also includes the methods frames that we talked about in class. These are a way of giving us a visual repreentation of what is going on in the computer when it runs Java. The purpose of this assignment is to see if you can keep track of what goes where in an object.


Diagramming Conventions

Most of our diagramming conventions follow the lecture notes. However, in some places the lecture notes cut corners in order to be able to fit all the information on the slide. We do not want you to do that here. Therefore, we make several explicit conventions.

Diagramming Variables

A diagram for a variable should include the variable name, a box for the value, and a type. For example, here is a diagram for the variable x:

We sometimes dropped the type in lecture, but you should not do that for this assignment.

Diagramming Objects

When you diagram a folder (object), you should obey the following conventions:

  • The folder name should be unique, start with an @, and go to the right.
  • The class name should go in a box to the right
  • The fields should be listed just below the tab name and diagrammed just like variables.
  • The methods should go below the fields, separated by a dotted line.
  • You only need to give the method name and parameters for each method.
  • The constructor is a method; you should include it.

For example, a folder for a class Point1d might look like this:

Diagramming Classes

When you diagram a file drawer (class), you should obey the following conventions:

  • Draw the file drawer as a box.
  • Folders go at the top and are arranged horizontally.
  • Variables that belong in the file drawar (and not in the folders) go below the folders.
  • Methods that go in the file drawer go at the bottom and are separated by a vertical line (just as they are in the folders).

The following shows the basic structure of a class with all of the details removed

Diagramming a Frame for a Method Call

Frames should obey the following conventions:

  • The top left corner is a box with method name and instruction counter.
  • The top right corner is a box containing the scope of the method call.
  • The local variables and parameters diagrammed as variables in the frame body.

See the lecture notes for more details on how to create a frame for a method call. In this exercise, we will not worry about putting local variables in a frame. Furthermore, the instruction counter will start at 1.


Assignment Instructions

The Class Example

This entire assignment will revolve around the following class:


public class Example {
   public static final int ZERO = 0;
   private int h;

   public Example(int ph) {
      h = ph;
   }
   
   public int getH() {
      return h;
   }

   public void setH(int h) {
      this.h = h;
   }
   
   public String toString() { ... }
   public static int what(int x) { ... }
}

You should not need to know anything about the method bodies of toString() or what(int) in order to do this assignment.


Step 1: Diagramming Folders

Given the class above, suppose we were to execute the following commands:


  Example e = new Example(3);
  Example f = new Example(5);
  Example g = e;

Diagram the variables e, f, and g and the folders associated with them. The folders should only include fields and methods that are specifically associated with folders (and not those associated with file drawers).


Step 2: Diagramming a File Drawer

Extend the diagram in the previous step so that the folders that you created are now in a file drawer representing the class Example. If there are any variables or methods that should go in the file drawer (e.g. you did not include them in the previous step), add them now.


Step 3: Diagramming Frames for some Method Calls

Extend your diagram once again to include frames for the following three method calls:


  e.setH(4);
  f.toString();
  Example.what(8);

As we said previously, you should not worry about local variables in your frames.


Step 4: Diagramming a Frame for a Constructor Call

In step 1, you were given the command


  Example e = new Example(3);
The constructor for Example is a (special type of) method, and so the new command creates a method frame for the constructor when you use is. Diagram this method frame.

Hint: We made a mistake in class: constructors are instance methods, not static methods. See the revised lecture notes for how to do this step.