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
3. Example 1
Create a new Project in
shp1 = IrregularShape()
Aptana "Lab2", then shp1.AddShape(Square(3))
add a new PyDev shp1.AddShape(Triangle(4, 5))
shp1.AddShape(Square(10))
module called main print shp1.Area()
Write the following code ========
in your main file: Output:
119.0
class Square:
def __init__(self, l):
self.length = l
def Area(self):
return self.length**2
class Triangle:
def __init__(self, b, h):
4. Example 1
class Square:
def __init__(self, l):
self.length = l
def Area(self):
return self.length**2
class Triangle:
def __init__(self, b, h):
self.base = b
self.height = h
def Area(self):
return 0.5*self.base*self.height
5. Example 1
class IrregularShape:
def __init__(self):
self.shapes = []
def AddShape(self, shape):
self.shapes.append(shape)
def Area(self):
area = 0
for shape in self.shapes:
area += shape.Area()
return area
7. 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
9. Example 2
Type this SquareMatrix def addNumber(self, number):
resultMatrix = SquareMatrix()
class in your main file:
matrixDimension = len(self.matrix)
for rowIndex in range(0,matrixDimension):
#square matrix only
newRow = []
class SquareMatrix:
for columnIndex in range(0,matrixDimension):
def __init__(self):
self.matrix = [] newRow.append(self.matrix[rowIndex][columnIndex] +
number)
def appendRow(self, row): resultMatrix.appendRow(newRow)
self.matrix.append(row)
return resultMatrix
def printMatrix(self):
for row in self.matrix:
Output:
================================ [-1, 1]
print row mat = SquareMatrix() [8, 4]
mat.appendRow([0, 2])
mat.appendRow([9, 5])
mat.addNumber(-1)
10. 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
11. Example 2
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() Output:
mat.appendRow([0, 2]) [-1, 1]
[8, 4]
mat.appendRow([9, 5])
mat.addNumber(-1)
mat.printMatrix()
12. 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
13. 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]) 1 2 1 -2 2 0
mat1.appendRow([4, 5])
4 5 -5 1 -1 6
mat2 = SquareMatrix()
mat2.appendRow([1, -2])
mat2.appendRow([-5, 1])
mat3 = mat1.add(mat2)
mat3.printMatrix()
14. 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() Fun: add 3 matrices in one line like this:
mat1.appendRow([1, 2]) mat1.add(mat2).add(mat3).printMatrix()
mat1.appendRow([4, 5])
mat2 = SquareMatrix()
mat2.appendRow([1, -2])
mat2.appendRow([-5, 1])
15. 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 5 -5
-1 1 5 -5 5
16. Exercise 3 (Solution)
def multiplyNumber(self, times):
result = self
for 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)