You are on page 1of 17

Case Study II for Object Oriented Programming using

Python – For Practice

Author(s) Roy Antony Arnold G

Authorized by Satheesha B N

Creation/Revision Date 1.1

Version June 2017


COPYRIGHT NOTICE
© 2017 Infosys Limited, Bangalore, India. All Rights Reserved.
Infosys believes the information in this document is accurate as of its publication date; such
information is subject to change without notice. Infosys acknowledges the proprietary rights of
other companies to the trademarks, product names and such other intellectual property rights
mentioned in this document. Except as expressly permitted, neither this documentation nor any
part of it may be reproduced, stored in a retrieval system, or transmitted in any form or by any
means, electronic, mechanical, printing, photocopying, recording or otherwise, without the prior
permission of Infosys Limited and/ or any named intellectual property rights holders under this
document.

Education, Training and Assessment Department


Infosys Limited
Electronics City
Hosur Road
Bangalore – 561 229, India.

Tel: 91 80 852 0261-270


Fax: 91 80 852 0362
www.infosys.com
mailto:ETA@infosys.com
Document Revision History

Version Date Author(s) Reviewer(s) Description

1.0 Nov 2015 Roy Antony Arnold G Vani K N Mapped for Campus Connect

1.1 Dec 2015 Roy Antony Arnold G Vani K N Mapped for Campus Connect

Dr. Sundaresan Campus Connect FP 4.1


1.2 June 2017 Vani K N
Krishnan Iyer release

CONFIDENTIAL
CONTENTS

COPYRIGHT NOTICE .............................................................................................................................................................. i


Document Revision History....................................................................................................................................................... ii
CONTENTS ............................................................................................................................................................................. iii
Case Study II for Object Oriented Programming using Python ................................................................................................. 1
Case Study II : Course Registration System .......................................................................................................................... 1
Practice Assignment 1: Identify OO Concepts ...................................................................................................................... 1
Practice Assignment 2: Create class and objects in Python ................................................................................................... 2
Practice Assignment 3: Debugging – self reference .............................................................................................................. 3
Practice Assignment 4: Using default parameters in Method ................................................................................................ 4
Practice Assignment 5: Using default parameters in __init__() Method ............................................................................... 4
Practice Assignment 6: Relationships – ‘has-a’ relationship ................................................................................................. 5
Practice Assignment 7: Relationships – ‘is-a’ relationship .................................................................................................... 7
Practice Assignment 8: Self Review in Python ..................................................................................................................... 9

CONFIDENTIAL
Infosys Limited Case Study for Object Oriented Programming using Python

Case Study II for Object Oriented Programming using Python


Case Study II : Course Registration System

Note: Read this case study fully and understand as this is going to be the business
scenario for all the hands-on assignments given in this document.

Situation: A Course Registration System needs to be developed for an engineering


college. The college wants an automated system to replace its manual system for the
purpose of registration of students to branches and calculation of fees for each year. The
engineering college provides graduation courses in various branches of engineering.
The system will be used by the admin staff to register students admitted to the college to
the branches at the time of joining the college and also to calculate the yearly fees for the
students. The student has to register every year for the next academic year. The Admin
takes care of the yearly registration of the students and the calculation of yearly fees. The
system needs to be authenticated with a login id and password.
Registration of a student to a branch is based on the qualifying exam marks and the
entrance counseling. For every branch, a yearly branch fee is applicable. Discounts are
given to the branch fees of the first year based on the qualifying exam marks. There is a
registration fees also applicable to the first year students. Students can opt to be a day
scholar or hostelite. Yearly bus fees are applicable for all the day scholars based on the
distance of travel. Yearly hostel fees are applicable for all the hostelites. Yearly
infrastructure fees and library fees are also applicable to all the students. Admin
calculates the yearly college fees for each student and the college fees include all the
fees specified earlier based on the type of student. Admin will provide a printed receipt of
the fees to the students once the annual college fees have been paid.
At the time of registration, student has to provide the permanent address and in case the
student is opting to be a day scholar, he/she has to provide the residential address also.

Assumption:
1. Decision of the branch of study a student is allocated, is not within the scope of
this case study

Practice Assignment 1: Identify OO Concepts

Objective: Read the above given business scenario to understand and identify OO
concepts

