w3resource

Java: Calculate area and perimeter of a rectangle

Java OOP: Exercise-3 with Solution

Write a Java program to create a class called "Rectangle" with width and height attributes. Calculate the area and perimeter of the rectangle.

Sample Solution:

Java Code:

// Define the Rectangle class
public class Rectangle {
    // Declare a private variable to store the width of the rectangle
    private double width;
    // Declare a private variable to store the height of the rectangle
    private double height;

    // Constructor for the Rectangle class that initializes the width and height variables
    public Rectangle(double width, double height) {
        // Set the width variable to the provided width parameter
        this.width = width;
        // Set the height variable to the provided height parameter
        this.height = height;
    }

    // Method to retrieve the width of the rectangle
    public double getWidth() {
        // Return the value of the width variable
        return width;
    }

    // Method to set the width of the rectangle
    public void setWidth(double width) {
        // Set the width variable to the provided width parameter
        this.width = width;
    }

    // Method to retrieve the height of the rectangle
    public double getHeight() {
        // Return the value of the height variable
        return height;
    }

    // Method to set the height of the rectangle
    public void setHeight(double height) {
        // Set the height variable to the provided height parameter
        this.height = height;
    }

    // Method to calculate and return the area of the rectangle
    public double getArea() {
        // Calculate the area by multiplying width and height, and return the result
        return width * height;
    }

    // Method to calculate and return the perimeter of the rectangle
    public double getPerimeter() {
        // Calculate the perimeter by adding width and height, multiplying by 2, and return the result
        return 2 * (width + height);
    }
}

The above class has two private attributes: 'width' and 'height', a constructor that initializes these attributes with the values passed as arguments, and getter and setter methods to access and modify these attributes. It also has two methods 'getArea()' and 'getPerimeter() ' to calculate the area and perimeter of the rectangle.

// Define the Main class
public class Main {
    // Define the main method which is the entry point of the program
    public static void main(String[] args) {
        // Create an instance of the Rectangle class with the width 7 and height 12
        Rectangle rectangle = new Rectangle(7, 12);

        // Print the area of the rectangle to the console
        System.out.println("The area of the rectangle is " + rectangle.getArea());
        // Print the perimeter of the rectangle to the console
        System.out.println("The perimeter of the rectangle is " + rectangle.getPerimeter());

        // Set a new width for the rectangle
        rectangle.setWidth(6);
        // Set a new height for the rectangle
        rectangle.setHeight(12);

        // Print the updated area of the rectangle to the console
        System.out.println("\nThe area of the rectangle is now " + rectangle.getArea());
        // Print the updated perimeter of the rectangle to the console
        System.out.println("The perimeter of the rectangle is now " + rectangle.getPerimeter());
    }
} 

In the Main() function we create an instance of the "Rectangle" class with a width of 7 and a height of 12, and call its methods to calculate the area and perimeter. We then modify the width and height using the setter methods and print the updated rectangle area and perimeter.

Sample Output:

The area of the rectangle is 84.0
The perimeter of the rectangle is 38.0

The area of the rectangle is now 72.0
The perimeter of the rectangle is now 36.0

Flowchart:

Flowchart: Java  OOP Exercises: Calculate area and perimeter of a rectangle.
Flowchart: Java  OOP Exercises: Calculate area and perimeter of a rectangle.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Create and Modify Dog Objects.
Java OOP Next: Create a Circle class with area and circumference calculation.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/oop/java-oop-exercise-3.php