0% found this document useful (0 votes)
21 views16 pages

Java Programming Lab: 15 Experiments

The document contains 15 Java programming experiments, each with code examples and sample outputs. Topics covered include variable scope, class and object creation, constructors, inheritance, polymorphism, interfaces, exception handling, multithreading, string manipulation, file I/O, inter-thread communication, event handling, and task management. Each experiment demonstrates key programming concepts through practical examples.

Uploaded by

asharfnaser123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views16 pages

Java Programming Lab: 15 Experiments

The document contains 15 Java programming experiments, each with code examples and sample outputs. Topics covered include variable scope, class and object creation, constructors, inheritance, polymorphism, interfaces, exception handling, multithreading, string manipulation, file I/O, inter-thread communication, event handling, and task management. Each experiment demonstrates key programming concepts through practical examples.

Uploaded by

asharfnaser123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programming Lab - Code and Output

Contains 15 Experiments with Code and Sample Output


Experiment 1: Scope of Variables and Types
Program:
class ScopeDemo {
static int staticVar = 10;
int instanceVar = 20;

void methodDemo() {
int localVar = 30;
[Link]("Inside method - localVar = " + localVar);
[Link]("Inside method - instanceVar = " + instanceVar);
[Link]("Inside method - staticVar = " + staticVar);
}

public static void main(String[] args) {


ScopeDemo obj = new ScopeDemo();
[Link]();
[Link]("From main - instanceVar = " + [Link]);
[Link]("From main - staticVar = " + [Link]);
}
}

Sample Output:
Inside method - localVar = 30
Inside method - instanceVar = 20
Inside method - staticVar = 10
From main - instanceVar = 20
From main - staticVar = 10
Experiment 2: Class and Object Creation
Program:
class Student {
String name;
int roll;

Student(String name, int roll) {


[Link] = name;
[Link] = roll;
}

void display() {
[Link]("Name: " + name + ", Roll: " + roll);
}

public static void main(String[] args) {


Student s1 = new Student("Asha", 101);
Student s2 = new Student("Rahul", 102);
[Link]();
[Link]();
}
}

Sample Output:
Name: Asha, Roll: 101
Name: Rahul, Roll: 102
Experiment 3: Constructors and Constructor Overloading
Program:
class Book {
String title;
double price;

Book() {
[Link] = "Unknown";
[Link] = 0.0;
}

Book(String title, double price) {


[Link] = title;
[Link] = price;
}

void display() {
[Link]("Title: " + title + ", Price: " + price);
}

public static void main(String[] args) {


Book b1 = new Book();
Book b2 = new Book("Java Basics", 499.50);
[Link]();
[Link]();
}
}

Sample Output:
Title: Unknown, Price: 0.0
Title: Java Basics, Price: 499.5
Experiment 4: Methods and Method Overloading
Program:
class MathOps {
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}

public static void main(String[] args) {


MathOps m = new MathOps();
[Link]([Link](2, 3));
[Link]([Link](2.5, 3.5));
[Link]([Link](1, 2, 3));
}
}

Sample Output:
5
6.0
6
Experiment 5: Inheritance and Super Keyword
Program:
class Animal {
String type = "Animal";
Animal() {
[Link]("Animal created");
}
void showType() {
[Link]("Type: " + type);
}
}

class Dog extends Animal {


String type = "Dog";
Dog() {
super();
[Link]("Dog created");
}
void showType() {
[Link]();
[Link]("Type in Dog: " + type);
}

public static void main(String[] args) {


Dog d = new Dog();
[Link]();
}
}

Sample Output:
Animal created
Dog created
Type: Animal
Type in Dog: Dog
Experiment 6: Polymorphism and Method Overriding
Program:
class Vehicle {
void run() {
[Link]("Vehicle is running");
}
}

class Car extends Vehicle {


void run() {
[Link]("Car is running");
}

public static void main(String[] args) {


Vehicle v = new Car();
[Link]();
}
}

Sample Output:
Car is running
Experiment 7: Interfaces and Multiple Inheritance
Program:
interface Printable {
void print();
}

interface Showable {
void show();
}

