Question: Write a class named Forest which represents a set of specific trees. The Forest class tracks the set of trees, along with how old each

Write a class named Forest which represents a set of specific trees. The Forest class tracks the set of trees, along with how old each tree is.

You will also need to include your Tree class from the previous question in the answer box.

You must complete the following functions in the Forest class:

__init__()

The init function should take as a parameter a list of tuples, where each Tuple contains a Tree and an associated age.

__str__()

The str function should print the total number of trees within the forest, and then summarize what each one is.

Consider the following code fragment:

ash = Tree("Ash", 4, (150,75,0), (0,255,0), 30) my_forest = Forest([(ash, 4), (ash, 7)]) print(my_forest)

The output should be:

Forest containing 2 trees: 4 year old Ash 7 year old Ash
add_tree()

The add_tree function takes as parameters an instance of the Tree class and an age, and appends to the Forest a new tree of that species.

Consider the following code fragment:

ash = Tree("Ash", 4, (150,75,0), (0,255,0), 30) my_forest = Forest([(ash, 4), (ash, 7)]) my_forest.add_tree(ash, 6) print(my_forest)

The output should be:

Forest containing 3 trees: 
4 year old Ash 7 year old Ash 6 year old Ash

class Tree:

class Tree: def __init__(self,name,length,color_b,color_l,angle): # defining init method self.name = name self.length = length self.color_b = color_b self.color_l = color_l self.angle = angle def __str__(self): # defining str method return self.name def __repr__(self): # defining repr method return 'Tree("{}", {}, {}, {}, {})'.format(self.name,self.length,self.color_b,self.color_l,self.angle)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!