Python object oriented programming (lab2) (2)
Click here to load reader
-
Upload
iloveallahsomuch -
Category
Technology
-
view
442 -
download
2
Transcript of Python object oriented programming (lab2) (2)
Play with PythonLab2
Object Oriented Programming
About Lecture 2 and Lab 2
Lecture 2 and its lab aims at covering Basic Object Oriented Programming concepts
Classes, Constructor (__init__) and Objects
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
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
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
Example 1
shp1 = IrregularShape()shp1.AddShape(Square(3))shp1.AddShape(Triangle(4, 5))shp1.AddShape(Square(10))
print shp1.Area()
========Output:119.0
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
Exercise 1 (Solution)
shp2 = IrregularShape()shp2.AddShape(shp1)shp2.AddShape(shp1)print shp2.Area()
=====Output:238.0
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]
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
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]
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
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
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()
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
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)