0% found this document useful (0 votes)
12 views

Program 7

Uploaded by

zaheerone86
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)
12 views

Program 7

Uploaded by

zaheerone86
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
You are on page 1/ 2

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
ResizeDemo.java Command :

Javac ResizeDemo.java
Java ResizeDemo.java

import java.util.Scanner;

// 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) {
this.width = width;
this.height = height;
}

// Implementation of Resizable interface methods


public void resizeWidth(int width) {
this.width = width;
System.out.println("Resized width to: " + width);
}

public void resizeHeight(int height) {


this.height = height;
System.out.println("Resized height to: " + height);
}

// Method to display the rectangle's information


public void displayInfo() {
System.out.println("Rectangle: Width = " + width + ", Height = " + height);
}
}

// Main class to test the implementation


public class ResizeDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

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
System.out.print("Enter the initial width of the rectangle: ");
int initialWidth = sc.nextInt();

System.out.print("Enter the initial height of the rectangle: ");


int initialHeight = sc.nextInt();

// Creating a Rectangle object with user-provided dimensions


Rectangle rct = new Rectangle(initialWidth, initialHeight);

// Displaying the original information


System.out.println("\nOriginal Rectangle Info:");
rct.displayInfo();

// Getting new dimensions for resizing the rectangle from the user
System.out.print("\nEnter the new width to resize the rectangle: ");
int new Width = sc.nextInt();

System.out.print("Enter the new height to resize the rectangle: ");


int new Height = sc.nextInt();

// Resizing the rectangle with user-provided values


rct.resizeWidth(new Width);
rct.resizeHeight(new Height);

// Displaying the updated information


System.out.println("\nUpdated Rectangle Info:");
rct.displayInfo();

// Close the scanner


sc.close();
}
}

Dept of CSE & ISE SDIT, Kenjar Mr. ABHISHEK M GOWDA

You might also like