Question: Exercise 1 Write a Python class named Circle constructed by a radius and two methods which will compute the area and the perimeter of a

Exercise 1

Write a Python class named Circle constructed by a radius and two methods which will compute the area and the perimeter of a circle. Test your code with examples.

Exercise 2

Define a class called Songs, it will show the lyrics of a song. Its __init__() method should take lyrics. Lyrics is a list. Inside your class create a method called sing_me_a_song that prints each element of lyrics on his own line. Test your code with examples.

Exercise 3

Define a class called Pet that takes a name and species at initialisation. The class has two methods that return the name and species respectively. Test your code with examples.

Exercise 4

Refactor the Pet class from the previous example so that it has a class variable allowed_species that contains a list of all the species that can be added to the class. During initialisation check that the species that is being created is an allowed species.

Exercise 5

Write a getter and setter method for the variable species. The setter method needs to check that species is an allowed species. Test the code using examples.

Exercise 6

Implement the task from 5 in a more Pythonic way using a property decorator. Test the code using examples.

Exercise 7

(Stretch) Write a decorator function @slow_down that takes another function as argument and slows the execution of the function down and only executes the function after 10 seconds. The function that takes the decorator should print out "Hello!". Test the code using examples.

Exercise 8

Implement an example for Polymorphism. Assume you have an abstract class Vehicle that provides the structure for the methods accelerate() and brake(). Declare two child classes Motorcycle and Bus that inherit from Vehicle. Implement the methods and write some test code. The methods should return a string that indicates in which class the method has been called.

Exercise 9

Inheritance - Create a class Person that takes the firstname, lastname and age as parameters in the initialisation. Add a method to the Person class that prints the name and age of the person. Create a class Student that inherits from Person. Create an instance of Student with the parameters required by the parent class and test if the method to print name and age that is implemented in the parent class is executed as expected. The mechanism to implement this is inheritance.

Exercise 10

Apply the concepts that were discussed in the lecture to your coursework. Think about your design. What classes do you need? Would it make sense to apply inheritance and/or polymorphism to your code? Think about alternative implementations. You can do this last task individually or in your coursework groups. Start defining the classes that you need.

Exercise 11

Implement a Product class with the following attributes (price, product_name, brand). Create a custom string representation for your product class by using the dunder methods __str__ and __repr__. Keep the goals for these methods in mind __str__ is targeted at end users and is about readability while the focus of __repr__ is to provide an unambiguous representation that can be used for debugging and development. Both methods should show the value of all attributes of the instance. Write test code for both methods.

Exercise 12

Create a class ShoppingBasket that has the attributes (customer_id, products). Products is a list of instances from your class Product in exercise 1. Implement the sequence protocol so that your class ShoppingBasket becomes iterable (based on the products list) and implement a dunder method so that you can call len() on your class ShoppingBasket. The result of the methods should be based on the number of products in your shopping basket. Write test code to test both methods.

Exercise 13

Add another dunder method to your class ShoppingBasket from the previous exercise so that you can add a new product to your shopping basket by using the + operator. The method should add the product and return the current value of the shopping basket. If an incompatible content type is added to the ShoppingBasket then your code should raise a TypeError with a meaningful error message. Test your code by using the + operator to add a new product to your basket and by trying to add an incompatible data type.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!