The Register
Time for the last project before coroutines! This checkpoint puts the strings and objects from the last two lessons to work, together with everything you learned before them. As in the previous checkpoints, the instructions say what the program should print, how you write it is up to you, and each exercise also runs your program with other data.
The project. Emma keeps a register of her animals. Over three exercises, your program will read it from a text, turn each animal into an object, print the register in neat columns, and finally count the eggs her hens laid this week.
The data. register is a text, just as Emma wrote it in her notebook: for each animal its name, its kind and the year it was born, separated by colons, and the animals separated by a comma and a space. year is this year. The exercise also tries other registers and other years.
The task. Write a class Animal, read the register and make an object of the class for each animal. Then print every animal with its age, in the order of the register:
Bella the cow, age 5
Rex the dog, age 3
Zoé the cat, age 4
Goldie the hen, age 2
Pearl the hen, age 1An animal's age is this year minus the year it was born. And watch out for Zoé the cat: her name has an accent.
A tip: gmatch with three captures gives you the name, the kind and the year of each animal; remember which pieces of a pattern can handle an accent. And with __tostring, print can print an animal by itself.
Instructions
Print every animal in the register, as in the example.
Start programming for free
1/3