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

Java Scenario Based Programs

The document presents Java scenario-based programs covering threads, synchronization, AWT components, and layout design. It includes code examples for a file downloader application with progress indication, a synchronized bank withdrawal simulation, a simple login form using AWT, and a student registration form utilizing various layouts. Each scenario illustrates practical applications of Java concepts in a concise manner.

Uploaded by

jilikajithendar
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 views4 pages

Java Scenario Based Programs

The document presents Java scenario-based programs covering threads, synchronization, AWT components, and layout design. It includes code examples for a file downloader application with progress indication, a synchronized bank withdrawal simulation, a simple login form using AWT, and a student registration form utilizing various layouts. Each scenario illustrates practical applications of Java concepts in a concise manner.

Uploaded by

jilikajithendar
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 Scenario-Based Programs

1. Threads Creation and Lifecycle

Scenario:
You are developing a file downloader application that shows progress while downloading
in the background.

Design a Java program using two threads - one for downloading a file and another for
updating the progress bar.

Code:
class DownloadTask implements Runnable {
public void run() {
for(int i = 1; i <= 10; i++) {
[Link]("Downloading... " + i*10 + "%");
try { [Link](500); } catch (InterruptedException e) {}
}
}
}

public class DownloadApp {


public static void main(String[] args) {
Thread t1 = new Thread(new DownloadTask());
[Link]();
}
}

2. Synchronization

Scenario:
Two customers are trying to withdraw money from a joint bank account at the same time.

Write a Java program to simulate race condition and solve it using synchronized keyword.

Code:
class Bank {
private int balance = 10000;

public synchronized void withdraw(int amount) {


if(balance >= amount) {
[Link]([Link]().getName() + " is withdrawing " +
amount);
balance -= amount;
[Link]("Balance after withdrawal: " + balance);
} else {
[Link]("Insufficient Balance for " +
[Link]().getName());
}
}
}
Java Scenario-Based Programs

3. AWT Components

Scenario:
Design a simple login form using AWT with text fields for username and password.

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

class LoginForm extends Frame implements ActionListener {


TextField user, pass;
Button login;

LoginForm() {
setLayout(new FlowLayout());
user = new TextField(20);
pass = new TextField(20);
[Link]('*');
login = new Button("Login");

add(new Label("Username:"));
add(user);
add(new Label("Password:"));
add(pass);
add(login);

[Link](this);

setSize(300,200);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


if([Link]().equals("admin") && [Link]().equals("1234")) {
[Link]("Login Successful");
} else {
[Link]("Login Failed");
}
}

public static void main(String args[]) {


new LoginForm();
}
}

4. Layout Program

Scenario:
Create a student registration form using AWT with GridLayout, FlowLayout, and
Java Scenario-Based Programs

BorderLayout.

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

public class LayoutExample extends Frame {

Label title, nameLbl, emailLbl, genderLbl;


TextField nameField, emailField;
CheckboxGroup genderGroup;
Checkbox male, female;
Button submit, reset;

public LayoutExample() {
setTitle("Student Registration Form");
setSize(400, 300);
setLayout(new BorderLayout());
setVisible(true);

title = new Label("Student Registration Form", [Link]);


[Link](new Font("Arial", [Link], 16));
add(title, [Link]);

Panel centerPanel = new Panel();


[Link](new GridLayout(3, 2, 10, 10));

nameLbl = new Label("Name:");


emailLbl = new Label("Email:");
genderLbl = new Label("Gender:");

nameField = new TextField();


emailField = new TextField();

genderGroup = new CheckboxGroup();


male = new Checkbox("Male", genderGroup, true);
female = new Checkbox("Female", genderGroup, false);

[Link](nameLbl);
[Link](nameField);
[Link](emailLbl);
[Link](emailField);
[Link](genderLbl);

Panel genderPanel = new Panel(new FlowLayout([Link]));


[Link](male);
[Link](female);
[Link](genderPanel);

add(centerPanel, [Link]);
Java Scenario-Based Programs

Panel bottomPanel = new Panel();


[Link](new FlowLayout());

submit = new Button("Submit");


reset = new Button("Reset");

[Link](submit);
[Link](reset);

add(bottomPanel, [Link]);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
});
}

public static void main(String[] args) {


new LayoutExample();
}
}

You might also like