Episode 6 - Methods Calling Methods
Exercises
-
Add an ‘info’ instance method to your Song class that returns a string that contains information about the song in an easy to read format.
-
Create a new class called Product, that will produce products that are available for sale.
- Write a constructor such that each product will have a name, a description, and a price.
- Write methods that retrieve each of these attributes.
- Write methods that allow us to redefine those attributes.
Once you write the class, you should be able to run this code:
product = Product.new("Printer", "It prints pages!", 94)
p product
p product.name
product.name = "Awesome Printer"
p product.name
The code should output:
#<Product:0x007fb18987bf30 @name="Printer", @description="It prints pages!", @price=94>
"Printer"
"Awesome Printer"
-
Now on the Product class, add the following features.
- Add a method called
tax
that returns the price multiplied by 0.09 (which will represent sales tax.) This tax method should call the price
method.
- Add a method called
total
which returns the sum of the price plus the tax. This method should reference both the price
method as well as the tax
method.