Object Oriented Information Systems

More on Classes

In this unit we cover Inheritance, Abstraction, and Polymorphism.

As an exercise, write an __lt__ method for Time objects. You can use tuple comparison, but you also might consider comparing integers.

class Time:
   pass

   def __init__(self, hour, minute, second):
      self.hour = hour
      self.minute = minute
      self.second = second

   def __lt__(self, other):
      t1 = self.hour, self.minute, self.second
      t2 = other.hour, other.minute, other.second

      return t1 < t2

obj_time1 = Time(10, 29, 56)
obj_time2 = Time(11, 29, 57)

print(obj_time1 > obj_time2)

Write a Python program with polymorphism that is usable within the summative assessment for the driverless car.

The LIDAR and the RADAR are sensors that calculate the distance from the driverless car to surrounding objects. The main difference between them is that the first uses laser rays, and the second uses radio waves.

class Seeder():
   objects_seeds = ['car', 'person', 'wall']
   distances_seeds = range(3, 15)
   object_list = []

   def __init__(self, n):
      for n in range(n):
         self.object_list.append([
         random.choice(self.distances_seeds),
         random.choice(self.objects_seeds)
      ])

class LIDAR():
   # The objects within the car environment

   current_object_list = []

   def __init__(self, frequency, wavelength):
         self.frequency = frequency
      self.wavelength = wavelength
      self.objects = Seeder(10)

   # We require a list of current objects
   def read_objects(self):
      random.shuffle(self.objects)
      return random.sample(self.objects, 3)

class RADAR():
   # The objects within the car environment

   current_object_list = []

   def __init__(self, frequency, range):
      self.frequency = frequency
      self.range = range
      self.objects = Seeder(10)

   # We require a list of current objects
   def read_objects(self):
      random.shuffle(self.objects)
      return random.sample(self.objects, 3)


The seeder class is used to generate a random list of objects. In the LIDAR and RADAR classes, a sublist of elements is selected randomly.