Unit 5 Assignment Questions
Unit 5 Assignment Questions
Easy Questions
1.Define a class named Rectangle which can be constructed by a length and width.The Rectangle
class has a method which can compute the area.
2. Define a class called Lunch. It’s __init__() method should have two arguments: self and menu
where menu is a string. Add a method called menu_price. It will involve an if statement:
if "menu 1" print "Your choice:", menu, "Price 12.00", if "menu 2" print "Your choice:",
menu, "Price 13.40", else print "Error in menu".
To check if it works define: Paul=Lunch("menu 1") and call Paul.menu_price()
3.Create a class named triangle. The constructor for this class should take two numeric
arguments, which are the width and height. Add methods to compute the area and perimeter of
the triangle, as well as methods that simply return the height and width. Add a method isequi()
that returns a Boolean value if the triangle is equilateral.
Medium Questions
a)__init__():Initializes the bank account balance to the value of the input argument ,or to 0 if
no input argument is given
>>>x=BankAccount(500)
>>>x.balance()
500.00
>>>x.withdraw(70)
>>>x.balance()
430.00
>>>x.deposit(10)
>>>x.balance()
440.00
a) __init__():A constructor that takes as input a person’s name (as a string) ,birth year(as an
integer)and person’s degree
b) age(): Returns the age of a person(use function localtime() from the Standard library
module time to compute the age)
c) name():Returns the name of the person
d) degree():Returns the degree of the instructor.
__init__():Constructor that takes the person’s major in addition to name and birth year
major(): Returns the major of the student
Your implementation of the three classes should behave as shown in the text code:
>>>x=Instructor(‘Guido Von Rossum’,1973,’PhD’)
>>>x.age()
47
>>>y=Student(‘jones’,’1987,’’computer science’)
>>>y.age()
33
>>>y.major()
‘Computer Science’
>>>x.degree()
‘PhD’
6.Write a program which has list of user data (tuples containing a username, email and age) and
adds each user to a directory if the user is at least 16 years old. You do not need to store the age.
Write a exception which defines a different exception for each of these error conditions:
7.Write a class named Circle with attributes center and radius ,where center is a Point object and
radius is a number.Instantiate a Circle object that represents a circle with its center at ( 150, 100 )
and radius 70.
a) Write a function point_inside that takes a Circle and a Point and returns True if the Point
lies in or on the boundary of the circle.
b) Write a function named rect_inside that takes a Circle and a Rectangle and returns True
ifthe Rectangle lies entirely in or on the boundary of the circle.
c) Write a function rect_overlaps_circle() that takes a Circle and a Rectangle and returns
True if any of the corners of the Rectangle fall inside the circle.
iv) ratingmovie-display the rating based on number of days running (suppose100 days:10 90
days:9 etc...)
10. Create a deck of cards class.( Internally, the deck of cards should use another class, a card
class. Make methods to:
iii)shuffle()-method which makes sure the deck of cards has all 52 cards and then rearranges
them randomly.
Note:The Card class should have a suit (Hearts, Diamonds, Clubs, Spades) and a value
(A,2,3,4,5,6,7,8,9,10,J,Q,K)
Difficult Questions
11. Create a Pizza class with the attributes order_number and ingredients . Only the ingredients
will be given as input.Make to choose a ready made pizza flavour rather than typing out the
ingredients manually.
Name Ingredients
hawaiian ham, pineapple
meat_festival beef, meatball, bacon
garden_feast spinach, olives, mushroom
13.Write a function, repack_boxes, which takes any number of boxes as parameters, gathers up all
the items they contain, and redistributes them as evenly as possible over all the boxes. Order is
unimportant. Test your code with a ListBox with 25 items, a ListBox with 10 items and a DictBox
with 6 items. You should end up with two boxes with 10 items each, and one box with 11 items.
15.Create a class called Numbers, which has a single class attribute called MULTIPLIER, and a
constructor which takes the parameters x and y (these should all be numbers).
a) Write a method called add which returns the sum of the attributes x and y.
b) Write a class method called multiply, which takes a single number parameter a and
returns the product of a and MULTIPLIER.
c) Write a static method called subtract, which takes two number parameters, b and c,
and returns b - c.
d) Write a method called value which returns a tuple containing the values of x and y.
Make this method into a property, and write a setter and a deleter for manipulating
the values of x and y.
16. Briefly describe a possible collection of classes which can be used to represent a music
collection (for example, inside a music player), focusing on how they would be related by
composition. You should include classes for songs, artists, albums and playlists. Hint: write down
the four class names, draw a line between each pair of classes which you think should have a
relationship, and decide what kind of relationship would be the most appropriate.
For simplicity you can assume that any song or album has a single “artist” value (which could
represent more than one person), but you should include compilation albums (which contain
songs by a selection of different artists). The “artist” of a compilation album can be a special value
like “Various Artists”. You can also assume that each song is associated with a single album, but
that multiple copies of the same song (which are included in different albums) can exist.
Write a simple implementation of this model which clearly shows how the different classes are
composed. Write some example code to show how you would use your classes to create an
album and add all its songs to a playlist. Hint: if two objects are related to each other
bidirectionally, you will have to decide how this link should be formed – one of the objects will
have to be created before the other, so you can’t link them to each other in both directions
simultaneously!