Problem Description:
Identify the OO concepts applicable for these scenarios:
1. More than 100 faculty members are working in the college to teach various
engineering subjects. Every faculty has some attributes and perform some
activities. How can these attributes and activities be represented using OO
concepts?

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 1 of 13


Infosys Limited Case Study for Object Oriented Programming using Python

2. Student can choose either to be a Day Scholar or a hosteller. If he is a Day


Scholar, Transportation fees is applicable according to the travel distance. If he is
staying hostel, hostel fee and mess fee are applicable. Identify the OO Concepts
used here and write the appropriate class names to implement this scenario.
3. Accountant collects the fees to be paid from all the students irrespective of type
of student. But, the formula used to calculate the fees depends on the student
type. Which OO concept is used here?
4. Certain attributes of the student need to be accessed by the accountant and
some attributes need not be. How can bring this feature into the system and what
is the OO concept used for that?

Estimated time: 15 minutes

Summary of this assignment: In this assignment, you have learnt object oriented
concepts using scenarios

Practice Assignment 2: Create class and objects in Python

Objective: Read the above given business scenario to create student class and to
create objects for that class

Problem Description:
Write a Python program for the class diagram given below:

Student
-studentid : int
-qualifyingexammarks : float
-residentialstatus : char
-yearofengg : int
+setstudentid(int) : void
+setqualifyingexammarks(float) : void
+setresidentialstatus(char) : void
+setyearofengg(int) : void
+getstudentid() : int
+getqualifyingexammarks() : float
+getresidentialstatus(): char
+getyearofengg() : int

Step 1: Create Student.py as per the class diagram


Step 2: Define all setter and getter methods
Step 3: Create a reference for student class with the name of objstu
Step 4: Invoke the corresponding setter methods using objstu and set following values
to object

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 2 of 13


Infosys Limited Case Study for Object Oriented Programming using Python

Step 5: Invoke the corresponding getter method to display the student details as given
below:
Student Id :
Qualifying Marks :
Residential Status :
Current Year of Engineering :

Estimated time: 20 minutes

Summary of this assignment: In this assignment, you have learnt


 How to create a class, instantiate object
 Invoking methods using object
 Execute a Python program

Practice Assignment 3: Debugging – self reference

Objective: Understand the importance of self reference to access the object members.

Problem Description:
Debug the below given Python code to give the output, shown after the code.

class Registration:
def setregistrationid (self, rid):
__registrationid = rid

def getregistrationid(self):
return __registrationid

objreg = Registration()
objreg.setregistrationid(1001)
print("Registration Id : ", objreg.getregistrationid())

Output Expected:
Registration Id : 1001

Estimated time: 10 minutes

Summary of this assignment: In this assignment, you have learnt


 Importance of self
 Usage of self for accessing class members

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 3 of 13


Infosys Limited Case Study for Object Oriented Programming using Python

Practice Assignment 4: Using default parameters in Method


Objective: For one of the above given business scenario to use default parameters in
__init__() method

Problem Description:
To initialize the members of the class during object instantiation __init__() method is
needed. By using default parameters in __init__() method we can initialize the members
as per the requirement. Write a Python program for the class diagram given below:
Student

+displayheader(char c) : void
+displayheader(char c, int n) : void
+displayheader(string s) : void

Default parameters need to be assigned properly in such a way to get following output
on the screen:

Annual Report – FY16


====================

****************************************************************
Rict Engineering College
****************************************************************

Estimated time: 20 minutes

Summary of this assignment: In this assignment, you have learnt


 Creating the method with default arguments
 Invoking method which is having default arguments

Practice Assignment 5: Using default parameters in __init__()


Method
Objective: For one of above given business scenario to use default parameters in
method

Problem Description:
Chief accountant wants to take printout of some report. For which, need to print some
headers depends on the need. Should have ability of printing a string, or a character n
no. of times. Write a Python program for the class diagram given below:

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 4 of 13


Infosys Limited Case Study for Object Oriented Programming using Python

Student
-studentid : int
-studentname : string
-qualifyingexammarks : float
-residentialstatus : char
-yearofengg : int
-branchname : string
+Student(int, string, float, char, int, string)
……

Test Case 1:
Display the default values of the __init__ method as given below
Student Id :
Student Name :
Qualifying Exam Marks :
Residential Status :
Current Year of Engineering :
Branch Name :

