Making our own Classes and objects  As in real life, we’re now creating classes of objects.  a class that defines basic characteristics and functions.

Slides:



Advertisements
Similar presentations
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
Advertisements

Classes and Inheritance. 2 As the building blocks of more complex systems, objects can be designed to interact with each other in one of three ways: Association:
Sorting a class of students? class Student(object): def __init__(self, lastname, firstname, testscore, labscore): self.lastname = lastname self.firstname.
Creating your own classes class Elephant(object): """A virtual pet""" def __init__(self, name, age, weight, trunkradius, trunklength): self.name = name.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/18/2012i206 Lab 1 - Exercise1.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Python classes: new and old. New and classic classes  With Python 2.2, classes and instances come in two flavors: old and new  New classes cleaned up.
Classes and Objects, Part 1 Victor Norman CS104. Reading Quiz, Q1 A class definition define these two elements. A. attributes and functions B. attributes.
Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then.
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING DATA ABSTRACTION Jehan-François Pâris
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
Classes 3 COMPSCI 105 S Principles of Computer Science.
Facts about Area of Shapes Dr. Kent Bryant 5/2011.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Practice with Area, Perimeter and Circumference Sometimes we have to find area or perimeter of odd shapes. Find the area of the following shape. 15 m.
Polymorphism, Abstraction and Virtual Functions. In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
Objects and Classes Procedural Programming A series of functions performing specific tasks Data items are passed from one function to another by arguments.
PYTHON OBJECTS & CLASSES. What is an object? The abstract idea of anything What is in an object: Attributes Characteristics Represented by internal variables.
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Object Oriented Programming In Python
Python – May 19 Review –What is the difference between: list, tuple, set, dictionary? –When is it appropriate to use each? Creating our own data types:
Classes and Objects, Part 1 Victor Norman CS104. “Records” In Excel, you can create rows that represent individual things, with each column representing.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.
Perimeter and Area Formulas.  Perimeter is the distance around an object. It is easily the simplest formula. Simply add up all the sides of the shape,
INTRO2CS Tirgul 8 1. What We’ll Be Seeing Today  Introduction to Object-Oriented Programming (OOP).  Using Objects  Special methods 2.
Week 5 Extending classes in Java. Extending classes u Using an existing classes as the basis for a new one u Defining only the differences between the.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Python programming - Defining Classes
Object Oriented Programming
Data Structures and Algorithms revision
Classes and OOP.
Software Development Java Classes and Methods
CS-104 Final Exam Review Victor Norman.
Fundamentals of Programming I More Data Modeling
Object Oriented Programming in Python
Engineering Computing
Object-oriented programming
Introduction to Object-Oriented Programming (OOP) II
Learning Objectives Inheritance Virtual Function.
Python Classes Python has Classes, Methods, Overloaded operators, as well as Inheritance. Its somewhat 'loose' in the since it does not have true encapsulation,
Built-In Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work.
Suggested Layout ** Designed to be printed on white A3 paper.
Fundamentals of Programming I Commonly Used Methods More Modeling
Fundamentals of Programming I More Data Modeling
Conrad Huang Genentech Hall, N453A x6-0415
By Rajanikanth B Classes in Java By Rajanikanth B
Making our own Classes and objects
Terry Scott University of Northern Colorado 2007 Prentice Hall
Introduction to Object-Oriented Programming (OOP) II
Object-Oriented Programming and class Design
Presentation transcript:

Making our own Classes and objects  As in real life, we’re now creating classes of objects.  a class that defines basic characteristics and functions that apply to all objects of that class  Think of a class as a set of functions and variables that belong together to define a particular thing.  E.g., Savings Account classes would have:  A balance #variable, or attribute  An owner’s name #variable, or attribute  The ability to add money to the account #function, or method  The ability to withdraw money from the account #function, or method  An individual’s Savings Account might have:  $ as a balance  Roderick Feckelbocker as the owner’s name

Like creating our own Types  Think of lists:  Lists are a type.  They have methods (functions) associated with them  E.g.,  alist.append(x)  alist.index(x)  alist.pop()  Append(), index(), and pop() are examples of methods (functions) associated with lists.  You can’t use the pop() method (function) with something of type int, right?

If we were to write this: class OurList(object): def __init__(self, alist ): self.thisList = alist # a property! def ourPop(self): # Notice this is still part of the OurList class! x = self.thisList[len(self.thisList) – 1] self.thisList = self.thisList[0:len(self.thisList)-1] return(x)

To create something of the class OurList: class OurList(object): def __init__(self, alist ): self.thisList = alist # a property! def ourPop(self): x = self.thisList[len(self.thisList) – 1] self.thisList = self.thisList[0:len(self.thisList)-1] return(x) ls = OurList([3,2,4,1,5]) # Note that now we have to tell the computer what Class type we’re creating y = ls.ourPop()

Let’s make a class for Rectangles: What methods do we want our rectangle class to have? What properties? Class Rectangle(object): def __init__(self,h,w): self.height = h self.width = w def area(self): return(self.height * self.width)

Let’s make a class for Rectangles: What methods do we want our rectangle class to have? What properties? Class Rectangle(object): def __init__(self,h,w): self.height = h self.width = w def area(self): return(self.height * self.width) rect1 = Rectangle(10, 10) rect2 = Rectangle(20, 5) print(rect1.area())

