You are on page 1of 5

EXPERIMENT NO.

10
NAME: SHASHWAT WANKHEDE
ROLL NO: 58
SECTION: A
DATE OF PERFORMANCE: 22/12/21

AIM: Define appropriate classes for any one of the following with
standard methods:

i) Polynomial class

ii) Matrix class

iii)Stack class

Also write driver program to demonstrate the use of these

Classes.

THEORY:

Java Collection framework provides a Stack class that models and


implements a Stack data structure. The class is based on the basic principle
of last-in-first-out. In addition to the basic push and pop operations, the class
provides three more functions of empty, search, and peek. The class can
also be said to extend Vector and treats the class as a stack with the five
mentioned functions.
The class supports one default constructor Stack() which is used to create
an empty stack.
Addition is simpler than multiplication of polynomials. We initialize result as
one of the two polynomials, then we traverse the other polynomial and add
all terms to the result.
we can add two matrices in java using binary + operator. A matrix is also known as array of
arrays. We can add, subtract and multiply matrices.

CODE:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname

def printname(self):
print(self.firstname, self.lastname)

#Use the Person class to create an object, and then execute the printname
method:

x = Person("John", "Doe")
x.printname()
#########
class Student(Person):
pass

x = Student("Mike", "Olsen")
x.printname()

# importing package
import numpy

class Polynomial:
def __init__(self):
self.px1 = (5,-2,5)
self.px2 = (2,-5,2)

def addition(self):
rx = numpy.polynomial.polynomial.polyadd(self.px1,self.px2)
print(rx)

def addition1(self,px1,px2):
rx = numpy.polynomial.polynomial.polyadd(px1,px2)
print(rx)

P=Polynomial()
px3=(1,2,3)
px4=(4,5,6)
P.addition1(px3,px4)
###########Matrix##############
# Program to add two matrices using nested loop

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]

Y = [[5,8,1],
[6,7,3],
[4,5,9]]

result = [[0,0,0],
[0,0,0],
[0,0,0]]

# iterate through rows


for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]

for r in result:
print(r)

# Python program to
# demonstrate stack implementation
# using list

stack = []

# append() function to push


# element in the stack
stack.append('a')
stack.append('b')
stack.append('c')

print('Initial stack')
print(stack)

# pop() function to pop


# element from stack in
# LIFO order
print('\nElements popped from stack:')
print(stack.pop())
print(stack.pop())
print(stack.pop())

print('\nStack after elements are popped:')


print(stack)

# uncommenting print(stack.pop())
# will cause an IndexError
# as the stack is now empty

SNAPSHOTS:
OUTPUT:

CONCLUSION:

In this experiment we defined appropriate classes for the following with


standard methods: i) Polynomial class ii) Matrix class iii) Stack class

We also wrote a driver program to demonstrate the use of these

Classes and executed it successfully.

You might also like