Java Assignment Solutions - Assignment 2
1. Write a program to implement the concept of Exception Handling using predefined
Exception.
public class PredefinedExceptionHandling {
public static void main(String[] args) {
try {
int result = 10 / 0;
[Link](result);
} catch (ArithmeticException e) {
[Link]("Exception caught: " + [Link]());
2. Write a program to implement the concept of Exception Handling using throw keyword.
public class ThrowExample {
static void validate(int age) {
if (age < 18) {
throw new IllegalArgumentException("Age not valid for voting.");
} else {
[Link]("Welcome to vote!");
}
public static void main(String[] args) {
try {
validate(16);
} catch (IllegalArgumentException e) {
[Link]("Exception caught: " + [Link]());
3. Write a program to write a string in a text file using IO package.
import [Link];
import [Link];
public class WriteToFile {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("[Link]")) {
[Link]("Hello, this is written to a file!");
[Link]("Successfully written to the file.");
} catch (IOException e) {
[Link]("An error occurred: " + [Link]());
}
4. Write a program to draw different shapes in an applet using graphic class.
import [Link];
import [Link];
public class DrawShapes extends Applet {
public void paint(Graphics g) {
[Link](10, 10, 50, 10);
[Link](60, 10, 50, 50);
[Link](120, 10, 50, 50);
5. Write a program using Applet to display a message in the Applet.
import [Link];
import [Link];
public class DisplayMessage extends Applet {
public void paint(Graphics g) {
[Link]("Hello, Applet!", 20, 20);
6. Write a java program which will create a window and an empty area within that window
(extends Frame class).
import [Link];
public class CreateWindow extends Frame {
CreateWindow() {
setTitle("My Window");
setSize(400, 300);
setVisible(true);
public static void main(String[] args) {
new CreateWindow();
7. Write a Java Program to demonstrate Keyboard event.
import [Link].*;
import [Link].*;
public class KeyboardEventDemo extends Frame implements KeyListener {
Label label;
KeyboardEventDemo() {
label = new Label();
[Link](50, 50, 200, 20);
add(label);
addKeyListener(this);
setSize(400, 300);
setLayout(null);
setVisible(true);
public void keyPressed(KeyEvent e) {
[Link]("Key Pressed: " + [Link]());
public void keyReleased(KeyEvent e) {
[Link]("Key Released: " + [Link]());
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
new KeyboardEventDemo();
8. Write a Java Program to demonstrate Mouse event.
import [Link].*;
import [Link].*;
public class MouseEventDemo extends Frame implements MouseListener {
Label label;
MouseEventDemo() {
label = new Label();
[Link](50, 50, 200, 20);
add(label);
addMouseListener(this);
setSize(400, 300);
setLayout(null);
setVisible(true);
public void mouseClicked(MouseEvent e) {
[Link]("Mouse Clicked at X: " + [Link]() + ", Y: " + [Link]());
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public static void main(String[] args) {
new MouseEventDemo();
}
9. Write a program to enter two numbers in two different text fields, add a button, and display
their sum in a third non-editable text field.
import [Link].*;
import [Link].*;
public class SumCalculatorGUI extends Frame implements ActionListener {
TextField tf1, tf2, tf3;
Button addButton;
SumCalculatorGUI() {
tf1 = new TextField();
[Link](50, 50, 150, 20);
tf2 = new TextField();
[Link](50, 100, 150, 20);
tf3 = new TextField();
[Link](50, 150, 150, 20);
[Link](false);
addButton = new Button("Add");
[Link](50, 200, 80, 30);
[Link](this);
add(tf1);
add(tf2);
add(tf3);
add(addButton);
setSize(400, 300);
setLayout(null);
setVisible(true);
public void actionPerformed(ActionEvent e) {
int num1 = [Link]([Link]());
int num2 = [Link]([Link]());
[Link]([Link](num1 + num2));
public static void main(String[] args) {
new SumCalculatorGUI();