CS151 HW#8

Tara Davidson '10

Write up:

  1. A brief description of the task (in your own words): In this lab I created two classes: a Datum class (that could hold raw, input data and draw the data) and a DataCollection class (that held a list of Datum objects and graphed them). My classes allowed me to plot data that could come from an outside file. Having this ability, my classes can work with different sets of data. A skeleton for both classes was provided. I started with my Datum class. In Datum.py I first created and set my input variables. I also created a field called circle. I then created accessor functions that simply returned the values in the x, y, and z fields. I then created a draw function. The draw function checked to make sure the circle field was set to None, then created a circle. If the circle field was not set to None, the existing circles were undrawn, moved, and redrawn. I created move, undraw, and setFill functions that again checked the circle field to see if it was set to None or not. If the circle field was not set to None then the methods proceded to be called. Finally, I create read and write functions that could take a single line from a pre-existing file (such as testdata.txt). The read function took the line, split the spaces breaks out of the line, and extracted x, y and z data from the list. The write function wrote these three pieces of data as well as a new line character to a file. For my DataCollection class, I just worked with the write, add, and move functions from the given skeleton. For the add function, I appended new data to a data list. For the write function, I wrote all the data into a file. For the move function, I moved all the data as well as updated the background. Both my classes created graphed images when run. The images are below.
  2. Images showing my Datum class working





  3. An image showing my Data Collection class working

  4. How are fields (data) in an object created? Give an example from your code Fields are created by creating functions that setting variables to (data) values. In my Datum class. I have a lot of examples of this in my set function. In self I was creating fields for x, y, z, x0, y0, and color. One example is when I created and set my x values to a certain data value:

    def set(self, tx, ty, tz, drawX=0, drawY=0, drawR=1, drawColor = 'blue'):
    # set x to tx
    self.x = tx

    In this case, I was setting my x field (that is in self) to an input data value, tx.
  5. How are fields in an object accessed inside a method? Give an example from your code Fields in an object are accessed by creating functions that refering to self and the field in self. A simple example of this can be seen by looking at one of my accessor functions. One example is when I created an accessor function that returned the data value in the x field:

    def X(self):
    # return the x field value
    return self.x