w3resource

Java Interface Programming - Resizable interface for object resizing with rectangle class implementation

Java Interface: Exercise-5 with Solution

Write a Java program to create an interface Resizable with methods resizeWidth(int width) and resizeHeight(int height) that allow an object to be resized. Create a class Rectangle that implements the Resizable interface and implements the resize methods.

Sample Solution:

Java Code:

// Resizable.java
// Interface Resizable

// Declare the Resizable interface
interface Resizable {
    // Declare the abstract method "resizeWidth" to resize the width
    void resizeWidth(int width);

    // Declare the abstract method "resizeHeight" to resize the height
    void resizeHeight(int height);
}

// Rectangle.java

// Declare the Rectangle class, which implements the Resizable interface
class Rectangle implements Resizable {
    // Declare private instance variables to store width and height
    private int width;
    private int height;

    // Constructor for initializing the width and height
    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }

    // Implement the "resizeWidth" method to resize the width
    public void resizeWidth(int width) {
        this.width = width;
    }

    // Implement the "resizeHeight" method to resize the height
    public void resizeHeight(int height) {
        this.height = height;
    }

    // Method to print the current width and height of the rectangle
    public void printSize() {
        System.out.println("Width: " + width + ", Height: " + height);
    }
}
// Main.java

// Declare the Main class
public class Main {
    public static void main(String[] args) {
        // Create an instance of the Rectangle class with an initial size
        Rectangle rectangle = new Rectangle(100, 150);
        
        // Print the initial size of the rectangle
        rectangle.printSize();

        // Resize the rectangle by changing its width and height
        rectangle.resizeWidth(150);
        rectangle.resizeHeight(200);
        
        // Print the updated size of the rectangle
        rectangle.printSize();
    }
}

Sample Output:

Width: 100, Height: 150
Width: 150, Height: 200 

Explanation:

In the above exercise –

  • First, we define an interface "Resizable" with the methods resizeWidth(int width) and resizeHeight(int height) to allow an object to be resized.
  • The "Rectangle" class implements the Resizable interface and provides its own implementations of the resize methods. It also has a printSize() method to display the current rectangle size.
  • In the main() method, we create an instance of the "Rectangle" class and display its size. Then, we call the resizeWidth() and resizeHeight() methods to change the rectangle size.
  • Finally, we print the updated size.

Flowchart of Resizable Java:

Flowchart: Resizable Java

Flowchart of Rectangle Java:

Flowchart: Rectangle Java

Flowchart of Main Java:

Flowchart: Main Java

Java Code Editor:

Previous: Bank Account, Savings Account, and Current Account.
Next: Drawable interface & shape implementations.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.