Python object oriented programming (lab2) (2)

817 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
817
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Python object oriented programming (lab2) (2)

  1. 1. Play with Python Lab2Object Oriented Programming
  2. 2. About Lecture 2 and Lab 2Lecture 2 and its lab aims at covering Basic Object Oriented Programming conceptsClasses, Constructor (__init__) and Objects
  3. 3. Example 1Create 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.0class Square: def __init__(self, l): self.length = l def Area(self): return self.length**2class Triangle: def __init__(self, b, h):
  4. 4. Example 1class Square: def __init__(self, l): self.length = l def Area(self): return self.length**2class Triangle: def __init__(self, b, h): self.base = b self.height = h def Area(self): return 0.5*self.base*self.height
  5. 5. Example 1class 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
  6. 6. Example 1shp1 = IrregularShape()shp1.AddShape(Square(3))shp1.AddShape(Triangle(4, 5))shp1.AddShape(Square(10))print shp1.Area()========Output:119.0
  7. 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
  8. 8. Exercise 1 (Solution)shp2 = IrregularShape()shp2.AddShape(shp1)shp2.AddShape(shp1)print shp2.Area()=====Output:238.0
  9. 9. Example 2Type 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. 10. Example 2#square matrix onlyclass 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. 11. 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() Output:mat.appendRow([0, 2]) [-1, 1] [8, 4]mat.appendRow([9, 5])mat.addNumber(-1)mat.printMatrix()
  12. 12. Example 2This class represents a square matrix (equal row, column dimensions), the add number functionmatrix data is filled by row, using the appendRow() functionaddNumber() function adds a number to every element in the matrix
  13. 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 0mat1.appendRow([4, 5]) 4 5 -5 1 -1 6mat2 = SquareMatrix()mat2.appendRow([1, -2])mat2.appendRow([-5, 1])mat3 = mat1.add(mat2)mat3.printMatrix()
  14. 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. 15. Exercise 3 (10 minutes)Add a new function mutliplyNumber(t), which multiplies a positiveinteger t to the matrix, ONLY using the add(othermatrix) method 1 -1 5 -5 -1 1 5 -5 5
  16. 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)

×