JAVA PRACTICAL PROGRAMS WITH OUTPUT
LIST OF PRACTICALS
1. Program to define structure of a basic JAVA program
2. Program to define the data types, variables, operators, arrays and control structures.
3. Program to define classes and constructors. Demonstrate constructors.
4. Program to define class, methods and objects.
5. Program to demonstrate method overloading.
6. Program to define inheritance and show method overriding.
7. Program to demonstrate Packages.
8. Program to demonstrate Exception Handling.
9. Program to demonstrate Multithreading.
10. Program to demonstrate I/O operations.
11. Program to demonstrate Network Programming.
12. Program to demonstrate Layout managers.
1. Basic Structure of a Java Program
public class BasicStructure {
public static void main(String[] args) {
[Link]("Hello, Java!");
}
}
Output:
Hello, Java!
2. Data Types, Variables, Operators, Arrays, Control Structures
public class DataTypesDemo {
public static void main(String[] args) {
int a = 10, b = 5;
int sum = a + b;
int arr[] = {1, 2, 3};
if (sum > 10) {
[Link]("Sum is greater than 10");
}
for (int i : arr) {
[Link](i);
}
}
}
Output:
Sum is greater than 10
1
2
3
3. Classes and Constructors
class Student {
String name;
Student() {
name = "Default";
}
Student(String n) {
name = n;
}
void display() {
[Link]("Name: " + name);
}
}
public class ConstructorDemo {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Adesh");
[Link]();
[Link]();
}
}
Output:
Name: Default
Name: Adesh
4. Class, Methods and Objects
class Calculator {
void add(int a, int b) {
[Link]("Sum: " + (a + b));
}
}
public class MethodDemo {
public static void main(String[] args) {
Calculator c = new Calculator();
[Link](4, 6);
}
}
Output:
Sum: 10
5. Method Overloading
class Overload {
void add(int a, int b) {
[Link](a + b);
}
void add(double a, double b) {
[Link](a + b);
}
}
public class OverloadingDemo {
public static void main(String[] args) {
Overload obj = new Overload();
[Link](2, 3);
[Link](2.5, 3.5);
}
}
Output:
5
6.0
6. Inheritance and Method Overriding
class Animal {
void sound() {
[Link]("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
public class InheritanceDemo {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
}
}
Output:
Dog barks
7. Packages
package mypack;
public class PackageDemo {
public void show() {
[Link]("This is a package demo");
}
}
import [Link];
public class TestPackage {
public static void main(String[] args) {
PackageDemo obj = new PackageDemo();
[Link]();
}
}
Output:
This is a package demo
8. Exception Handling
public class ExceptionDemo {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Error: Division by zero");
}
}
}
Output:
Error: Division by zero
9. Multithreading
class MyThread extends Thread {
public void run() {
[Link]("Thread is running");
}
}
public class ThreadDemo {
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}
Output:
Thread is running
10. I/O Operations
import [Link].*;
public class IODemo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter name: ");
String name = [Link]();
[Link]("Hello " + name);
}
}
Sample Output:
Enter name: Adesh
Hello Adesh
11. Network Programming
import [Link].*;
public class InetDemo {
public static void main(String[] args) throws Exception {
InetAddress ip = [Link]("[Link]");
[Link]("IP Address: " + [Link]());
}
}
Output:
IP Address: [Link]
12. Layout Managers
import [Link].*;
public class FlowLayoutDemo {
Frame f;
FlowLayoutDemo() {
f = new Frame();
[Link](new FlowLayout());
[Link](new Button("Button 1"));
[Link](new Button("Button 2"));
[Link](300, 200);
[Link](true);
}
public static void main(String[] args) {
new FlowLayoutDemo();
}
}
Output:
A window opens with two buttons arranged using FlowLayout.