Object Oriented Basics(homework for 03)
来源:互联网 发布:乌克兰男女比例知乎 编辑:程序博客网 时间:2024/06/05 17:06
Object Oriented Basics
——孙琨SealSun
1、问题
Define a class BookInStock which represents a book with an ISBN number, isbn, and price of the book as a floating-point number, price, as attributes.
The constructor should accept the ISBN number (a string, since in real life ISBN numbers can begin with zero and can include hyphens) as the first argument and price as second argument, and should raise ArgumentError (one of Ruby's built-in exception types) if the ISBN number is the empty string or if the price is less than or equal to zero. Include the proper getters and setters for these attributes.
Include a method price_as_string that returns the price of the book formatted with a leading dollar sign and two decimal places, that is, a price of 20 should format as "$20.00" and a price of 33.8 should format as "$33.80".
2、解决代码
<span style="font-size:14px;">#-------------------------------#-----程序名称:homework for 03#-----编译环境:ruby 2.2.3#-----作 者:孙琨SealSun#-----编写地点:UCAS #-----编写时间:2015年10月05日 #--------------------------------class BookInStock# YOUR CODE HERE def initialize(isbn,price) #构造函数 @isbn=isbn @price=price if(@isbn===""||@price<=0) #异常处理 raise ArgumentError end end #set,get方法 def set_isbn(isbn) @isbn=isbn if(@isbn==="") raise ArgumentError end end def set_price(price) @price=price if(@price<=0) raise ArgumentError end end def get_isbn puts @isbn end def get_price puts @price end def price_as_string #price处理 puts "$"+format("%.2f", @price) endend begin # a = BookInStock.new(123,2) #for testing # #a.set_price(15.2) # a.get_price # a.price_as_string rescue ArgumentError=>e puts "发生错误,错误为ISBN为空,或者价格<=0" end</span>
- Object Oriented Basics(homework for 03)
- Finished the homework of Object-Oriented courses
- Object-Oriented PHP for Beginners
- Back To Basics: Intro to Object Oriented Programming
- Object-Oriented ActionScript For Flash 8
- Validated Designs for Object-oriented Systems
- A Laboratory For Teaching Object-Oriented Thinking
- Design Patterns For Object-Oriented JavaScript
- Conclusion for Inheritance and Object Oriented Design
- Java Basics 【Oriented Process】
- OOD(object oriented design)
- object-oriented&procedure-oriented
- ADO.NET for the Object-Oriented Programmer – Part One
- The Commercial Case For Object Oriented Software Development
- db4object - the object oriented database not only for embedded
- Coursera课程Python for everyone:Quiz: Object Oriented Programming
- 面向对象(Object-Oriented)的基础
- JavaScript 面向对象(object-oriented)方法
- AndroidStudio 中引用so文件并使用
- Java多线程面试问题集锦(转)
- 解决LINK : fatal error LNK1140: 用于程序数据库的模块太多;链接时使用 /PDB:NONE 的错误
- RecyclerView学习和使用(基本使用)
- jquery实现checkbox全选反选
- Object Oriented Basics(homework for 03)
- 剑指offer:翻转单词顺序列
- IOS开发UI进阶之UITableView四
- struts2+ajax中json数据返回格式参数详解
- 替换字符串中空格
- linux之sort用法
- TCP与UDP的应用
- 关于linux下 gcc 编译时for循环的报错。c99
- 【日程表】