100% found this document useful (1 vote)
172 views16 pages

Cs25c09 Java Programming Lab Manual

The document outlines a series of Java programming experiments demonstrating various concepts including constructors, inheritance, interfaces, user-defined exceptions, string handling, event handling, file I/O, Swing applications, JDBC database connections, and the use of ArrayList and HashMap. Each experiment includes an aim, algorithm, program code, output, and result indicating successful implementation. Overall, the document serves as a practical guide for understanding fundamental Java programming techniques.

Uploaded by

dharani.m
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
100% found this document useful (1 vote)
172 views16 pages

Cs25c09 Java Programming Lab Manual

The document outlines a series of Java programming experiments demonstrating various concepts including constructors, inheritance, interfaces, user-defined exceptions, string handling, event handling, file I/O, Swing applications, JDBC database connections, and the use of ArrayList and HashMap. Each experiment includes an aim, algorithm, program code, output, and result indicating successful implementation. Overall, the document serves as a practical guide for understanding fundamental Java programming techniques.

Uploaded by

dharani.m
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

EXP NO: 1

CONSTRUCTORS AND STATIC MEMBERS


Date:

Aim

To demonstrate the use of constructors and static members in Java.


.
Algorithm
1. Create a class Student.
2. Declare static variable college.
3. Create a constructor to initialize student details.
4. Display student information.

PROGRAM:

class Student {
int id;
String name;
static String college = "ABC Engineering College";
Student(int i, String n) {
id = i;
name = n;
}
void display() {
[Link](id + " " + name + " " + college);
}
public static void main (String args[]) {
Student s1 = new Student (101, "Arun");
Student s2 = new Student (102, "Babu");

[Link]();
[Link]();
}}
OUTPUT

101 Arun ABC Engineering College


102 Babu ABC Engineering College

RESULT:

Thus, the use of constructors and static members was successfully demonstrated.
EXP NO: 2
INHERITANCE
Date:

Aim

To implement single inheritance in Java.

Algorithm

1. Create superclass Animal.


2. Create subclass Dog.
3. Inherit methods.
4. Display inherited behaviour.

PROGRAM:
class Animal {
void eat() {
[Link]("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog is barking");
}
public static void main(String args[]) {
Dog d = new Dog();
[Link]();
[Link]();
}}

OUTPUT
Animal is eating
Dog is barking

RESULT: Thus, inheritance was implemented successfully.


EXP NO:3
PACKAGES AND INTERFACES
Date:

Aim
To implement interfaces in Java.

Algorithm

1. Create an interface.

2. Implement the interface.

3. Call interface method.

PROGRAM:

interface Shape {
void area();
}

class Circle implements Shape {


public void area() {
double r = 5;
[Link]("Area = " + (3.14 * r * r));
}

public static void main(String args[]) {


Circle c = new Circle();
[Link]();
}
}

OUTPUT
Area = 78.5

RESULT:

Thus, interface implementation was demonstrated.


EXP NO:4

Date: USER-DEFINED EXCEPTION AND MULTITHREADING

AIM:

To create a user-defined exception and multiple threads

ALGORITHM:
1. Create custom exception.
2. Throw exception.
3. Create two threads.
4. Execute simultaneously.

PROGRAM:
class MyException
extends Exception {
MyException(String s)
{
super(s);
}
}
class ThreadDemo
extends Thread {
public void run() {
[Link]("Thread Running");
}
public static void main(String args[]) {
ThreadDemo t1 = new ThreadDemo();
ThreadDemo t2 = new ThreadDemo();
[Link]();
[Link]();

try {
throw new MyException("Custom Exception");
} catch (Exception e) {
[Link](e);
}
}
}

OUTPUT

Thread Running
Thread Running
My Exception: Custom Exception

RESULT:
Thus, user-defined exception and multithreading were implemented successfully.
EXP NO:5

Date: STRING HANDLING

AIM :

To perform string operations.

ALGORITHM:

1. Create string.
2. Find length.
3. Convert to uppercase.
4. Display substring.

PROGRAM:

