# Chapter 14 : Classes and invariants¶

### example 14.1 page no :149¶

In [1]:
'''
example 14.1 page no :149
'''

class Card:
def __init__(self,s=None,r=None):
if s==None:
self.suit = 0
self.rank = 0
else:
self.suit = s;
self.rank = r;

def getRank (self):
return self.rank

def getSuit (self):
return self.suit

def setRank(self,r):
self.rank = r

def setSuit (self, s):
self.suit = s


### example 14.2 page no :151¶

In [2]:
'''
example 14.2 page no :151
defining class
'''

class Complex:
def __init__(self,r=None,i=None):
if r==None:
pass
else:
self.real = r
self.imag = i


### example 14.3 page no : 152¶

In [3]:
'''
example 14.3 page no : 152
'''
import math
class Complex:
def __init__(self,r=None,i=None):
if r==None:
self.real = 0
self.imag = 0
self.polar = False
self.cartesian = False
else:
self.real = r
self.imag = i
self.cartesian = True
self.polar = True
self.mag = 0
self.theta = 0

def getReal (self):
if (self.cartesian == False):
self.calculateCartesian ();
return self.real;

def getImag(self):
return self.imag

def calculateCartesian (self):
self.real = self.mag * math.cos(self.theta);
self.imag = self.mag * math.sin (self.theta);
self.cartesian = True;

def printCartesian (self):
print self.getReal() , " + " , self.getImag() , "i"

def printPolar(self):
print self.mag , " e^ " , self.theta , "i"

c1 = Complex(2.0, 3.0);
c1.printCartesian();
c1.printPolar();

2.0  +  3.0 i
0  e^  0 i


### example 14.4 page no : 155¶

In [4]:
'''
example 14.4 page no : 155
'''
class Complex:
def __init__(self,r=None,i=None):
if r==None:
self.real = 0
self.imag = 0
self.polar = False
self.cartesian = False
else:
self.real = r
self.imag = i
self.cartesian = True
self.polar = True
self.mag = 0
self.theta = 0

def getReal (self):
if (self.cartesian == False):
self.calculateCartesian ();
return self.real;

def getImag(self):
return self.imag

def add(self,b):
real = self.getReal() + b.getReal();
imag = self.getImag() + b.getImag();
s = Complex(real, imag);
return s

def printCartesian (self):
print self.getReal() , " + " , self.getImag() , "i"

c1 = Complex(2.0, 3.0);
c2 = Complex(3.0, 4.0);
s = c1.add (c2);
s.printCartesian();

5.0  +  7.0 i


### example 14.5 page no :156¶

In [8]:
'''
example 14.5 page no :156
'''
import math
class Complex:
def __init__(self,r=None,i=None):
if r==None:
self.real = 0
self.imag = 0
self.polar = False
self.cartesian = False
else:
self.real = r
self.imag = i
self.cartesian = True
self.polar = True
self.mag = 0
self.theta = 0

def getReal (self):
if (self.cartesian == False):
self.calculateCartesian ();
return self.real;

def getImag(self):
return self.imag

def calculateCartesian (self):
self.real = self.mag * math.cos(self.theta);
self.imag = self.mag * math.sin (self.theta);
self.cartesian = True;

def printCartesian (self):
print self.getReal() , " + " , self.getImag() , "i"

def mult(self,b):
mag = self.mag * b.mag
theta = self.theta + b.theta
product = Complex()
product.setPolar (mag, theta);
return product;

def setPolar(self,m,t):
self.mag = m
self.theta = t;
self.cartesian = False
self.polar = True;


### example 14.6 page no :159¶

In [9]:
'''
example 14.6 page no :159
defining class Complex
'''

class Complex:
def __init__(self,r=None,i=None):
if r==None:
self.real = 0
self.imag = 0
self.polar = False
self.cartesian = False
else:
self.real = r
self.imag = i
self.cartesian = True
self.polar = True
self.mag = 0
self.theta = 0

def getReal (self):
if (self.cartesian == False):
self.calculateCartesian ();
return self.real;

def getImag(self):
return self.imag

def printCartesian (self):
print self.getReal() , " + " , self.getImag() , "i"

def mult(self,b):
mag = self.mag * b.mag
theta = a.theta + b.theta
product = Complex()
product.setPolar (mag, theta);
return product;

def setPolar(self,m,t):
self.mag = m
self.theta = t;
self.cartesian = False
self.polar = true;

In [ ]: