0% found this document useful (0 votes)
18 views3 pages

Resizable Rectangle Implementation in Java

The document describes a JAVA program that implements a Resizable interface with methods for resizing width and height. A Rectangle class is created that implements this interface, allowing it to adjust its dimensions. The main method demonstrates creating a Rectangle object, resizing it, and displaying the updated dimensions.

Uploaded by

fevawov660
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Resizable Rectangle Implementation in Java

The document describes a JAVA program that implements a Resizable interface with methods for resizing width and height. A Rectangle class is created that implements this interface, allowing it to adjust its dimensions. The main method demonstrates creating a Rectangle object, resizing it, and displaying the updated dimensions.

Uploaded by

fevawov660
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 07 : Resizable interface

Develop 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.

// Resizable interface

interface Resizable {

void resizeWidth(int width);

void resizeHeight(int height);

// Rectangle class implementing Resizable interface

class Rectangle implements Resizable {

private int width;

private int height;

public Rectangle(int width, int height) {

[Link] = width;

[Link] = height;

// Implementation of Resizable interface

@Override

public void resizeWidth(int width) {

[Link] = width;

[Link]("Resized width to: " + width);

@Override
public void resizeHeight(int height) {

[Link] = height;

[Link]("Resized height to: " + height);

// Additional methods for Rectangle class

public int getWidth() {

return width;

public int getHeight() {

return height;

public void displayInfo() {

[Link]("Rectangle: Width = " + width + ", Height = " + height);

// Main class to test the implementation

public class ResizeDemo {

public static void main(String[] args) {

// Creating a Rectangle object

Rectangle rectangle = new Rectangle(10, 5);

// Displaying the original information

[Link]("Original Rectangle Info:");

[Link]();
// Resizing the rectangle

[Link](15);

[Link](8);

// Displaying the updated information

[Link]("\nUpdated Rectangle Info:");

[Link]();

In this program, the Resizable interface declares the methods resizeWidth and resizeHeight.
The Rectangle class implements this interface and provides the specific implementation for resizing the
width and height. The main method in the ResizeDemo class creates a Rectangle object, displays its
original information, resizes it, and then displays the updated information.

Output

$ java ResizeDemo

Original Rectangle Info:

Rectangle: Width = 10, Height = 5

Resized width to: 15

Resized height to: 8

Updated Rectangle Info:

Rectangle: Width = 15, Height = 8

Common questions

Powered by AI

If the Rectangle was initialized with a width of 20 and a height of 10, the output of the program would be as follows: Original Rectangle Info: Rectangle: Width = 20, Height = 10 Resized width to: 15 Resized height to: 8 Updated Rectangle Info: Rectangle: Width = 15, Height = 8 The newly initialized dimensions will be displayed first, followed by the resized dimensions according to the calls to resizeWidth and resizeHeight in the main method .

Potential real-world applications benefiting from a resizable interface mechanism include graphic design software, where objects like shapes and images need dynamic resizing without coupling to a specific shape class. Another application is in UI frameworks, where components can resize based on user input or layout changes. Game development also benefits, particularly in managing scalable assets that resize according to screen resolutions or game objects that need resizing based on gameplay dynamics, achieving this scalability while maintaining code modularity and flexibility .

The Resizable interface could be extended to support three-dimensional shapes by including additional methods to handle the resizing of the third dimension, such as resizeDepth(int depth). For example, a Box class implementing this extended interface could include private fields for width, height, and depth and implement all resizing methods. Example implementation might look like: interface Resizable3D extends Resizable { void resizeDepth(int depth); } class Box implements Resizable3D { private int width, height, depth; public Box(int width, int height, int depth) { this.width = width; this.height = height; this.depth = depth; } @Override public void resizeWidth(int width) { this.width = width; } @Override public void resizeHeight(int height) { this.height = height; } @Override public void resizeDepth(int depth) { this.depth = depth; } // Additional methods to access box properties } This illustrates how interface extension can easily adapt the design for more complex requirements without altering the existing two-dimensional interface .

To make the displayInfo method more informative, it could include additional details such as the area and perimeter of the rectangle, which provides a more comprehensive understanding of the object's current state. Here’s how the method could be modified: public void displayInfo() { int area = width * height; int perimeter = 2 * (width + height); System.out.println("Rectangle: Width = " + width + ", Height = " + height); System.out.println("Area = " + area + ", Perimeter = " + perimeter); } This addition would help users quickly grasp the rectangle's size characteristics beyond its basic dimensions .

While inheritance could theoretically be used to achieve a similar outcome by having a base class with resizeWidth and resizeHeight methods, it comes with limitations compared to using interfaces. Inheritance would tie the implementing classes to a specific hierarchy, which can be limiting and lead to issues with multiple inheritances since Java does not support it. Using interfaces like Resizable avoids these limitations, allowing different classes to implement behavior as needed without hierarchical constraints. Interfaces also improve flexibility and adhere to SOLID principles better by supporting behavior extension without modifying existing code .

Using an interface like Resizable provides several advantages over concrete class methods, including promoting a separation of concepts, enhancing flexibility, and supporting polymorphism. Interfaces allow different classes to implement the same methods in ways that are most suitable for their context, increasing reusability and adherence to a principle known as coding to an interface. This makes maintenance easier and the system more scalable, as adding new types that follow the Resizable interface doesn't require changing existing code that uses the interface .

The JAVA program demonstrates the principle of abstraction by defining an interface Resizable that abstracts resizing operations independent of any specific implementation. The Rectangle class implements this abstract behavior by providing specific concrete implementations of the resizeWidth and resizeHeight methods, separating the concept of resizing from the actual implementation. This allows users of the Rectangle class and others implementing Resizable to interact with objects' resizing functionality without knowing the underlying details .

The Rectangle class ensures encapsulation by keeping its fields, width and height, private and providing only public methods to modify and access them. The class defines public methods resizeWidth and resizeHeight, as required by the Resizable interface, to control how width and height can be changed. This hides the internal representation of the rectangle's size from external code, enforcing encapsulation while still allowing external modification through controlled interface methods and accessors like getWidth and getHeight .

The implementation of the Resizable interface in the Rectangle class facilitates polymorphism by allowing the Rectangle objects to be treated as instances of the Resizable interface, which defines methods for resizing. This means that any object that implements the Resizable interface can be used interchangeably, and specific implementations of resizing logic can be applied without changing the object's code. This promotes code flexibility and reusability, as the ResizeDemo class demonstrates by resizing the Rectangle object through the interface's methods .

To add validation checks for minimum width and height, methods resizeWidth and resizeHeight in the Rectangle class need to include conditions that verify the input before assigning it to the class fields. For example: @Override public void resizeWidth(int width) { if (width >= minimumWidth) { this.width = width; System.out.println("Resized width to: " + width); } else { System.out.println("Width cannot be less than " + minimumWidth); } } @Override public void resizeHeight(int height) { if (height >= minimumHeight) { this.height = height; System.out.println("Resized height to: " + height); } else { System.out.println("Height cannot be less than " + minimumHeight); } } This ensures that invalid dimensions are not accepted, and the system maintains consistency .

You might also like