Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

Students also viewed these Databases questions