DOC PREVIEW
USF CS 110 - Object-Oriented Programming

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Object-Oriented ProgrammingHistorical PerspectiveGrouping DataGrouping Data in PythonPython ClassesTerminologyIn-Class Problems 1Class functions (methods)In Class Problems 2CS 110: Introduction to Computer Science Spring 2008Object-Oriented ProgrammingHistorical PerspectiveObject-oriented programming was a huge conceptual change in 80s and 90s.Simula, Algol, Smalltalk were early O-O languagesC++ was born from the language‘C’ Java was born Grouping DataOur brains can only handle 7 +- 2 things at a time.We must create abstractions and think hierarchically.Function allow us to abstract sequence of operations.We also need to abstract groupings of data, something we call ‘complex data types’ or ‘classes’. Consider a social network site. What data might be recorded for each person in the system?Other examples of grouping data: Google resultA colorHow about the Mastermind program? What data did we use that could have been groupedbut wasn’t?Grouping Data in PythonHow would you represent a person in Python?‘Lists’ don’t really help here as you have related but heterogeneous data. You want to name each part, e.g., person.lastName, person.firstName, etc.CS 110: Introduction to Computer Science Spring 2008Python ClassesThe Class construct allows a programmer to define custom types (programmer-defined types).Similar syntax to function def, but it’s a class definition. Define your fields by setting them to initial values. class Person: # class definition name="" age=0 friends = []Note that the variables are being set outside any function!Once you define a class, you can create instances of it. We call these instances objects. Create them with an object creation statement, set them using dot notation. # main joe = Person() # this creates a Person object with name="" and age=0 bill = Person() # this creates a Person object with name="" and age=0 print joe.age # prints 0 joe.age=34 joe.name="Joe Williams" bill.name="William Joes" bill.age=22Formally, object creation statements are of the form: <object> = <nameOfClass>()Field assignment statements use "." to specify which field: <objectName>.<fieldName>TerminologyPerson is a class.joe and bill are instances of the class Person.joe and bill are objects of type Person.name and age are fields (also known as data members) of the class Person.A class can also have functions defined within it (we’ll revisit this later).CS 110: Introduction to Computer Science Spring 2008In-Class Problems 11. Write a class 'Coordinate' with two fields, x and y, which are initialized to zero.2. Write a function 'distance' which accepts two coordinates as parameters and returns thedistance between them. This function should NOT be object-oriented (define it after the class definition).3. Write a main program to test your class and function-- it should create two coordinates,call distance, then print the coordinates and the distance.To do the math, use Python's Math Module and in particular: ‘sqrt’, and ‘pow’.Class functions (methods)In the discussion above, we discussed programmer-defined types and used Python classesto represent them.We didn't really write an object-oriented function.In the world of object-oriented programming, a class is really two things: class = data fields + functions that work on that dataIn other words, we want to group data along with the specific functions that access and manipulate that data.To do this, we define functions within the class construct: class Coordinate: x=0 y=0 def distance(self,point2): # code for distanceThen we call the functions using the form: <object>.<function>For example, point1 = Coordinate() point1.x=4 point1.y=7 distance = point1.distance(point2)This is an object-oriented function call. Method is another name for function.CS 110: Introduction to Computer Science Spring 2008The first parameter is usually, by convention, called self. Self refers to the object on the left-hand side of the "." in the object-oriented function callThere are really still two parameters sent to the function distance.In Class Problems 21. Copy your coordinate program into another file.2. Rewrite the code so that distance is a method inside the Coordinate class. You'll need to rewrite the main so that distance is called in an object-oriented manner.3. Add a method 'equal' which returns true if two coordinate objects have the same x and y values. Write a test for this method and show how 'equal' is different from


View Full Document

USF CS 110 - Object-Oriented Programming

Download Object-Oriented Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object-Oriented Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-Oriented Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?