部分来着网络,侵删,仅供参考
- (Stock类)设计一个名为Stock 的类来表示一个公司的股票,它包括∶
(1) 一个名为 symbol 的私有字符串数据域表示股票的符号。
(2) 一个名为 name 的私有字符串数据域表示股票的名字。
(3) 一个名为 previousClosingPrice 的私有浮点数据域存储前一天的股票价。
(4) 一个名为 currentPrice 的私有浮点数据域存储当前的股票价。
(5) 一个构造方法创建一支具有特定的符号、名字、之前价和当前价的股票。
(6) 一个返回股票名字的 get 方法。
(7) 一个返回股票符号的get 方法。
(8) 获取/设置股票之前价的 get 和 set 方法。
(9) 获取/设置股票当前价的 get 和 set 方法。
(10) 一个名为 getChangePercent()的方法返回从previousClosingPrice到 curentPrie所改变的百分比。
实现这个类。编写一个测试程序,创建一个 Stock 对象,它的符号是INTC,它的名字是 Intel Corporation,前一天的结束价是20.5,新的当前价是20.35,并且显示价格改变的百分比。
class Stock():
def __init__(self):
self.symbol = ""
self.name = ""
self.previousClosingPrice = 0
self.currentPrice = 0
def creatStock(self,stockInfo):
self.symbol = stockInfo[0]
self.name = stockInfo[1]
self.previousClosingPrice = stockInfo[2]
self.currentPrice = stockInfo[3]
def getStockName(self):
return(self.name)
def getStockNo(self):
return(self.symbol)
def setPreviousClosingPrice(self,price):
self.previousClosingPrice = price
def getPreviousClosingPrice(self):
return(self.previousClosingPrice)
def setCurrentPrice(self,price):
self.currentPrice = price
def getCurrentPrice(self):
return(self.currentPrice)
def getChangePercent(self):
return((self.currentPrice - self.previousClosingPrice)/self.currentPrice)
INTC = Stock()
INTC.creatStock(["000001","Intel Corporation",20.5,20.35])
print(INTC.getStockNo())
print(INTC.getStockName())
print(INTC.getCurrentPrice())
print(INTC.getPreviousClosingPrice())
print("%.2f%%" % (INTC.getChangePercent()*100))
- (Account 类)设计一个名为 Account 的类,它包括∶
(1) 账户的一个名为 id 的私有 int 数据域。
(2) 账户的一个名为 balance 的私有浮点数据域。
(3) 一个名为 annuallnterestRate 的私有浮点数据域存储当前利率。
(4) 一个构造方法创建具有特定 id(默认值O)、初始额(默认值100)以及年利率(默认值0)。
(5) id、balance 和 annuallnterestRate 的访问器和修改器。
(6) 一个名为 getMonthlyInterestRate()的方法返回月利率。
(7) 一个名为 getMonthlyInterest()的方法返回月利息。●
(8) 一个名为 withdraw 的方法从账户取出特指定数额。
(9) 一个名为 deposit 的方法向账户存入指定数额。
绘制这个类的UML类图,然后实现这个类(提示∶ 方法getMonthlylnterestO)返回每月利息额,而不是返回利率。使用这个公式计算月利息∶balance*monthlyInterestRate。monthly-InterestRate是annuallnterestRate/l12。注意∶annuallnterestRate是一个百分数(如4.5%)。你需要将它除以100。 编写测试程序创建一个Account 对象,这个账户的id是122,账户额是20000美元而年利率是4.5%。使用withdraw方法取2500美元,使用deposit方法存3000美元,并打印id、金额、月利率和月利息。
class Account(object):
def __init__(self):
self.id=0
self.balance=100.0
self.annuallnterestRate=0.0
def creatAccount(self, id, balance, annuallnterestRate):
self.id=id
self.balance=balance
self.annuallnterestRate=annuallnterestRate
def setAccountid(self,id):
self.id = id
def getAccountid(self):
return(self.id)
def setAccountbalance(self,balance):
self.balance = balance
def getAccountbalance(self):
return(self.balance)
def setAccountannuallnterestRate(self,annuallnterestRate):
self.annuallnterestRate = annuallnterestRate
def getAccountannuallnterestRate(self):
return(self.annuallnterestRate)
def getMonthlyInterestRate(self):
return(self.annuallnterestRate/12)
def getMonthlyInterest(self):
return(self.balance*self.annuallnterestRate/12)
def withdraw(self,balance):
if balance <= self.balance :
self.balance=self.balance-balance
else:
print('余额不足!')
def deposit(self,balance):
self.balance=self.balance+balance
Account1=Account()
Account1.creatAccount(122,20000,0.045)
Account1.withdraw(2500)
Account1.deposit(3000)
print(Account1.id)
print(Account1.balance)
print("%.2f%%" % (Account1.getMonthlyInterestRate()*100))
print ('%.2f' % Account1.getMonthlyInterest())
- (Fan类)设计一个名为 Fan 的类表示一个风扇。这个类包括∶
(1) 三个名为SLOW、MEDIUM和FAST的常量,它们的值分别是1、2 和3 以表示风扇速度。(2) 一个名为 speed 的私有整型数据域表明风扇的速度。
(3)一个名为 on 的私有布尔数据域表明风扇是否是打开状态(默认值是 False)。
(4) 一个名为 radius 的私有浮点数据域表明风扇的半径。
(5) 一个名为 color 的私有字符串数据域表明风扇的颜色。
(6) 四个数据域的访问方法和修改方法。
(6)一个构造方法创建一个具有特定速度(默认值为SLOW)、半径(默认值为5)、颜色(默认值为 blue)以及是否打开(默认值为 False)。
编写测试程序创建两个Fan 对象。对第一个对象,赋值最大速度、半径为10、颜色为yellow,打开它。对第二个对象,赋值中速、半径为5、颜色为 blue,关闭它。显示每个对象的speed、radius、color 和 on属性。
class Fan(object):
def __init__(self):
self.speed=1
self.on=False
self.radius=5.0
self.color="blue"
def creatFan(self, speed, on, radius, color):
self.speed=speed
self.color=color
self.radius=radius
self.on=on
def setFanspeed(self,speed):
self.speed = speed
def getFanspeed(self):
return(self.speed)
def setFanon(self,on):
self.on = on
def getFanon(self):
return(self.on)
def setFanradius(self,radius):
self.radius = radius
def getFanradius(self):
return(self.radius)
def setFancolor(self,color):
self.color = color
def getFancolor(self):
return(self.color)
def show(self):
if self.speed==1:
speed1="SLOW"
elif self.speed==2:
speed1="MEDIUMK"
else:
speed1="FAST"
print (speed1, self.radius,self.color,self.on)
fan1 = Fan()
fan1.creatFan(3,True,10.0, " yellow")
fan1.show()
fan2 = Fan()
fan2.creatFan(2,False,5.0, " blue")
fan2.show()