Java Interface Programming - Resizable interface for object resizing with rectangle class implementation
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:
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 of Rectangle Java:

Flowchart of Main Java:

For more Practice: Solve these Related Problems:
- Write a Java program to extend the Resizable interface by adding a method resize(double factor) and implement it in a Rectangle class.
- Write a Java program to create a Resizable interface and apply it to dynamically adjust the dimensions of various shapes based on user input.
- Write a Java program to implement the Resizable interface in a custom UI component, simulating dynamic window resizing.
- Write a Java program to design a generic resizable data structure that implements the Resizable interface for multiple data types.
Java Code Editor:
Previous: Bank Account, Savings Account, and Current Account.
Next: Drawable interface & shape implementations.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics