converting iterative code to object oriented code