list7.7_TeseLoanClass.py
from Loan import Loan
def main():
# Enter yearly interest rate
annualInterestRate = eval(input("Enter yearly interest rate, for example, 7.25: "))
# Enter number of years
numberOfYears = eval(input("Enter number of years as an integer: "))
# Enter loan amount
loanAmount = eval(input("Enter loan amount, for example, 120000.95: "))
# Enter a borrower
borrower = input("Enter a borrower's name: ")
# Create a Loan object
loan = Loan(annualInterestRate, numberOfYears,loanAmount, borrower)
# Display loan date, monthly payment, and total payment
print("The loan is for", loan.getBorrower())
print("The monthly payment is",format(loan.getMonthlyPayment() , ".2f"))
print("The total payment is",format(loan.getTotalPayment() , ".2f"))
main() # Call the main function
?
?
list7.8_Loan.py
class Loan:
def __init__(self, annualInterestRate = 2.5,
numberOfYears = 1, loanAmount = 1000, borrower = " "):
self.__annualInterestRate = annualInterestRate
self.__numberOfYears = numberOfYears
self.__loanAmount = loanAmount
self.__borrower = borrower
def getAnnualInterestRate(self):
return self.__annualInterestRate
def getNumberOfYears(self):
return self.__numberOfYears
def getLoanAmount(self):
return self.__loanAmount
def getBorrower(self):
return self.__borrower
def setAnnualInterestRate(self, annualInterestRate):
self.__annualInterestRate = annualInterestRate
def setNumberOfYears(self, numberOfYears):
self.__numberOfYears = numberOfYears
def setLoanAmount(self, loanAmount):
self.__loanAmount = loanAmount
def setBorrower(self, borrower):
self.__borrower = borrower
def getMonthlyPayment(self):
monthlyInterestRate = self.__annualInterestRate / 1200
monthlyPayment = \
self.__loanAmount * monthlyInterestRate / (1 - (1 /
(1 + monthlyInterestRate) ** (self.__numberOfYears * 12)))
return monthlyPayment
def getTotalPayment(self):
totalPayment = self.getMonthlyPayment() * \
self.__numberOfYears * 12
return totalPayment
list7.9_UseBMIClass.py
from BMI import BMI
def main():
bmi1 = BMI("John Doe", 18, 145, 70)
print("The BMI for", bmi1.getName(), "is", bmi1.getBMI(), bmi1.getStatus())
bmi2 = BMI("Peter King", 50, 215, 70)
print("The BMI for", bmi2.getName(), "is", bmi2.getBMI(), bmi2.getStatus())
main()
?
?
list7.10_BMI.py
class BMI:
def __init__(self, name, age, weight, height):
self.__name = name
self.__age = age
self.__weight = weight
self.__height = height
def getBMI(self):
KILOGRAMS_PER_POUND = 0.45359237
METERS_PER_INCH = 0.0254
bmi = self.__weight * KILOGRAMS_PER_POUND / \
((self.__height * METERS_PER_INCH) * \
(self.__height * METERS_PER_INCH))
return round(bmi * 100) / 100
def getStatus(self):
bmi = self.getBMI()
if bmi < 18.5:
return "Underweight"
elif bmi < 25:
return "Normal"
elif bmi < 30:
return "Overweight"
else:
return "Obese"
def getName(self):
return self.__name
def getAge(self):
return self.__age
def getWeight(self):
return self.__weight
def getHeight(self):
return self.__height
exe7.3
class Account:
def __init__(self, id=0, balance=100.0, annualIntersetRate=0.0):
self.__id = id
self.__balance = balance
self.__annualIntersetRate = annualIntersetRate
def getId(self):
return self.__id
def getBalance(self):
return self.__balance
def getMonthlyInterestRate(self):
return self.__annualIntersetRate / 12
def getMonthlyInterest(self):
return self.__annualIntersetRate * self.__balance / 12
def withdraw(self, balance):
self.__balance = self.__balance-balance
return self.__balance
def deposit(self, balance):
self.__balance = self.__balance+balance
return self.__balance
def main():
Account1 = Account(id=1122, balance=20000, annualIntersetRate=0.045)
Account1.withdraw(2500)
Account1.deposit(3000)
print("id = ",Account1.getId())
print("balance =", Account1.getBalance())
print("monthly interest rate =", Account1.getMonthlyInterestRate())
print("monthly interest =",Account1.getMonthlyInterest())
main()
?
?
exe7.4
SLOW = 1
MEDIUM = 2
FAST = 3
class Fan:
def __init__(self, on=False, radius=5, speed=SLOW, color="blue"):
self.__speed = speed
self.__on = on
self.__radius = radius
self.__color = color
def getSpeed(self):
return self.__speed
def getOn(self):
return self.__on
def getRadius(self):
return self.__radius
def getColor(self):
return self.__color
def main():
Fan1 = Fan(on=True, radius=10, speed=FAST, color="yellow")
Fan2 = Fan(on=False, radius=5, speed=MEDIUM, color="blue")
print("Speed=", Fan1.getSpeed(), "On=", Fan1.getOn(),
"getRadius=", Fan1.getRadius(), "Color=", Fan1.getColor())
print("Speed=", Fan2.getSpeed(), "On=", Fan2.getOn(),
"getRadius=", Fan2.getRadius(), "Color=", Fan2.getColor())
main()
?
?
exe7.5
import math
class RegularPolygon:
def __init__(self, n=3, side=1, x=0, y=0):
self.__n = n
self.__side = side
self.__x = x
self.__y = y
def getPerimeter(self):
perimeter = self.__n * self.__side
return perimeter
def getArea(self):
area = (self.__n * self.__side * self.__side) / (4 * math.tan(math.pi / self.__n))
return area
def main():
RegularPolygon1 = RegularPolygon()
RegularPolygon2 = RegularPolygon(6, 4)
RegularPolygon3 = RegularPolygon(10, 4, 5.6, 7.8)
print("Area1=", format(RegularPolygon1.getArea(), ".4"), "Perimeter1=", RegularPolygon1.getPerimeter())
print("Area2=", format(RegularPolygon2.getArea(), ".4"), "Perimeter2=", RegularPolygon2.getPerimeter())
print("Area3=", format(RegularPolygon3.getArea(), ".4"), "Perimeter3=", RegularPolygon3.getPerimeter())
main()
?
|