Presentation is loading. Please wait.

Presentation is loading. Please wait.

LECTURE 27 Designing Classes. The Course Class  What data members do we need to represent a class?

Similar presentations


Presentation on theme: "LECTURE 27 Designing Classes. The Course Class  What data members do we need to represent a class?"— Presentation transcript:

1 LECTURE 27 Designing Classes

2 The Course Class  What data members do we need to represent a class?

3 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

4 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

5 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?

6 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 = []

7 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

8 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__()

9 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

10 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

11 add_student()  What parameters will this method need? Is there anything that can go wrong?

12 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)

13 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()

14 A Whole list of Course objects  Suppose I have all the course information in some file’ CSC 117 4 28 Intro to Computer Science CSC 223 4 15 Intermediate Programming and Data Structures ECO 110 3 30 Intro to Economics ECO 210 3 30 Macroeconomic Analysis ECO 220 3 30 Microeconomic Analysis BIO 110 4 28 The Unity and Diversity of Life BIO 210 4 28 Intro to Evolutionary Genetics

15 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()


Download ppt "LECTURE 27 Designing Classes. The Course Class  What data members do we need to represent a class?"

Similar presentations


Ads by Google