{"id":10068,"date":"2021-09-30T12:00:06","date_gmt":"2021-09-30T06:30:06","guid":{"rendered":"https:\/\/python-programs.com\/?p=10068"},"modified":"2021-11-22T18:35:27","modified_gmt":"2021-11-22T13:05:27","slug":"python-program-to-find-the-area-of-a-rectangle-using-classes","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-area-of-a-rectangle-using-classes\/","title":{"rendered":"Python Program to Find the Area of a Rectangle Using Classes"},"content":{"rendered":"
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output<\/a> & achieve your goal in improving java coding skills.<\/p>\n Object-Oriented Programming(OOPS):<\/strong><\/p>\n Object-oriented programming (OOP) is a form of program structure that involves grouping related characteristics and activities into separate objects.<\/p>\n Objects are conceptually similar to system components. Consider a program to be a sort of factory assembly line. A system component processes some material at each step of the assembly line, eventually changing raw material into a finished product.<\/p>\n An object comprises data, such as the raw or preprocessed materials at each step of an assembly line, as well as behavior, such as the action performed by each assembly line component.<\/p>\n Given length and breadth, the task is to calculate the area of the rectangle with the given length and breadth using classes.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the full approach to calculate the area of the rectangle with the given length and breadth using classes in Python.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills. Object-Oriented Programming(OOPS): Object-oriented programming (OOP) is a form of program structure that involves grouping related characteristics …<\/p>\ngiven length of rectangle = 15\r\ngiven breadth of rectangle = 11<\/pre>\n
The area of the rectangle with the given sides 15 , 11 = 165<\/pre>\n
given length of rectangle = 31\r\ngiven breadth of rectangle = 19<\/pre>\n
The area of the rectangle with the given sides 31 , 19 = 589<\/pre>\n
Program to Find the Area of a Rectangle Using Classes in Python<\/h2>\n
\n
1)Using Classes(Static Input)<\/h3>\n
\n
# creating a class\r\nclass rectangle():\r\n # parameterized constructor with breadth and length as arguments\r\n # parameterized constructor is used to initialize its values (length and breadth of the rectangle).\r\n def __init__(self, rectbreadth, rectlength):\r\n self.rectbreadth = rectbreadth\r\n self.rectlength = rectlength\r\n # Creating a method called areaofRect that returns the area with the given length and breadth of the rectangle\r\n\r\n def areaofRect(self):\r\n return self.rectbreadth*self.rectlength\r\n\r\n\r\n# Give the length and breadth as static input and store it in two variables.\r\nrectlength = 31\r\nrectbreadth = 19\r\n# Creating an object to represent the class.\r\n# Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input.\r\nrectObj = rectangle(rectlength, rectbreadth)\r\nprint(\"The area of the rectangle with the given sides\",\r\n rectlength, ',', rectbreadth, '=', rectObj.areaofRect())\r\n<\/pre>\n
The area of the rectangle with the given sides 31 , 19 = 589<\/pre>\n
2)Using Classes(User Input separated by spaces)<\/h3>\n
\n
# creating a class\r\nclass rectangle():\r\n # parameterized constructor with breadth and length as arguments\r\n # parameterized constructor is used to initialize its values (length and breadth of the rectangle).\r\n def __init__(self, rectbreadth, rectlength):\r\n self.rectbreadth = rectbreadth\r\n self.rectlength = rectlength\r\n # Creating a method called areaofRect that returns the area with the given length and breadth of the rectangle\r\n\r\n def areaofRect(self):\r\n return self.rectbreadth*self.rectlength\r\n\r\n\r\n# Scan the given length and breadth as user input using a map, int, \r\n#and split() functions and store them in two variables.\r\nrectlength, rectbreadth = map(int, input('Enter the length and breadth of the rectangle separated by spaces = ').split())\r\n# Creating an object to represent the class.\r\n# Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input.\r\nrectObj=rectangle(rectlength, rectbreadth)\r\nprint(\"The area of the rectangle with the given sides\",\r\n rectlength, ',', rectbreadth, '=', rectObj.areaofRect())\r\n<\/pre>\n
Enter the length and breadth of the rectangle separated by spaces = 7 9\r\nThe area of the rectangle with the given sides 7 , 9 = 63\r\n\r\n<\/pre>\n
\n
3)Using Classes(User Input separated by newline)<\/h3>\n
\n
# creating a class\r\nclass rectangle():\r\n # parameterized constructor with breadth and length as arguments\r\n # parameterized constructor is used to initialize its values (length and breadth of the rectangle).\r\n def __init__(self, rectbreadth, rectlength):\r\n self.rectbreadth = rectbreadth\r\n self.rectlength = rectlength\r\n # Creating a method called areaofRect that returns the area with the given length and breadth of the rectangle\r\n\r\n def areaofRect(self):\r\n return self.rectbreadth*self.rectlength\r\n\r\n\r\n# Scan the rectangle's length as user input using the int(input()) function and store it in a\u00a0 variable.\r\nrectlength = int(input('Enter some random length of the rectangle = '))\r\n# Scan the rectangle's breadth as user input using the int(input()) function and store it in another variable.\r\n# Here int() is used to convert the given number to integer datatype.\r\nrectbreadth = int(input('Enter some random breadth of the rectangle = '))\r\n# Creating an object to represent the class.\r\n# Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input.\r\nrectObj = rectangle(rectlength, rectbreadth)\r\nprint(\"The area of the rectangle with the given sides\",\r\n rectlength, ',', rectbreadth, '=', rectObj.areaofRect())\r\n<\/pre>\n
Enter some random length of the rectangle = 15\r\nEnter some random breadth of the rectangle = 11\r\nThe area of the rectangle with the given sides 15 , 11 = 165<\/pre>\n
\n
\n