Question
Exercise-1: 5 points Plant class. Define a class Plant that satisfies the spec below. Hints For this problem, provide an implementation for the Plant spec
Exercise-1: 5 points
Plant class. Define a class Plant that satisfies the spec below.
Hints
- For this problem, provide an implementation for the Plant spec below, and test it using the example calls provided.
- Although the constructor is described in the spec (and called) using the name Plant(), the internal name for a class's constructor is __init__(). (There are two underscores _ before and to underscores after init.)
- A "zeroeth" argument self should be included with the constructor and each method. For example, the spec shows two arguments for the constructor, but __init__() should be defined using three arguments: def __init__(self, species, mass):
- Likewise, the spec shows 0 arguments for the method getSpecies(), but that doesn't count self:
def getSpecies(self):
return self.species
- This "zeroeth" argument self must appear first in an argument list
Plant class
Represents selected information about a plant.
State variables
- name - String, common name for the plant
- mass - Positive float, the biomass of the plant, in kilograms
Constructor
Plant 2 arguments: A string representing a plant's common name, and a positive float representing the plant's initial biomass. State change: A new object of type Plant is created. The state variable name is assigned the value arg1, and the state variable mass is assigned the value arg2. Return: That newly created Plant object. |
Methods
getName No arguments. Return: String, the name for this plant
|
getMass No arguments.
Return: Float, the current biomass for this plant |
setMass One argument: Positive float State change: The state variable mass is assigned the value arg1 Return: Positive float, the new value of the state variable mass. |
Example calls:
tree = Plant('red oak', 1042)
flower = Plant('rose', 2.7)
tree.getName() --> 'red oak'
flower.setMass(2.85)
flower.getMass() --> 2.85
Note: insert appropriate amount of comments in your program
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