Project Writeup
The goal of this homework assignment was to create two different classes that would create plots of data that would be read from a file.
Professor Maxwell gave us two different skeletons to work with, and our goal was to write appropriate code for each method that would allow the class to function properly when tested.
For the first class, which we called the Datum class, we had to write code for an __init__ , set, draw, drawParameters, move, undraw, setFill, read and write methods, as well as accessor functions for X, Y, and Z. These methods are attributes of the class that will allow the Datum object (a Zelle graphics circle) to perform different actions. The role of the __init__ method is special for the Python language. The role of __init__ is to provide intial values for the instance variables of the Datum object being created. We accomplish this with a set function that is called within the __init__ method. The set method acts like the constructor for the Datum object. The set method if a function that sets values to each of the object fields. It sets the x, y, and z fields and the visual draw parameters x0, y0, dr, and color to variables that will be used in the methods for the rest of the Datum class. The only field that the __init__ method holds that is not in in the set function is a field for a Zelle circle object that will visually represent the datum points in a plot. The example code below illustrates how the data fields in the Datum class object are created:
As an aside note, that each method has a first parameter "self." This is special because it always contains a reference to the object on which the method is acting. Thus the self parameter allows the fields in the object to be accessed within many different methods
The setdrawParameters was a light version of set function displayed above. It sets the visible draw locations of the datum points by seting the self.x0, self.y0, and self.dr fields to parameters entered when the function is called. For the draw method, I created a function that would draw a zelle graphics object circle for to represent a datum point. The draw methods took arguments that were created by the fields created for the visual datum point, and the circle was draw at the x-coordinate of self.x0 and the y-coordinate self.y0, with a radius of self.dr, and a filled with a color stored in the self.color field. The draw function first had to check to see if the circle field was equal to None (as it was set in the __init__ method), and if not, to move the visual data representation to the new location and draw it again. Notice how the fields that hold the visual datum point representations (self.x0, self.y0, self.dr) are accessed inside the draw method I created:
The setFill, undraw, and move methods are all very straight foward. They call the respective zelle graphics package methods on the self.circle field to perform the appropriate method. The only additional code necessary for each method is a check on the self.circle field to make sure that it is equalto None. The last two methods, the write and the read function were tricky. Each took only one parameter, a file. For the read function, I used the readline() function to read one line of a file, and stored it into a variable. The using the split function from the Zelle sting package, I split the string into three parts (one for each x, y, and z value), and then stored this list into a different variable, "subDivide". Using a conditional if statement, I checked to make sure the read function was successful by checking that the length of the string was greater than or equal to three, and then indexed the appriate values in the subDivide list into the self.x, .y, and .z fields, and returned True at the end. The write method had to write a data point to the current line of a file, given the file reference. To do this I used a formatted string to take a given self.x, self.y, self.z into floating point values, and I stored this string into a variable. Then I used the write() function to write the string stored into the variable into the given file reference.
After completing all of the methods, I was able to run Professor Maxwell's test function and I got the following result. Each time the "click to continue" text changes color represents a mouse click and a next step in the test function.
The data collection skeleton created by Professor Maxwell only required us to write code for three methods. The first was an append method. Given a datum point, it appends it to the existing list of datum points created in the data collection skeleton. All this required was finding the list that Professor created in the skeleton that held the list, "self.data", and then using the append method to add the datum point to the list. The write method takes a given file and writes all the datum points into the file. In Professor's read method, he sets the variable dm equal to the Datum() class we previously created. For the write method, we encorporate this dm variable into a for loop that iterates for every dm (Datum object) in the self.data list. Then using the write method, each Datum object was written to the given file reference. And finally, the last method, the move method. This one is slightly more complex than the last one. It uses a similar for loop as in the write method to move each item in the self.data list by an amounted given by the user, but it also has to move the background. If the background did not move with the data points, then the data points could be moved off of the area contained by the background. Testing these methods with Professor's test function I got the following result:
I need to thank Brittany Thomas for helping me with this project, especially with the methods for the data collection skeleton.