Modified code by object oriented approach