Assignment 5 - Class Folders
CS 1110 Spring 2019
Due to Gradescope by Wednesday, April 10, 2019 @ 11:59pm
Solutions now available here.
Overview
This is a written assignment similar to Assignment 2. Yes, you will diagram a few more call frames. But this time we are more concerned with folders: both object and class folders. The purpose of this assignment is to help you understand the issues with name resolution that we discussed in class.
This assignment is going to overlap Assignment 6. We will post Assignment 6 shortly after we post this assignment, but this one is due earlier. Since this is a written assignment, and you already have a lot of experience with call frames, it should not be a problem for you to finish this on time.
Learning Objectives
This assignment is designed to help you understand the following concepts.
- How to draw object folders to represent the contents of an instance.
- How to draw class folders to represent the shared content of all instances.
- How object and class folders are used to resolve naming issues.
- How method calls are similar to function calls.
Table of Contents
- Before You Get Started
- Diagramming Conventions
- Diagramming Class Folders
- Diagramming Object Folders
- Diagramming Method Calls
- Diagramming Constructor Calls
- Assignment Instructions
- Question 1: Diagram the two Class Folders
- Question 2: the Execution of p = X(3,5)
- Question 3: Diagram the Execution of q = Y(7,6)
- Question 4: Diagram the Execution of q.g()
- Question 5: Diagram the Execution of q.h()
- Finishing the Assignment
Before You Get Started
Academic Integrity
With the exception of your Gradescope-registered partner, we ask that you do not look at anyone else's solutions, seek solutions online or from students of past offerings of this course, or show your answers to anyone else (except a CS1110 staff member) in any form whatsoever. This is in keeping with the the Academic Integrity Policy for CS 1110.
Partner Policy
You may do this assignment with one other person. When you upload your pdf to Gradescope, you will be given the option to add a person to the submisison. You may find these instructions helpful.
If you do this assignment with a partner, you must work together. It is against the rules for one person to complete the assignment and just add the other person to their submission without the other person having contributed. You should actively collaborate on this assignment. You may not collaborate with anyone other than your Gradescope partner.
Using the Online Python Tutor
By now, everyone is very familiar with the Online Python Tutor. Once again, using the Tutor makes parts of (but not all of) this assignment easy. We are perfectly okay with this. It is a good way to help you understand the formalism if you are particularly lost.
If you used the Python Tutor to help you with part of the problem, please cite this (for each part that you used it) in your submission. We will not take off any points if you use the Tutor and cite it. We only require a citation because it is part of our Academic Integrity Policy.
Getting Help
Even though this is just a written assignment, some of you may still have some difficulty with it. You might not completely understand how method calls work or the difference between the two folders. If you are lost, please see someone in the course staff. This can be the course instructor, a TA, or a consultant. Furthermore, do not wait until the last minute, as the course staff is generally overwhelmed just before an assignment deadline.
Diagramming Conventions
You are already familiar with most of diagramming conventions from Assignment 2. Our new conventions build on top of these. All of our examples below use the following example
1 class Example():
2 CONST = 3
3
4 def __init__(self):
5 pass
6
7 def foo(self):
8 return 42
Diagramming Class Folders
When you define a class, Python creates a special folder called a class folder that stores the information about that class. Technically, Python also creates a global variable with the same name as the class and puts the name of that folder in the global variable; that is what you see when you put the class above in the Python tutor.
To simplify things, we do not want you to show the variable in global space (just like we never ask you to show the variable for a function name in global space). We just want you create a folder in heap space, and put the name of the class in the tab. We also want you to put the tab on the right so that we can tell this is a class folder. This is illustrated below.
The class folder contains all the methods and any class attributes. In a class definition, any assignment statement outside of a method definition creates a class attribute. In the example above, CONST is a class attribute.
Technically, the methods are also variables themselves. This is what you would see if you used the Python visualizer. However, the contents of this variable are really complicated, and the Python visualizer just writes the method header (the name plus all of its parameters) as its value. Therefore, that is what we want you to write for the methods in a class: the method header.
The example above works when the class does not explicitly inherit from another class (which actually means it inherits from object). Suppose we have a class that is a subclass of a user-defined class, like the example Demo below.
1 class Demo(Example):
2 pass
To indicate this relationship in our class folders, we put the name of the parent class in parentheses, as shown below. Note that this class has no methods or class attributes in it, because there are none in the class definition.
Diagramming Object Folders
A object is an instance of a class. Although there is only one class Example, there may be more than one Example object. Each time you call the constructor, you create a new object. For example, suppose we execute the following statements:
e = Example() e.x = 10
The constructor for Example creates an empty folder (why?), whereas the second assignment statement adds an attribute x to the folder. The end result is as follows:
The instance folder only contains attributes added to the instance. It does not contain class attributes, and (for this course) it never contains methods.
Diagramming Method Calls
A method class works almost exactly like a function call. You create the call frame, and plug the arguments into the parameters. You then execute each line in the method, changing the call frame as necessary. Finally, you erase the frame.
The only real difference is the function name that goes in the upper left box. We want both the name of the class and the method name in this box. For example. for the method call
e.foo()
the initial diagram for the call frame is as follows
Note the name is not foo in the top left folder, but Example.foo. That tells us to use the version of foo that is stored in the class folder Example.
Diagramming Constructor Calls
A constructor is just a function, so Python should create a call frame when it is called. However, this function is built-in, so we do not really know what it looks like. You will notice that the Python visualizer does not create a call frame for the constructor either. However, it does create a call frame for the initializer __init__, which the constructor uses as a helper.
We are going to do it the way that the Python visualizer does it. Remember the 4 steps of a constructor call:
- It creates a new empty folder of that class.
- It puts the folder into heap space
- It executes the method __init__ (creating a call frame)
- It returns the folder name
In our diagram, we combine all four steps, but leave indications of what happened in this process. For example, if we executed the constructor
e = Example()
The resulting diagram would be as follows.
The diagram shows that the folder and call frame are created, the single line of the __init__ function is executed. Notice that although the return value of __init__ is NONE, the Constructor (which called the __init__ function) returns the identifier of the folder which it created. The call frame is crossed out because it is deleted when the __init__ function returns to the Constructor.
Assignment Instructions
This entire assignment will involve the following two mysterious (and undocumented) classes.
1 class X():
2 x = 2
3 y = 4
4
5 def __init__(self, x, y=10):
6 self.x = y - X.y
7 self.y = x - X.x
8
9 def f(self):
10 self.x += X.x
11 self.y += Y.y
12
13 def h(self):
14 return self.y - X.x
15
|
16 class Y(X):
17 x = 9
18
19 def __init__(self,x,y):
20 super().__init__(x+1)
21 self.y = self.x - Y.y
22
23 def g(self):
24 self.f()
25 self.y += Y.y
26
27 def h(self):
28 self.x = Y.y
29 self.y = super().h()
30
|
Question 1: Diagram the two Class Folders
Draw two class folders. One for the class X
and another for the class Y
.
Do not draw global space (even though there are global variables X
and
Y
that refer to these class folders). There is also no call frame to draw.
You can complete the class folder for X before the Thursday, March 28 lecture. However, the folder for class Y requires the subclasses lecture.
Question 2.1: Diagram the Execution of p = X(3,5)
Draw the execution of the statement p =
X(3,5)
. This is a constructor call, so it will
involve the creation of a call frame
for __init__
. Your diagram should show a
single picture that is the state of memory after this
statement is executed, but with crossed out elements
still legible so we can see all the intermediary
values of variables and line numbers that were
executed. Remember every call frame needs a return
value, even if it is None.
You do not need to add anything to global space beyond what is required for the
statement p = X(3,5)
. In particular, you do not need a global variable for
class X
.
Question 2.2: Multiple Choice
Because our drawing convention does not have a good way of annotating the time of a variable's creation, we ask you to answer the following multiple choice question after you draw your diagram:
The variable p
is created:
---------------------------
A) Before the Constructor is called.
B) By the Constructor.
C) By the __init__
function
D) After the __init__
funciton returns, but before the Constructor returns
E) After the Constructor returns.
Somewhere near your answer to Question 2.1, please write "The answer to Question 2.2 is F." But replace F with your answer.
Question 3: Diagram the Execution of q = Y(7,6)
This problem is exactly like Question 2. We need the final state of the
global space, heap space, and the call frame for __init__
, with deleted/changed elements legibly crossed out. The
difference this time is that there is a call stack created with more than one frame. You should draw that properly.
You do not need to include p
(from Question 2) in your diagram of global
space.
Question 4: Diagram the Execution of q.g()
In this problem, q is the variable
from Question 3. Diagram final state
of the call frame for method g
, with
deleted/changed elements legibly crossed out. This is
a normal method call (not a constructor),
as described above.
If this method calls another method as a helper, you
should draw the complete call stack, just like you did
in the previous problem.
Your diagrams should include the contents of global space and heap space.
Question 5: Diagram the Execution of q.h()
In this problem, q is the variable from Question 3. For the purposes of this question, assume that Question 4 never happened. Diagram
the final state of memory after calling method h
, with deleted/changed elements legibly crossed out.
This is a normal method call
(not a constructor), as described above. If this method
calls another method as a helper, you should draw the complete call stack, just like you
did in the previous problem.
Your diagrams should include the contents of global space and heap space.
Finishing the Assignment
When you finish the assignment, put your name and netid at the top of the page. If you are working with a partner, then the partner's name and NetID must be on the assignment as well.
Creating a PDF for Submission
You will submit your work as a PDF. If you created your assignment on a computer, this should be relatively easy. Otherwise, you will need to scan your paper as a PDF. There are scanners in Olin and Uris Library to help you with this. You can also scan your homework using a mobile device. Please allow enough time do the scanning. Don't miss the deadline because of a scanning fiasco.
You may not submit multiple PDF files. If your scanner makes more than one file, you must combine them. On Windows, the program PDFtk can merge multiple PDF files into one file. In OS X, the built-in application Preview can be used to merge PDF files.
Upload your PDF to Gradescope
Please submit your assignment to Gradescope. If you worked in a pair, remember to add your partner to the submission on Gradescope or else they will get a zero for the assignment. Due to a barrage of recent requests to retroactively pair students after the deadline, if we have to pair you up, we will assign a 4 point deduction to the partnership (yes, to both of you) for your assignment grade.
Once you've performed the upload, it's a good idea to view your online submission to make sure it is readable and complete.
Survey
We ask that you complete a feedback survey, which will be posted on Canvas on April 5. We would like to know how long you spent on the assignment, your impression of the difficulty, and what could be done to improve it. Please complete the survey within a day of turning in the assignment. Remember that participation in surveys compromise 1% of your final grade. Your honest feedback is very helpful to us.
Authors: S. Agarwal, A. Bracy, E. Cheong, K. Healy, L. Lee, S. Marschner, A. Pidugu, E. Reynolds, W. White.