Assignment Tittle
Assignment Tittle
Engineering
Birla Institute of Technology, Mesra Patna Campus
Assignment Report
By
ABHISHEK ANAND
BCA/15013/19
Under the guidance of:-
Prof. Sheel Shalini
[CA 278] Python Programming Lab Assignment 09
Name:- ABHISHEK ANAND
Roll No:- BCA/15013/19
Subject:- Python Programming Lab
Semester:- 5th
Date:- 04/10/2021
Q1. (The Triangle class) Design a class named Triangle that
extends the GeometricObject class. The Triangle class
contains:
Three float data fields named side1, side2, and side3 to
denote the three sides of the triangle.
A constructor that creates a triangle with the specified
side1, side2, and side3 with default values 1.0.
The accessor methods for all three data fields.
A method named getArea() that returns the area of this
triangle.
A method named getPerimeter() that returns the
perimeter of this triangle.
A method named __str__() that returns a string
description for the triangle.
Code:-
import math
class GeometricObject:
def __init__(self, color = "green", filled = True):
self.__color = color
self.__filled = filled
def getColor(self):
return self.__color
def setColor(self, color):
self.__color = color
def isFilled(self):
return self.__filled
def setFilled(self, filled):
self.__filled = filled
def __str__(self):
return "color: " + self.__color + " and filled: " + str(self.__filled)
class Triangle(GeometricObject):
def __init__(self, side1=1.0, side2=1.0, side3=1.0):
super()._init_()
self.__side1 = side1
self.__side2 = side2
self.__side3 = side3
def get__side1(self):
return self.__side1
def get__side2(self):
return self.__side2
def get__side3(self):
return self.__side3
def getPerimeter(self):
return self.__side1 + self.side2 + self.__side3
def getArea(self):
s = self.getPerimeter()/2
area = math.sqrt(s*(s-self.__side1)*(s-self.side2)*(s-self.__side3))
return area
def main():
triangle=Triangle(10,15,20)
str=input("Enter the color of triangle : ")
triangle.setColor(str)
print("\nA triangle",triangle)
print("The area is",triangle.getArea())
print("The perimeter is",triangle.getPerimeter())
main()
class Location():
def __init__(self, row, column, maxValue):
self.row = row
self.column = column
self.maxValue = maxValue
def getRow(self):
return self.row
def getColumn(self):
return self.column
def getMaxValue(self):
return self.maxValue
from location import Location
def main():
row, column = eval(input("Enter the number of rows and columns of the list: "))
mat = []
for i in range(row):
s = input("Enter row " + str(i) + ": ")
items = s.split()
list = [ eval(x) for x in items ]
mat.append(list)
location = locateLargest(mat)
print("The location of the largest element is "
+ str(location.getMaxValue()) + " at ("
+ str(location.getRow()) + ", " + str(location.getColumn()) + ")")
def locateLargest(a):
maxValue = a[0][0]
row = 0
column = 0
for i in range(len(a)):
for j in range(len(a[i])):
if maxValue < a[i][j]:
maxValue = a[i][j]
row = i
column = j
return Location(row, column, maxValue)
main()