Python question Define a class Plant that satisfies the spec

Python question.

Define a class Plant that satisfies the spec below. Represents selected information about a plant. name - String, common name for the plant mass - Positive float, the biomass of the plant, in kilograms Plant A string representing a plant\'s common name, and a positive float representing the plant\'s initial biomass. 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. That newly created Plant object. getName String, the name for this plant getMass Float, the current biomass for this plant setMass Positive float The state variable mass is assigned the value arg1 Positive float, the new value of the state variable mass. tree = Plant(\'red oak\', 1042) flower = Plant(\'rose\', 2.7) tree.getName() rightarrow \'red oak\' flower.setMass(2.85) flower.getMass() rightarrow 2.85

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.

class Plant:
name = \"\"
mass = 0

def __init__(self, name, mass):   #constructor using name and mass
self.name = name
self.mass = mass
  
def getName(self):   #getter method for name
return self.name

def getMass(self):   #getter method for mass
return self.mass
  
def setMass(self,m): #setter method for mass
self.mass = m
return self.mass
  
tree = Plant(\'red oak\',1042)   #tests
flower = Plant(\'rose\',2.7)
print(tree.getName())
flower.setMass(2.85)
print(flower.getMass())

-------------------------------------------------------

OUTPUT:

Python question. Define a class Plant that satisfies the spec below. Represents selected information about a plant. name - String, common name for the plant mas

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site