Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help with this Cloud9 Ruby programming project. Create a class Dessert wi

ID: 3886558 • Letter: P

Question

Please help with this Cloud9 Ruby programming project.

Create a class Dessert with getters and setters for name and calories. The constructor should accept arguments for name and calories.

Define instance methods healthy?, which returns true iff a dessert has less than 200 calories, and delicious?, which returns true for all desserts.

Create a class JellyBean that inherits from Dessert. The constructor should accept a single argument giving the jelly bean's flavor; a newly-created jelly bean should have 5 calories and its name should be the flavor plus "jelly bean", for example, "strawberry jelly bean".

Modify delicious? to return false if the flavor is licorice, but true for all other flavors. The behavior of delicious? for non-jelly-bean desserts should be unchanged.

class Dessert
def initialize(name, calories)
# your code here
end
def healthy?
# your code here
end
def delicious?
# your code here
end
end


class JellyBean < Dessert
def initialize(flavor)
# your code here
end
end

Add a getter and setter for the flavor.

Explanation / Answer

Desert.rb
------------------------------------------
class Dessert

def initialize(name, calories)
    @name = name
    @calories = calories
end

def healthy?
    @calories < 200
end

def delicious?
    true
end
end
----------------------------------
jellybean.rb
----------------------
class JellyBean < Dessert

attr_accessor :flavor

def initialize(name, calories, flavor)
    super(name, calories)
    @name = name
    @calories = calories
    @flavor = flavor
end

def delicious?
    if @flavor == "black licorice"
      false
    else
      true
    end
end

end
--------------------------------------
dessert_spec.rb
---------------------
require File.join(File.dirname(__FILE__), "..", "lib", "dessert")

describe Dessert do

subject { Dessert.new("Cake", 180) }

it "should have a name and calories" do
    subject.should nil
end

it "should return true, healthy < 200" do
    subject.should be_healthy
end

it "should return false, healthy > 200" do
    @dessert = Dessert.new("Cake", 500)
    @dessert.should_not be_healthy
end

it "should have a delicious" do
    subject.should be_delicious
end
end
---------------------------------------
jellybean_spec.rb
-------------------------
require File.join(File.dirname(__FILE__), "..", "lib", "dessert")
require File.join(File.dirname(__FILE__), "..", "lib", "jellybean")

describe JellyBean do

subject { JellyBean.new("Cake", 100, "black licorice") }

it "should have a delicious" do
    subject.should nil
end

it "should return true, healthy < 200" do
    subject.should be_healthy
end

it "should return false, healthy > 200" do
    @dessert = JellyBean.new("Cake", 500, "black licorice")
    @dessert.should_not be_healthy
end

it "should return false, a delicious" do
    subject.should_not be_delicious
end

it "should return true, a delicious" do
    @dessert = JellyBean.new("Cake", 180, "chocolate")
    @dessert.should be_delicious
end

end