class Rectangle(object): def __init__(self, width, height): #[CONSTRUCTOR] self.width = width self.height = height def area(self): return self.width * self.height def boxvol(self, x ): return self.width * self.height * x rectangle1 = Rectangle(10, 10) #[EXAMPLES] rectangle2 = Rectangle(20, 5) print(rectangle1.area()) print(rectangle2.boxvol(2)) print(Rectangle(6,8).area())

Classes  Does the problem involve data that is related to a single concept? (e.g., a student, an airline ticket, a car)  Write down the kinds of data in the problem and organize them into classes (some problems may need multiple classes).  Write:  class definition  a list of properties belonging to objects of that class  a constructor (__init__)  An example function  some example class instances.

Employee Class class Employee: def __init__(self, n, s): self.name = n self.salary = s def displayEmployee(self): print("Name : "+self.name+", Salary: "+ self.salary) x = Employee(“bob”,32000) x.displayEmployee()

Employee Class class Employee: def __init__(self, n, s): self.name = n self.salary = s def displayEmployee(self): print("Name : "+self.name+", Salary: "+ self.salary) def getRaise(self,x): self.salary = self.salary + x x = Employee(“bob”,32000) x.displayEmployee() x.getRaise(2000) x.displayEmployee()

Student Class? class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project def getgrade(self): x= self.lab * self.exam * self.project * 0.25 return(x)

Write a class for a circle?  Functions?  Properties?

class Circle(object): """ A circle is a shape with radius, a circumference, and an area radius - number circumference – number (2*pi*radius) area – number (pi*radius**2) """ def __init__(self, x,y,z): self.radius = x self.area=y self.circumference=z x= Circle(3,7,8)

class Circle(object): def __init__(self,x): self.radius = x self.circumference = self.getcirc() self.area = 7 def getcirc(self): return(self.radius * 2 * pi) def area(self): return(self.radius **2 * pi) def changerad(self,x): self.radius = x self.circumference = self.getcirc() circ1 = Circle(3) Print(circ1.area) circ1.changerad(4) Print(circ1.area()) Can we include in the class a function that checks if this circle is bigger than another circle? What type should it return?

from math import * class Circle(object): def __init__(self,x): self.radius = x self.circumference = self.getcirc() self.area = self.getarea() def getcirc(self): return(self.radius * 2 * pi) def getarea(self): return(self.radius **2 * pi) def isbigger(self, circ2): return(self.area > circ2.area) firstcirc = Circle(3) secondcirc = Circle(4) thirdcirc = Circle(2) print(firstcirc.area) print(firstcirc.isbigger(secondcirc)) print(firstcirc.isbigger(thirdcirc))

Student – what if we wanted a grade property? class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project self.grade = self.getgrade() def getgrade(self): x= self.lab * self.exam * self.project * 0.25 return(x) Now make a student. Print his/her grade.

Printing a student object? class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project self.grade = self.getgrade() def getgrade(self): x= self.lab * self.exam * self.project * 0.25 return(x) def __str__(self): return(self.lastname + ","+self.firstname+": \n\tLab: "+\ str(self.lab) + "\n\tExam: " + str(self.exam) +\ "\n\tProject: "+str(self.project)+"\n\tGrade: "+str(self.grade)) x = Student('Bill','Williams',88,76,94) print(x) y = Student('Anne','Stewart',97,67,73) print(y)

What about comparing students? class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project self.grade = self.getgrade() def getgrade(self): x= self.lab * self.exam * self.project * 0.25 return(x) def __str__(self): return(self.lastname + ","+self.firstname+": \n\tLab: "+\ str(self.lab) + "\n\tExam: " + str(self.exam) +\ "\n\tProject: "+str(self.project)+"\n\tGrade: "+str(self.grade)) def __lt__(self,x): if (self.lastname != x.lastname): return self.lastname < x.lastname elif (self.firstname != x.firstname): return(self.firstname < x.firstname) else: return(self.grade < x.grade)

class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project self.grade = self.getgrade() def getgrade(self): x= self.lab * self.exam * self.project * 0.25 return(x) def __str__(self): return(self.lastname + ","+self.firstname+": \n\tLab: "+\ str(self.lab) + "\n\tExam: " + str(self.exam) +\ "\n\tProject: "+str(self.project)+"\n\tGrade: "+str(self.grade)) def __lt__(self,x): if (self.lastname != x.lastname): return self.lastname < x.lastname elif (self.firstname != x.firstname): return(self.firstname < x.firstname) else: return(self.grade < x.grade) x = Student("Robert","Jones",64,71,86) print(x) y = Student("Tom","Miller",72,44,87) print(y) z = Student("Anne","Jones",87,78,92) x = Student("Robert","Jones",64,71,86) print(x) y = Student("Tom","Miller",72,44,87) print(y) z = Student("Anne","Jones",87,78,92) if (x < y): print('hi') else: print('low') if (x < z): print('hi2') else: print('low2')

Others: __eq__(self, other) Defines behavior for the equality operator, ==. __ne__(self, other) Defines behavior for the inequality operator, !=. __lt__(self, other) Defines behavior for the less-than operator, <. __gt__(self, other) Defines behavior for the greater-than operator, >. __le__(self, other) Defines behavior for the less-than-or-equal-to operator, <=. __ge__(self, other) Defines behavior for the greater-than-or-equal-to operator, >=.