Warning

# Computer Lab 12, objects and classes III

## Rectangle 2

Implement a class named Rectangle to store the coordinates of a rectangle given by its top-left point P1(x1, y1) and bottom-right point P2(x2, y2).

1. Use the Point class from a previous PC lab to represent 2D points.
2. Implement the class constructor with 2 parameters: p1, p2 (both type Point). Store them as object variables.
3. Implement width() and height() methods which return, respectively, the width and height of a rectangle.
4. Implement the method area to return an area of a rectangle.
5. Implement the method circumference to return the perimeter of the rectangle.
6. Implement the __str__ method such that when you print one of the objects its coordinates (x1,y1), (x2, y2) are printed.

## Fridge

First, implement a class Product representing products to store in a fridge:

• Constructor should receive 3 parameters: name - product name, qty - quantity, unit (piece, kilogram, liter, etc.)
• Method inc(self, by) increases the quantity of the product by the by value.
• Method use(self, qty) decreases the quantity of the product by the qty value. You have to take care of the case when the required quantity is greater than the available quantity of the product.
• Method __str__(self).

Then, implement a class Fridge to represent a collection of products (food) in a fridge.

• Food stored in the fridge is represented as a list of Product objects.
• Method add(self, product_name, qty, unit) appends a new item to the list of products.
• Method inc(self, product_name, by) increases an amount of food already present in the fridge by the by value.
• Method use(self, product_name, qty) decreases an amount of food present in the fridge by the qty value. In case you use the whole amount of the product, delete its corresponding Product object from the list.
• Method __str__(self) lists all items/products (incl. quantity and unit) stored in the fridge.

if __name__ == "__main__":
fridge = Fridge()

print(fridge)

fridge.inc("onions", 8)

print(fridge)

fridge.use("butter", 200)

print(fridge)

fridge.use("butter", 500)

print(fridge)

Fridge:
...10 pieces of tomatoes
...3 pieces of onions
...12 pieces of eggs
...500 grams of butter
...1 liter of oil

Fridge:
...10 pieces of tomatoes
...11 pieces of onions
...12 pieces of eggs
...500 grams of butter
...1 liter of oil

Using 200 grams of butter.

Fridge:
...10 pieces of tomatoes
...11 pieces of onions
...12 pieces of eggs
...300 grams of butter
...1 liter of oil

Using 300 grams of butter.

Fridge:
...10 pieces of tomatoes
...11 pieces of onions
...12 pieces of eggs
...1 liter of oil

#### Weekly Homework 12 - Card sorting

Create a class representing playing cards (standard 52-card deck). A card (object) is described with 2 pieces of information:

• Rank - (in ascending order): 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace.
• Suit - (in ascending order): clubs, diamonds, hearts, spades

Notes:

• Ranks received by the constructor should be either numbers (ints) (2, 3, 4, …, 10) or single-character strings (J, Q, K, A)
• Suits received by the constructor should be single-character strings (C, D, H, S)
• __str__ should output full names of ranks and suits (not acronyms). Examples:

>>> print(Card('A', 'S'))

>>> print(Card(2, 'D'))
Card: 2 of diamonds

>>> print(Card('K', 'H'))
Card: king of hearts

>>> print(Card(9, 'C'))
Card: 9 of clubs

• Importance (sorting) of the cards: Cards are always compared by rank first, and only then by suit. We will use the “reverse alphabetical order” ranking, the ace of clubs ranks higher than any king, but lower than the ace of diamonds.
• Rank importance: 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < jack < queen < king < ace
• Suit importance (reverse alphabetical order): clubs < diamonds < hearts < spades
• __repr__ is need for printing a list of objects. Re-use the already implemented __str__ method.
• Both __eq__ and __lt__ are needed for the .sort() method to work.

Required filename: 12_weekly_hw.py.

class Card:

def __init__(self, rank, suit):

def __str__(self):

def __repr__(self):
return str(self)

def __eq__(self, other):

def __lt__(self, other):

if __name__ == "__main__":
cards = []
cards.append(Card('A', 'D'))
cards.append(Card(10, 'S'))
cards.append(Card('K', 'H'))
cards.append(Card('A', 'C'))
cards.append(Card(3, 'S'))
cards.append(Card(3, 'D'))

print(cards)

cards.sort()

print(cards)

[Card: ace of diamonds,
Card: ace of diamonds]