7---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;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
@Override
public void resizeWidth(int width) {
this.width = width;
}
@Override
public void resizeHeight(int height) {
this.height = height;
}
public void display() {
System.out.println("Rectangle - Width: " + width + ", Height: " + height);
}
}
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 10);
System.out.println("Initial state:");
rectangle.display();
rectangle.resizeWidth(8);
rectangle.resizeHeight(12);
System.out.println("\nResized state:");
rectangle.display();
}
}
8---class Outer {
void display() {
System.out.println("Outer class display method");
}
class Inner {
void display() {
System.out.println("Inner class display method");
}
}
}
public class Main {
public static void main(String[] args) {
Outer outerObject = new Outer();
outerObject.display();
Outer.Inner innerObject = outerObject.new Inner();
innerObject.display();
}
}
9---class DivisionByZeroException extends Exception {
public DivisionByZeroException(String message) {
super(message);
}
}
class Calculator {
public static double divide(int numerator, int denominator) throws DivisionByZeroException {
try {
if (denominator == 0) {
throw new DivisionByZeroException("Division by zero is not allowed.");
}
return (double) numerator / denominator;
} catch (DivisionByZeroException e) {
System.out.println("Exception: " + e.getMessage());
throw e;
} finally {
System.out.println("Finally block executed");
}
}
}
public class Main {
public static void main(String[] args) {
try {
double result = Calculator.divide(10, 2);
System.out.println("Result of division: " + result);
result = Calculator.divide(5, 0); /
System.out.println("Result of division: " + result);
} catch (DivisionByZeroException e) {
System.out.println("Main Exception: " + e.getMessage());
}
}
}
10— package mypack;
public class Mypackage{
public void display(){
System.out.println("Hello from Mypackage " );
}
}
import mypack.Mypackage;
public class Mypackdemo{
public static void main(String[] args) {
Mypackage m = new MyPackage();
m.display();
}
}
11---class MyRunnable implements Runnable{
@Override
public void run(){
try{
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName()+":"+i);
Thread.sleep(500);
}
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+"interrupted.");
}
}
}
public class ThreadCreationWithRunnable {
public static void main(String[] args){
System.out.println("Main thread started.");
Thread thread1=new Thread(new MyRunnable(),"Thread 1");
Thread thread2=new Thread(new MyRunnable(),"Thread 2");
Thread thread3=new Thread(new MyRunnable(),"Thread 3");
thread1.start();
thread2.start();
thread3.start();
try{
thread1.join();
thread2.join();
thread3.join();
}
catch(InterruptedException e){
System.out.println("Main thread interrupted while waiting for other threads to complete");
}
System.out.println("Main thread exiting");
}
}
12-- class MyThread extends Thread{
MyThread(){
super(“Child Thread”);
System.out.println("Child thread " + this);
start();
}
public void run(){
try{
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName()+":"+i);
Thread.sleep(500);
}
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+"interrupted.");
}
}
public class Main{
public static void main(String[] args) {
new Mythread();
try{
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName()+":"+i);
Thread.sleep(500);
}
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+"interrupted.");
}}