Difference between revisions of "CSC111 Exercises on Writing Classes"
(→Exercise 2) |
|||
Line 33: | Line 33: | ||
</source> | </source> | ||
<br /> | <br /> | ||
− | + | =Exercise 3= | |
+ | * Create a class that will allow one to maintain a list of students. | ||
+ | * Each student is represented by a name, a year of graduation, and a GPA. | ||
+ | * This is in fact a poor-man (woman?) database | ||
+ | * The list of ''actions'' (read methods) that we want to be able to carry out on such a list are: | ||
+ | ** store the list to file (because the user is logging out of the system) | ||
+ | ** read the list from file (because the user is ready to use the database) | ||
+ | ** add a new student and her information | ||
+ | ** display the list of all the students, in alphabetical order | ||
+ | ** display the list of all the seniors, or all the juniors, or all the sophomores, or all the first-year. | ||
+ | ** return the list of all the students with a GPA greater than or equal to x | ||
+ | ** return the list of all the students with a GPA lower than or equal to x | ||
+ | ** return the list of all the seniors with a GPA greater than or equal to x | ||
<br /> | <br /> | ||
Revision as of 07:38, 6 December 2011
--D. Thiebaut 07:24, 6 December 2011 (EST)
Exercise 1
- Create a class for a traffic light that can be drawn on the graphics window, with the bus and trees.
- Your class should allow objects instantiated from it to
- Turn the green light on. This will set the other ones off
- Turn the yellow light on. This will set the other ones off
- Turn the red light on. This will set the other ones off
- The default mode is for TrafficLight objects to be created with the green light on.
Exercise 2
- Same as Exercise 1, but the objects are designed to be part of an animation loop. In the loop objects such as cars, buses or trees move left or right. TrafficLight object may move as well. However, their main function is to slowly change state, going from Green to Yellow, to Red, and back to Green. But they do change state slowly, every couple seconds or so. This is done by having a method called tick(), which is called once every time through the animation loop. Every 10 loops the traffic light changes state.
- The Traffic Light objects can be probed in the animation loop to see which light is on.
- Here is an example of an animation loop with a bus, trees, and a traffic light:
position = Point( ..., ... )
light = TrafficLight( position )
while True:
light.tick()
if light.isGreen():
bus.move( )
Exercise 3
- Create a class that will allow one to maintain a list of students.
- Each student is represented by a name, a year of graduation, and a GPA.
- This is in fact a poor-man (woman?) database
- The list of actions (read methods) that we want to be able to carry out on such a list are:
- store the list to file (because the user is logging out of the system)
- read the list from file (because the user is ready to use the database)
- add a new student and her information
- display the list of all the students, in alphabetical order
- display the list of all the seniors, or all the juniors, or all the sophomores, or all the first-year.
- return the list of all the students with a GPA greater than or equal to x
- return the list of all the students with a GPA lower than or equal to x
- return the list of all the seniors with a GPA greater than or equal to x