0% found this document useful (0 votes)
2 views5 pages

Java Lab Manual Full Code

The document is a Java Lab Manual containing ten programming exercises, each demonstrating different Java concepts such as classes, inheritance, exception handling, multithreading, file handling, array operations, constructors, interfaces, method overloading and overriding, and collections. Each program includes the objective, code, and expected output. This manual serves as a practical guide for learning Java programming through hands-on examples.

Uploaded by

inbox.ayesha1989
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)
2 views5 pages

Java Lab Manual Full Code

The document is a Java Lab Manual containing ten programming exercises, each demonstrating different Java concepts such as classes, inheritance, exception handling, multithreading, file handling, array operations, constructors, interfaces, method overloading and overriding, and collections. Each program includes the objective, code, and expected output. This manual serves as a practical guide for learning Java programming through hands-on examples.

Uploaded by

inbox.ayesha1989
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/ 5

Java Lab Manual (With Full Code and

Output)
Program 1: Class and Object Demo
Objective: Demonstrate use of class and object.

Code:

class Demo {
void show() {
System.out.println("Hello from object!");
}

public static void main(String[] args) {


Demo d = new Demo();
d.show();
}
}

Expected Output:
Hello from object!

Program 2: Inheritance Example


Objective: Demonstrate single inheritance.

Code:

class A {
void msg() {
System.out.println("Hello from A");
}
}
class B extends A {
public static void main(String args[]) {
B obj = new B();
obj.msg();
}
}
Expected Output:
Hello from A

Program 3: Exception Handling


Objective: Handle arithmetic exception.

Code:

public class Main {


public static void main(String[] args) {
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Exception caught");
}
}
}

Expected Output:
Exception caught

Program 4: Multithreading
Objective: Create and run a thread.

Code:

class MyThread extends Thread {


public void run() {
System.out.println("Thread running");
}

public static void main(String[] args) {


MyThread t = new MyThread();
t.start();
}
}

Expected Output:
Thread running
Program 5: File Handling
Objective: Write to a file using FileWriter.

Code:

import java.io.*;

class FileWrite {
public static void main(String args[]) throws IOException {
FileWriter fw = new FileWriter("test.txt");
fw.write("Hello File");
fw.close();
System.out.println("File written");
}
}

Expected Output:
File written

Program 6: Array Operations


Objective: Find maximum number in an array.

Code:

public class MaxInArray {


public static void main(String[] args) {
int[] arr = {2, 10, 5};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
System.out.println("Max: " + max);
}
}

Expected Output:
Max: 10

Program 7: Constructor Usage


Objective: Demonstrate constructor invocation.
Code:

class Cons {
Cons() {
System.out.println("Constructor called");
}

public static void main(String[] args) {


new Cons();
}
}

Expected Output:
Constructor called

Program 8: Interface Implementation


Objective: Implement interface methods.

Code:

interface Animal {
void sound();
}

class Dog implements Animal {


public void sound() {
System.out.println("Bark");
}

public static void main(String[] args) {


new Dog().sound();
}
}

Expected Output:
Bark

Program 9: Overloading and Overriding


Objective: Demonstrate method overriding.

Code:
class A {
void show() {
System.out.println("From A");
}
}

class B extends A {
void show() {
System.out.println("From B");
}

public static void main(String[] args) {


B obj = new B();
obj.show();
}
}

Expected Output:
From B

Program 10: Collections Usage


Objective: Use ArrayList from Java Collections.

Code:

import java.util.*;

class Example {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1); list.add(2); list.add(3);
System.out.println("List: " + list);
}
}

Expected Output:
List: [1, 2, 3]

You might also like