-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
. #75
base: master
Are you sure you want to change the base?
. #75
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,35 @@ | ||
class NoApplesError < StandardError; end | ||
|
||
class Tree | ||
attr_#fill_in :height, :age, :apples, :alive | ||
attr_reader :height, :age, :apples, :alive | ||
|
||
def initialize | ||
def initialize(height, age, apples, alive) | ||
@height = height | ||
@age = age | ||
@apples = apples | ||
@alive = alive | ||
end | ||
|
||
def age! | ||
add_apples unless @age < 3 | ||
@age += 1 | ||
end | ||
|
||
def add_apples | ||
rand(5..15).times do @apples.push(Apple.new()) end | ||
end | ||
|
||
def any_apples? | ||
@apples.size > 0 | ||
end | ||
|
||
def pick_an_apple! | ||
raise NoApplesError, "This tree has no apples" unless self.any_apples? | ||
@apples.shift() | ||
end | ||
|
||
def dead? | ||
age > 10 | ||
end | ||
end | ||
|
||
|
@@ -29,19 +39,17 @@ def initialize | |
end | ||
end | ||
|
||
class Apple < | ||
attr_reader #what should go here | ||
class Apple < Fruit | ||
attr_reader :color, :diameter | ||
|
||
def initialize(color, diameter) | ||
def initialize() | ||
@color = ['red'].sample | ||
@diameter = rand(5..10) | ||
matthewlee07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
tree = Tree.new(5, 1, [], true) | ||
|
||
tree.age! until tree.any_apples? | ||
|
||
|
@@ -50,7 +58,6 @@ def tree_data | |
until tree.dead? | ||
basket = [] | ||
|
||
# It places the apple in the basket | ||
while tree.any_apples? | ||
basket << tree.pick_an_apple! | ||
end | ||
|
@@ -61,14 +68,13 @@ def tree_data | |
diameter_sum += apple.diameter | ||
end | ||
|
||
avg_diameter = # It's up to you to calculate the average diameter for this harvest. | ||
avg_diameter = diameter_sum/basket.size | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this is doing integer division. Is that what is intended? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's what I meant to do. Am I doing it incorrectly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suppose you a had basket with two apples in it, with diameters of 7 and 8, respectively. What would you expect the average diameter to be? What does this code say the average diameter is? |
||
|
||
puts "Year #{tree.age} Report" | ||
puts "Tree height: #{tree.height} feet" | ||
puts "Harvest: #{basket.size} apples with an average diameter of #{avg_diameter} inches" | ||
puts "" | ||
|
||
# Ages the tree another year | ||
tree.age! | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,25 @@ | |
it 'should be a Class' do | ||
expect(described_class.is_a? Class).to eq true | ||
end | ||
it 'should create an instance of Tree with correct attributes' do | ||
tree = Tree.new(5, 1, 0, true) | ||
expect(tree.height).to eq(5); | ||
expect(tree.age).to eq(1); | ||
expect(tree.apples).to eq(0); | ||
expect(tree.alive).to eq(true); | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice test |
||
end | ||
|
||
describe 'Fruit' do | ||
describe Fruit do | ||
it 'should be a Class' do | ||
expect(described_class.is_a? Class).to eq true | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This kind of test is quite possibly not worth having in a project. If |
||
|
||
end | ||
|
||
describe 'Apple' do | ||
describe Apple do | ||
it 'apple should be a an instance of Apple and subclass of Fruit' do | ||
apple = Apple.new() | ||
expect(apple).to be_a_kind_of(Fruit) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you find yourself putting 'and' in the description of your test, then you should consider writing two tests instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed it to be two tests, one for Apple < Fruit, and one for apple to be_an_instance_of(Apple) |
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple ruby style points in here:
{ @apples.push(Apple.new()) }
.do
-end
is preferred for multi-line blocks.()
for method calls with no arguments.