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

  1. For this problem, provide an implementation for the Plant spec below, and test it using the example calls provided.
  2. 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.)
  3. 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):
  4. Likewise, the spec shows 0 arguments for the method getSpecies(), but that doesn't count self:

def getSpecies(self):

return self.species

  1. This "zeroeth" argument self must appear first in an argument list

Plant class

Represents selected information about a plant.

State variables

  1. name - String, common name for the plant
  2. 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

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!