Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

OOP in Python Exercise Sheet, Exercises of Object Oriented Programming

A collection of exercises on object-oriented programming in python, covering topics such as the strategy pattern, vectors, scatter plots, understanding oop, decorator pattern, and extending classes. Students are asked to write code to implement various features and functions, as well as to analyze and extend existing classes.

Typology: Exercises

2021/2022

Uploaded on 02/11/2022

shekhar_hin
shekhar_hin 🇺🇸

4.9

(9)

226 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
OOP in Python, Scientific Programming with Python 2021 1
Department of Physics Scientific Programming with Python
OOP in Python June 28, 2021
Exercises
Exercise 1: Ducks [basic]
Look again at the slides on the strategy pattern and use the code examples to define the
following duck-classes1:
NormalDuck (use as base class)
RedheadedDuck
BlackDuck
RubberDuck
DecoyDuck
Once you defined these classes:
(a) Write a script in which you create the following duck-instances: 3 normal, 3 red-
headed, 1 black, 1 rubber and 1 decoy duck. Store all ducks in a list and then call
display for each of the ducks.
(b) In your script, let one of the normal ducks ,,break its wings”. (Replace the flying_behavoir
of the unlucky duck-object.) Compare what happens when you call the flying-
functions of the unlucky duck and one of the other normal ducks.
(c) Change your duck classes such that you can give your individual ducks a name. Use
that name when displaying the duck.
(d) [intermediate] Change your duck classes such that they store their position (as a
string). Let fly_to change the position and display the present position on take off
and landing.
(e) Add more functionality, be creative.
1You are of course free to use real duck breeds (see https://en.wikipedia.org/wiki/List_of_duck_
breeds) if you prefer.
Jonas Eschle June 28, 2021
pf3
pf4

Partial preview of the text

Download OOP in Python Exercise Sheet and more Exercises Object Oriented Programming in PDF only on Docsity!

OOP in Python June 28, 2021

Exercises

Exercise 1: Ducks [basic]

Look again at the slides on the strategy pattern and use the code examples to define the following duck-classes^1 :

  • NormalDuck (use as base class)
  • RedheadedDuck
  • BlackDuck
  • RubberDuck
  • DecoyDuck

Once you defined these classes:

(a) Write a script in which you create the following duck-instances: 3 normal, 3 red- headed, 1 black, 1 rubber and 1 decoy duck. Store all ducks in a list and then call display for each of the ducks.

(b) In your script, let one of the normal ducks ,,break its wings”. (Replace the flying_behavoir of the unlucky duck-object.) Compare what happens when you call the flying- functions of the unlucky duck and one of the other normal ducks.

(c) Change your duck classes such that you can give your individual ducks a name. Use that name when displaying the duck.

(d) [intermediate] Change your duck classes such that they store their position (as a string). Let fly_to change the position and display the present position on take off and landing.

(e) Add more functionality, be creative. (^1) You are of course free to use real duck breeds (see https://en.wikipedia.org/wiki/List_of_duck_

breeds) if you prefer.

Exercise 2: Vectors [basic]

The file vector.py contains an implementation of an n-dimensional vector. Some of the functions are not yet complete:

(a) Implement the addition of two vectors via the magic function add. Make sure that the dimensions of the two vectors are aligned.

(b) Do the same for the scalar product with the function mul.

(c) Implement str which is the magic function to represent the vector as string (i.e. str(v)). Define a reasonable string representation.

(d) Create the property length that

  • returns the Euclidiean length of the vector
  • allows to scale the vector to a new length by v.length =
  • sets the vector to zero via del v.length.

(e) Create a subclass for three-dimensional vectors Vector3D with a suitable constructor and implement the magic function matmul (the operator @) as cross-product^2. Important: The implementation should NOT lead to any change in the parent class.

Exercise 3: Scatter plot [basic]

Take pen and paper and design a class representing scatter plots that can be drawn.

  • What variables does a scatter plot have?
  • What methods does it have?
  • How do the signatures of these methods look like?

Exercise 4: Understanding OOP [basic]

The graph module (provided in the archive) contains a set of classes for representing graphs (i.e. nodes and edges connecting them). On a piece of paper reverse engineer its design:

(a) Write down all class names, their methods and data attributes; try to understand what all of them do (read the documentation!).

(b) Figure out how different classes are related. Where is inheritance used, where com- position? Draw a simple diagram.

(^2) The cross-product is defined as v = v 1 × v 2 =

(x 1 y 1 z 1

) ×

(x 2 y 2 z 2

)

( (^) y 1 z 2 − y 2 z 1 z 1 x 2 − z 2 x 1 x 1 y 2 − x 2 y 1

)

(c) Implement the following city graph as an example:

(d) Now we want to find the quickest path from Berlin to Cologne. Open the shortest path.py file. It contains a SearchAlgorithm class, which implements the Dijkastra algorithm for finding the shortest path in a graph.

(e) Define a new class SearchGraph extending the Graph class with methods for search- ing the shortest path. Which design pattern(s) can you use in the example?

(f) Define new search algorithms to find the cheapest and fastest paths.

(g) Find the cheapest and fastest paths between Berlin and Cologne.

This exercise sheet is based on the exercises written by Bartosz Telenczuk, Niko Wilbert for the Advanced Scientific Programming in Python School 2011