Java Programs - Part B (Exam Format)
1. Negative Array Size Exception
Aim:
To demonstrate NegativeArraySizeException.
Program:
public class NegativeArrayDemo {
public static void main(String[] args) {
try {
int size = -5;
int arr[] = new int[size];
} catch (NegativeArraySizeException e) {
[Link]("Exception: " + e);
}
}
}
Output:
Exception: [Link]
2. NullPointerException
Aim:
To demonstrate NullPointerException.
Program:
public class NullPointerDemo {
public static void main(String[] args) {
try {
String str = null;
[Link]([Link]());
} catch (NullPointerException e) {
[Link]("Exception: " + e);
}
}
}
Output:
Exception: [Link]
3. NumberFormatException
Aim:
To read two integers and handle NumberFormatException.
Program:
import [Link];
public class NumberFormatDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
String a = [Link]();
String b = [Link]();
int num1 = [Link](a);
int num2 = [Link](b);
[Link]("Sum = " + (num1 + num2));
} catch (NumberFormatException e) {
[Link]("Invalid input! " + e);
}
}
}
Output:
Invalid input! [Link] (if wrong input)
4. AWT Window with Buttons
Aim:
To create AWT window with buttons and display greetings.
Program:
import [Link].*;
import [Link].*;
public class GreetingAWT extends Frame implements ActionListener {
Label l;
Button m, a, e, close;
GreetingAWT() {
l = new Label("Click a button");
[Link](100, 50, 200, 30);
m = new Button("M");
a = new Button("A");
e = new Button("E");
close = new Button("Close");
[Link](50, 100, 50, 30);
[Link](120, 100, 50, 30);
[Link](190, 100, 50, 30);
[Link](260, 100, 60, 30);
add(l); add(m); add(a); add(e); add(close);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
setSize(350, 200);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if ([Link]() == m)
[Link]("Good Morning");
else if ([Link]() == a)
[Link]("Good Afternoon");
else if ([Link]() == e)
[Link]("Good Evening");
else
[Link](0);
}
public static void main(String[] args) {
new GreetingAWT();
}
}
Output:
Displays window with greeting messages
5. Mouse Handling Events
Aim:
To demonstrate mouse events.
Program:
import [Link].*;
import [Link].*;
public class MouseDemo extends Frame implements MouseListener {
Label l;
MouseDemo() {
l = new Label("Mouse Event");
[Link](100, 100, 200, 30);
add(l);
addMouseListener(this);
setSize(400, 300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
[Link]("Mouse Clicked");
}
public void mousePressed(MouseEvent e) {
[Link]("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
[Link]("Mouse Released");
}
public void mouseEntered(MouseEvent e) {
[Link]("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
[Link]("Mouse Exited");
}
public static void main(String[] args) {
new MouseDemo();
}
}
Output:
Displays mouse event messages on screen
6. Binary File Read and Write
Aim:
To read and write binary file.
Program:
import [Link].*;
public class BinaryIODemo {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("[Link]");
DataOutputStream dos = new DataOutputStream(fos);
[Link](100);
[Link](99.99);
[Link]();
FileInputStream fis = new FileInputStream("[Link]");
DataInputStream dis = new DataInputStream(fis);
int i = [Link]();
double d = [Link]();
[Link]("Integer: " + i);
[Link]("Double: " + d);
[Link]();
} catch (IOException e) {
[Link]("Exception: " + e);
}
}
}
Output:
Integer: 100
Double: 99.99