Test Case 2:
Pass following values to the parameters:
studentid 15001
studentname Jack
qualifyingexammarks 125.5
residentialstatus H
yearofengg 1
branchname Civil

Estimated time: 20 minutes

Summary of this assignment: In this assignment, you have learnt


 Creating the __init__() method with default arguments
 Invoking __init__() method which is having default arguments

Practice Assignment 6: Relationships – ‘has-a’ relationship


Objective: To understand the ‘has-a’ relationship using above given business scenario.

Problem Description:
Some students and faculty members are expected to have two addresses namely
permanent address, and communication address. Write a python program to bring the
relationship between student and address class and also between faculty and address
class.

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 5 of 13


Infosys Limited Case Study for Object Oriented Programming using Python

Address
-street : string
-area : string
-city : string
-zip : string
-state : string
+Address(string, string, string, string, string)
+setstreet(string) : void
+setarea(string) : void
+setcity(string) : void
+setzip(string) : void
+setstate(string) : void
+getstreet() : string
+getarea() : string
+getcity() : string
+getzip() : string
+getstate() : string

Student Faculty
-studentid : int -facultyid : int
-studentname : string -facultyname : string
-qualifyingexammarks : float -branchname : string
-residentialstatus : char -designation : string
-yearofengg : int -permanentaddress : Address
-branchname : string -commaddress : Address
-permanentaddress : Address ……
-commaddress : Address +setpermanentaddress(Address) : void
…… +setcommaddress(Address) : void
+setpermanentaddress(Address) : void +getpermanentaddress() : Address
+setcommaddress(Address) : void +getcommaddress() : Address
+getpermanentaddress() : Address
+getcommaddress() : Address

Estimated time: 30 minutes

Summary of this assignment: In this assignment, you have learnt


 Aggregation relationship (has-a relationship)

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 6 of 13


Infosys Limited Case Study for Object Oriented Programming using Python

Practice Assignment 7: Relationships – ‘is-a’ relationship


Objective: To understand the ‘is-a’ relationship using above given business scenario.

Problem Description:
Students are categorized as day scholar and hosteller. Hence, write a python program
to implement day scholar and hosteller class which are inherit from the student
class. The class diagram for this scenario is given below:
Student
-studentid : int Address
-studentname : string
-qualifyingexammarks : float -addressline : string
-residentialstatus : char -city : string
-yearofengg : int -zip : string
-branchname : string -state : string
-permanentaddress : Address +Address(string, string,
-counter : int ->static string, string)
+Student() +setaddressline(string) : void
+Student(int, string, float, char, int, string, Address) +setcity(string) : void
+setstudentid(int) : void +setzip(string) : void
+setstudentname(string) : void +setstate(string) : void
+setqualifyingexammarks(float) : void +getaddressline() : string
+setresidentialstatus(char) : void +getcity() : string
+setyearofengg(int) : void +getzip() : string
+setbranchname(string) : void +getstate() : string
+setpermanentaddress(Address) : void
+getstudentid() : int
+getstudentname() : string
+getqualifyingexammarks() : float
+getresidentialstatus() : char
+getyearofengg() : int
+getbranchname() : string
+getpermanentaddress() : Address
+getstudentcount() : int -> static
+validatestudentname() : boolean
+validatebranchname() : boolean
+validateexammarks() : boolean

Hosteller DayScholar
-hostelname : string
-roomnumber : int -residentialaddress : Address
-roomtype : string -distance : float
+Hosteller( int, string, float, char, int, string, Address, +DayScholar( int, string, float, char, int, string, Address,
string, int, string) Address, float)
+sethostelname(string) : void +setresidentialaddress(Address) : void
+setroomnumber(int) : void +setdistance(float) : void
+setroomtype(string) : void
+gethostelname() : string
+getroomnumber() : int
+getroomtype() : string

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 7 of 13


Infosys Limited Case Study for Object Oriented Programming using Python

Guidelines:
 Create a reference to the object created from the Hosteller class and initialize the
same with the following values through __init__() method:
Instance Variable Initial value
studentid 15001
studentname Jimmy
qualifyingexammarks 88.5
residentialstatus H
branchname CSE
yearofengg 1
addressline K-58, Xavier Enclave
city Chennai
state Tamilnadu
zip 600018
hostelname Boys Hostel – 1
roomnumber 102
roomtype shared

Note: create reference variables for Address class wherever an address is


