PR 9

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

NAME : Mayur Singh Rathour

EN_NO : 21162171012
Subject : FP

PRACTICAL-9

1. Design a library application based on the following requirements.


Each book in the library has related title and author.
All the books in library are soft-books which imply that book may
exist as either an EBook or Audio book.
Every EBook has: Format: PDF, EPUB, MOBI, AZW. Anything else
supplied as format should give compilation error.
Every EBook has number of pages.
Audiobook has: Track length measured in minutes. Format: MP3,
WMA, WAV. Anything else supplied as format should give
compilation error.
Application should be able to display books’ name, author, format and
description if required

CODE :

class Book:

def __init__(self,title,author):

self.title = title

self.author = author

class EBook(Book):

def __init__(self,title,author,format,pages):

super().__init__(title,author)
self.format = format

self.pages = pages

if self.format not in ["PDF","EPUB","MOBI","AZW"]:

raise ValueError("Invalid Format")

def __str__(self):

return "Title : {0}\nAuthor : {1}\nFormat : {2}\nPages :


{3}".format(self.title,self.author,self.format,self.pages)

class AudioBook(Book):

def __init__(self,title,author,format,track_length):

super().__init__(title,author)

self.format = format

self.track_length = track_length

if self.format not in ["MP3","WMA","WAV"]:

raise ValueError("Invalid Format")

def __str__(self):

return "Title : {0}\nAuthor : {1}\nFormat : {2}\nTrack Length :


{3}".format(self.title,self.author,self.format,self.track_length)

e = EBook("Python","John","kjl",100)

print(e)

a = AudioBook("Python","John","MP3",100)

print(a)
OUTPUT :

2. Implement Stack using concept of Object oriented programming.


At minimum, any stack should be able to perform the following three
operations:
Push: Add an object passed as an argument to the top of the stack.
Pop: Remove the object at the top of the stack and return it.
Peek (or peep): Return the object at the top of the stack (without
removing it).
Display: Print the current status of stack.

CODE :

class Stack:

def __init__(self):

self.items = []

def isEmpty(self):

return self.items == []
def push(self, item):

self.items.append(item)

def pop(self):

return self.items.pop()

def peek(self):

return self.items[len(self.items)-1]

def size(self):

return len(self.items)

def display(self):

print("stack:",self.items)

s = Stack()

s.push(1)

s.push(2)

s.display()

pop_element=s.pop()
print("poped element :",pop_element)

s.display()

t_item=s.peek()
print("top item :",t_item)
Output :

You might also like