Making our own Classes and objects

Slides:



Advertisements
Similar presentations
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Advertisements

Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
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:
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
Creating your own classes class Elephant(object): """A virtual pet""" def __init__(self, name, age, weight, trunkradius, trunklength): self.name = name.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
Chapter 6 Horstmann Programs that make decisions: the IF command.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
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.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Classes and Objects, Part 1 Victor Norman CS104. Reading Quiz, Q1 A class definition define these two elements. A. attributes and functions B. attributes.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
Introduction to Objects A way to create our own types.
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.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
How to start Visual Studio 2008 or 2010 (command-line program)
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL.Net Assignments K. R. C. Wijesinghe Trainer Virtusa Corporation.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.
Agenda Object Oriented Programming Reading: Chapter 14.
Polymorphism, Abstraction and Virtual Functions. In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
Area of Complex Figures. What are complex figures? Figures that can be subdivided into simple figures.
Weak Entity Sets A weak entity is an entity that cannot exist in a database unless another type of entity also exists in that database. Weak entity meets.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Arithmetic, Class Variables and Class Methods Week 11
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.
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.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
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.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
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.
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.
Inheritance Doing More with Your Classes. Review of Classes Collection of instance variables and instance methods Treated like a data type Encapsulation.
What Do You Expect? MEAN, MEDIAN, MODE. Average Salary? What do you expect the average employee at McDonald’s makes?
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.
Our project is over finding area and perimeter of triangles circles rectangle and trapezoid.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
Object Oriented Programming
Data Structures and Algorithms revision
Classes and OOP.
CS-104 Final Exam Review Victor Norman.
UNIT 8: 2-D MEASUREMENTS PERIMETER AREA SQUARE RECTANGLE PARALLELOGRAM
Object Oriented Programming
CS 302 Week 10 Jim Williams.
Learning Objectives Inheritance Virtual Function.
Writing Methods AP Computer Science A.
Coding Concepts (Sub- Programs)
Data Modeling with Entity Relationship Diagrams (Cont.)
Arrays .
Fundamentals of Programming I Commonly Used Methods More Modeling
Python classes: new and old
Object-Oriented Programming and class Design
Python classes: new and old
Just Enough Java 17-May-19.
By Rajanikanth B Classes in Java By Rajanikanth B
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: $542.79 as a balance Roderick Feckelbocker as the owner’s name

Classes: creating our own Types Making more complex Types Simple types: Int Float Boolean String Double Think of lists: Lists are a more complex 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?

Making new classes Examples of things we might want to make classes for: Students: Properties: lastname, firstname, Methods (functions) –calculate gpa, calculate grade in class, calculate when you’ll graduate, calculate how much you owe Bank account: Properties: name, account#, balance Methods (functions addmoney, removemoney, calculateinterest,etc.) Anything in which you want to group together different properties and functions into one element

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,w,h): self.height = h self.width = w def getArea(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) x = Rectangle(10, 10) y = Rectangle(20, 5) print(x.area()) print(y.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 x = Rectangle(10, 10) #[EXAMPLES] y = Rectangle(20, 5) print(x.area()) print(y.boxvol(5)) #weird one, but you can do it! print(Rectangle(6,8).area()) class 1 (started matlab)

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 objects.

Employee Class class Employee: def __init__(self, n, s): #Again,2 underscores on both sides!! 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)

Student class? Properties: Methods: calcGrade? first name, last name, lab score, exam score, Project score Methods: calcGrade?

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 calcgrade(self): x= self.lab * 0.25 + self.exam * 0.5 + self.project * 0.25 return(x)

Write a class for a circle? Functions? Properties?

class Circle(object): def __init__(self, x,y,z): self.radius = x self.area=y self.circumference=z x= Circle(3,7,8) Problem here: area and circumference are dependent on the radius Can you write methods to calculate the area and the circumference?

What type should it return? class Circle(object): def __init__(self,k): self.radius = k self.circumference = self.getcirc() self.area = self.calcarea() def getcirc(self): return(self.radius * 2 * pi) def calcarea(self): return(self.radius **2 * pi) def changerad(self,k): x = Circle(3) Print(x.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,k): self.radius = k self.circumference = self.getcirc() self.area = self.calcarea() def getcirc(self): return(self.radius * 2 * pi) def calcarea(self): return(self.radius **2 * pi) def isbigger(self, circ2): return(self.area > circ2.area) x = Circle(3) y = Circle(4) z = Circle(2) print(x.area) print(x.isbigger(y)) print(x.isbigger(z))

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 * 0.25 + self.exam * 0.5 + self.project * 0.25 return(x) Now make a student. Print his/her grade.