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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started