Difference between revisions of "CSC111 Exercises on Writing Classes"

From dftwiki3
Jump to: navigation, search
(Exercise 3)
 
(2 intermediate revisions by the same user not shown)
Line 33: Line 33:
 
</source>
 
</source>
 
<br />
 
<br />
 
+
=Exercise 3=
 +
[[Image:GPA.gif|100px|right]]
 +
* 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
 +
* Below is the list of ''actions'' (read methods) that we want to be able to carry out on such a list:
 +
** 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 />
  

Latest revision as of 08:42, 6 December 2011

--D. Thiebaut 07:24, 6 December 2011 (EST)


TrafficLight.jpg

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

GPA.gif
  • 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
  • Below is the list of actions (read methods) that we want to be able to carry out on such a list:
    • 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