Adding the codes for exercise 4.2