public class StringDemo

public static void main(String args[]) {

String s = "Java Programming";

[Link]("Length = " + [Link]());

[Link]([Link]());

[Link]([Link](5));

OUTPUT
Length = 16
JAVA PROGRAMMING
Programming

RESULT:
Thus, string handling operations were performed successfully. Programming
EXP NO:6
EVENT HANDLING
Date:

AIM :

To implement button click event using AWT.

ALGORITHM:

1. Create Frame.
2. Add Button.
3. Register ActionListener.
4. Display message on click.

PROGRAM:

import [Link].*;

import [Link].*;

class EventDemo extends Frame implements ActionListener {

Button b;

EventDemo() {

b = new Button("Click");

[Link](100,100,80,30);

add(b);

[Link](this);

setSize(300,300);

setLayout(null);

setVisible(true);

}
public void actionPerformed(ActionEvent e) {

[Link]("Button Clicked");

public static void main(String args[]) {

new EventDemo();

OUTPUT:

Button Clicked

RESULT:

Thus, event handling using AWT was implemented successfully.


EXP NO:7
FILE I/O
Date:

AIM :

To write and read data from a file

ALGORITHM:
1. Create file.
2. Write text.
3. Read text.
4. Display output.
Program:

import [Link].*;

public class FileDemo {

public static void main(String args[]) throws Exception {

FileWriter fw = new FileWriter("[Link]");

[Link]("Welcome to Java");

[Link]();

FileReader fr = new FileReader("[Link]");

int ch;

while((ch = [Link]()) != -1)

[Link]((char)ch);

[Link]();

}}

OUTPUT:

Welcome to Java

RESULT:

Thus, file input and output operations were performed successfully.


EXP NO:8

Date:
SWING APPLICATION

AIM:

To create a simple Swing application.

ALGORITHM:
1. Create JFrame.
2. Add JLabel.
3. Display frame.

PROGRAM:

import [Link].*;

public class SwingDemo {

public static void main(String args[]) {

JFrame f = new JFrame("Swing Example");

JLabel l = new JLabel("Welcome to Java Swing");

[Link](60,80,200,30);

[Link](l);

[Link](300,300);

[Link](null);

[Link](true);

}
OUTPUT:

A Swing window displaying:


Welcome to Java Swing

RESULT:

Thus, the Swing application was created successfully.


EXP NO: 9

Date: JDBC DATABASE CONNECTION

AIM:

To connect Java with MySQL database using JDBC.

ALGORITHM:
1. Load JDBC driver.
2. Establish database connection.
3. Execute SQL query.
4. Display records.
5. Close connection.

PROGRAM:
import [Link].*;

public class JDBCExample {

public static void main(String args[]) throws Exception {

[Link]("[Link]");

Connection con = [Link](

"jdbc:mysql://localhost:3306/studentdb",

"root",

"root");

Statement st = [Link]();

ResultSet rs = [Link]("SELECT * FROM student");

while([Link]()) {

[Link]([Link](1) + " " + [Link](2));

[Link]();

}
OUTPUT:

101 Arun
102 Babu
103 Kumar

RESULT:

Thus, JDBC database connectivity and retrieval of records were implemented successfully.
EXP NO: 10

ARRAYLIST AND MAP INTERFACE


Date:

AIM:

To demonstrate the use of ArrayList and HashMap.

ALGORITHM:

1. Create an Array List.


2. Add elements.
3. Display list.
4. Create HashMap.
5. Store key-value pairs.
6. Display map.

PROGRAM:
import [Link].*;

public class CollectionDemo {

public static void main(String args[]) {

ArrayList<String> list = new ArrayList<>();

[Link]("Java");

[Link]("Python");

[Link]("C++");

[Link](list);
HashMap<Integer,String> map = new HashMap<>();

[Link](101,"Arun");

[Link](102,"Babu");

[Link](map);

OUTPUT:

[Java, Python, C++]


{101=Arun, 102=Babu}

RESULT:

Thus, Array List and HashMap operations were implemented successfully.

You might also like