Write a class named "LandParcel" with constructor method included. The class should accept three attributes at object initiation: length, width, and unit cost. Define three more methods in the class to calculate the perimeter, area, and total cost of the plot. Afterwards, instantiate an object, call the required methods, and display the length, width, and total cost on the screen. Use proper naming convention and formatting.
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
In python:
Write a class named "LandParcel" with constructor method included. The class should accept three attributes at object initiation: length, width, and unit cost. Define three more methods in the class to calculate the perimeter, area, and total cost of the plot. Afterwards, instantiate an object, call the required methods, and display the length, width, and total cost on the screen. Use proper naming convention and formatting.
class LandParcel:
#decalre variables
length = 0
width = 0
unit_cost = 0
# parameterized constructor
def __init__(self, l, w, cost):
self.length = l
self.width = w
self.unit_cost = cost
#This method calculate perimeter
def calculatePerimeter(self):
print("Perimeter = " + str(2*(self.length+self.width)))
#This method calculate area
def calculateArea(self):
print("Area = " + str(self.length*self.width))
#This method calculate total cost
def calculateTotalCost(self):
print("Total Cost = " + str(self.length*self.width*self.unit_cost))
#This method print details of total cost
def displayDetails(self):
print("Length = " + str(self.length))
print("Width = " + str(self.width))
self.calculateTotalCost()
# Creating the object of the Class LandParcel, And invoke parameterized constructor
obj = LandParcel(2,3,6)
# call displayDetails
obj.displayDetails()
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images