Docsity

Tracing Object-Oriented Programs in CS110: A Java Perspective - Prof. David W. Wolber, Study notes of Computer Science

Toggle sidebar
bg1
CS 110: Introduction to Computer Science Spring 2008
Tracing Object-Oriented Programs
Tracing of Object-Oriented programs is different than for structured programs. These
notes discuss key differences using Java code to illustrate.
1. All objects are created on the heap. The heap is a separate area in memory from where
the locals and parameters of functions are stored. When a statement like:
Person p = new Person();
is executed, create a box for a Person on the heap and point p to it.
2. When an object is created, draw a box with a section for each data member.
3. After creating an object, call the constructor.
4. When a method is called, create boxes for all locals and parameters next to the code for
the function. Don't forget about the implicit parameter 'this'.
Sample
public class Student {
public String name;
public boolean gradStudent;
public int numUnits;
public Student(String name,boolean gradStudent, int numUnits)
{
this.name=name;
this.gradStudent=gradStudent;
this.numUnits=numUnits;
}
public int fees()
{
if (this.gradStudent)
return this.numUnits*1000;
else
return 18000;
}
public static void main(String args[])
{
Student jones = new Student("David Jones",true,12);
int fees = jones.fees();
System.out.println(fees);
}
}
In-Class Problem: Trace the code at:
http://www.cs.usfca.edu/~wolber/courses/110/javaSamples/midterm/Student.
java

Partial preview of the text

Download Tracing Object-Oriented Programs in CS110: A Java Perspective - Prof. David W. Wolber and more Study notes Computer Science in PDF only on Docsity!

CS 110: Introduction to Computer Science Spring 2008

Tracing Object-Oriented Programs

Tracing of Object-Oriented programs is different than for structured programs. These

notes discuss key differences using Java code to illustrate.

1. All objects are created on the heap. The heap is a separate area in memory from where

the locals and parameters of functions are stored. When a statement like:

Person p = new Person();

is executed, create a box for a Person on the heap and point p to it.

2. When an object is created, draw a box with a section for each data member.

3. After creating an object, call the constructor.

4. When a method is called, create boxes for all locals and parameters next to the code for

the function. Don't forget about the implicit parameter 'this'.

Sample

public class Student { public String name; public boolean gradStudent; public int numUnits; public Student(String name,boolean gradStudent, int numUnits) { this.name=name; this.gradStudent=gradStudent; this.numUnits=numUnits; } public int fees() { if (this.gradStudent) return this.numUnits*1000; else return 18000; } public static void main(String args[]) { Student jones = new Student("David Jones",true,12); int fees = jones.fees(); System.out.println(fees); }

In-Class Problem: Trace the code at:

http://www.cs.usfca.edu/~wolber/courses/110/javaSamples/midterm/Student. java