EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

write a program to find area of rectangle by using class and objects in java without using static method

 

// Instance variables to store length and width

double length;

double width;

 

// Constructor to initialize length and width

public Rectangle(double length, double width) {

this.length = length;

this.width = width;

}

 

// Method to calculate and return the area

public double calculateArea() {

return length * width;

}

 

public static void main(String[] args) {

// Create an instance of the Rectangle class

Rectangle rect1 = new Rectangle(10.5, 5.0);

 

// Calculate the area using the instance method

double area = rect1.calculateArea();

 

// Print the area

System.out.println(“The area of the rectangle is: ” + area);

 

// Another instance of Rectangle

Rectangle rect2 = new Rectangle(7.2, 3.8);

System.out.println(“The area of the second rectangle is: ” + recpublic class Rectangle {

// Instance variables to store length and width

double length;

double width;

 

// Constructor to initialize length and width

public Rectangle(double length, double width) {

this.length = length;

this.width = width;

}

 

// Method to calculate and return the area

public double calculateArea() {

return length * width;

}

 

public static void main(String[] args) {

// Create an instance of the Rectangle class

Rectangle rect1 = new Rectangle(10.5, 5.0);

 

// Calculate the area using the instance method

double area = rect1.calculateArea();

 

// Print the area

System.out.println(“The area of the rectangle is: ” + area);

 

// Another instance of Rectangle

Rectangle rect2 = new Rectangle(7.2, 3.8);

System.out.println(“The area of the second rectangle is: ” + rect2.calculateArea());

}

}t2.calculateArea());

}

}

 

Constructor

public class Rectangle {

// Instance variables to store length and width

private double length;

private double width;

// Constructor to initialize length and width

public Rectangle(double length, double width) {

this.length = length;

this.width = width;

}

 

// Method to calculate and return the area

public double calculateArea() {

return length * width;

}

 

public static void main(String[] args) {

// Create an object of the Rectangle class

// The constructor is called here to initialize length and width

Rectangle rect1 = new Rectangle(10.5, 5.0);

 

// Calculate the area using the object’s method

double area1 = rect1.calculateArea();

System.out.println(“Area of rectangle 1: ” + area1);

 

// Create another object with different dimensions

Rectangle rect2 = new Rectangle(7.2, 3.8);

 

// Calculate the area for the second rectangle

double area2 = rect2.calculateArea();

System.out.println(“Area of rectangle 2: ” + area2);

}

}

Java program finding area of rectangle using constructor

Leave a Reply

Your email address will not be published. Required fields are marked *

EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Popular Courses

[Course Image]

Introduction to Programming

Learn the fundamentals of programming with Python in this beginner-friendly course.

12 Hours Beginner
[Course Image]

Data Science Essentials

Master the basics of data analysis, visualization, and machine learning.

20 Hours Intermediate
[Course Image]

Web Development Bootcamp

Build modern websites with HTML, CSS, JavaScript and popular frameworks.

30 Hours Beginner
[Course Image]

Digital Marketing Fundamentals

Learn SEO, social media marketing, email campaigns and analytics.

15 Hours Beginner
Educational Website Footer