class Demo implements Printable, Showable {


public void print() {
[Link]("Print method");
}
public void show() {
[Link]("Show method");
}

public static void main(String[] args) {


Demo d = new Demo();
[Link]();
[Link]();
}
}

Sample Output:
Print method
Show method
Experiment 8: Exception Handling and Finally Block
Program:
class ExceptionDemo {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Caught Exception: " + [Link]());
} finally {
[Link]("Finally block executed");
}
}
}

Sample Output:
Caught Exception: / by zero
Finally block executed
Experiment 9: Multithreading
Program:
class MyThread extends Thread {
String name;
MyThread(String n) { name = n; }

public void run() {


for (int i = 1; i <= 3; i++) {
[Link](name + " - " + i);
}
}

public static void main(String[] args) {


MyThread t1 = new MyThread("T1");
MyThread t2 = new MyThread("T2");
[Link]();
[Link]();
}
}

Sample Output:
T1 - 1
T2 - 1
T1 - 2
T2 - 2
T1 - 3
T2 - 3
Experiment 10: String Manipulation
Program:
class StringDemo {
public static void main(String[] args) {
String s = "Hello Java";
[Link]("Length: " + [Link]());
[Link]("Upper: " + [Link]());
[Link]("Substring(6): " + [Link](6));
[Link]("Concat: " + [Link](" World"));
[Link]("Equals 'Hello Java': " + [Link]("Hello Java"));
}
}

Sample Output:
Length: 10
Upper: HELLO JAVA
Substring(6): Java
Concat: Hello Java World
Equals 'Hello Java': true
Experiment 11: Packages
Program:
package mypack;
public class HelloPackage {
public static void main(String[] args) {
[Link]("This is inside mypack package");
}
}

Sample Output:
This is inside mypack package
Experiment 12: File I/O Operations
Program:
import [Link].*;
class FileDemo {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello File Handling in Java");
[Link]();

BufferedReader br = new BufferedReader(new FileReader("[Link]"));


[Link]("File Content: " + [Link]());
[Link]();
} catch (Exception e) {
[Link](e);
}
}
}

Sample Output:
File Content: Hello File Handling in Java
Experiment 13: Inter-thread Communication
Program:
class Customer {
int amount = 10000;

synchronized void withdraw(int amount) {


[Link]("Going to withdraw...");
if ([Link] < amount) {
[Link]("Less balance; waiting for deposit...");
try { wait(); } catch (Exception e) {}
}
[Link] -= amount;
[Link]("Withdraw completed! Remaining balance: " + [Link]);
}

synchronized void deposit(int amount) {


[Link]("Going to deposit...");
[Link] += amount;
[Link]("Deposit completed! New balance: " + [Link]);
notify();
}
}

class Test {
public static void main(String[] args) {
Customer c = new Customer();
new Thread(() -> [Link](15000)).start();
new Thread(() -> [Link](10000)).start();
}
}

Sample Output:
Going to withdraw...
Less balance; waiting for deposit...
Going to deposit...
Deposit completed! New balance: 20000
Withdraw completed! Remaining balance: 5000
Experiment 14: Event Handling (Delegation Event Model)
Program:
import [Link].*;
import [Link].*;

class ButtonExample {
public static void main(String[] args) {
Frame f = new Frame("Button Example");
Button b = new Button("Click Me");
[Link](50, 100, 80, 30);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked!");
}
});
[Link](b);
[Link](200, 200);
[Link](null);
[Link](true);
}
}

Sample Output:
(On clicking the button) → Button Clicked!
Experiment 15: Simple Task Management System
Program:
import [Link].*;
class TaskManager {
public static void main(String[] args) {
List<String> tasks = new ArrayList<>();
[Link]("Complete assignment");
[Link]("Attend Java lecture");
[Link]("Submit project");

[Link]("All Tasks:");
for (String t : tasks) {
[Link]("- " + t);
}
[Link]("Marking first task completed...");
[Link](0);
[Link]("Remaining Tasks: " + tasks);
}
}

Sample Output:
All Tasks:
- Complete assignment
- Attend Java lecture
- Submit project
Marking first task completed...
Remaining Tasks: [Attend Java lecture, Submit project]

You might also like