import numpy as np
class Circle:
# Constructor: a special type of subroutine called to create an object
def __init__(self, radius):
# class attributes (or variables of the class)
self.radius = radius
self.area = 0
self.perimeter = 0
# Methods of the class
def compute_area(self):
self.area = np.pi * self.radius ** 2
def compute_perimeter(self):
self.perimeter = 2 * np.pi * self.radius
def get_info(self):
print()
print("Radius of the circle = {}".format(self.radius))
print("Area of the circle = {}".format(round(self.area, 3)))
print("Perimeter of the circle = {}".format(round(self.perimeter, 3)))
# First instance of the class Circle
circle1 = Circle(3)
circle1.compute_area()
circle1.compute_perimeter()
circle1.get_info()
# Second instance of the class Circle
circle2 = Circle(5)
circle2.compute_area()
circle2.compute_perimeter()
circle2.get_info()
Radius of the circle = 3
Area of the circle = 28.274
Perimeter of the circle = 18.85
Radius of the circle = 5
Area of the circle = 78.54
Perimeter of the circle = 31.416
# code to get the class of the objects
print (type(circle1))
print(type (circle2))
x = 5
print(type(x))
<class '__main__.Circle'>
<class '__main__.Circle'>
<class 'int'>
# Class
class Person:
# Constructor: a special type of subroutine called to create an object
def __init__(self, name_par, age_par, citizenship_par):
# class attributes (or variables of the class)
self.name = name_par
self.age = age_par
self.citizenship = {citizenship_par}
# Methods of the class
def update_age(self, new_age):
self.age = new_age
def add_citizenship(self, new_citizenship):
self.citizenship.add(new_citizenship)
def remove_citizenship(self, citizenship_to_remove):
if citizenship_to_remove in self.citizenship:
self.citizenship.remove(citizenship_to_remove)
def change_name(self, new_name):
self.name = new_name
def get_info(self):
print("Name of the person = {}".format(self.name))
print("Age of the person = {}".format(self.age))
print("Citizenships of the person = {}".format(self.citizenship))
# object kevin of the class Person
print("State 0")
kevin = Person("Kevin", 25, "indonesian")
kevin.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 1")
kevin.update_age(27)
kevin.add_citizenship("chinese")
kevin.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 2")
kevin.update_age(29)
kevin.add_citizenship("german")
kevin.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 3")
kevin.remove_citizenship("german")
kevin.get_info()
State 0
Name of the person = Kevin
Age of the person = 25
Citizenships of the person = {'indonesian'}
-----------------------------------------------------------------
State 1
Name of the person = Kevin
Age of the person = 27
Citizenships of the person = {'chinese', 'indonesian'}
-----------------------------------------------------------------
State 2
Name of the person = Kevin
Age of the person = 29
Citizenships of the person = {'german', 'chinese', 'indonesian'}
-----------------------------------------------------------------
State 3
Name of the person = Kevin
Age of the person = 29
Citizenships of the person = {'chinese', 'indonesian'}
# object antonio of the class Person
print("State 0")
antonio = Person("Antonio", 23, "mexican")
antonio.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 1")
antonio.update_age(25)
antonio.add_citizenship("french")
antonio.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 2")
antonio.update_age(27)
antonio.add_citizenship("german")
antonio.get_info()
print("-----------------------------------------------------------------")
# modify some info
print("State 3")
antonio.remove_citizenship("french")
antonio.get_info()
State 0
Name of the person = Antonio
Age of the person = 23
Citizenships of the person = {'mexican'}
-----------------------------------------------------------------
State 1
Name of the person = Antonio
Age of the person = 25
Citizenships of the person = {'french', 'mexican'}
-----------------------------------------------------------------
State 2
Name of the person = Antonio
Age of the person = 27
Citizenships of the person = {'french', 'mexican', 'german'}
-----------------------------------------------------------------
State 3
Name of the person = Antonio
Age of the person = 27
Citizenships of the person = {'mexican', 'german'}
# define the class Student with the following attributes and methods
# Attributes:
# - Person
# - Major (name)
# - School (name)
# Methods:
# - a method to change the major of the student
# - a method to change the school of the student
# - a method to get the info about the object
# Test the class with two different instances (objects)
class Student:
def __init__(self, person, major, school):
self.person = person # from class Person
self.major = major # a string
self.school = school # a string
def change_major(self, new_major):
self.major = new_major
def change_school(self, new_school):
self.school = new_school
def get_info(self):
print("Infor about {}".format(self.person.name))
self.person.get_info()
print("Major: ", self.major)
print("School: ", self.school)
kevin_student = Student(kevin, "Physics", "Heidelberg University")
kevin_student.get_info()
Infor about Kevin
Name of the person = Kevin
Age of the person = 29
Citizenships of the person = {'chinese', 'indonesian'}
Major: Physics
School: Heidelberg University