Table of Contents

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:

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

if __name__ == "__main__":
    fridge = Fridge()
    fridge.add("tomatoes", 10, "pieces")
    fridge.add("onions", 3, "pieces")
    fridge.add("eggs", 12, "pieces")
    fridge.add("butter", 500, "grams")
    fridge.add("oil", 1, "liter")
 
    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

Adding 8 pieces of onions.

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'))
Card: ace of spades

>>> 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: 10 of spades, 
Card: king of hearts, 
Card: ace of clubs, 
Card: 3 of spades, 
Card: 3 of diamonds]

[Card: 3 of diamonds, 
Card: 3 of spades, 
Card: 10 of spades,
Card: king of hearts, 
Card: ace of clubs, 
Card: ace of diamonds]