Object Oriented Programming Projects in Python
OOP based projects covering Encapsulation, Inheritance, Polymorphism and Abstraction
Phase 1: Build a simple calculator class containing add, subtract, multiply, divide.
Phase 2: Expand by creating:
Divisible by method that returns true or false dependent on the outcome
Work out and return the area of a triangle
inch to cm converter
NOTE -> Must be in class and method format
A string is simply an ordered collection of symbols selected from some alphabet and formed into a word; the length of a string is the number of symbols that it contains.
An example of a length 21 DNA string (whose alphabet contains the symbols 'A', 'C', 'G', and 'T') is "ATGCTTCAGAAAGGTCTTACG."
Given: A DNA string s of length at most 1000 nt.
Return: Four integers (separated by spaces) counting the respective number of times that the symbols 'A', 'C', 'G', and 'T' occur in s.
Sample Dataset:
AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC
Sample Output:
20 12 17 21
NOTE -> Must be in class and method format
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”."
NOTE -> Must be in class and method format
Base Scrabble word calculator instructions
Given the below scoring create a Scrabble word calculator that will provide the correct scores dependent on the string provided.
Letter Value
A, E, I, O, U, L, N, R, S, T 1
D, G 2
B, C, M, P 3
F, H, V, W, Y 4
K 5
J, X 8
Q, Z 10
Project criteria:
Student_data_inheritance
Student_data_encapsulation
Student_data_polymorphism
Student_data_abstraction
Using Inheritance in Python
# parent class
class Animal:
def __init__(self, eat, walk, hungry=True, mood): # Setting default value of hunger to True
self.eat = eat
self.walk = walk
self.hungry = hungry
self.mood = mood
# child class
class Bird(Animal):
# using super(), (temporary object of the superclass) allows us to access methods of the base class (parent class)
def __init__(self):
super().__init__()
print("I am a bird")
def tweet(self):
print("tweet tweet")
def fly(self):
print("I am a free bird")
pippa = Bird()
pippa.tweet()
In the above program, we created two classes i.e. Animal
(parent class) and Penguin
(child class). The child class inherits the functions of parent class.
The child class modified the behaviour of the parent class. We also extended the functions of the parent class, by creating new methods like tweet()
and fly()
.
We used the super()
function inside the __init__()
method. This allows us to run the __init__()
method of the parent class inside the child class.
_
or dunder (double underscore) __
as the prefix.
class Robot(object):
def __init__(self):
self.a = 123
self.b = 123
self.__c = 123
obj = Robot()
print(obj.a)
print(obj._b)
print(obj.__c)
Outcome:
123
123
Traceback (most recent call last):
File "test.py", line 10, in <module>
print(obj.__c)
AttributeError: 'Robot' object has no attribute '__c'
_
Single underscore denotes private variable. It should not be accessed directly.
__
Dunder or double underscore also denotes private variable.
## Example of class polymorphism and inheritance
from math import pi
class Shape:
def __init__(self, name):
self.name = name
def area(self):
pass
def fact(self):
return "I am a two-dimensional shape."
def __str__(self):
return self.name
class Square(Shape):
def __init__(self, length):
super().__init__("Square")
self.length = length
def area(self):
return self.length**2
def fact(self):
return "Squares have each angle equal to 90 degrees."
class Circle(Shape):
def __init__(self, radius):
super().init__("Circle")
self.radius = radius
def area(self):
return pi*self.radius**2
a = Square(4)
b = Circle(7)
print(b)
print(b.fact())
print(a.fact())
print(b.area())
Output
Circle
I am a two-dimenional shape.
Squares have each angle equal to 90 degrees.
153.93804002589985
Methods such as __str__()
, which have not been overridden in the child classes, are used from the parent class.
The fact()
method for object a(Square class)
is overridden. However, fact()
method for object b
has not been overridden. It is inheriting from the Parent Shape
class.