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

Java Programs PartB

The document contains several Java programs demonstrating exception handling, GUI creation with AWT, mouse event handling, and binary file I/O. It includes examples of handling NegativeArraySizeException, NullPointerException, and NumberFormatException, as well as creating a simple GUI with buttons and mouse event responses. Additionally, it shows how to read and write binary data to a file.

Uploaded by

Manasa M R
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Java Programs PartB

The document contains several Java programs demonstrating exception handling, GUI creation with AWT, mouse event handling, and binary file I/O. It includes examples of handling NegativeArraySizeException, NullPointerException, and NumberFormatException, as well as creating a simple GUI with buttons and mouse event responses. Additionally, it shows how to read and write binary data to a file.

Uploaded by

Manasa M R
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Programs - Part B

1. Negative Array Size Exception


public class NegativeArrayDemo {
public static void main(String[] args) {
try {
int size = -5;
int arr[] = new int[size];
} catch (NegativeArraySizeException e) {
[Link]("Exception: " + e);
}
}
}

2. NullPointerException
public class NullPointerDemo {
public static void main(String[] args) {
try {
String str = null;
[Link]([Link]());
} catch (NullPointerException e) {
[Link]("Exception: " + e);
}
}
}

3. NumberFormatException
import [Link];

public class NumberFormatDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

try {
[Link]("Enter first number: ");
String a = [Link]();

[Link]("Enter second number: ");


String b = [Link]();
int num1 = [Link](a);
int num2 = [Link](b);

[Link]("Sum = " + (num1 + num2));


} catch (NumberFormatException e) {
[Link]("Invalid input! " + e);
}
}
}

4. AWT Window with Buttons


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();
}
}

5. Mouse Handling Events


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();
}
}

6. Binary File Read and Write


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);
}
}
}

You might also like