- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Find out the Area and Perimeter of Rectangle using Class Concept
Java Language is one of the most used popular object - oriented programming language in present world. Class concept is one of the most important feature in Object - oriented languages. A Class is a like a blue print of an object. For example, when we want to build a house we first create a blue print of the house in other words we create a plan showing how we are going to build the house. Based on the plan we can create a number of houses. Similarly using class, we can create a number of objects. A class is a blueprint for creating a number of objects where objects are the real world entities like car, bike, pen etc. A class has all the characteristics of an object and objects has values for those characteristics. In this article, we are going to write a java program to find the perimeter and area of a rectangle using class concept.
A Class consists of the following −
Data Members − Data Members represent the characteristics/properties of a collection of objects
Methods − Methods represents the actions performed by an object.
For an example, if we consider a person as a class, then the properties like name, age, address are the data members and the actions performed by a person like sit, stand, eat, walk are the methods of the class.
Syntax to create a class
class ClassName { //data members //methods }
ClassName always starts with a capital letter. For example, Person, House, Bank, etc.
Example
class Person{ //data members String name; int age; String city; //methods void read(){ System.out.println(“Reading”); } }
Syntax to create an object
ClassName objectname = new ClassName();
Example
Person person_one =new Person();
Perimeter of a rectangle
The perimeter of a rectangle is the total area covered by the sides of the rectangle, which is the area covered by the length and width of the rectangle.
Formula
Perimeter of the rectangle = area covered by the sides of the rectangle = 2(l+w) where, l : length of rectangle w : width of rectangle
Area of a rectangle
Area of a rectangle is the total space occupied by the rectangle in a 2-d plane.
Formula
Area of the rectangle = area covered by the rectangle = l*w where , l : length of rectangle w : width of rectangle
Algorithm
STEP 1 − Create a custom class name Rectangle which has “area()” and “perimeter()” methods. These functions give us the area of the Rectangle and the Perimeter of a rectangle as output.
STEP 2 − Now, in the main class create an Object of the rectangle using the constructor.
STEP 3 − Now call the respective functions to find the area of rectangle and perimeter of rectangle using the created Object.
Example
In this example, we create a custom Rectangle class which has “area()” and “perimeter()” methods. We then create an object of Rectangle class in main class using a constructor of main class and call respective methods area() and perimeter() on the created object. Once, the methods are called they are executed and the output is printed
// Java program to calculate the area and perimeter of a rectangle using class concept import java.util.*; // Rectangle Class File class Rectangle { // data members int length, width; // methods //constructor to create Object Rectangle(int length, int width) { this. length = length; this.width = width; } // prints the area of rectangle public void area() { int areaOfRectangle; areaOfRectangle = this.length * this.width; System.out.println("Area of rectangle with the given input is : " + areaOfRectangle); } // prints the perimeter of rectangle public void perimeter() { int perimeterOfRectangle; perimeterOfRectangle = 2 * (this.length + this.width); System.out.println("Perimeter of rectangle with the given input is : " + perimeterOfRectangle); } } public class Main { public static void main(String args[]) { Rectangle rect_obj = new Rectangle(10,5); // obect creation System.out.println("Length = " + rect_obj.length); System.out.println("Width = " + rect_obj.width); rect_obj.area(); // returns area of rectangle rect_obj.perimeter(); //returns perimeter of rectangle } }
Output
Length = 10 Width = 5 Area of rectangle with the given input is : 50 Perimeter of rectangle with the given input is : 30
Time Complexity: O(1) Auxiliary Space: O(1)
Thus in this article, we have learnt how to implement java code to find the area and perimeter of a rectangle using Class concept.