Code for Ch.2 examples