handled

 Invoke the validatestudentname() method. If the name is valid, invoke


validatebranchname() and validateexammarks() method. If both are valid, invoke
the necessary getter methods to display the following:

Student Id :
Student Name :
Qualifying Marks :
Residential Status :
Current Year of Engineering :
Branch Name :
Permanent Address :
Hostel Name :
Room Number :
Room Type :

 Create a reference to access an object instantiated from DayScholar class and


initialize the members with following:
Instance Variable Initial value
studentid 15002
studentname Tommy
qualifyingexammarks 80.6
residentialstatus D
branchname CSE
yearofengg 1

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 8 of 13


Infosys Limited Case Study for Object Oriented Programming using Python

For Permanent Address


addressline A59, West Fort Street
city Kochin
state Kerala
zip 560015
For Residential Address
addressline No.38, King Circle
city Mysore
state Karnataka
zip 570017
distance 15

Note: create reference variables for Address class wherever an address is


handled

 Invoke the validatestudentname() method. If the name is valid, invoke


validatebranchname() and validateexammarks() method. If both are valid, invoke
the necessary getter methods to display the following:

Student Id :
Student Name :
Qualifying Marks :
Residential Status :
Current Year of Engineering :
Branch Name :
Permanent Address :
Residential Address :
Distance from College :

Estimated time: 60 minutes

Summary of this assignment: In this assignment, you have learnt


 Inheritance relationship (is-a relationship)

Practice Assignment 8: Debugging using Python


Objective: To do a self-review on python programming level

Problem Description:
What would be the output for the following programs? If there is an error in the program,
analyze it, fix it and execute it. Also write the inference drawn from each code snippet.

Code – 1:
class Parent:
def setnum(self, val):

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 9 of 13


Infosys Limited Case Study for Object Oriented Programming using Python

self.__num = val

def getnum(self):
return self.__num

def display(self):
print("Number : ", self.__num)

class Child(Parent):
def setval(self, num):
self.__val = num

def getval(self):
return self.__val

def display(self):
print("Value : ", self.__val)
print("Number : ", self.__num)

child = Child()
child.setnum(10)
child.setval(20)
child.display()

Code – 2:
class Parent:
def setnum(self, val):
self.__num = val

def getnum(self):
return self.__num

def display(self):
print("Number : ", self.__num)

class Child(Parent):
def setval(self, num):
self.__val = num

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 10 of 13


Infosys Limited Case Study for Object Oriented Programming using Python

def getval(self):
return self.__val

def display(self):
print("Value : ", self.__val)
super().display()

child = Child()
child.setnum(10)
child.setval(20)
child.display()

Code – 3:
class StaticDemo:
count = 10
def __init__(self):
StaticDemo.count = StaticDemo.count + 1

@staticmethod
def display():
print(count)

s1 = StaticDemo()
s2 = StaticDemo()
s1.display()

Code – 4:
class Animal:
@classmethod
def testclassmethod(cls):
print("Class Method in Animal class is invoked")

def testinstancemethod(self):
print("Instance Method in Animal class is invoked")

class Cat(Animal):
@classmethod
def testclassmethod(cls):
print("Class Method in Cat class is invoked")

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 11 of 13


Infosys Limited Case Study for Object Oriented Programming using Python

def testinstancemethod(self):
print("Instance Method in Cat class is invoked")

mycat = Cat()
myanimal = Animal()
myanimal = mycat
Animal.testclassmethod()
myanimal.testinstancemethod()

Code – 5:
class ClassA:
def methodone(self):
print("First method in Class A")

def methodtwo(self):
print("Second method in Class A")

@staticmethod
def methodthree():
print("Third static method in Class A")

@staticmethod
def methodfour():
print("Fourth static method in Class A")

class ClassB(ClassA):
@staticmethod
def methodone():
print("First static method in Class B")

def methodtwo(self):
print("Second method in Class B")
super().methodtwo()

def methodthree(self):
print("Third method in Class B")

@staticmethod

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 12 of 13


Infosys Limited Case Study for Object Oriented Programming using Python

def methodfour():
print("Fourth static method in Class B")
super().methodfour()

b = ClassB()
b.methodone()
b.methodtwo()
b.methodthree()
b.methodfour()

Estimated time: 30 minutes

ER/CORP/CRS/FP16-GEN-003 CONFIDENTIAL Version No: Page 13 of 13

You might also like