Object-Oriented Programming and class Design

Slides:



Advertisements
Similar presentations
Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors.
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
C++ data types. Structs vs. Classes C++ Classes.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
ECE122 Feb. 22, Any question on Vehicle sample code?
CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
P YTHON ’ S C LASSES Ian Wynyard. I NTRODUCTION TO C LASSES A class is the scope in which code is executed A class contains objects and functions that.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Scoping and Namespaces CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Chapter 7 Objects and Classes
Introduction to Programming G50PRO University of Nottingham Unit 9 : Classes and objects Paul Tennent
Software Development I/O and Numbers
Making Choices with if Statements
COMPSCI 107 Computer Science Fundamentals
Fundamentals of Programming I Managing the Namespace
This technique is Called “Divide and Conquer”.
Lecture VI Objects The OOP Concept Defining Classes Methods
Lecture 14 Writing Classes part 2 Richard Gesick.
CS 302 Week 11 Jim Williams, PhD.
C++ Classes & Object Oriented Programming
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Lecture 13 Writing Classes Richard Gesick.
Chapter 9 Objects and Classes
Chapter 8 Objects and Classes
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Recap Week 2 and 3.
Classes.
Objects and Classes Creating Objects and Object Reference Variables
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Module 3 Selection Structures 4/4/2019 CSE 1321 Module 3.
Module 4 Loops and Repetition 4/4/2019 CSE 1321 Module 4.
JAVA An Introduction to Java OOP Basics
Introduction to Objects & Classes
Module 2 - Part 1 Variables, Assignment, and Data Types
Chapter 6 Objects and Classes
Object-Oriented Programming and class Design
The Lifecycle of an Object
Test Automation For Web-Based Applications
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Chapter 8 Objects and Classes
Object-Oriented Programming
Object-Oriented Programming and class Design
Chapter 9 Objects and Classes Part 01
OO Programming Concepts
Making our own Classes and objects
Object-Oriented Design AND CLASS PROPERTIES
C++ data types.
Chapter 9 Objects and Classes
Object-Oriented Programming and class Design
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Chapter 7 Objects and Classes
Slides Courtesy: Prof. Paul Fodor, SBU
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

Object-Oriented Programming and class Design Ps Module 6 Object-Oriented Programming and class Design 5/15/2019 CSE 1321 Module 5

Class Circle Pseudocode BEGIN CREATE Radius ← 1.0 CONSTRUCTOR Circle //called default constructor BEGIN END CONSTRUCTOR CONSTRUCTOR Circle (NewRadius) //called constructor Radius ← NewRadius END CONSTRUCTOR METHOD getArea () //compute and return circle area RETURN (Radius * Radius * 3.14159) END METHOD END CLASS Ps 4/26/2018 CSE 1321 Module 6 5

Python Class Circle import math class Circle: #construct a circle object def __init__(self, radius): self.radius = radius #get area method def getArea(self): return self.radius * self.radius * math.pi 8 4/26/2018 CSE 1321 Module 6

Ps Driver in Pseudocode CLASS TestCircle BEGIN CREATE circle1, circle2 AS Circle // create two variables circle1 ← NEW circle() // create circle1 object circle2 ← NEW circle(25.0) // create circle2 object PRINT ("Circle 1 area = " + circle1.getArea()) PRINT ("Circle 2 area = " + circle2.getArea()) END TestCircle Outputs: Circle 1 area = 3.14159 Circle 2 area = 1963.4937499999999 Ps 4/26/2018 CSE 1321 Module 6 5

Python Class TestCircle def main(): #create circle with radius 1 circle1 = Circle(1) print ('The area of the circle with radius', circle1.radius, 'is', circle1.getArea()) #create circle with radius 25 circle2 = Circle(25) print ('The area of the circle with radius', circle2.radius, 'is', circle2.getArea()) main() #call to main function 4/26/2018 CSE 1321 Module 6 8

Example CLASS BMW_Z4 BEGIN CONSTRUCTOR BMW_Z4() // constructor #1 BEGIN ModelYear ← 2004 TopUp ← false LicensePlate ← "DEALER" END CONSTRUCTOR CONSTRUCTOR BMW_Z4(parameter: year) // constructor #2 BEGIN ModelYear ← year TopUp ← false LicensePlate ← "DEALER" END CONSTRUCTOR END CLASS Ps 5/15/2019 CSE 1321 Module 6 6

Python – Overload Constructor Example Python doesn’t support overloading, but there are ways to work around this. We can still write only one constructor, but create it so that it accept multiple arguments. Then, in the constructor, we can look for certain values and assign them accordingly, as shown below: #construct a BMW object def __init__(self, modelYear = 2018, TopUp = True, licensePlate = "DEALER"): self.modelYear = modelYear self.topUp = TopUp self.licensePlate = licensePlate 5/15/2019 CSE 1321 Module 6 7

Properties Example Pseudocode CLASS BMW_Z4 BEGIN PRIVATE ModelYear PRIVATE LicensePlate PUBLIC METHOD SetModelYear (Year) BEGIN ModelYear ← Year END PUBLIC METHOD GetModelYear () BEGIN RETURN ModelYear END PUBLIC METHOD SetLicensePlate (value) BEGIN LicensePlate ← value END PUBLIC METHOD GetLicensePlate () BEGIN RETURN LicensePlate END END CLASS Ps 4/26/2018 CSE 1321 Module 6 5

Class BMW_Z4 class BMW: def setModelYear(self, modelYear): self.modelYear = modelYear def getModelYear(self): return self.modelYear def setTopUp(self, topUp): self.topUp = topUp def getTopUp(self): return self.topUp def setLicensePlate(self, licensePlate): self.licensePlate = licensePlate def getLicensePlate(self): return self.licensePlate 8 4/26/2018 CSE 1321 Module 6