Python Q&A – Class Design

Question 1: Using Classes
A class is a user-defined prototype for an object that defines a set of attributes that characterize any object in the class.

Task: write a function print_friend_info(person) which accepts a single argument, of type Person, and:

  • prints out their name
  • prints out their age
  • if the person has any friends, prints ‘Friends with {name}’

Write a function create_fry() which returns a Person instance representing Fry. Fry is 25 and his full name is ‘Philip J. Fry’

Write a function make_friends(person_one, person_two) which sets each argument as the friend of the other.

bob = Person(‘Bob Smith’, 40, ‘M’); print_friend_info(bob)
ed = Person(‘Ed’, 8, ‘M’); bob.set_friend(ed); print_friend_info(bob)
fry = create_fry(); str(fry) -> ‘Mr Philip J. Fry 25’
leela = Person(‘T. Leela’, 22, ‘F’); make_friends(fry, leela)
given that the class is:

Answer:

Explanation: The print_friend_info(person) function will require an input of a person. Since the function wants us to print a string that requires us to get the person’s name, age and friends. To do this, we assign variables, and call upon the functions of the Person class. (E.g. To get the person’s name, i called my variable person_name and called the function .get_name() of the Person class.). An if statement is created since we want to differentiate between people who have friends and people who do not. If the person has no friends, print the person’s name and the person’s age (both on separate lines). If the person has friends, print the person’s name, the person’s age, and the person’s friend’s name.

The create_fry() function is relatively basic, because it just involves us to assign a variable to an instance of the Person class, where we can see that the initialisation of the class requires us to have inputs of name, age and gender. We then return the variable fry.

The make_friends(person_one, person_two) function requires us to call upon the Person’s class function of set_friend. This function allows us to assign the friend to that person. We need to do it for both sides, and thus it needs to be done for the person and the friend.

Question 2: Defining a Class
Creating our own classes can be extremely useful when we want to keep certain functions in a group for a specific purpose.

Task: Write a Rectangle class that can be moved and resized. Its constructor takes a pair of numbers representing the top-left corner, and two other numbers representing the width and height. It needs the methods:

  • get_bottom_right() – return the bottom right corner as a pair of numbers.
  • move(p) – move the rectangle so that p becomes the top-left corner (leaving the width and height unchanged).
  • resize(width, height) – set the width and height of the rectangle to the supplied arguments (leaving the top-left corner unchanged).
  • __str__() – return the string representation of the rectangle as a pair of pairs – i.e. the top left and bottom right corners.

Defined Rectangle class
Rectangle((0, 0), 1, 1).get_bottom_right() -> (1, 1)
Rectangle((2, 3), 2, 1).get_bottom_right() -> (4, 4) str(Rectangle((0, 0), 1, 1)) -> ‘((0, 0), (1, 1))’
r = Rectangle((0, 0), 1, 1); r.move((2, 2)); str(r) -> ‘((2, 2), (3, 3))’
r = Rectangle((0, 0), 1, 1); r.resize(2, 2); str(r) -> ‘((0, 0), (2, 2))’

Answer:

Explanation: We need to first initialize the class, where the task requires us to have a variable that is assigned to the top left corner of the rectangle (x and y) and also variables assigned to the width and length of the rectangle.

The method get_bottom_right(self) will require us to find the bottom right of the rectangle. We need to first create variables and to get from the left to the right for the x-component, the x-component needs the width to be added to it (and this is assigned to the right variable). To get the y-component, the height needs to be added to it (and this is assigned to the bottom variable). Remember that the y-component in Python, increases as you go down (instead of the y-component increasing when you are going up like in Mathematics).

The method move(self, p) will require us to make it so that whenever you move the rectangle the top-left corner will become p. To do this, we simply reassign the x and y component (self._x and self._y) from the initialization to become p[0] and p[1] respectively.

The method resize(self, width, height) requires us to change the rectangle’s width and height according to the input of resize. Therefore we assign the input width and height to self._width and self._height.

For __str__ method (it is in every class, usually to return a string to represent the class), we want to output the rectangle as a pair of pairs (top left and bottom right corners). There are many ways to do this, but an easy and quick way is to use the .format() function, where we put two input slots {0} and {1} and have two pairs of values to insert it into (self._x, self._y) for {0} and (self._x + self._width, self._y + self._height) for {1}.

Question 3: A Point Class Definition
A two-dimensional point may be represented by an x- and y-coordinate. In this problem, you will write a Point class which stores this information and provides useful methods to work with points.

Task: Write a method dist_to_point, which accepts another instance of the Point class as an argument, and returns the Euclidean distance between the two points. It may be helpful to use math.sqrt.

Using dist_to_point, now write a method is_near which returns whether two points are close. Remember that you can call other methods in a method definition using self, eg self.other_method(arg)

A point is considered ‘close’ if it is within epsilon of another point. We defined epsilon to be a really small number using scientific notation (so, eg, 1e-3 is 1×10^-3 is 0.001

Finally, we want to be able to add two points together. Write a method add_point that adds the position of the Point object given as an argument to the position of self

Given the initialization of:

Tests to Satisfy:

Answer:

Explanation: For the dist_to_point(self, point) method, we want to calculate the “width” of the line from x to point (only in the x-component) and we also want to calculate the “height” of the line from y to point (only in the y-component). This will mean that Pythagoras’ Theorem will be used to find the hypotenuse of the “triangle created” (which is the distance from point (x,y) to point). By using a built-in function from the Python math library, we square the x-component and y-component of the distance. We then square root the addition of these two to find the the hypotenuse (distance from point (x,y) to point).

For the is_near(self, point) function, we want to see whether the two points are near, and to do this we compare the distance found from dist_to_point to epsilon, which is a value of 1e-5. An if statement is used to analyse whether it is smaller than epsilon (return True) or larger than it (return False).

For the add_point(self, point) function, we want to add the two points together and to do this, we just make it so that the initialization value of x and y (self._x and self._y) have itself and the point’s x and y component (point._x and point_y) added to it. We can do this because point is a instance of the Point class.

, , , ,

No comments yet.

Leave a Reply

Powered by WordPress. Designed by WooThemes

Free WordPress Themes