Java program to find the area of rectangle using class and object: 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):
Class rectangle python: Object-oriented programming (OOP) is a form of program structure that involves grouping related characteristics and activities into separate objects.
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.
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.
Given length and breadth, the task is to calculate the area of the rectangle with the given length and breadth using classes.
- Java Program to Find Perimeter of Parallelogram
- Java Interview Questions and Answers (2021) for Freshers and Experienced | Most Common Core Java Programming Interview Questions
- Java Lecture Notes PDF Free Download
Examples:
Example1:
Input:
given length of rectangle = 15 given breadth of rectangle = 11
Output:
The area of the rectangle with the given sides 15 , 11 = 165
Example2:
Input:
given length of rectangle = 31 given breadth of rectangle = 19
Output:
The area of the rectangle with the given sides 31 , 19 = 589
Program to Find the Area of a Rectangle Using Classes in Python
Java program to find the area of rectangle and circle using class and object:
Below is the full approach to calculate the area of the rectangle with the given length and breadth using classes in Python.
- Using Classes(Static Input)
- Using Classes(User Input separated by spaces)
- Using Classes(User Input separated by newline)
1)Using Classes(Static Input)
Approach:
- Give the length and breadth as static input and store it in two variables.
- Create a class and use a parameterized constructor to initialize its values (length and breadth of the rectangle).
- Create a method called areaofRect that returns the area with the given length and breadth of the rectangle.
- Create an object to represent the class.
- Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input.
- Print the area of the rectangle.
- The exit of the program.
Below is the implementation:
# creating a class class rectangle(): # parameterized constructor with breadth and length as arguments # parameterized constructor is used to initialize its values (length and breadth of the rectangle). def __init__(self, rectbreadth, rectlength): self.rectbreadth = rectbreadth self.rectlength = rectlength # Creating a method called areaofRect that returns the area with the given length and breadth of the rectangle def areaofRect(self): return self.rectbreadth*self.rectlength # Give the length and breadth as static input and store it in two variables. rectlength = 31 rectbreadth = 19 # Creating an object to represent the class. # Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input. rectObj = rectangle(rectlength, rectbreadth) print("The area of the rectangle with the given sides", rectlength, ',', rectbreadth, '=', rectObj.areaofRect())
Output:
The area of the rectangle with the given sides 31 , 19 = 589
2)Using Classes(User Input separated by spaces)
Approach:
- Scan the given length and breadth as user input using a map, int, and split() functions and store them in two variables.
- Create a class and use a parameterized constructor to initialize its values (length and breadth of the rectangle).
- Create a method called areaofRect that returns the area with the given length and breadth of the rectangle.
- Create an object to represent the class.
- Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input.
- Print the area of the rectangle.
- The exit of the program.
Below is the implementation:
# creating a class class rectangle(): # parameterized constructor with breadth and length as arguments # parameterized constructor is used to initialize its values (length and breadth of the rectangle). def __init__(self, rectbreadth, rectlength): self.rectbreadth = rectbreadth self.rectlength = rectlength # Creating a method called areaofRect that returns the area with the given length and breadth of the rectangle def areaofRect(self): return self.rectbreadth*self.rectlength # Scan the given length and breadth as user input using a map, int, #and split() functions and store them in two variables. rectlength, rectbreadth = map(int, input('Enter the length and breadth of the rectangle separated by spaces = ').split()) # Creating an object to represent the class. # Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input. rectObj=rectangle(rectlength, rectbreadth) print("The area of the rectangle with the given sides", rectlength, ',', rectbreadth, '=', rectObj.areaofRect())
Output:
Enter the length and breadth of the rectangle separated by spaces = 7 9 The area of the rectangle with the given sides 7 , 9 = 63
Explanation:
- The user must provide the length and breadth values.
- A rectangle class is created, and the __init__() method is used to initialize the class’s values.
- The area method returns self. length*self. breadth, which is the class’s area.
- The class’s object is created.
- Using the object, the method area() is invoked with the length and breadth as the parameters given by the user.
- The area has been printed.
3)Using Classes(User Input separated by newline)
Approach:
- Scan the rectangle’s length as user input using the int(input()) function and store it in a variable.
- Scan the rectangle’s breadth as user input using the int(input()) function and store it in another variable.
- Here int() is used to convert the given number to integer datatype.
- Create a class and use a parameterized constructor to initialize its values (length and breadth of the rectangle).
- Create a method called areaofRect that returns the area with the given length and breadth of the rectangle.
- Create an object to represent the class.
- Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input.
- Print the area of the rectangle.
- The exit of the program.
Below is the implementation:
# creating a class class rectangle(): # parameterized constructor with breadth and length as arguments # parameterized constructor is used to initialize its values (length and breadth of the rectangle). def __init__(self, rectbreadth, rectlength): self.rectbreadth = rectbreadth self.rectlength = rectlength # Creating a method called areaofRect that returns the area with the given length and breadth of the rectangle def areaofRect(self): return self.rectbreadth*self.rectlength # Scan the rectangle's length as user input using the int(input()) function and store it in a variable. rectlength = int(input('Enter some random length of the rectangle = ')) # Scan the rectangle's breadth as user input using the int(input()) function and store it in another variable. # Here int() is used to convert the given number to integer datatype. rectbreadth = int(input('Enter some random breadth of the rectangle = ')) # Creating an object to represent the class. # Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input. rectObj = rectangle(rectlength, rectbreadth) print("The area of the rectangle with the given sides", rectlength, ',', rectbreadth, '=', rectObj.areaofRect())
Output:
Enter some random length of the rectangle = 15 Enter some random breadth of the rectangle = 11 The area of the rectangle with the given sides 15 , 11 = 165
Explanation:
- The user must provide the length and breadth values.
- A rectangle class is created, and the __init__() method is used to initialize the class’s values.
- The area method returns self. length*self. breadth, which is the class’s area.
- The class’s object is created.
- Using the object, the method area() is invoked with the length and breadth as the parameters given by the user.
- The area has been printed.
Related Programs:
- Python Program to Create a Class and Get All Possible Subsets from a List
- Python Program to Create a Class and Compute the Area and the Perimeter of the Circle
- Python Program to Create a Class in which One Method Accepts a String from the User and Another Prints it
- Python Program to Create a Class which Performs Basic Calculator Operations