Introduction to Computer Programming: Lab 10

Classes and objects


Instructions


Exercises

  1. Read over the starter file, particularly the first part that shows the Counter class. Experiment in the shell with constructing counters, reading their values and incrementing them like this:

    >>> c = Counter()
    >>> d = Counter()
    >>> c.read()
    0
    >>> d.read()
    0
    >>> c.inc()
    >>> c.read()
    1
    >>> d.read()
    0
    ...
  2. Add a reset method to the Counter class that resets the counter to 0:

    >>> c = Counter()
    >>> c.inc()
    >>> c.inc()
    >>> c.read()
    2
    >>> c.reset()
    >>> c.read()
    0
  3. Add an optional argument initial_value to the Counter constructor (its __init__ method) so that counters can be constructed to start at initial_value (which should default to zero) as in:

    >>> c = Counter()
    >>> c.read()
    0
    >>> d = Counter(4103)
    >>> d.read()
    4103

    "Record" initial_value in the object that is constructed and modify reset so that it resets the counter to its initial value:

    >>> c = Counter(99)
    >>> c.inc()
    >>> c.read()
    100
    >>> c.reset()
    >>> c.read()
    99
  4. Add a method is_cleared that returns True if the counter is at its initial value:

    >>> c = Counter(53)
    >>> c.is_cleared()
    True
    >>> c.inc()
    >>> c.is_cleared()
    False
    >>> c.reset()
    >>> c.is_cleared()
    True
  5. Add a method add that takes an argument other_counter and adds the value of other_counter to this counter:

    >>> c = Counter(37)
    >>> d = Counter(43)
    >>> c.add(d)
    >>> d.read()
    43
    >>> c.read()
    80
    >>> c.reset()
    >>> c.read()
    37

  6. [Courtesy of Jim Marshall] Design and implement a Card class to represent playing cards. Your Card constructor (its init method) should take two parameters (beyond self): a number from 2 to 14 representing the card's rank (with 11=Jack, 12=Queen, 13=King, and 14=Ace), and another number from 0 to 3 representing the suit (0=Clubs, 1=Diamonds, 2=Hearts, 3=Spades). Card objects should have the following methods:

    You can test your class and its instances in the shell. For example:

        >>> qh = Card(12, 2)
        >>> jd = Card(11, 1)
        >>> c2 = Card(2, 0)
        >>> qh.get_rank()
        'Queen'
        >>> jd.get_suit()
        'Diamonds'
        >>> print(c2)
        2 of Clubs
        >>> jd.trumps(qh)
        False
        >>> qh.trumps(jd)
        True    
        >>> c2.trumps(c2)
        False

    If examples like those work, try using it via the supplied war() function.


  7. Examine the code in the final section of the starter file. Create a class Hopper (somewhat similar to the Thingy class we defined in lecture) that offers a constructor and methods hop, is_reset, and undo that are consistent with the way they are used by the add, hop, undo, and reset functions that are triggered by click and key events. Each time a hopper hops, it should record its former position on the end of its queue (which should be a Python list; use the append method for lists). A hopper's undo method should put the hopper back at its last position (and remove that position) from its queue (use pop). is_reset returns True if the hopper's queue is empty.

    The supplied go function should create a square_graphics window such that a new Hopper appears where the user clicks. 'h' should make all Hoppers hop up to one space in both the vertical and horizontal directions. 'u' should undo: all Hoppers should hop back to their previous location. 'r' should reset: all Hoppers should hop back, one hop at a time, all the way to their starting location.

    For this problem you should place a copy of the square_graphics module in your working folder.


Time permitting