Your instructor may assign one or more of the following problems. Don’t feel that you must do all the problems; for the homeworks, you are only required to do those that are explicitly assigned via Moodle.
Extend the Fraction
class by adding these methods:
float
equivalent of the fraction.
Extend the console testing application that you started in the lab to include appropriate test cases for these new features.
Create a python program named war.py
that simulates a
simplified version of the card game War. Use the following approach:
Card
that models a standard playing
card. Objects
of this type will have a number between 1 and 13 (inclusive) and a
suit (Spade, Heart, Club or Diamond). You should not include
Jokers. This class must include:
__str__
method to allow printing of Card
objects. For example, a card representing the 9 of spades will
print as 9S
, and a card representing the 12 (i.e.,
Queen) of hearts will print as QH
. A card with a
number of 1 should be represented with A (for Ace).
The code to implement this will probably look like this:
<
,
==
and >
to allow comparison of cards based on their
number (suit does not matter). Note: An Ace should be considered
higher than a King. Note that these methods return a boolean value
-- True or False.__str__
is correct. You'll want to
compare different Card objects with <, >, and == to make sure
those functions return the correct boolean values. The more
testing you do of your code now, the less testing you'll have to
do later, so it is definitely to your benefit to test *everything*
you can now.
Create a program called circle.py
that implements a
class that represents circle objects. Each circle has a center (modeled as
an x-y coordinate tuple), a radius, color (modeled as a Python
turtle color string, e.g., ‘black’) and a Boolean flag
indicating whether it is to be drawn as filled or or just outlined.
Circles cannot have negative radii.
Implement the following features.
__str__()
method
that produces a properly formatted string for printing the
circle’s internal state.get_area()
), its circumference (get_circumference()
)
and any additional accessors needed by other methods.modify_radius()
)
that receives a “delta” value from the calling program
and adds this value to the circle’s radius.overlaps()
)
that receives another circle object from its calling program and
determines if that other circle overlaps with itself. Return true
if the circles overlap in any way. (Cf. homework 4.1, which also
distinguishes overlap from containment.)render()
)
that receives a turtle object from its calling program and uses
that object to draw itself on the canvas.Add code at the bottom of the module to exercise this class by creating a default circle, an invalid circle, circles that do and do not overlap, and generally testing the features provided by the class.
Create a program called rectangle.py
that implements a
class that represents rectangle objects. Each rectangle has an upper left point (modeled as
an x-y coordinate tuple), a width, a height and a color (modeled as a Python
turtle color string, e.g., ‘black’).
Rectangles cannot have negative widths or heights.
Implement the following features.
__str__()
method
that produces a properly formatted string for printing the
rectangle’s internal state.get_area()
), its perimeter (get_perimeter()
)
and any additional accessors needed by other methods.modify_width()
and modify_height()
)
that receive a “delta” value from the calling program
and adds this value to the rectangle’s width or height as appropriate.overlaps()
)
that receives another rectangle object from its calling program and
determines if that other rectangle overlaps with itself. Return true
if the rectangles overlap in any way. (Cf. homework 4.2.)render()
)
that receives a turtle object from its calling program and uses
that object to draw itself on the canvas.Add code at the bottom of the module to exercise this class by creating a default rectangle, an invalid rectangle, rectangles that do and do not overlap, and generally testing the features provided by the class.
Submit all appropriate files for grading, including code files, screen captures, supplemental files (e.g., image files), and text files. We will grade this exercise according to the following criteria: