Python object oriented programming (lab2) (2)

16

Click here to load reader

Transcript of Python object oriented programming (lab2) (2)

Page 1: Python object oriented programming (lab2) (2)

Play with PythonLab2

Object Oriented Programming

Page 2: Python object oriented programming (lab2) (2)

About Lecture 2 and Lab 2

Lecture 2 and its lab aims at covering Basic Object Oriented Programming concepts

Classes, Constructor (__init__) and Objects

Page 3: Python object oriented programming (lab2) (2)

Example 1

Create a new Project in Aptana "Lab2", then add a new PyDev module called main

Write the following code in your main file:

class Square:def __init__(self, l):self.length = ldef Area(self):return self.length**2

class Triangle:def __init__(self, b, h):

self.base = bself.height = h

def Area(self):return 0.5*self.base*self.height

class IrregularShape:def __init__(self):

self.shapes = []def AddShape(self, shape):self.shapes.append(shape)def Area(self):area = 0for shape in self.shapes:

area += shape.Area()return area

shp1 = IrregularShape()shp1.AddShape(Square(3))shp1.AddShape(Triangle(4, 5))shp1.AddShape(Square(10))

print shp1.Area()

========Output:119.0

Page 4: Python object oriented programming (lab2) (2)

Example 1

class Square:def __init__(self, l):

self.length = ldef Area(self): return self.length**2

class Triangle:def __init__(self, b, h):

self.base = bself.height = h

def Area(self):return 0.5*self.base*self.height

Page 5: Python object oriented programming (lab2) (2)

Example 1

class IrregularShape:def __init__(self):

self.shapes = []def AddShape(self, shape):self.shapes.append(shape)def Area(self):area = 0for shape in self.shapes:

area += shape.Area()return area

Page 6: Python object oriented programming (lab2) (2)

Example 1

shp1 = IrregularShape()shp1.AddShape(Square(3))shp1.AddShape(Triangle(4, 5))shp1.AddShape(Square(10))

print shp1.Area()

========Output:119.0

Page 7: Python object oriented programming (lab2) (2)

Exercise 1 (5 Minutes)

Add another IrregularShape that has double the area of shp1 in the previous example, ONLY by using shp1 object that you have just created, not using any other shapes

Page 8: Python object oriented programming (lab2) (2)

Exercise 1 (Solution)

shp2 = IrregularShape()shp2.AddShape(shp1)shp2.AddShape(shp1)print shp2.Area()

=====Output:238.0

Page 9: Python object oriented programming (lab2) (2)

Example 2

Type this SquareMatrix class in your main file:

#square matrix only

class SquareMatrix:

def __init__(self):

self.matrix = []

def appendRow(self, row):

self.matrix.append(row)

def printMatrix(self):

for row in self.matrix:

print row

def addNumber(self, number):

resultMatrix = SquareMatrix()

matrixDimension = len(self.matrix)

for rowIndex in range(0,matrixDimension):

newRow = []

for columnIndex in range(0,matrixDimension):

newRow.append(self.matrix[rowIndex][columnIndex] + number)

resultMatrix.appendRow(newRow)

return resultMatrix

================================

mat = SquareMatrix()

mat.appendRow([0, 2])

mat.appendRow([9, 5])

mat.addNumber(-1)

mat.printMatrix()

Output: [-1, 1][8, 4]

Page 10: Python object oriented programming (lab2) (2)

Example 2

#square matrix only

class SquareMatrix:

def __init__(self):

self.matrix = []

def appendRow(self, row):

self.matrix.append(row)

def printMatrix(self):

for row in self.matrix:

print row

Page 11: Python object oriented programming (lab2) (2)

Example 2def addNumber(self, number):

resultMatrix = SquareMatrix()

matrixDimension = len(self.matrix)

for rowIndex in range(0,matrixDimension):

newRow = []

for columnIndex in range(0,matrixDimension):

newRow.append(self.matrix[rowIndex][columnIndex] + number)

resultMatrix.appendRow(newRow)

return resultMatrix

================================

mat = SquareMatrix()

mat.appendRow([0, 2])

mat.appendRow([9, 5])

mat.addNumber(-1)

mat.printMatrix()

Output: [-1, 1][8, 4]

Page 12: Python object oriented programming (lab2) (2)

Example 2

This class represents a square matrix (equal row, column dimensions), the add number function

matrix data is filled by row, using the appendRow() function

addNumber() function adds a number to every element in the matrix

Page 13: Python object oriented programming (lab2) (2)

Exercise 2 (10 minutes)

Add a new fucntion add() that returns a new matrix which is the result of adding this matrix with another matrix:

mat1 = SquareMatrix()

mat1.appendRow([1, 2])

mat1.appendRow([4, 5])

mat2 = SquareMatrix()mat2.appendRow([1, -2])mat2.appendRow([-5, 1])

mat3 = mat1.add(mat2)

mat3.printMatrix()

1 24 5

1 -2-5 1

2 0-1 6

Page 14: Python object oriented programming (lab2) (2)

Exercise 2 (Solution)

def add(self, otherMatrix):resultMatrix = SquareMatrix()matrixDimension = len(self.matrix)

for rowIndex in range(0,matrixDimension):newRow = []for columnIndex in range(0,matrixDimension):

newRow.append(self.matrix[rowIndex][columnIndex] + otherMatrix.matrix[rowIndex][columnIndex])

resultMatrix.appendRow(newRow)return resultMatrix

=======================mat1 = SquareMatrix()mat1.appendRow([1, 2])mat1.appendRow([4, 5])

mat2 = SquareMatrix()mat2.appendRow([1, -2])mat2.appendRow([-5, 1])

mat1.add(mat2).printMatrix()

Fun: add 3 matrices in one line like this:mat1.add(mat2).add(mat3).printMatrix()

Page 15: Python object oriented programming (lab2) (2)

Exercise 3 (10 minutes)

Add a new function mutliplyNumber(t), which multiplies a positive integer t to the matrix, ONLY using the add(othermatrix) method

1 -1-1 1

5 -5-5 55

Page 16: Python object oriented programming (lab2) (2)

Exercise 3 (Solution)

def multiplyNumber(self, times):result = selffor i in range(0, times-1):result = result.add(self)

return result==========================

mat = SquareMatrix()mat.appendRow([1, -1])mat.appendRow([-1, 1])

mat.multiplyNumber(5)