Important Questions
Important Questions
1st way:
try {
System.out.println("Statement 1");
System.out.println("Statement 2");
System.out.println("Statement 3");
System.out.println("Statement 4");
int result = 10 / 0;
System.out.println("Statement 6");
System.out.println("Statement 7");
System.out.println("Statement 8");
System.out.println("Statement 9");
System.out.println("Statement 10");
catch (ArithmeticException e)
}
// Remaining statements after the catch block
Output:
Statement 1
Statement 2
Statement 3
Statement 4
2nd way:
import java.util.Scanner;
try
catch (NumberFormatException e)
{
return Integer.parseInt(userInput);
Output:
>java ThrowsHandling
Enter a number: 4
You entered: 4
>java ThrowsHandling
Enter a number: f
super(message);
}
// Class with a method that throws the custom exception
class AgeValidator
} else
try
AgeValidator.validateAge(userAge);
catch (InvalidAgeException e)
Output;
java UserException
super(name);
printThreadInfo();
// Creating threads
thread1.printThreadInfo();
thread2.printThreadInfo();
thread1.setPriority(Thread.MAX_PRIORITY);
thread2.setPriority(Thread.MIN_PRIORITY);
thread1.printThreadInfo();
thread2.printThreadInfo();
// Starting the threads
thread1.start();
thread2.start();
Output:
Thread Priority: 5
--------------------------
Thread Priority: 5
--------------------------
Thread Priority: 10
--------------------------
Thread Priority: 1
--------------------------
Thread Priority: 1
--------------------------
Thread Priority: 10
while (isFull)
buffer = item;
isFull = true;
while (!isFull)
isFull = false;
return consumedItem;
this.sharedResource = sharedResource;
@Override
try
sharedResource.produce(i);
catch (InterruptedException e)
Thread.currentThread().interrupt();
}
class Consumer extends Thread
this.sharedResource = sharedResource;
@Override
try
catch (InterruptedException e)
Thread.currentThread().interrupt();
producerThread.setSharedResource(sharedResource);
consumerThread.setSharedResource(sharedResource);
producerThread.start();
consumerThread.start();
Output:
Produced: 1
Consumed: 1
Produced: 2
Consumed: 2
Produced: 3
Consumed: 3
Produced: 4
Consumed: 4
Produced: 5
Consumed: 5
Inter Thread Communication through Wait () and Notify () methods.
int total = 0;
synchronized(this)
{
for (int i = 1; i < 100; i++)
{
total=total+i;
this.notify();
}
}
}
}
class Interthread
{
public static void main(String[] args) throws InterruptedException
{
Rupdate r=new Rupdate();
r.start();
synchronized(r)
{
r.wait();
System.out.println(r.total);
}
}
}
Output:
>java Interthread
4950
}
}
}
// Check again after the thread has finished
System.out.println("Is the thread alive? " + t.isAlive());
}
}
Output:
>java Alive1
Is the thread alive? false
main Working...
main Working...
child Working...
child Working...
main Working...
main Working...
main Working...
child Working...
child Working...
Is the thread alive? true
child Working...
{
for(int i=0;i<5;i++)
{
System.out.println("child thread");
try
{
r.join();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class JoinThread
{
public static void main(String[] args)throws InterruptedException
{
MyThread.r=Thread.currentThread();
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
Output:
>java JoinThread
main thread
child thread
main thread
main thread
main thread
main thread
child thread
child thread
child thread
child thread
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("Program completed.");
}
}
Output:
>java SleepThread
Program started.
Countdown: 1
Countdown: 2
Countdown: 3
Countdown: 4
Countdown: 5
Program completed.
Creating three threads using the class Thread and then run
Output:
>java ThreeThread
Thread A: Count 1
Thread B: Count 1
Thread C: Count 1
Thread A: Count 2
Thread B: Count 2
Thread C: Count 2
Thread A: Count 3
Thread B: Count 3
Thread C: Count 3
Thread A: Count 4
Thread B: Count 4
Thread C: Count 4
Thread A: Count 5
Thread B: Count 5
Thread C: Count 5
// Creating three Thread instances and associating them with Runnable instances
Thread thread1 = new Thread(myRunnable1);
Thread thread2 = new Thread(myRunnable2);
Thread thread3 = new Thread(myRunnable3);
Output:
>java ThreeRunnable
Thread X: Count 1
Thread Z: Count 1
Thread Y: Count 1
Thread X: Count 2
Thread Z: Count 2
Thread Y: Count 2
Thread Z: Count 3
Thread X: Count 3
Thread Y: Count 3
Thread X: Count 4
Thread Y: Count 4
Thread Z: Count 4
Thread X: Count 5
Thread Y: Count 5
Thread Z: Count 5
Registration Form With Swing Components :
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public RegistrationForm() {
initializeUI();
setTitle("Registration Form");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
genderButtonGroup.add(maleRadioButton);
genderButtonGroup.add(femaleRadioButton);
inputPanel.add(firstNameLabel);
inputPanel.add(firstNameField);
inputPanel.add(lastNameLabel);
inputPanel.add(lastNameField);
inputPanel.add(emailLabel);
inputPanel.add(emailField);
inputPanel.add(passwordLabel);
inputPanel.add(passwordField);
inputPanel.add(genderLabel);
inputPanel.add(maleRadioButton);
inputPanel.add(femaleRadioButton);
inputPanel.add(addressLabel);
inputPanel.add(addressField);
displayArea.setEditable(false);
displayPanel.add(scrollPane, BorderLayout.CENTER);
submitButton.addActionListener(new ActionListener() {
@Override
submitForm();
});
getContentPane().add(inputPanel);
getContentPane().add(displayPanel);
getContentPane().add(submitButton);
displayArea.setText(displayText);
SwingUtilities.invokeLater(new Runnable() {
@Override
new RegistrationForm().setVisible(true);
});
Output:
Applet total syllabus ( all topics)
Registration Form With AWT Components :
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public RegistrationFormAWT() {
initializeUI();
setTitle("Registration Form");
setSize(500, 300);
submitButton.addActionListener(new ActionListener() {
@Override
submitForm();
});
// Add components to the display panel
displayArea.setEditable(false);
displayPanel.add(displayArea, BorderLayout.CENTER);
buttonPanel.add(submitButton);
add(inputPanel);
add(displayPanel);
add(buttonPanel);
addWindowListener(new java.awt.event.WindowAdapter() {
System.exit(0);
});
genderPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
genderPanel.add(maleCheckbox);
genderPanel.add(femaleCheckbox);
return genderPanel;
panel.add(label);
panel.add(field);
displayArea.setText(displayText);
registrationForm.setVisible(true);
}
Output:
Differ Btw AWT and Swing
Event Classes
EventListener Interfaces
EventListenerInterface Methods
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public FrameExample()
setLayout(new FlowLayout());
// Create components
createComponents();
setTitle("Frame Example");
setSize(1500, 1200);
setVisible(true);
addWindowListener(new java.awt.event.WindowAdapter() {
System.exit(0);
});
textArea.setEditable(false);
choice.add("rose");
choice.add("lilly");
choice.add("jasmine");
add(button1);
add(textField);
add(textArea);
add(checkbox);
add(choice);
button1.addActionListener(new ActionListener() {
});
checkbox.addItemListener(e -> {
});
choice.addItemListener(e -> {
new FrameExample();
Output: