w3resource

Creating a Java Rectangle Class with Parameterized and Copy Constructors

Java Constructor: Exercise-5 with Solution

Copy Constructor
Write a Java program to create a class called Rectangle with instance variables length and width. Implement a parameterized constructor and a copy constructor that initializes a new object using the values of an existing object. Print the values of the variables.

Sample Solution:

Java Code:

Rectangle.java

// Define the Rectangle class
public class Rectangle {
    // Private instance variables
    private double length;
    private double width;

    // Parameterized constructor
    public Rectangle(double length, double width) {
        // Initialize length with the provided parameter
        this.length = length;
        // Initialize width with the provided parameter
        this.width = width;
    }

    // Copy constructor
    public Rectangle(Rectangle rectangle) {
        // Initialize length with the length of the provided rectangle object
        this.length = rectangle.length;
        // Initialize width with the width of the provided rectangle object
        this.width = rectangle.width;
    }

    // Main method to test the Rectangle class
    public static void main(String[] args) {
        // Create a new Rectangle object using the parameterized constructor
        Rectangle rect1 = new Rectangle(12.5, 4.5);
        // Print the values of the instance variables for rect1
        System.out.println("Rectangle 1 Length: " + rect1.length);
        System.out.println("Rectangle 1 Width: " + rect1.width);

        // Create a new Rectangle object using the copy constructor
        Rectangle rect2 = new Rectangle(rect1);
        // Print the values of the instance variables for rect2
        System.out.println("Rectangle 2 Length: " + rect2.length);
        System.out.println("Rectangle 2 Width: " + rect2.width);
    }
}

Output:

Rectangle 1 Length: 12.5
Rectangle 1 Width: 4.5
Rectangle 2 Length: 12.5
Rectangle 2 Width: 4.5

Explanation:

  • Define the Rectangle class:
    • The Rectangle class is defined with the keyword class.
  • Private instance variables:
    • The Rectangle class has two private instance variables: length (a double) and width (a double).
  • Parameterized constructor:
    • The parameterized constructor Rectangle(double length, double width) is defined.
    • Inside the constructor, this.length is initialized with the provided length parameter.
    • this.width is initialized with the provided width parameter.
  • Copy constructor:
    • The copy constructor Rectangle(Rectangle rectangle) is defined.
    • Inside the constructor, this.length is initialized with the length of the provided rectangle object.
    • this.width is initialized with the width of the provided rectangle object.
  • Main method:
    • The main method is defined to test the Rectangle class.
    • A Rectangle object (rect1) is created using the parameterized constructor.
    • The values of the instance variables for rect1 are printed to the console.
    • Another Rectangle object (rect2) is created using the copy constructor, with rect1 as the argument.
    • The values of the instance variables for rect2 are printed to the console.

Note on Constructors:

In the above exercise, the constructors for the Rectangle class work by:

  • Parameterized Constructor: Initializes the length and width instance variables with specific values provided as arguments.
  • Copy Constructor: Initializes the length and width instance variables using the values from an existing Rectangle object, promoting object copying with the same attributes.
  • Encapsulation: By keeping the instance variables private and initializing them through constructors, the class ensures controlled initialization and data integrity, encapsulating the logic for setting values within the class itself.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java Constructor Previous: Creating a Java Student Class with Constructor Chaining.
Java Constructor Next: Creating a Java Account Class with Validated Parameterized Constructor.

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://w3resource.com/java-exercises/constructor/java-constructor-exercise-5.php