Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.0 kB
2
Indexable
Never
7
package Programs;
interface Resizable {
 void resizeWidth(int width);
 void resizeHeight(int height);
}
class Rectangle implements Resizable {
 private int width;
 private int height;
 public Rectangle(int width, int height) {
 this.width = width;
 this.height = height;
 }
 @Override
 public void resizeWidth(int width) {
 this.width = width;
 }
 @Override
 public void resizeHeight(int height) {
 this.height = height;
 }
 public void displayDimensions() {
 System.out.println("Width: " + width + ", Height: " + height);
 }
}
public class Main {
public static void main(String[] args) {
 Rectangle rectangle = new Rectangle(10, 20);
 System.out.println("Original Dimensions:");
 rectangle.displayDimensions();
 rectangle.resizeWidth(15);
 rectangle.resizeHeight(25);
 System.out.println("Dimensions after resizing:");
 rectangle.displayDimensions();
 }
}


8
package Programs;
class OuterClass {
 public void display() {
 System.out.println("OuterClass display method");
 }
 class Inner {
 public void display() {
 System.out.println("InnerClass display method");
 }
 }
}
public class Main {
 public static void main(String[] args) {
 OuterClass outerObject = new OuterClass();
 outerObject.display();
 OuterClass.Inner innerObject = outerObject.new Inner();
 innerObject.display(); // Output: InnerClass display method
 }
}

9
class DivisionByZeroException extends Exception {
 public DivisionByZeroException(String message) {
 super(message);
 }
}
public class Excep {
 public static void main(String[] args) {
 int numerator = 10;
 int denominator = 0;
 try {

 if (denominator == 0) {

 throw new DivisionByZeroException("Division by zero is not allowed.");
 }

 int result = numerator / denominator;
 System.out.println("Result of division: " + result);
 }
 catch (DivisionByZeroException e) {

 System.out.println("Exception caught: " + e.getMessage());
 }
 finally {

 System.out.println("Finally block executed.");
 }
 }
}


Leave a Comment