Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming and class Design

Similar presentations


Presentation on theme: "Object-Oriented Programming and class Design"— Presentation transcript:

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

2 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 * ) END METHOD END CLASS Ps 4/26/2018 CSE 1321 Module 6 5

3 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

4 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 = Circle 2 area = Ps 4/26/2018 CSE 1321 Module 6 5

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

6 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

7 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

8 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

9 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


Download ppt "Object-Oriented Programming and class Design"

Similar presentations


Ads by Google