LECTURE 27 Designing Classes
The Course Class What data members do we need to represent a class?
The Course Class What data members do we need to represent a class? Possible choices Department Course number Maximum number Number of Hours Title List of students
The course class continued What about the methods? A constructor A __str__ to print some or all of the data in the object The “get” functions An add_student() method to put a new student in the class
The constructor The object representing each class will be created before anyone registers for the class. What things will I know at the very beginning? What parameters will have to be supplied when it is created and which data members will simply be given default values?
def __init__(self, dept, code, max, hrs, aTitle): self.dept = dept self.nbr = code self.max = max self.hrs = hrs self.title = aTitle self.count = 0 self.students = []
The __str__() function This is a design issue – you choose what you want to have available to you when you call the “print” command on a Course object. I will print the department, number and title
def __str__(self): s = self.dept + self.nbr + " " + self.title return s Remember: __str__() should always return a string If c1 is a Course object, print c1 will actually call c1.__str__() to get the string to print print c1 is actually equivalent to print c1.__str__()
The “getters” Most of these are completely straightforward def get_dept(self): return self.dept def get_nbr(self): return self.nbr def get_max(self): return self.max def get_hrs(self): return self.hrs def get_count(self): return self.count
get_students() Here one can make a choice – it could return the list of students, or a nicely formatted string – it is the designer’s choice def get_students(self): #return a string giving the names formatted one #name per line theList = "" for s in self.students: theList = theList + s + "\n" return theList
add_student() What parameters will this method need? Is there anything that can go wrong?
add_student() What parameters will this method need? Is there anything that can go wrong? def add_student(self, name): if self.count < self.max: self.count = self.count+1 self.students.append(name)
Testing my class Write a main() that creates a few members of this class and then perhaps try a few of the methods. def main(): c1 = Course("CSC", "117", 28,4, "Intro to Computer Science") print c1 c1.add_student("Mary Smith") c1.add_student("Bill Brown") c1.add_student("George Wilson") print c1.get_students()
A Whole list of Course objects Suppose I have all the course information in some file’ CSC Intro to Computer Science CSC Intermediate Programming and Data Structures ECO Intro to Economics ECO Macroeconomic Analysis ECO Microeconomic Analysis BIO The Unity and Diversity of Life BIO Intro to Evolutionary Genetics
A program to create a list of Courses import course import string def main(): courseList = [] infile = open("courseData.txt", "r") for line in infile: data = line.split() title = string.join(data[4:]) c = course.Course(data[0], data[1],data[3], data[2],title) courseList.append(c) print item main()