Table of Contents
Syntax for creating classes
Example
Syntax for creating objects
Perimeter of the rectangle
Area of ​​rectangle
algorithm
Output
Home Java javaTutorial Write a Java program to calculate the area and perimeter of a rectangle using the concept of classes

Write a Java program to calculate the area and perimeter of a rectangle using the concept of classes

Sep 03, 2023 am 11:37 AM
rectangle concept java program

Write a Java program to calculate the area and perimeter of a rectangle using the concept of classes

The Java language is one of the most commonly used object-oriented programming languages ​​in the world today. The concept of class is one of the most important features in object-oriented languages. A class is like a blueprint for an object. For example, when we want to build a house, we first create a blueprint of the house, in other words, we create a plan that shows how we are going to build the house. According to this plan we can build many houses. Likewise, using classes, we can create many objects. Classes are blueprints for creating many objects, where objects are real-world entities like cars, bikes, pens, etc. A class has the characteristics of all objects, and the objects have the values ​​of these characteristics. In this article, we will write a Java program to find the perimeter and area of ​​a rectangle using the concept of classes.

A class includes the following contents −

  • Data members - Data members represent the characteristics/properties of the object collection

  • Methods - Methods represent operations performed by an object.

For example, if we regard a person as a class, then attributes such as name, age, and address are data members, and actions such as sitting, standing, eating, and walking are methods of the class.

Syntax for creating classes

class ClassName
{
   //data members
   //methods
}
Copy after login

Class names always start 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”);
   }
}
Copy after login

Syntax for creating objects

ClassName objectname = new ClassName();
Copy after login

Example

Person person_one =new Person();
Copy after login

Perimeter of the rectangle

The perimeter of a rectangle is the total area enclosed by the four sides of the rectangle, that 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
Copy after login

Area of ​​rectangle

The area of ​​a rectangle is the total space occupied by the rectangle on a two-dimensional plane.

formula

Area of the rectangle 
= area covered by the rectangle
=  l*w
where , l : length of rectangle
             w : width of rectangle
Copy after login

algorithm

Step 1 − Create a custom class named Rectangle, which has "area()" and "perimeter()" methods. These functions give the area and perimeter of the rectangle as output respectively.

Step 2 − Now, create a rectangular object using the constructor in the main class.

Step 3 − Now call the corresponding functions to find the area and perimeter of the rectangle using the created object.

Example

In this example, we created a custom Rectangle class with "area()" and "perimeter()" methods. Then, use the constructor of the main class in the main class to create an object of the Rectangle class, and call the corresponding 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
    }
}
Copy after login

Output

Length = 10
Width = 5
Area of rectangle with the given input is : 50
Perimeter of rectangle with the given input is : 30
Copy after login

Time complexity: O(1) Auxiliary space: O(1)

So, in this article, we learned how to implement Java code using the concept of classes to find the area and perimeter of a rectangle.

The above is the detailed content of Write a Java program to calculate the area and perimeter of a rectangle using the concept of classes. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the area of ​​a circle within a rectangle inscribed in a semicircle? What is the area of ​​a circle within a rectangle inscribed in a semicircle? Sep 13, 2023 am 08:45 AM

A circle inscribed in a rectangle is tangent to the longer side of the rectangle, that is, its length is tangent to the circle. A rectangle inscribed in a semicircle touches two points on the arc of the semicircle. The width of the rectangle is equal to the diameter of the circle. If R is the radius of the semicircle. The length of the rectangle = √2R/2 The width of the rectangle = R/√2 The radius of the inscribed circle is r = b/2 = R/2√2 Using this formula we can calculate the rectangle inscribed in the semicircle The area of ​​a circle, area = (π*r2)=π*R/8 Example Demonstration #include<stdio.h>intmain(){&

What does the metaverse concept mean? What is the metaverse concept? What does the metaverse concept mean? What is the metaverse concept? Feb 22, 2024 pm 03:55 PM

