Java Programs Part B 1 to 10 1
Java Programs Part B 1 to 10 1
char ch = str.charAt(8);
System.out.println("Character at index 8: " + ch);
// 2. ArrayIndexOutOfBoundsException
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]); // Invalid index
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught ArrayIndexOutOfBoundsException: " +
e.getMessage());
}
// 3. NullPointerException
try {
String text = null;
System.out.println(text.length()); // Calling method on null
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException: " + e.getMessage());
}
// 4. NumberFormatException
try {
String invalidNumber = "abc";
int num = Integer.parseInt(invalidNumber); // Invalid parsing
} catch (NumberFormatException e) {
System.out.println("Caught NumberFormatException: " +
e.getMessage());
}
System.out.println("Program continues after handling exceptions.");
}
}
javax is a package namespace in Java that contains extended functionalities beyond the
core java package.
Initially, javax was used for Java extensions, but over time, many important libraries
remained under javax instead of being moved to java.
• java Package: Contains core Java classes like java.lang, java.util, java.io, etc.
//File-2 : PersonalInfo.html
<html>
<body>
<applet code="PersonalInfo.class" width="300" height="100"></applet>
</body>
</html>
// Create components
add(new Label("Enter number:"));
inputField = new TextField(10);
add(inputField);
add(new Label("Factorial:"));
outputField = new TextField(10);
outputField.setEditable(false);
add(outputField);
}
10 | P a g e Department of BCA
JAVA Lab Programs BCA-2nd SEM(SEP)
11 | P a g e Department of BCA
JAVA Lab Programs BCA-2nd SEM(SEP)
producerThread.start();
consumerThread.start();
}
@Override
public void run() {
while (true) {
synchronized (buffer) {
while (buffer.size() == BUFFER_SIZE) {
try {
System.out.println("Buffer is full, Producer is waiting...");
buffer.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
buffer.offer(value);
System.out.println("Produced: " + value);
value++;
buffer.notify();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
12 | P a g e Department of BCA
JAVA Lab Programs BCA-2nd SEM(SEP)
return;
}
}
}
}
13 | P a g e Department of BCA