Class and Object | Python
P1 / CLASS WITH PUBLIC ATTRIBUTES
Create a class named Person with the following 2 public attributes.
name
age
Include a constructor .
__init__(self,name, age)
Create an object of class Person to test the above class.
Input and Output Format:
Refer Sample Input and Output for formatting specifications.
Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]
Enter name
Mahirl
Enter age
10
Person Details
Mahirl
10
Additional Sample TestCases
Sample Input and Output 1 :Enter name Mahirl Enter age 10 Person Details Mahirl 10
P2 / CLASS WITH PRIVATEATTRIBUTES
Create a class named Person with the following 2 private attributes.
__name
__age
Include a constructor .
___init__(self,name, age)
Include a method
display()
In this method, display the person details in the format specified in the sample output.
Create an object of class Person and invoke the display method to test the above class.
Note : The output statement "Person Details" is in the Main.py file.
Input and Output Format:
Refer Sample Input and Output for formatting specifications.
Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]
Enter name
Mahirl
Enter age
10
Person Details
Mahirl
10
Additional Sample TestCases
Sample Input and Output 1 :Enter name Mahirl Enter age 10 Person Details Mahirl 10
P3 / __STR__ METHOD
Create a class named Person with the following 2 private attributes.
__name
__age
Include a constructor .
___init__(self,name, age)
Include a method
__str__(self)
This method returns a string corresponding to person details in the format specified in the sample output.
Create an object of class Person to test the above class.
Note:
The __str__ method is useful for a string representation of the object, either when someone codes in str(your_object), or even when someone might do print(your_object).
Input and Output Format:
Refer Sample Input and Output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
Sample Input and Output:
Enter name
Mahirl
Enter age
10
Person Details
Mahirl is 10 years old
P4 / MODIFIED PERSON CLASS
Create a class named Person with the following 4 private attributes.
__first_name
__last_name
__age
__email
Include a constructor .
___init__(self,first_name, last_name,age)
email is formed by concatenating the first name, last name and gmail.com in the format
specified in the output.
Include a method
__str__(self)
This method returns a string corresponding to person details in the format specified in the
sample output.
fullname(self)
This method returns the full name of the person as a concatenation of first name and last name
separated by a space.
Create an object of class Person to test the above class.
Input and Output Format:
Refer Sample Input and Output for formatting specifications.
Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]
Enter first name
Mahirl
Enter last name
Malar
Enter age
10
Full name of the person is Mahirl Malar
Person Details
Mahirl Malar is 10 years old and her email id is Mahirl.Malar@gmail.com
P5 / CLASS VARIABLE
Create a class named Person with the following 4 private attributes.
__first_name
__last_name
__age
__email
Include a class variable named domain_name in the Person class.
This domain_name is used for generating the email id.
Include a constructor .
___init__(self,first_name, last_name,age)
email is formed by concatenating the first name, last name and with given domain name in the format
specified in the output.
Include a method
__str__(self)
This method returns a string corresponding to person details in the format specified in the
sample output.
fullname(self)
This method returns the full name of the person as a concatenation of first name and last name
separated by a space.
Create an object of class Person to test the above class.
Note:
Class or static variables are shared by all objects. Instance or non-static variables are different for different objects (every object has a copy of it).
In C++ and Java, we can use static keyword to make a variable as class variable. The variables which don’t have preceding static keyword are instance variables.
The Python approach is simple, it doesn’t require a static keyword. All variables which are assigned a value in class declaration are class variables. And variables which are assigned values inside class methods are instance variables.
Input and Output Format:
Refer Sample Input and Output for formatting specifications.
Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]
Enter domain name
amphi@in
Enter details of first person
Enter first name
Mahirl
Enter last name
Malar
Enter age
10
Enter details of second person
Enter first name
Madhu
Enter last name
Kavi
Enter age
15
First Person Details
Full name of the person is Mahirl Malar
Mahirl Malar is 10 years old and her email id is Mahirl.Malar@amphi@in
Second Person Details
Full name of the person is Madhu Kavi
Madhu Kavi is 15 years old and her email id is Madhu.Kavi@amphi@in
P6 / LIST OF OBJECTS
Create a class named Person with the following 4 private attributes.
__first_name
__last_name
__age
__email
Incude a class variable named domain_name in the Person class.
This domain_name is used for generating the email id.
Incude a class variable named counter in the Person class.
counter is used for keeping track of the number of objects created.
Include a constructor .
__init__(self,first_name, last_name,age)
email is formed by concatenating the first name, last name and with domain name in the format
specified in the output.
Class variable counter is incremented by 1.
Include a method
__str__(self)
This method returns a string corresponding to person details in the format specified in the
sample output.
fullname(self)
This method returns the full name of the person as a concatenation of first name and last name
separated by a space.
Use property and setter decorator for the attribute age.
Create multiple objects of the person class and test the above class.
Note:
The following code uses the built-in @property decorator to define the x property in the C class.
Example:
class C :
def __init__(self):
self.__x = None
@property
def x(self):
return self.__x
@x.setter
def x(self, value):
self.__x = value
The x(self) function is marked with the @property decorator which indicates that the x(self) method is a getter method and the name of the property is the method name only, in this case x. Now, x(self, value) is assigning a value to the private attribute __x. So, to mark this method as a setter method for the x property, the @x.setter decorator is applied. This is how we can define a property and its getter and setter methods.
Input and Output Format:
Refer Sample Input and Output for formatting specifications.
Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]
Enter domain name
amphi.in
Enter details of person
Enter first name
Mahirl
Enter last name
Malar
Enter age
10
Do you want to add the details of another person? Type yes/no
yes
Enter details of person
Enter first name
Banu
Enter last name
Kumar
Enter age
25
Do you want to add the details of another person? Type yes/no
yes
Enter details of person
Enter first name
Chandra
Enter last name
Kala
Enter age
50
Do you want to add the details of another person? Type yes/no
no
Number of person objects created
3
List Person Details
Full name of the person is Mahirl Malar
Mahirl Malar is 10 years old and her email id is Mahirl.Malar@amphi.in
Full name of the person is Banu Kumar
Banu Kumar is 25 years old and her email id is Banu.Kumar@amphi.in
Full name of the person is Chandra Kala
Chandra Kala is 50 years old and her email id is Chandra.Kala@amphi.in
Update Person Details
Enter the full name of the person whose details are to be updated
Mahirl Malar
Old age : 10
Enter age
11
Updated Person Details :
Mahirl Malar is 11 years old and her email id is Mahirl.Malar@amphi.in
P7 / CLASS METHOD
Create a class named Person with the following 2 private attributes.
__name
__age
Include a constructor .
___init__(self,name, age)
Include a method
__str__(self)
This method returns a string corresponding to person details in the format specified in the sample output.
Include a class method
from_string(cls, person_str)
This method creates and returns an object of Person class from a string. This method accepts a class variable and a string. String contains the values for name and age in a comma separated format.
Create objects of class Person to test the above class.
Note:
A class method is one that belongs to the class as a whole. It doesn't require an instance. Instead, the class will automatically be sent as the first argument. A class method is declared with the @classmethod decorator.
Input and Output Format:
Refer Sample Input and Output for formatting specifications.
Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]
Creating object using __init__ method
Enter name
Mahirl
Enter age
10
Person Details
Mahirl is 10 years old
Creating object using class method
Enter name and age in comma separated format
Chandra,40
Person Details
Chandra is 40 years old
P8 / STATIC METHOD
Create a class named Person with the following 3 private attributes.
__name
__age
__id
Include a class variable named counter and initialize its value to 0.
Include a constructor .
___init__(self,name, age)
Generate the value for id attribute from the counter. The id should be unique for every person and it should be sequential.
Include a method
__str__(self)
This method returns a string corresponding to person details in the format specified in the sample output.
Include a class method
from_string(cls, person_str)
This method creates and returns an object of Person class from a string. This method accepts a class variable and a string. String contains the values for name and age in a comma separated format.
Include a static method
initialize_counter(value)
This method is used to set the value of the class variable counter.
Create objects of class Person to test the above class.
Note:
Static Methods:
Simple functions with no self argument.
Work on class attributes; not on instance attributes.
Can be called through both class and instance.
The decorator @staticmethod is used to create them.
Input and Output Format:
Refer Sample Input and Output for formatting specifications.
Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]
Enter starting value of counter
100
Creating object using __init__ method
Enter name
Mahirl
Enter age
10
Person Details
Mahirl is 10 years old and her id is 101
Creating object using class method
Enter name and age in comma separated format
Devi,40
Person Details
Devi is 40 years old and her id is 102
P9 / CHECK FOR OBJECT EQUALITY
Create a class named Person with the following 2 private attributes.
__name
__age
Include a constructor .
___init__(self,name, age)
Include a method
__str__(self)
This method returns a string corresponding to person details in the format specified in the sample output.
Include a comparison method
def __eq__(self,other)
This method should returns true if both the objects have the same attribute values.
Create objects of class Person to test the above class.
Note :
Perform case-sensitive comparison
Input and Output Format:
Refer Sample Input and Output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
Sample Input and Output 1:
Person1
Enter name
Mahirl
Enter age
10
Person Details
Mahirl is 10 years old
Person2
Enter name
Mahirl
Enter age
15
Person Details
Mahirl is 15 years old
Check For Equality
Person1 and Person2 are not equal
Sample Input and Output 2:
Person1
Enter name
Mahirl
Enter age
10
Person Details
Mahirl is 10 years old
Person2
Enter name
Mahirl
Enter age
10
Person Details
Mahirl is 10 years old
Check For Equality
Person1 and Person2 are equal
P10 / ONE TO ONE RELATIONSHIP – 1
P10 / One to One Relationship – 1
Create a class named Address with the following private_attributes
__street
__city
__state
Include a constructor
__init__(self, street, city, state)
Include a method
__str__(self)
This method returns a string corresponding to Address details in the format specified
in the sample output.
Create a class named Person with the following private_attributes
__name
__age
__address (of type Address)
Include a constructor
__init__(self, name, age, address)
Include a method
__str__(self)
This method returns a string corresponding to Person details in the format specified
in the sample output.
Create objects of the above classes and test them.
Input and Output Format:
Refer Sample Input and Output for formatting specifications.
Sample Input and Output 1:
[All text in bold corresponds to input and the rest corresponds to output]
Enter name
Mahirl
Enter age
10
Enter address
Enter street
LMC Street
Enter city
Salem
Enter state
Tamilnadu
Person Details
Mahirl is a person who is 10 years old and lives in the following address : LMC Street , Salem , Tamilnadu
P12 / ONE TO MANY RELATIONSHIP
Create a class named City with the following private_attributes
__name
__state (of type State)
Include a constructor
__init__(self, name, state)
Include a method
__str__(self)
This method returns a string corresponding to City details in the format specified
in the sample output.
Create a class named State with the following private_attributes
__name
__city_list (of type City)
Include a constructor
__init__(self, name, city_list)
Include a method
__str__(self)
This method returns a string corresponding to State details in the format specified in the sample output.
Include @property decorator for name
Include @property decorator for city_list
Include @setter decorator for city_list
Create objects of the above classes and test them.
Note :
Perform case-sensitive string comparison.
Initially state_list have the below states.
Tamilnadu
Andhra
Karnataka
Kerala
If any new state is added other than the state in the list, it must be added to the state_list. [Refer sample input and output]
Input and Output Format:
Refer Sample Input and Output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
Sample Input and Output 1:
Enter City Details
Enter city name
Coimbatore
Enter state name
Tamilnadu
Do you want to add another city? Type Yes / No
Yes
Enter city name
Chennai
Enter state name
Tamilnadu
Do you want to add another city? Type Yes / No
Yes
Enter city name
Bangalore
Enter state name
Karnataka
Do you want to add another city? Type Yes / No
Yes
Enter city name
Vijayawada
Enter state name
Andhra
Do you want to add another city? Type Yes / No
Yes
Enter city name
Cochin
Enter state name
Kerala
Do you want to add another city? Type Yes / No
Yes
Enter city name
Bhopal
Enter state name
Madhya Pradesh
Do you want to add another city? Type Yes / No
No
City Details
Coimbatore is in state Tamilnadu
Chennai is in state Tamilnadu
Bangalore is in state Karnataka
Vijayawada is in state Andhra
Cochin is in state Kerala
Bhopal is in state Madhya Pradesh
State Details
Tamilnadu has 2 cities
Andhra has 1 cities
Karnataka has 1 cities
Kerala has 1 cities
Madhya Pradesh has 1 cities
Comments
Post a Comment