Advertisement
trds

tema

Jan 13th, 2021
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. #Ex. 7.1
  2. class Rectangle (object):
  3.     def __init__(self,w=1,h=2):
  4.         self.width=w
  5.         self.height=h
  6.  
  7.     def getArea(self):
  8.         return self.width*self.height
  9.     def getPerimeter(self):
  10.         return 2*(self.width+self.height)
  11.  
  12. w=4
  13. print('The width of the rectangle is '+str(w))
  14. h=40
  15. print('The width of the rectangle is '+str(h))
  16. r = Rectangle(w,h)
  17. print('The area of the rectangle is ',r.getArea())
  18. print('The perimeter of the rectangle is ' , r.getPerimeter())
  19. w=3.5
  20. print('The width of the rectangle is '+str(w))
  21. h=35.7
  22. print('The width of the rectangle is '+str(h))
  23. r=Rectangle(w,h)
  24. print('The area of the rectangle is ',(r.getArea()))
  25. print('The perimeter of the rectangle is ', (r.getPerimeter()))
  26.  
  27. #Ex. 7.2
  28. class Stock(object):
  29.     def __init__(self, symbol, name, previousClosingPrice, currentPrice):
  30.         self.symbol = symbol
  31.         self.name = name
  32.         self.previousClosingPrice = previousClosingPrice
  33.         self.currentPrice = currentPrice
  34.  
  35.     def getSymbol(self):
  36.         return self.symbol
  37.     def getName(self):
  38.         return self.name
  39.     def previousClosingPrice(self):
  40.         return self.previousClosingPrice
  41.     def currentPrice(self):
  42.         return self.__currentPrice
  43.     def setSymbol(self, symbol):
  44.         self.symbol = symbol
  45.     def setName(self, name):
  46.         self.name = name
  47.     def setpreviousClosingPrice(self, previousClosingPrice):
  48.         self.previousClosingPrice = previousClosingPrice
  49.     def setcurrentPrice(self, currentPrice):
  50.         self.currentPrice = currentPrice
  51.     def getChangePercent(self):
  52.         return (self.currentPrice - self.previousClosingPrice) * 100.0 / (self.previousClosingPrice)
  53.  
  54. s = Stock
  55. s = Stock('INTC', 'Intel Corporation', 20.5, 20.35)
  56. print('%.2f%%' % s.getChangePercent())
  57.  
  58. #Ex. 7.3
  59. class Account(object):
  60.     def __init__(self, id = 0, balance = 100.0, annualInterestRate = 0.0):
  61.         self.__id = id
  62.         self.__balance = balance
  63.         self.__annualInterestRate = annualInterestRate
  64.  
  65.     def getId(self):
  66.         return self.__id
  67.     def getBalance(self):
  68.         return self.__balance
  69.     def getAnnualInterestRate(self):
  70.         return self.__annualInterestRate
  71.     def setId(self, id):
  72.         self.__id = int(id)
  73.     def setBalance(self, balance):
  74.         self.__balance = float(balance)
  75.     def setAnnualInterestRate(self, annualInterestRate):
  76.         self.__annualInterestRate = float(annualInterestRate)
  77.     def getMonthlyInterestRate(self):
  78.         return self.__annualInterestRate / 12
  79.     def getMonthlyInterest(self):
  80.         return self.__balance * self.getMonthlyInterestRate()
  81.     def withdraw(self,withdraw):
  82.         amount = 30
  83.         self.__balance=self.__balance-withdraw
  84.     def deposit(self,dep):
  85.         amount = 50
  86.         self.__balance=self.__balance-dep
  87.     def main(self):
  88.         Account = acc
  89. acc = Account(1122,20000,4.5)
  90. acc.withdraw(2500)
  91. acc.deposit(3000)
  92. print('Account Id: '+str(acc.getId()))
  93. print('Balance: '+str(acc.getBalance()))
  94. print('Monthly Interest rate: '+str(acc.getMonthlyInterestRate()))
  95. print('Monthly Interest amount: '+str(acc.getMonthlyInterest()))
  96.  
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement