Created
March 3, 2012 06:46
-
-
Save feiskyer/1964748 to your computer and use it in GitHub Desktop.
Berkeley SaaS class HW1 Part 4: Basic OOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (a) Create a class Dessert with getters and setters for name and calories. Define | |
# instance methods healthy?, which returns true if a dessert has less than 200 | |
# calories, and delicious?, which returns true for all desserts. | |
# | |
# (b) Create a class JellyBean that extends Dessert, and add a getter and setter for | |
# flavor. Modify delicious? to return false if the flavor is black licorice (but delicious? | |
# should still return true for all other flavors and for all non-JellyBean desserts). | |
class Dessert | |
attr_accessor :name | |
attr_accessor :calories | |
def initialize(name, calories) | |
@name=name | |
@calories=calories | |
end | |
def healthy? | |
@calories<200 | |
end | |
def delicious? | |
true | |
end | |
end | |
class JellyBean < Dessert | |
attr_accessor :flavor | |
def initialize(name, calories, flavor) | |
@name=name | |
@calories=calories | |
@flavor=flavor | |
end | |
def delicious? | |
if @flavor== "black licorice" | |
false | |
else | |
true | |
end | |
end | |
end | |
a=JellyBean.new("a",232,"asd") | |
p a.delicious? | |
p a.healthy? | |
b=Dessert.new("asd",190) | |
p b.delicious? | |
p b.healthy? |
FerPerales, I agree that it can be written more concisely, but I think you accidentally reversed the logic.
def delicious?
@flavor != "black licorice"
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
def delicious? in JellyBean could be written shorter by doing this
def delicious?
return @flavor=="black licorice"
end
I think you can also ommit the word return