0% found this document useful (0 votes)
3 views6 pages

Adv Java Practical

The document outlines practical programs for an Advanced Java Programming course, including creating threads, inter-thread communication, file handling, GUI development, and database connectivity. Each section contains a program example with code and expected output. The submission date for the practical programs is set for March 6, 2026.

Uploaded by

abpro86000
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)
3 views6 pages

Adv Java Practical

The document outlines practical programs for an Advanced Java Programming course, including creating threads, inter-thread communication, file handling, GUI development, and database connectivity. Each section contains a program example with code and expected output. The submission date for the practical programs is set for March 6, 2026.

Uploaded by

abpro86000
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

Advanced Java Programming

4th Semester – Practical Programs

Q1. Thread by Extending Thread Class


Create a thread by extending the Thread class. Print "Hello from Thread" five times.

Program:
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("Hello from Thread");
}
}
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}

Output:
Hello from Thread
Hello from Thread
Hello from Thread
Hello from Thread
Hello from Thread

Q2. Thread by Implementing Runnable Interface


Create a thread by implementing the Runnable interface. Print "Runnable Thread Running" five times.

Program:
class MyRunnable implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("Runnable Thread Running");
}
}
public static void main(String[] args) {
MyRunnable obj = new MyRunnable();
Thread t = new Thread(obj);
[Link]();
}
}

Output:
Runnable Thread Running
Runnable Thread Running
Runnable Thread Running
Runnable Thread Running
Runnable Thread Running

Q3. Thread Priority


Set and get thread priority for two simple threads. Print the priority of each thread.

Program:
class ThreadPriority extends Thread {
public void run() {
[Link]([Link]().getName()
+ " Priority: " + [Link]().getPriority());
}
public static void main(String[] args) {
ThreadPriority t1 = new ThreadPriority();
ThreadPriority t2 = new ThreadPriority();

[Link]("Thread-1");
[Link]("Thread-2");

[Link](Thread.MIN_PRIORITY); // Priority = 1
[Link](Thread.MAX_PRIORITY); // Priority = 10

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

Output:
Thread-1 Priority: 1
Thread-2 Priority: 10

Q4. Inter-Thread Communication using wait() and notify()


Demonstrate inter-thread communication using wait() and notify().

Program:
class SharedResource {
int data;
boolean valueSet = false;

synchronized void produce(int value) {


while (valueSet) {
try { wait(); } catch (InterruptedException e) {}
}
data = value;
[Link]("Produced: " + data);
valueSet = true;
notify();
}

synchronized void consume() {


while (!valueSet) {
try { wait(); } catch (InterruptedException e) {}
}
[Link]("Consumed: " + data);
valueSet = false;
notify();
}
}

class Producer extends Thread {


SharedResource res;
Producer(SharedResource res) { [Link] = res; }
public void run() {
for (int i = 1; i <= 3; i++) [Link](i);
}
}

class Consumer extends Thread {


SharedResource res;
Consumer(SharedResource res) { [Link] = res; }
public void run() {
for (int i = 1; i <= 3; i++) [Link]();
}
}

public class InterThreadDemo {


public static void main(String[] args) {
SharedResource res = new SharedResource();
new Producer(res).start();
new Consumer(res).start();
}
}

Output:
Produced: 1
Consumed: 1
Produced: 2
Consumed: 2
Produced: 3
Consumed: 3

Q5. Write Text to File using FileWriter


Write some text into a file using FileWriter.

Program:
import [Link];
import [Link];

public class FileWriterDemo {


public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello, this is written using FileWriter.\n");
[Link]("Advanced Java Programming - 4th Semester.\n");
[Link]();
[Link]("Data written to file successfully.");
} catch (IOException e) {
[Link]("Error: " + [Link]());
}
}
}

Output:
Data written to file successfully.

(File '[Link]' is created with the following content:)


Hello, this is written using FileWriter.
Advanced Java Programming - 4th Semester.
Q6. File Properties using File Class
Display file properties: File name, Absolute path, Writable, Readable, File size.

Program:
import [Link];

public class FileProperties {


public static void main(String[] args) {
File f = new File("[Link]");
[Link]("File Name : " + [Link]());
[Link]("Absolute Path : " + [Link]());
[Link]("Writable : " + [Link]());
[Link]("Readable : " + [Link]());
[Link]("File Size : " + [Link]() + " bytes");
}
}

Output:
File Name : [Link]
Absolute Path : C:\Users\Student\[Link]
Writable : true
Readable : true
File Size : 75 bytes

Q7. Swing GUI – JTextField, JButton, JCheckBox, JRadioButton


Create a Swing GUI with a JTextField, JButton, two JCheckBoxes, and two JRadioButtons in a
ButtonGroup to display user input on button click.

Program:
import [Link].*;
import [Link].*;
import [Link].*;

public class SwingGUI extends JFrame implements ActionListener {


JTextField tf;
JCheckBox cb1, cb2;
JRadioButton rb1, rb2;
JButton btn;
JLabel result;

SwingGUI() {
setTitle("Swing GUI Demo");
setSize(400, 300);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);

tf = new JTextField(15);
cb1 = new JCheckBox("Java");
cb2 = new JCheckBox("Python");
rb1 = new JRadioButton("Male");
rb2 = new JRadioButton("Female");
btn = new JButton("Submit");
result = new JLabel("Output will appear here");

ButtonGroup bg = new ButtonGroup();


[Link](rb1);
[Link](rb2);

[Link](this);

add(new JLabel("Name:")); add(tf);


add(cb1); add(cb2);
add(rb1); add(rb2);
add(btn); add(result);

setVisible(true);
}

public void actionPerformed(ActionEvent e) {


String name = [Link]();
String lang = ([Link]() ? "Java " : "")
+ ([Link]() ? "Python" : "");
String gender = [Link]() ? "Male" :
[Link]() ? "Female" : "Not selected";
[Link]("Name: " + name + " | Lang: " + lang
+ " | Gender: " + gender);
}

public static void main(String[] args) {


new SwingGUI();
}
}

Output:
(A window opens with a text field, checkboxes, radio buttons, and a button.)

After entering Name = "Rahul", selecting Java, Male, and clicking Submit:
Name: Rahul | Lang: Java | Gender: Male

Q8. Connect to MySQL Database and Display Student Records


Connect to a MySQL database and display all records from the student table.

Program:
import [Link].*;

public class MySQLDemo {


public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/college";
String user = "root";
String pass = "password";

try {
[Link]("[Link]");
Connection con = [Link](url, user, pass);
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM student");

[Link]("ID\tName\t\tMarks");
[Link]("--------------------------------");
while ([Link]()) {
[Link]([Link]("id") + "\t"
+ [Link]("name") + "\t"
+ [Link]("marks"));
}
[Link]();
} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}

Note: Add [Link] to classpath before running.

Output:
ID Name Marks
--------------------------------
1 Rahul 85
2 Priya 90
3 Amit 78
4 Sneha 92

Submission Date: 6th March 2026

You might also like