T-Th 9:05 |
CS 1110: Introduction to Computing Using Python Fall 2012 |
||||||
Main
About: Announcements Staff Consultants Times & Places Calendar Materials: Texts Python Command Shell Terminology Handouts: Lectures Assignments Labs Assessment: Grading Exams Resources: CMS Piazza (link) Piazza (about) AEWs FAQ Python API Style Guide Academic Integrity |
Encapsulation and PropertiesThere is a PDF version of these instructions, if you would prefer to have that instead. This lab gives you some experience with encapsulation and abstraction. It also contains a recursion problem, to give you some more practice for the upcoming exam. The concepts of this lab were, believe it or not, part of assignments A1 and A3 in previous semesters of this course. The lab this week is a little longer than the last one. But you should definitely try to finish it before next week because it provides some good practice for the exam (particularly the recursion question). Requirements For This Lab
There is just one file necessary for this lab: lab9.py. You
should download this file and put it in a new directory. You will note that this file
contains a class
python lab9.py
to run the module as an application. If you run it right now, you will see that
it fails right away, because
In addition, we are using simple assert statements for our unit test, not
In this lab, you will modify the class Lab Instructions
The class
If you look at the class Task 1: Complete the setters for
|
Method | Description |
---|---|
children.append(p) |
Adds p to this list of children . |
children.remove(p) |
Removes the first occurence of p from this list. |
In the case of remove
, each child should appear at most once in the list of
children. Therefore, a single remove
will remove all occurrences.
children
; use _children
You will notice that every time you call the getter for children
it makes a copy of the list. So if you try to use
self.children(p)
it will add to the copy and not the original list of children. If you want
to add to the actual list of children, use the field _children
instead.
Why do we do this? Because we want children
to be immutable. Even
though there is no setter for children
, you could still modify the
contents of children
since a list is itself mutable. This
negates the whole purpose of making children
immutable. So the
getter gives a copy of _children
, which can be modified without
affecting this object.
You might be tempted to write the following:
self._children.append(self)
If you do this, you are claiming that the current object (self
) has
itself as its own child. While this is the subject of many interesting science
fiction novels, it is not what we want here. Modify the _children
fields in the parents.
None
is a valid value for a mother
or
father
. None
means that the appropriate
parent is unknown. Even if we had an actual value for that parent
at one time, we may reassign it to None
later (perhaps
on the revelation that this person was adopted). You have to
be prepared for this, as you cannot access the _children
field in None
To test out your code, simply run lab9.py
as an application.
If you have done it correctly, you should see the following output:
Family Tree ----------- Smith,John [0 kids] Evans,Ellen [1 kid] O'Reilly,Ellen [1 kid] Chase,Heather [1 kid] O'Reilly,Dan [1 kid] Evans,John [1 kid] Smith,John [1 kid] Dare,Jane [1 kid] Brown,Eva [1 kid] Smith,John [1 kid] Grey,Pamela [1 kid] Smith,John [1 kid] Properties mother and father working correctly
The program should then fail immediately afterwards. That is okay; that error message is about the test for the next task.
familyNamed
Recall the famiy tree created when we link objects together.
Up until now, our use of recursion has been limited to sequences. But we can use recursion on this family tree as well. Note that "ancestor" has a recursion definition.
Given this recursive definition, we can use recursion to define the following function.
def familyNamed(self,name):
"""Returns: number of family with name as first name.
The number of family members includes this person
(e.g. self) as well as all family members. You should
implement this method recursively.
Precondition: name is a string."""
In the picture above, p.familyNamed('John')
is 5 (remember to count p
as well), while
p.familyNamed('Ellen')
is 2.
Write this recursive function. In order to figure out how
to do this, you might find the method fullstr
above it to be quite useful. This is a recurisve procedure
that creates a string representing the entire family tree,
indenting four spaces at each generation. In fact, you should
recognize it, as it is what produced the output in the
previous task.
When you are finished, run lab9.py
again as an application.
If you did it correctly, the output should end with the message
Method familyNamed working correctly
At this point show your lab9.py
to your instructor.