[go: up one dir, main page]

0% found this document useful (0 votes)
12 views6 pages

Program 7,8,9 JAVA

The document contains three Java programs demonstrating different concepts. Program 7 implements a Resizable interface with a Rectangle class that can resize its dimensions. Program 8 showcases an outer class with a nested inner class, both having display methods, while Program 9 illustrates a custom exception for division by zero using try, catch, and finally blocks.

Uploaded by

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

Program 7,8,9 JAVA

The document contains three Java programs demonstrating different concepts. Program 7 implements a Resizable interface with a Rectangle class that can resize its dimensions. Program 8 showcases an outer class with a nested inner class, both having display methods, while Program 9 illustrates a custom exception for division by zero using try, catch, and finally blocks.

Uploaded by

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

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.

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) {

this.width = width;

this.height = height;

// Implementation of Resizable interface

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);


}

// Additional methods for Rectangle class

public int getWidth() {

return width;

public int getHeight() {

return height;

public void displayInfo() {

System.out.println("Rectangle: Width = " + width + ", Height = " +

height);

// Main class to test the implementation

class ResizeDemo {

public static void main(String[] args) {

// Creating a Rectangle object

Rectangle rectangle = new Rectangle(10, 5);

// Displaying the original information

System.out.println("Original Rectangle Info:");

rectangle.displayInfo();

// Resizing the rectangle

rectangle.resizeWidth(15);
rectangle.resizeHeight(8);

// Displaying the updated information

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

rectangle.displayInfo();

----------------------------------
Program 8: Develop a JAVA program to create an outer class with a function display.
Create another class inside the outer class named inner with a function called display
and call the two functions in the main class.

class Outer {

void display() {

System.out.println("Outer class display method");

class Inner {

void display() {

System.out.println("Inner class display method");

}}

class OuterInnerDemo {

public static void main(String[] args) {

// Create an instance of the Outer class

Outer outer = new Outer();

// Call the display method of the Outer class

outer.display();

// Create an instance of the Inner class (nested inside Outer)

Outer.Inner inner = outer.new Inner();

// Call the display method of the Inner class

inner.display();

}}
Program 9: Develop a JAVA program to raise a custom exception (user defined
exception) for DivisionByZero using try, catch, throw and finally.

// Custom exception class

class DivisionByZeroException extends Exception {

public DivisionByZeroException(String message) {

super(message);

class CustomExceptionDemo {

/*Method to perform division and throw custom exception if denominator is zero*/

static double divide(int numerator, int denominator) throws

DivisionByZeroException {

if (denominator == 0) {

throw new DivisionByZeroException("Cannot divide by zero!");

return (double) numerator / denominator;

public static void main(String[] args) {

int numerator = 10;

int denominator = 0;

try {

double result = divide(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");

You might also like