Java Practical Programs
Java Practical Programs
AIM
To implement method overloading using static methods in Java.
PROGRAM
class StaticOverload {
static void show(int a) {
System.out.println("Integer: " + a);
}
static void show(String a) {
System.out.println("String: " + a);
}
static void show(int a, double b) {
System.out.println("Integer and Double: " + a + ", " + b);
}
public static void main(String[] args) {
show(10);
show("Hello");
show(5, 2.5);
}
}
OUTPUT
Integer: 10
String: Hello
Integer and Double: 5, 2.5
CONCLUSION
The program successfully demonstrates method overloading using static methods in Java.
AIM
To implement method overloading within a single class in Java.
PROGRAM
class OverloadDemo {
void show(int a) {
System.out.println("Integer: " + a);
}
void show(double a) {
System.out.println("Double: " + a);
}
void show(String a) {
System.out.println("String: " + a);
}
public static void main(String[] args) {
OverloadDemo obj = new OverloadDemo();
obj.show(100);
obj.show(3.14);
obj.show("Java");
}
}
OUTPUT
Integer: 100
Double: 3.14
String: Java
CONCLUSION
The program demonstrates method overloading using multiple methods with different
parameter types in the same class.
3. Interface Implementation
AIM
To implement an interface in Java.
PROGRAM
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Dog barks");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Dog d = new Dog();
d.makeSound();
}
}
OUTPUT
Dog barks
CONCLUSION
The program successfully implements an interface and overrides its method in a class.
AIM
To implement multithreading by extending the Thread class in Java.
PROGRAM
OUTPUT
Thread 1: 1
Thread 2: 1
Thread 1: 2
Thread 2: 2
...
CONCLUSION
The program demonstrates multithreading by creating multiple threads using the Thread
class.
AIM
To implement a Java application with three independent threads.
PROGRAM
OUTPUT
Thread One is running
Thread Two is running
Thread Three is running
CONCLUSION
The program demonstrates a multi-threaded application with three independent threads
executing simultaneously.
AIM
To implement a multi-threaded Java application where one thread generates random
integers, and other threads compute square or cube based on the number’s parity.
PROGRAM
import java.util.Random;
class NumberGenerator extends Thread {
public void run() {
Random rand = new Random();
while (true) {
int num = rand.nextInt(100);
System.out.println("Generated: " + num);
if (num % 2 == 0) {
new Square(num).start();
} else {
new Cube(num).start();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
class Square extends Thread {
int num;
Square(int num) {
this.num = num;
}
public void run() {
System.out.println("Square: " + (num * num));
}
}
class Cube extends Thread {
int num;
Cube(int num) {
this.num = num;
}
public void run() {
System.out.println("Cube: " + (num * num * num));
}
}
public class RandomThreadApp {
public static void main(String[] args) {
NumberGenerator t = new NumberGenerator();
t.start();
}
}
OUTPUT
Generated: 23
Cube: 12167
Generated: 42
Square: 1764
...
CONCLUSION
The program successfully demonstrates multithreading with conditional execution based
on the random number’s parity.