0% found this document useful (0 votes)
41 views5 pages

Assignment Tittle

The document describes two Python programming assignments. The first involves creating a Triangle class that extends a GeometricObject class and includes methods to return the triangle's area, perimeter, and string description. The second assignment involves creating a Location class to store the row, column, and maximum value of the largest element in a two-dimensional list, and writing a function to locate this largest element and return a Location instance. A test program is also provided to test locating the largest element in a 2D list input by the user.

Uploaded by

Abhishek Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views5 pages

Assignment Tittle

The document describes two Python programming assignments. The first involves creating a Triangle class that extends a GeometricObject class and includes methods to return the triangle's area, perimeter, and string description. The second assignment involves creating a Location class to store the row, column, and maximum value of the largest element in a two-dimensional list, and writing a function to locate this largest element and return a Location instance. A test program is also provided to test locating the largest element in a 2D list input by the user.

Uploaded by

Abhishek Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of Computer Science and

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()

Q2. Design a class named Location for locating a maximal


value and its location in a two-dimensional list. The class
contains the public data fields row, column, and maxValue
that store the maximal value and its indexes in a two-
dimensional list, with row and column as int types and
maxValue as a float type.
Write the following method that returns the location of the
largest element in a two-dimensional list. def Location
locateLargest(a):
 The return value is an instance of Location.
Write a test program that prompts the user to enter a two-
dimensional list and displays the location of the largest
element in the list.
Code:-

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()

You might also like