The Metaverse is an illusory world that uses technology to map and interact with the real world. Analysis 1 Metaverse [Metaverse] is an illusory world that makes full use of technological methods to link and create, and maps and interacts with the real world. It is a data living space with the latest social development system. The 2-dimensional universe is essentially a virtual technology and digital process of the real world, which requires a lot of transformation of content production, economic system, customer experience and physical world content. 3 However, the development trend of the metaverse is gradual. It is finally formed by the continuous combination and evolution of many tools and platforms with the support of shared infrastructure, standards and protocols. Supplement: What is the metaverse composed of? 1 The metaverse is composed of Meta and Verse, Meta is transcendence, and V

How to merge a graphic after CAD rectangles are scattered How to merge a graphic after CAD rectangles are scattered Feb 28, 2024 pm 12:10 PM

When using CAD software, we often encounter situations where we need to recombine "scattered" rectangular objects into a single graphic. This need arises in many fields, such as space planning, mechanical design and architectural drawings. In order to meet this demand, we need to understand and master some key functions in CAD software. Next, the editor of this website will introduce you in detail how to complete this task in the CAD environment. Users who have doubts can come and follow this article to learn. Method for merging CAD rectangles into one graphic after breaking them up: 1. Open the CAD2023 software, create a rectangle, and then enter the X command and a space. As shown below: 2. Select the rectangular object and space it. You can break up the objects. 3. Select all open lines

Learn more about Gunicorn's fundamentals and features Learn more about Gunicorn's fundamentals and features Jan 03, 2024 am 08:41 AM

Basic concepts and functions of Gunicorn Gunicorn is a tool for running WSGI servers in Python web applications. WSGI (Web Server Gateway Interface) is a specification defined by the Python language and is used to define the communication interface between web servers and web applications. Gunicorn enables Python web applications to be deployed and run in production environments by implementing the WSGI specification. The function of Gunicorn is to

Java program opens command prompt and inserts command Java program opens command prompt and inserts command Aug 19, 2023 pm 12:29 PM

ThisarticleusesvariousapproachesforselectingthecommandsinsertedintheopenedcommandwindowthroughtheJavacode.Thecommandwindowisopenedbyusing‘cmd’.Here,themethodsofdoingthesamearespecifiedusingJavacode.TheCommandwindowisfirstopenedusingtheJavaprogram.Iti

Master the key concepts of Spring MVC: Understand these important features Master the key concepts of Spring MVC: Understand these important features Dec 29, 2023 am 09:14 AM

Understand the key features of SpringMVC: To master these important concepts, specific code examples are required. SpringMVC is a Java-based web application development framework that helps developers build flexible and scalable structures through the Model-View-Controller (MVC) architectural pattern. web application. Understanding and mastering the key features of SpringMVC will enable us to develop and manage our web applications more efficiently. This article will introduce some important concepts of SpringMVC

Java program used to check if TPP students are eligible for interviews Java program used to check if TPP students are eligible for interviews Sep 06, 2023 pm 10:33 PM

Please consider the table below to know the eligibility criteria of different companies - The Chinese translation of CGPA is: GPA greater than or equal to 8 Eligible companies Google, Microsoft, Amazon, Dell, Intel, Wipro greater than or equal to 7 Tutorial points, accenture, Infosys , Emicon, Rellins greater than or equal to 6rtCamp, Cybertech, Skybags, Killer, Raymond greater than or equal to 5Patronics, Shoes, NoBrokers Let us enter the java program to check the eligibility of tpp students for interview. Method 1: Using ifelseif condition Normally when we have to check multiple conditions we use

Introduction and core concepts of Oracle RAC Introduction and core concepts of Oracle RAC Mar 07, 2024 am 11:39 AM

Introduction and core concepts of OracleRAC (RealApplicationClusters) As the amount of enterprise data continues to grow and the demand for high availability and high performance becomes increasingly prominent, database cluster technology becomes more and more important. OracleRAC (RealApplicationClusters) is designed to solve this problem. OracleRAC is a high-availability, high-performance cluster database solution launched by Oracle.

See all articles