首页 > > 详细

辅导ITI1120 bject-Oriented Programming

ITI1120 D – Review Session
Monday, April 4, 2022

Part One: Object-Oriented Programming:

1) Write a class Rectangle that represents a rectangular two-dimensional region. Your
Rectangle objects should have the following methods:

Method Description
def init (self,x,y,w,h)
Initializes a new Rectangle whose top-left corner is
specified by the given (x, y) coordinates and with the
given width and height, w by h.

Raise a ValueError on a negative width or height.
def height(self) A property representing the rectangle’s height.
def width(self) A property representing the rectangle’s width.
def x(self) A property representing the rectangle’s x-coordinate.
def y(self) A property representing the rectangle’s y-coordinate.
def str (self)
Returns a string representation of this Rectangle,
such as "Rectangle[x=1,y=2,width=3,height=4]".

2) Add the following accessor method to your Rectangle class: def contains (self, x, y). Your
method should return True if the given coordinates lie inside the bounds of this
Rectangle.

3) Add following method to Rectangle class: union(self,rect). Your method should accept
another Rectangle as a parameter and return a new Rectangle that represents the area
occupied by the tightest bounding box that contains both the current Rectangle (self) and
the given other Rectangle.

4) Add the following method to Rectangle class: intersection(self,rect). Your method should
accept another Rectangle as a parameter and return a new Rectangle that represents the
largest rectangular region completely contained within both the current Rectangle (self)
and the given other Rectangle. If the rectangles don’t intersect at all, returns a Rectangle
with its width and height both equal to 0.

5) Add the following method to your Rectangle class: def__eq__(self,rect). Your method
should accept another Rectangle as a parameter and return True if the two rectangles
have exactly the same state, including their x, y, width, and height values.

Part Two: Dictionaries and Sets:

Write a function called overlap that takes a set of integers and a list of integers as parameters
and that returns a new set containing values that appear in both structures. For example, given
set and list:
set1: {0, 19, 8, 9, 12, 13, 14, 15}
list1: [0, 19, 2, 4, 5, 9, 10, 11]
If the following call is made:
overlap(set1, list1)
the function would return:
{0, 19, 9}

You are not allowed to construct any structures, besides the set you will return, to solve this
problem. You may not alter the passed in list or set. You may not convert the list to a set.

Part Three: Recursion:

Write a function called reverse that accepts a dictionary from strings to strings as a parameter
and returns a new dictionary that is the reverse of the original. The reverse of a dictionary is a
new dictionary that uses the values from the original as its keys and the keys from the original
as its values. Since a dictionary’s values need not be unique but its keys must be, you should
have each value map to a set of keys. For example, if passed the following dictionary:
{42: "Marty", 81: "Sue", 17: "Ed", 31: "Dave", 56: "Ed", 3: "Marty", 29: "Ed"}

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!