Jimmy
Jimmy

Reputation: 51

ruby program using instance variables

I am program using instance variables based on Chris Pine's Ruby tutorial: Link

The assignment is:

Make an OrangeTree class. It should have a height method which returns its height, and a oneYearPasses method, which, when called, ages the tree one year. Each year the tree grows taller (however much you think an orange tree should grow in a year), and after some number of years (again, your call) the tree should die. For the first few years, it should not produce fruit, but after a while it should, and I guess that older trees produce more each year than younger trees... whatever you think makes most sense. And, of course, you should be able to countTheOranges (which returns the number of oranges on the tree), and pickAnOrange (which reduces the @orangeCount by one and returns a string telling you how delicious the orange was, or else it just tells you that there are no more oranges to pick this year). Make sure that any oranges you don't pick one year fall off before the next year.

Here is my program:

class OrangeTree

    def initialize
        @tree = "orange tree"
        @age = 0
        @height = 0
        @fruit = 0
        @fruitPicked = 0
        puts "your " + @tree + " is budding"
    end

    def age
        puts "your " + @tree + " is #{@age} years old."
        @age = @age
        oneYearPasses
    end

    def height
        puts "your " + @tree + " is growing taller."
        @height = @height
    end

    def fruit
        puts "your " + @tree + " has #{@fruit} oranges."
        @fruit = @fruit
    end

    def pickOrange
        puts "pick how many oranges?"
        @fruitPicked = @fruitPicked + gets.chomp.to_i
        puts "you picked #{@fruitPicked} oranges."
        @fruit = @fruit - @fruitPicked
    end

    def oneYearPasses
        #increase age by 1 each year
        #at the end of each year, all fruits fall off
        if @age >= 0
            @age = @age + 1
            @fruit = @fruit - @fruit    
        end

        #tree keeps growing until it reaches a peak of 20 inches
        if @height >= 0 and @height < 20
            @height = @height + 1
        else
            puts "your " + @tree + " is no longer growing taller."
        end

        #tree starts producing fruit at age 3, each year it will be able to 
        #produce more fruit then subsequent years by multiples of 2
        if @age >= 3
            @fruit = @fruit + (@age * 2)
        end

        #the amount of fruit that is picked
        if @age >= 3
            @fruitPicked = @fruitPicked + @fruitPicked
        end

        if age > 20 and @fruit = 0
            puts "your tree died"
        end
    end
end

orange = OrangeTree.new
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.age
orange.fruit
orange.pickOrange
orange.fruit
orange.age
orange.age
orange.fruit
orange.pickOrange

I have 2 questions: 1) why doesn't the orange tree die when the age > 20 and fruit = 0?
2) Calling my last line orange.pickOrange, I input 2, but my program tells me I picked 20 oranges. Why is this?

Thanks.

Upvotes: 1

Views: 656

Answers (1)

cvibha
cvibha

Reputation: 713

Answer to Q1

Use == for comparison instead of =

and use @age which is an instance variable used within class instead of age

 if age > 20 and @fruit = 0 should be

 if @age > 20 and @fruit == 0  within method **oneYearPasses**

Perhaps, after age of 20, fruit count will not be zero, it will be more than it, so you should change the condition for fruit there

Answer to Q2

After modifying code to

  @age > 20 and @fruit == 0

I tried this program and, I input 2, and program says, "you picked 2 oranges."

Thanks

Upvotes: 1

Related Questions