Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Ex. 7.1
- class Rectangle (object):
- def __init__(self,w=1,h=2):
- self.width=w
- self.height=h
- def getArea(self):
- return self.width*self.height
- def getPerimeter(self):
- return 2*(self.width+self.height)
- w=4
- print('The width of the rectangle is '+str(w))
- h=40
- print('The width of the rectangle is '+str(h))
- r = Rectangle(w,h)
- print('The area of the rectangle is ',r.getArea())
- print('The perimeter of the rectangle is ' , r.getPerimeter())
- w=3.5
- print('The width of the rectangle is '+str(w))
- h=35.7
- print('The width of the rectangle is '+str(h))
- r=Rectangle(w,h)
- print('The area of the rectangle is ',(r.getArea()))
- print('The perimeter of the rectangle is ', (r.getPerimeter()))
- #Ex. 7.2
- class Stock(object):
- def __init__(self, symbol, name, previousClosingPrice, currentPrice):
- self.symbol = symbol
- self.name = name
- self.previousClosingPrice = previousClosingPrice
- self.currentPrice = currentPrice
- def getSymbol(self):
- return self.symbol
- def getName(self):
- return self.name
- def previousClosingPrice(self):
- return self.previousClosingPrice
- def currentPrice(self):
- return self.__currentPrice
- def setSymbol(self, symbol):
- self.symbol = symbol
- def setName(self, name):
- self.name = name
- def setpreviousClosingPrice(self, previousClosingPrice):
- self.previousClosingPrice = previousClosingPrice
- def setcurrentPrice(self, currentPrice):
- self.currentPrice = currentPrice
- def getChangePercent(self):
- return (self.currentPrice - self.previousClosingPrice) * 100.0 / (self.previousClosingPrice)
- s = Stock
- s = Stock('INTC', 'Intel Corporation', 20.5, 20.35)
- print('%.2f%%' % s.getChangePercent())
- #Ex. 7.3
- class Account(object):
- def __init__(self, id = 0, balance = 100.0, annualInterestRate = 0.0):
- self.__id = id
- self.__balance = balance
- self.__annualInterestRate = annualInterestRate
- def getId(self):
- return self.__id
- def getBalance(self):
- return self.__balance
- def getAnnualInterestRate(self):
- return self.__annualInterestRate
- def setId(self, id):
- self.__id = int(id)
- def setBalance(self, balance):
- self.__balance = float(balance)
- def setAnnualInterestRate(self, annualInterestRate):
- self.__annualInterestRate = float(annualInterestRate)
- def getMonthlyInterestRate(self):
- return self.__annualInterestRate / 12
- def getMonthlyInterest(self):
- return self.__balance * self.getMonthlyInterestRate()
- def withdraw(self,withdraw):
- amount = 30
- self.__balance=self.__balance-withdraw
- def deposit(self,dep):
- amount = 50
- self.__balance=self.__balance-dep
- def main(self):
- Account = acc
- acc = Account(1122,20000,4.5)
- acc.withdraw(2500)
- acc.deposit(3000)
- print('Account Id: '+str(acc.getId()))
- print('Balance: '+str(acc.getBalance()))
- print('Monthly Interest rate: '+str(acc.getMonthlyInterestRate()))
- print('Monthly Interest amount: '+str(acc.getMonthlyInterest()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement