0% found this document useful (0 votes)
23 views

Java_Assignment_2_Solutions (1)

Uploaded by

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

Java_Assignment_2_Solutions (1)

Uploaded by

maardanga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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;

System.out.println(result);

} catch (ArithmeticException e) {

System.out.println("Exception caught: " + e.getMessage());

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 {

System.out.println("Welcome to vote!");

}
public static void main(String[] args) {

try {

validate(16);

} catch (IllegalArgumentException e) {

System.out.println("Exception caught: " + e.getMessage());

3. Write a program to write a string in a text file using IO package.

import java.io.FileWriter;

import java.io.IOException;

public class WriteToFile {

public static void main(String[] args) {

try (FileWriter writer = new FileWriter("output.txt")) {

writer.write("Hello, this is written to a file!");

System.out.println("Successfully written to the file.");

} catch (IOException e) {

System.out.println("An error occurred: " + e.getMessage());

}
4. Write a program to draw different shapes in an applet using graphic class.

import java.applet.Applet;

import java.awt.Graphics;

public class DrawShapes extends Applet {

public void paint(Graphics g) {

g.drawLine(10, 10, 50, 10);

g.drawRect(60, 10, 50, 50);

g.drawOval(120, 10, 50, 50);

5. Write a program using Applet to display a message in the Applet.

import java.applet.Applet;

import java.awt.Graphics;

public class DisplayMessage extends Applet {

public void paint(Graphics g) {

g.drawString("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 java.awt.Frame;

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 java.awt.*;

import java.awt.event.*;

public class KeyboardEventDemo extends Frame implements KeyListener {

Label label;

KeyboardEventDemo() {

label = new Label();

label.setBounds(50, 50, 200, 20);


add(label);

addKeyListener(this);

setSize(400, 300);

setLayout(null);

setVisible(true);

public void keyPressed(KeyEvent e) {

label.setText("Key Pressed: " + e.getKeyChar());

public void keyReleased(KeyEvent e) {

label.setText("Key Released: " + e.getKeyChar());

public void keyTyped(KeyEvent e) {}

public static void main(String[] args) {

new KeyboardEventDemo();

8. Write a Java Program to demonstrate Mouse event.

import java.awt.*;

import java.awt.event.*;
public class MouseEventDemo extends Frame implements MouseListener {

Label label;

MouseEventDemo() {

label = new Label();

label.setBounds(50, 50, 200, 20);

add(label);

addMouseListener(this);

setSize(400, 300);

setLayout(null);

setVisible(true);

public void mouseClicked(MouseEvent e) {

label.setText("Mouse Clicked at X: " + e.getX() + ", Y: " + e.getY());

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 java.awt.*;

import java.awt.event.*;

public class SumCalculatorGUI extends Frame implements ActionListener {

TextField tf1, tf2, tf3;

Button addButton;

SumCalculatorGUI() {

tf1 = new TextField();

tf1.setBounds(50, 50, 150, 20);

tf2 = new TextField();

tf2.setBounds(50, 100, 150, 20);

tf3 = new TextField();

tf3.setBounds(50, 150, 150, 20);

tf3.setEditable(false);

addButton = new Button("Add");

addButton.setBounds(50, 200, 80, 30);

addButton.addActionListener(this);

add(tf1);

add(tf2);
add(tf3);

add(addButton);

setSize(400, 300);

setLayout(null);

setVisible(true);

public void actionPerformed(ActionEvent e) {

int num1 = Integer.parseInt(tf1.getText());

int num2 = Integer.parseInt(tf2.getText());

tf3.setText(String.valueOf(num1 + num2));

public static void main(String[] args) {

new SumCalculatorGUI();

You might also like