WEEK-9 JAVA - KCPD
WEEK-9 JAVA - KCPD
a. Write a Java program that creates three threads. First thread displays —Good
Morning every one second, the second thread displays —Hello every two seconds
and the third thread displays —Welcome every three seconds.
import java.util.LinkedList;
public class ProducerConsumerExample {
public static void main(String[] args) {
final PC pc = new PC();
Thread producerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
pc.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread consumerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
pc.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
producerThread.start();
consumerThread.start();
try {
producerThread.join();
consumerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static class PC {
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;
public void produce() throws InterruptedException {
int value = 0;
while (true) {
synchronized (this) {
while (list.size() == capacity) {
wait();
}
System.out.println("Producer produced-" + value);
list.add(value++);
notify();
Thread.sleep(1000);
}
}
}
public void consume() throws InterruptedException {
while (true) {
synchronized (this) {
while (list.size() == 0) {
wait();
}
int val = list.removeFirst();
System.out.println("Consumer consumed-" + val);
notify();
Thread.sleep(1000);
}
}
}
}
}
c. Create a Email registration Form using Java AWT. The UI should have fields such
as name, address, sex, age, email, contact number, etc.
import java.awt.*;
import java.awt.event.*;
public class EmailRegistrationForm extends Frame {
Label nameLabel, addressLabel, sexLabel, ageLabel, emailLabel, contactLabel;
TextField nameField, addressField, ageField, emailField, contactField;
Checkbox maleCheckbox, femaleCheckbox;
Button submitButton;
public EmailRegistrationForm() {
super("Email Registration Form");
setSize(500, 300);
setLayout(null);
nameLabel = new Label("Name:");
nameLabel.setBounds(50, 50, 100, 20);
add(nameLabel);
nameField = new TextField();
nameField.setBounds(150, 50, 200, 20);
add(nameField);
addressLabel = new Label("Address:");
addressLabel.setBounds(50, 80, 100, 20);
add(addressLabel);
addressField = new TextField();
addressField.setBounds(150, 80, 200, 20);
add(addressField);
sexLabel = new Label("Sex:");
sexLabel.setBounds(50, 110, 100, 20);
add(sexLabel);
maleCheckbox = new Checkbox("Male");
maleCheckbox.setBounds(150, 110, 100, 20);
add(maleCheckbox);
femaleCheckbox = new Checkbox("Female");
femaleCheckbox.setBounds(250, 110, 100, 20);
add(femaleCheckbox);
ageLabel = new Label("Age:");
ageLabel.setBounds(50, 140, 100, 20);
add(ageLabel);
ageField = new TextField();
ageField.setBounds(150, 140, 200, 20);
add(ageField);
emailLabel = new Label("Email:");
emailLabel.setBounds(50, 170, 100, 20);
add(emailLabel);
emailField = new TextField();
emailField.setBounds(150, 170, 200, 20);
add(emailField);
contactLabel = new Label("Contact Number:");
contactLabel.setBounds(50, 200, 100, 20);
add(contactLabel);
contactField = new TextField();
contactField.setBounds(150, 200, 200, 20);
add(contactField);
submitButton = new Button("Submit");
submitButton.setBounds(200, 230, 100, 20);
add(submitButton);
setVisible(true);
}
public static void main(String[] args) {
EmailRegistrationForm form = new EmailRegistrationForm();
}
}