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

java 6

Uploaded by

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

java 6

Uploaded by

karthan.official
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

interface Resizable {

void resizeWidth(int width);


void resizeHeight(int height);
}

class Rectangle implements Resizable {


private int width;
private int height;

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

// Method to resize the width


@Override
public void resizeWidth(int width) {
if (width > 0) {
this.width = width;
System.out.println("Width resized to: " + this.width);
} else {
System.out.println("Width must be greater than 0.");
}
}

// Method to resize the height


@Override
public void resizeHeight(int height) {
if (height > 0) {
this.height = height;
System.out.println("Height resized to: " + this.height);
} else {
System.out.println("Height must be greater than 0.");
}
}

// Method to display rectangle dimensions


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

public class MainClass {


public static void main(String[] args) {
// Create a Rectangle object
Rectangle rectangle = new Rectangle(10, 20);
rectangle.displayDimensions();

// Resize the width and height


rectangle.resizeWidth(30);
rectangle.resizeHeight(40);
rectangle.displayDimensions();

// Test invalid resize


rectangle.resizeWidth(-5);
rectangle.resizeHeight(0);
rectangle.displayDimensions();
}
}

You might also like