0% found this document useful (0 votes)
3 views

Java Practical Programs

The document contains several Java practical programs demonstrating key concepts such as method overloading, interface implementation, and multithreading. Each program includes an aim, code implementation, output, and conclusion summarizing the results. Key examples include method overloading with static methods and within a single class, as well as multithreading applications with independent threads and conditional execution based on random numbers.

Uploaded by

Divyanshi
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)
3 views

Java Practical Programs

The document contains several Java practical programs demonstrating key concepts such as method overloading, interface implementation, and multithreading. Each program includes an aim, code implementation, output, and conclusion summarizing the results. Key examples include method overloading with static methods and within a single class, as well as multithreading applications with independent threads and conditional execution based on random numbers.

Uploaded by

Divyanshi
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

Java Practical Programs

1. Method Overloading using Static Methods

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.

2. Method Overloading in a Single Class

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.

4. Multithreading using Thread Class

AIM
To implement multithreading by extending the Thread class in Java.

PROGRAM

class SimpleThread extends Thread {


public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + ": " +
i);
}
}
public static void main(String[] args) {
SimpleThread t1 = new SimpleThread();
SimpleThread t2 = new SimpleThread();
t1.setName("Thread 1");
t2.setName("Thread 2");
t1.start();
t2.start();
}
}

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.

5. Multi-thread Application with Three Threads

AIM
To implement a Java application with three independent threads.

PROGRAM

class ThreadOne extends Thread {


public void run() {
System.out.println("Thread One is running");
}
}
class ThreadTwo extends Thread {
public void run() {
System.out.println("Thread Two is running");
}
}
class ThreadThree extends Thread {
public void run() {
System.out.println("Thread Three is running");
}
}
public class ThreeThreadApp {
public static void main(String[] args) {
ThreadOne t1 = new ThreadOne();
ThreadTwo t2 = new ThreadTwo();
ThreadThree t3 = new ThreadThree();
t1.start();
t2.start();
t3.start();
}
}

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.

6. Random Number - Square or Cube with Threads

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy