Advertisement
GeorgiLukanov87

Classes and Objects - Lab

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