Advertisement
ALEXANDAR_GEORGIEV

oop_3

Jun 23rd, 2023
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. # Circle
  2. class Circle:
  3.     pi = 3.14
  4.  
  5.     def __init__(self, radius):
  6.         self.radius = radius
  7.  
  8.     def set_radius(self, new_radius):
  9.         self.radius = new_radius
  10.  
  11.     def get_area(self):
  12.         return Circle.pi * self.radius ** 2
  13.  
  14.     def get_circumference(self):
  15.         return Circle.pi * self.radius * 2
  16.  
  17. # Glass
  18. class Glass:
  19.     capacity = 250
  20.  
  21.     def __init__(self):
  22.         self.content = 0
  23.  
  24.     def fill(self, ml):
  25.         if self.content + ml > Glass.capacity:
  26.  
  27.             return f"Cannot add {ml} ml"
  28.  
  29.         self.content += ml
  30.  
  31.         return f"Glass filled with {ml} ml"
  32.  
  33.     def empty(self):
  34.         self.content = 0
  35.  
  36.         return "Glass is now empty"
  37.  
  38.     def info(self):
  39.         return f"{Glass.capacity - self.content} ml left"
  40.  
  41. # Point
  42. class Point:
  43.     def __init__(self, x, y):
  44.         self.x = x
  45.         self.y = y
  46.  
  47.  
  48.     def set_x(self, new_x):
  49.         self.x = new_x
  50.  
  51.     def set_y(self, new_y):
  52.         self.y = new_y
  53.  
  54.     def __str__(self):
  55.         return f"The point has coordinates ({self.x},{self.y})"
  56.  
  57.  
  58. p = Point(3, 4)
  59. print(p)
  60.  
  61. # Smartphone
  62. from typing import List
  63.  
  64.  
  65. class Smartphone:
  66.  
  67.     def __init__(self, memory):
  68.         self.memory = memory
  69.         self.apps: List[str] = []
  70.         self.is_on: bool = False
  71.  
  72.     def power(self) -> None:
  73.         self.is_on = not self.is_on
  74.  
  75.     def install(self, app, app_memory):
  76.         if self.is_on:
  77.             return f"Turn on your phone to install {app}"
  78.  
  79.         if self.memory > app_memory:
  80.             self.memory -= app_memory
  81.             self.apps.append(app)
  82.  
  83.             return f"Installing {app}"
  84.  
  85.         return f"Not enough memory to install {app}"
  86.  
  87.     def status(self):
  88.         return f"Total apps: {len(self.apps)}. Memory left: {self.memory}"
  89.  
  90. # Vehicle
  91. class Vehicle:
  92.     def __init__(self, mileage, max_speed: int=150):
  93.         self.max_speed = max_speed
  94.         self.mileage = mileage
  95.         self.gadgets = []
  96.  
  97. car = Vehicle(20)
  98.  
  99. car.gadgets.append("something")
  100.  
  101. print(car.gadgets)
  102.  
  103.  
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement