Java Programs 7 to 12
Java Programs 7 to 12
Java Code
// Resizable interface
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
@Override
public void resizeHeight(int height) {
this.height = height;
System.out.println("Resized height to: " + height);
}
Output
$ java ResizeDemo
Original Rectangle Info:
Rectangle: Width = 10, Height = 5
Resized width to: 15
Resized height to: 8
Java Code
class Outer {
void display() {
System.out.println("Outer class display method");
}
class Inner {
void display() {
System.out.println("Inner class display method");
}
}
}
Output
$ java OuterInnerDemo
Outer class display method
Inner class display method
Java Code
// Custom exception class
class DivisionByZeroException extends Exception {
public DivisionByZeroException(String message) {
super(message);
}
}
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");
}
}
}
In this program:
The DivisionByZeroException class is a custom exception that
extends the Exception class.
The divide method performs division and throws the custom exception if
the denominator is zero.
In the main method, we attempt to divide and catch the custom exception
if it occurs. The finally block is used for code that must be executed,
whether an exception is thrown or not.
When you run this program with a denominator of 0, it will throw the
DivisionByZeroException, catch it, print the error message, and then
execute the finally block.
Output
$ java CustomExceptionDemo
Exception caught: Cannot divide by zero!
Finally block executed
Program 10 : Packages
Java Code
Package mypack
// Inside a folder named 'mypack'
package mypack;
javac mypack/MyPackageClass.java
javac PackageDemo.java
Output
$ java PackageDemo
Hello from MyPackageClass in mypack package!
Result of adding numbers: 8
Java Code
class MyRunnable implements Runnable {
private volatile boolean running = true;
@Override
@SuppressWarnings("deprecation")
public void run() {
while (running) {
try {
// Suppress deprecation warning for Thread.sleep()
Thread.sleep(500);
System.out.println("Thread ID: " + Thread.currentThread().getId() + "
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
}
}
}
Output
$ java RunnableThreadExample
Thread ID: 24 is running.
Thread ID: 21 is running.
Thread ID: 20 is running.
Thread ID: 23 is running.
Thread ID: 22 is running.
Java Code
// The run method that will be executed when the thread starts
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " Count: " + i);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " Thread interru
}
}
}
}
// Main thread
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " Thread Count: " +
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " Thread interru
}
}
}
}
In this program:
The MyThread class extends Thread.
The constructor of MyThread calls the base class constructor using
super(name) to set the thread’s name and starts the thread.
The run method is overridden and contains a loop to print counts. The
thread sleeps for 500 milliseconds in each iteration.
In the main method, an instance of MyThread is created, which starts the
child thread concurrently.
The main thread also prints counts and sleeps for 500 milliseconds in each
iteration.
When you run this program, you’ll observe that both the main thread and
the child thread are executed concurrently, and their outputs may be
interleaved.
Output
$ java ThreadConcurrentExample
main Thread Count: 1