Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python help 2.2.3 Organism Class This is the “base class” and the Plant and Anim

ID: 3694379 • Letter: P

Question

Python help

2.2.3 Organism Class

This is the “base class” and the Plant and Animal classes inherit from it.

The constructor is Organism(position, name) where position is the position of the organism and name is its name.

Note that if the organism is a plant then the position is a list containing the xy-coordinates of the plant. For an animal it’s a list whose elements are of the form [[x,y], time].

Technical Aside:

It would be better to use tuples rather than lists but json doesn’t support tuples and so, to simplify loading and saving, we have used lists.

The attributes of this class are the position, name and generated ID. The ID’s are strings of the form id_ followed by a unique number. This can be programmed by having a class variable initialized to 1 and every time an object is created it is incremented.

The required methods are as follows.

get_id() Returns the ID of the object.

set_id(id) Sets the ID of the object - used when loading from a file.

get_position() Returns the position of the object - intended to be overridden in the Animal class.

get_full_position() Returns the position of the object (gets all positions of organism (previous))

get_name() Returns the name of the Organism

get_track() Returns an empty list - intended to be overridden in the Animal class.

to_dictionary() Returns a dictionary of the information contained in the object - extended in the Plant and Animal class.

This dictionary has the form

{‘name’: object_name, ‘id’: object_id, ‘position’: full_position}

2.2.4 Plant Class

This should inherit from the Organism class and its constructor is Plant(position, name).

The required methods are as follows.

__str__() returns a string representation of the plant using PLANT_FORMAT

to_dictionary() returns a dictionary of the information contained in the object - extends the Organism dictionary with {‘type’: ‘plant’}.

2.2.5 Animal Class This should inherit from the Organism class and its constructor is Animal(position, name, gender, tracker_id) where tracker_id is the empty string if no tracker ID is supplied.

The required methods are as follows.

__str__() 6 Returns a string representation of the animal using BASIC_ANIMAL_FORMAT or TRACKER_ANIMAL_FORMAT depending on whether a tracker ID is supplied. to_dictionary() Returns a dictionary of the information contained in the object - extends the Organism dictionary with {‘type’: ‘animal’, ‘tracker_id’: tracker_id, ‘gender’: gender} where tracker_id is the tracker ID and gender is either ‘male’ or ‘female’.

get_position() overwrites the method from the Organism class and returns the xycoordinates of the Animal’s current position (i.e. the last entry in the position list). add_location(position) adds the position together with the current time to the position list.

get_track() returns [] if there is only one entry in the position list, otherwise it returns the list of xy-coordinate pairs in the position list.

Explanation / Answer

Hey heres the code:

class Organism(object):
    def __init__(self,position,name):   
        self.position=position
        self.name=name
        self.id=0
    def get_id(self):
        if self.id==0:
            print "id not set so set id."
            return 0
        return self.id
    def set_id(self,id):
        self.id=id
    def get_position(self):
        return self.position
    def get_full_position(self):
        return self.position
    def get_name(self):
        return self.name
    def get_track(self):
        l=[]
        return l
    def to_dictionary(self):
        D={}
       
        D['name']=self.name
        D['id']=self.id
        D['position']=self.position
        return D
class Plant(Organism):
    def __init__(self,position,name):               
        Organism.__init__(self,position,name)
        self.position=position
        self.name=name
    def __str__(self):
        return "name : "+self.name+" id:"+self.id
    def to_dictionary(self):
        D={}
        D['type']='plant'
       
        return {D,Organism.to_dictionary(self)}
class Animal(Organism):
    def __init__(self,position,name,gender,tracker_id):
        self.name=name
        self.position=position
        self.gender=gender
        self.id=tracker_id
    def __str__(self):
        return "position: "+self.position+" name:"+name+" gender: "+gender+" id:"+id
    def to_dictionary(self):
        D['type']='animal'
        D['tracker_id']=self.id
        D['gender']=self.gender
        return {D,Organism.to_dictionary(self)}
    def get_position(self):
        return self.position
    def get_track(self):
        if len(self.position) > 1:
            return self.position
        else:
            return []