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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
Awk 1 – an introduction. Your scripts I will put them on the shared drive. Different solutions – diversity Title of (lab or question) Efficient?
Python Objects and Classes
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Functions Prototypes, parameter passing, return values, activation frams.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
Introduction to Trees Chapter Trees Why Trees? Trees are amazingly useful in Computer Science. They provide a structure for the efficient storage,
15-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
Lecture 05 – Classes.  A class provides the definition for the type of an object  Classes can store information in variables  Classes can provide methods.
Classes 2 COMPSCI 105 S Principles of Computer Science.
28/08/2015SJF L31 F21SF Software Engineering Foundations ASSUMPTIONS AND TESTING Monica Farrow EM G30 Material available on Vision.
1 CSC 221: Introduction to Programming Fall 2012 course overview  What did you set out to learn?  What did you actually learn?  Where do you go from.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.
Computing Science 1P Large Group Tutorial 17 Simon Gay Department of Computing Science University of Glasgow 2006/07.
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
10-Nov-15 Java Object Oriented Programming What is it?
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.
ACO 101: Intro to Computer Science Anatomy Part 3: The Constructor.
Word By Mrs. Yollis Chaparral Elementary. is a computer program that allows you to create written documents.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Lecture 09 – Classes.  At the end of this lecture, students should be able to:  Define a new class  Store state information about instances of the.
Chapter 17 Q and A Victor Norman, et al. CS104. What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects,
The Smart board Quiz Change the title and put intro information here.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Module 7: Constructors #1 2000/2001Scientific Computing in OOCourse code 3C59 Module 7: Constructors and Destructors: In this module we will cover: Constructors.
Computer Science 112 Fundamentals of Programming II Iterators.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
1 CS Sept Customizing a class & testing Quote for the day: There is no reason anyone would want a computer in their home. - -Ken Olson, founder.
CSC 231: Introduction to Data Structures Python and Objects – Day 3 Dr. Curry Guinn.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Andrew(amwallis) Classes!
Welcome to the Community College of Rhode Island On Line Economics
Classes and Objects – digging deeper
Example: Vehicles What attributes do objects of Sedan have?
CSC 458– Predictive Analytics I, Fall 2017, Intro. To Python
Department of Computer Science,
CSC 131: Introduction to Computer Science
Lecture Note Set 1 Thursday 12-May-05
CSC 113: Computer programming II
ITunes Lab Copyright © 2012 Pearson Education, Inc.
CSc 110, Spring 2017 Lecture 17: Line-Based File Input
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Adapted from slides by Marty Stepp and Stuart Reges
CSC 458– Predictive Analytics I, Fall 2018, Intro. To Python
Fundamentals of Programming I Commonly Used Methods More Modeling
Beginning Style 27-Feb-19.
Introduction to Objects & Classes
CSC 111 Exam 3 Review Fall 2005.
EE 194/BIO 196: Modeling,simulating and optimizing biological systems
Lecture 18 Python OOP.
CISC101 Reminders Assignment 3 due today.
def-ining a function A function as an execution control structure
Presentation transcript:

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