OOP WITH JAVA
PROGRAM 7 : 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.
Open notepad and write a program then save it by name
[Link] Command :
Javac [Link]
Java [Link]
import [Link];
// Interface Resizable
interface Resizable {
void resizeWidth(int width); // Method to resize width
void resizeHeight(int height); // Method to resize height
}
// Rectangle class implementing Resizable interface
class Rectangle implements Resizable {
private int width; // Width of the rectangle
private int height; // Height of the rectangle
// Constructor to initialize width and height
public Rectangle(int width, int height) {
[Link] = width;
[Link] = height;
}
// Implementation of Resizable interface methods
public void resizeWidth(int width) {
[Link] = width;
[Link]("Resized width to: " + width);
}
public void resizeHeight(int height) {
[Link] = height;
[Link]("Resized height to: " + height);
}
// Method to display the rectangle's information
public void displayInfo() {
[Link]("Rectangle: Width = " + width + ", Height = " + height);
}
}
// Main class to test the implementation
public class ResizeDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Dept of CSE & ISE SDIT, Kenjar Mr. ABHISHEK M GOWDA
OOP WITH JAVA
// Getting the initial width and height of the rectangle from the user
[Link]("Enter the initial width of the rectangle: ");
int initialWidth = [Link]();
[Link]("Enter the initial height of the rectangle: ");
int initialHeight = [Link]();
// Creating a Rectangle object with user-provided dimensions
Rectangle rct = new Rectangle(initialWidth, initialHeight);
// Displaying the original information
[Link]("\nOriginal Rectangle Info:");
[Link]();
// Getting new dimensions for resizing the rectangle from the user
[Link]("\nEnter the new width to resize the rectangle: ");
int new Width = [Link]();
[Link]("Enter the new height to resize the rectangle: ");
int new Height = [Link]();
// Resizing the rectangle with user-provided values
[Link](new Width);
[Link](new Height);
// Displaying the updated information
[Link]("\nUpdated Rectangle Info:");
[Link]();
// Close the scanner
[Link]();
}
}
Dept of CSE & ISE SDIT, Kenjar Mr. ABHISHEK M GOWDA