From a6eb8f753fc1aa23704d6f53bb55daab44259347 Mon Sep 17 00:00:00 2001 From: Chris Hutchinson Date: Tue, 17 Jan 2017 11:31:04 -0500 Subject: [PATCH 1/3] Completed the challenge --- lib/tree.rb | 56 +++++++++++++++---- spec/tree_spec.rb | 136 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 175 insertions(+), 17 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..327ac0e 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,25 +1,52 @@ class NoApplesError < StandardError; end -class AppleTree - attr_#fill_in :height, :age, :apples, :alive +class Tree + #fill_in :height, :age, :apples, :alive + attr_reader :apples, :height, :age, :alive - def initialize - end + def initialize + @apples = [] + @height = 0 + @age = 0 + @alive = true + end def age! + @age += 1 + + # Randomly decide if a tree lives or dies after it reaches 10 years old. + # The older it gets, the more likely it is to die. + # A tree cannot live longer than 50 years. + if (rand(10..50) < @age) then + @alive = false + elsif (@alive) + if (@age < 5) then + # Tree grows a random height every year + @height += rand(1..10) + else + # Produce a random number for apples + for i in 0..rand(0..5) + add_apples + end + end + end end def add_apples + @apples.push Apple.new 'red', rand(2..6) end def any_apples? + @apples.count > 0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples.pop end def dead? + !@alive end end @@ -29,17 +56,21 @@ def initialize end end -class Apple < - attr_reader #what should go here - - def initialize(color, diameter) - end +class Apple < Fruit + #what should go here + attr_reader :diameter + attr_reader :color + + def initialize(color, diameter) + @color = color + @diameter = diameter + end end #THERES ONLY ONE THING YOU NEED TO EDIT BELOW THIS LINE # avg_diameter (line 58) will raise an error. # it should calculate the diameter of the apples in the basket - + def tree_data tree = Tree.new @@ -60,8 +91,9 @@ def tree_data basket.each do |apple| diameter_sum += apple.diameter end - - avg_diameter = # It's up to you to calculate the average diameter for this harvest. + + # It's up to you to calculate the average diameter for this harvest. + avg_diameter = basket.count > 0 ? diameter_sum / basket.count : 0 puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 99c9184..ba4bda2 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -1,14 +1,140 @@ require 'rspec' require 'tree' -describe 'Tree' do - it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true +shared_examples 'sanity check' do |tree_class| + it 'is a class' do + expect(tree_class.is_a? Class).to eq true + end +end + +describe Tree do + before :each do + @tree = Tree.new + end + it 'should be a class' do + expect(described_class).to eq Tree end + + it 'should have no apples initially' do + expect(@tree.apples.count).to eq 0 + end + + it 'should have a height of 0 initially' do + expect(@tree.height).to eq 0 + end + + it 'should have age of 0 initially' do + expect(@tree.age).to eq 0 + end + + it 'should initially be alive' do + expect(@tree.alive).to eq true + end + + it 'should increase age by 1 when age is called' do + @tree.age! + expect(@tree.age).to eq 1 + end + + it 'should grow by atleast 1 unit every every time it ages and is less than 4 years old' do + @tree.age! + expect(@tree.height).to be > 0 + end + + it 'should not grow after 4 years' do + for i in 0..5 + @tree.age! + end + height_before = @tree.height + @tree.age! + expect(@tree.height).to eq height_before + end + + it 'should start producing apples when its 5 years old' do + for i in 0..6 + @tree.age! + end + expect(@tree.apples.count).to be > 0 + end + + it 'should not produce apples if its younger than 5 years old' do + @tree.age! + expect(@tree.apples.count).to eq 0 + end + + it 'should have no apples if younger than 5 years old' do + @tree.age! + expect(@tree.any_apples?).to eq false + end + + it 'should have apples if it is older than 5 years old' do + for i in 0..5 + @tree.age! + end + expect(@tree.any_apples?).to eq true + end + it 'should not be alive longer than 50 years' do + # Since a tree can't live past 50 years, this will ensure its dead. + for i in 0..51 + @tree.age! + end + expect(@tree.alive).to eq false + end + + it 'should not produce apples if the tree is dead' do + # Since a tree can't live past 50 years, this will ensure its dead. + for i in 0..51 + @tree.age! + end + number_of_apples = @tree.apples.count + @tree.age! + expect(@tree.apples.count).to eq number_of_apples + end + + it 'should be able to pick an apple' do + basket = [] + while @tree.apples.count == 0 do + @tree.age! + end + basket << @tree.pick_an_apple! + expect(basket.count).to eq 1 + end + + it 'should decrease the amount of apples on the tree when an apple is picked' do + basket = [] + while @tree.apples.count == 0 do + @tree.age! + end + num_apples_before = @tree.apples.count + basket << @tree.pick_an_apple! + expect(@tree.apples.count).to be < num_apples_before + end end -describe 'Fruit' do +describe Fruit do + it 'should be a class' do + expect(described_class).to eq Fruit + end end -describe 'Apple' do +describe Apple do + before :each do + @apple = Apple.new 'red', 3 + end + + it 'should be a class' do + expect(described_class).to eq Apple + end + + it 'should inherit from Fruit' do + expect(described_class).to be < Fruit + end + + it 'should have a color' do + expect(@apple.color).to eq 'red' + end + + it 'should have a diameter' do + expect(@apple.diameter).to eq 3 + end end From 8db2902abcb4d3d3e82842dd20e81e102e97db97 Mon Sep 17 00:00:00 2001 From: Chris Hutchinson Date: Wed, 18 Jan 2017 08:36:48 -0500 Subject: [PATCH 2/3] Used rand(5).times instead of a for loop --- lib/tree.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 327ac0e..469f4d6 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -25,9 +25,7 @@ def age! @height += rand(1..10) else # Produce a random number for apples - for i in 0..rand(0..5) - add_apples - end + rand(5).times { add_apples } end end end From 6f35a67e0a083bf41a6d31cf4bdd31e3eb4334a5 Mon Sep 17 00:00:00 2001 From: Chris Hutchinson Date: Wed, 18 Jan 2017 09:14:46 -0500 Subject: [PATCH 3/3] Fixed the indentation --- lib/tree.rb | 72 +++++++------- spec/tree_spec.rb | 238 +++++++++++++++++++++++----------------------- 2 files changed, 155 insertions(+), 155 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 469f4d6..d2d6bbe 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -4,47 +4,47 @@ class Tree #fill_in :height, :age, :apples, :alive attr_reader :apples, :height, :age, :alive - def initialize - @apples = [] - @height = 0 - @age = 0 - @alive = true - end + def initialize + @apples = [] + @height = 0 + @age = 0 + @alive = true + end def age! - @age += 1 - - # Randomly decide if a tree lives or dies after it reaches 10 years old. - # The older it gets, the more likely it is to die. - # A tree cannot live longer than 50 years. - if (rand(10..50) < @age) then - @alive = false - elsif (@alive) - if (@age < 5) then - # Tree grows a random height every year - @height += rand(1..10) - else - # Produce a random number for apples - rand(5).times { add_apples } - end - end + @age += 1 + + # Randomly decide if a tree lives or dies after it reaches 10 years old. + # The older it gets, the more likely it is to die. + # A tree cannot live longer than 50 years. + if (rand(10..50) < @age) then + @alive = false + elsif (@alive) + if (@age < 5) then + # Tree grows a random height every year + @height += rand(1..10) + else + # Produce a random number for apples + rand(5).times { add_apples } + end + end end def add_apples - @apples.push Apple.new 'red', rand(2..6) + @apples.push Apple.new 'red', rand(2..6) end def any_apples? - @apples.count > 0 + @apples.count > 0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? - @apples.pop + @apples.pop end def dead? - !@alive + !@alive end end @@ -55,20 +55,20 @@ def initialize end class Apple < Fruit - #what should go here - attr_reader :diameter - attr_reader :color + #what should go here + attr_reader :diameter + attr_reader :color - def initialize(color, diameter) - @color = color - @diameter = diameter - end + def initialize(color, diameter) + @color = color + @diameter = diameter + end end #THERES ONLY ONE THING YOU NEED TO EDIT BELOW THIS LINE # avg_diameter (line 58) will raise an error. # it should calculate the diameter of the apples in the basket - + def tree_data tree = Tree.new @@ -89,8 +89,8 @@ def tree_data basket.each do |apple| diameter_sum += apple.diameter end - - # It's up to you to calculate the average diameter for this harvest. + + # It's up to you to calculate the average diameter for this harvest. avg_diameter = basket.count > 0 ? diameter_sum / basket.count : 0 puts "Year #{tree.age} Report" diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index ba4bda2..c4dc840 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -2,139 +2,139 @@ require 'tree' shared_examples 'sanity check' do |tree_class| - it 'is a class' do - expect(tree_class.is_a? Class).to eq true - end + it 'is a class' do + expect(tree_class.is_a? Class).to eq true + end end describe Tree do - before :each do - @tree = Tree.new - end - it 'should be a class' do + before :each do + @tree = Tree.new + end + it 'should be a class' do expect(described_class).to eq Tree end - it 'should have no apples initially' do - expect(@tree.apples.count).to eq 0 - end - - it 'should have a height of 0 initially' do - expect(@tree.height).to eq 0 - end - - it 'should have age of 0 initially' do - expect(@tree.age).to eq 0 - end - - it 'should initially be alive' do - expect(@tree.alive).to eq true - end - - it 'should increase age by 1 when age is called' do - @tree.age! - expect(@tree.age).to eq 1 - end - - it 'should grow by atleast 1 unit every every time it ages and is less than 4 years old' do - @tree.age! - expect(@tree.height).to be > 0 - end - - it 'should not grow after 4 years' do - for i in 0..5 - @tree.age! - end - height_before = @tree.height - @tree.age! - expect(@tree.height).to eq height_before - end - - it 'should start producing apples when its 5 years old' do - for i in 0..6 - @tree.age! - end - expect(@tree.apples.count).to be > 0 - end - - it 'should not produce apples if its younger than 5 years old' do - @tree.age! - expect(@tree.apples.count).to eq 0 - end - - it 'should have no apples if younger than 5 years old' do - @tree.age! - expect(@tree.any_apples?).to eq false - end - - it 'should have apples if it is older than 5 years old' do - for i in 0..5 - @tree.age! - end - expect(@tree.any_apples?).to eq true - end - it 'should not be alive longer than 50 years' do - # Since a tree can't live past 50 years, this will ensure its dead. - for i in 0..51 - @tree.age! - end - expect(@tree.alive).to eq false - end - - it 'should not produce apples if the tree is dead' do - # Since a tree can't live past 50 years, this will ensure its dead. - for i in 0..51 - @tree.age! - end - number_of_apples = @tree.apples.count - @tree.age! - expect(@tree.apples.count).to eq number_of_apples - end - - it 'should be able to pick an apple' do - basket = [] - while @tree.apples.count == 0 do - @tree.age! - end - basket << @tree.pick_an_apple! - expect(basket.count).to eq 1 - end - - it 'should decrease the amount of apples on the tree when an apple is picked' do - basket = [] - while @tree.apples.count == 0 do - @tree.age! - end - num_apples_before = @tree.apples.count - basket << @tree.pick_an_apple! - expect(@tree.apples.count).to be < num_apples_before - end + it 'should have no apples initially' do + expect(@tree.apples.count).to eq 0 + end + + it 'should have a height of 0 initially' do + expect(@tree.height).to eq 0 + end + + it 'should have age of 0 initially' do + expect(@tree.age).to eq 0 + end + + it 'should initially be alive' do + expect(@tree.alive).to eq true + end + + it 'should increase age by 1 when age is called' do + @tree.age! + expect(@tree.age).to eq 1 + end + + it 'should grow by atleast 1 unit every every time it ages and is less than 4 years old' do + @tree.age! + expect(@tree.height).to be > 0 + end + + it 'should not grow after 4 years' do + for i in 0..5 + @tree.age! + end + height_before = @tree.height + @tree.age! + expect(@tree.height).to eq height_before + end + + it 'should start producing apples when its 5 years old' do + for i in 0..6 + @tree.age! + end + expect(@tree.apples.count).to be > 0 + end + + it 'should not produce apples if its younger than 5 years old' do + @tree.age! + expect(@tree.apples.count).to eq 0 + end + + it 'should have no apples if younger than 5 years old' do + @tree.age! + expect(@tree.any_apples?).to eq false + end + + it 'should have apples if it is older than 5 years old' do + for i in 0..5 + @tree.age! + end + expect(@tree.any_apples?).to eq true + end + it 'should not be alive longer than 50 years' do + # Since a tree can't live past 50 years, this will ensure its dead. + for i in 0..51 + @tree.age! + end + expect(@tree.alive).to eq false + end + + it 'should not produce apples if the tree is dead' do + # Since a tree can't live past 50 years, this will ensure its dead. + for i in 0..51 + @tree.age! + end + number_of_apples = @tree.apples.count + @tree.age! + expect(@tree.apples.count).to eq number_of_apples + end + + it 'should be able to pick an apple' do + basket = [] + while @tree.apples.count == 0 do + @tree.age! + end + basket << @tree.pick_an_apple! + expect(basket.count).to eq 1 + end + + it 'should decrease the amount of apples on the tree when an apple is picked' do + basket = [] + while @tree.apples.count == 0 do + @tree.age! + end + num_apples_before = @tree.apples.count + basket << @tree.pick_an_apple! + expect(@tree.apples.count).to be < num_apples_before + end end describe Fruit do - it 'should be a class' do - expect(described_class).to eq Fruit - end + it 'should be a class' do + expect(described_class).to eq Fruit + end end describe Apple do - before :each do - @apple = Apple.new 'red', 3 - end + before :each do + @apple = Apple.new 'red', 3 + end - it 'should be a class' do - expect(described_class).to eq Apple - end + it 'should be a class' do + expect(described_class).to eq Apple + end - it 'should inherit from Fruit' do - expect(described_class).to be < Fruit - end + it 'should inherit from Fruit' do + expect(described_class).to be < Fruit + end - it 'should have a color' do - expect(@apple.color).to eq 'red' - end + it 'should have a color' do + expect(@apple.color).to eq 'red' + end - it 'should have a diameter' do - expect(@apple.diameter).to eq 3 - end + it 'should have a diameter' do + expect(@apple.diameter).to eq 3 + end end