Practice exam final
Instructions
-
This is a "closed book" examination and you must work entirely on your own. The source code you submit must be entirely your work and must be written entirely during the class period. The use of any pre-existing code (other than that provided as part of the exam) is prohibited.
-
You must login using the username
student
(which does not have a password), not using your eid. -
You may only use a terminal/shell window, Thonny and a web browser. The only page you may load in the WWW browser is the course submission site.
-
Your code need not comply with the course style guidelines and need not include comments.
-
You must submit your code using Gradescope.
-
Your last submission is the one that will be graded. No limit will be placed on the number of submissions but, obviously, you must complete the exam during the class period and submissions take time.
Q1: Rectangles and Squares (15pts)
Complete the convert_to_square
function below so that it conforms to the docstring. Submit your completed code in a file named geometry.py
.
import math
class Rectangle():
def __init__(self, length, width, x, y):
self.length = length
self.width = width
self.x = x
self.y = y
def area(self):
return self.length * self.width
def convert_to_square(rectangle):
"""Return a square with the same area as the provided rectangle.
The returned Rectangle object will have the same area and x, y
location as the provided rectangle, but the length and width may
be different. The original rectangle must not be modified.
Args:
rectangle (Rectangle): An arbitrary rectangle object
Returns:
Rectangle: A new Rectangle object that has a length that is
equal to its width
"""
pass
HINT: For any square, the lengths of the sides are equal to the square-root of the area. The math.sqrt
function can be used to calculate square roots in Python.
Q2: Dress (15pts)
Complete the Dress
class illustrated in the following UML diagram:
-
The constructor must initialize the three instance variables using the provided parameter values. The
color
parameter must have a default value of'black'
. -
The
is_junior
method most returnTrue
if the dress size is an odd number, andFalse
otherwise. -
The
__eq__
method must return True if and only if the other object is a Dress that has the same style, size and color.
Submit your finished class in a file named dress.py.
Q3: DressInventory (20pts)
Complete the unfinished methods in the following Inventory
class so that they conform to the docstrings. Upload your completed code in a file named dress_inventory.py
.
class DressInventory:
"""Tracks the dresses currently in inventory.
Attributes:
inventory (list): A list of all Dress objects in stock
style_counts (dict): A dictionary that maps from styles names
to the number of dresses in that style
"""
def __init__(self):
"""Initialize the Inventory object."""
self.inventory = []
self.style_counts = {}
# THIS METHOD IS FINISHED. DO NOT MODIFY
def add(self, dress):
"""Add the provided dress to the inventory list.
The style counts dictionary will also be updated.
Args:
dress (Dress): A dress object
"""
pass # UNFINISHED
def num_junior(self):
"""Return the number of Junior-sized dresses in stock.
This method must loop through the inventory to count
junior-sized dresses.
Returns:
int: The number of junior-sized dresses.
"""
pass # UNFINISHED
def find_dresses(self, styles):
"""Return all dresses that match one of the provided styles.
HINT: This will require a nested loop: The outer loop will
iterate over dress styles and the inner loop will iterate over
the dresses in inventory.
Args:
styles (set): A set of strings representing styles to
search for.
Returns:
list: A list containing every Dress object in the
inventory that matches one of the provided styles.
"""
pass # UNFINISHED