Q1 (b) Write a program for computing (n power 2).
class Square {
public static void main(String[] args) {
int n = 5;
int result = n * n;
System.out.println("Square of " + n + " is: " + result);
}
}
Explanation: This program takes an integer `n` and multiplies it by itself to compute n squared (n^2).
Q2 (b) Write a program to showcase Applet HTML tag.
import java.applet.Applet;
import java.awt.Graphics;
/* <applet code="HelloApplet.class" width=200 height=100></applet> */
public class HelloApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello Applet", 20, 20);
}
}
Explanation: This applet displays 'Hello Applet' on the screen. The HTML comment shows how to embed it
using the <applet> tag.
Q2 (e) Write a program to depict the functionality of border layout.
import java.awt.*;
import javax.swing.*;
public class BorderLayoutExample {
public static void main(String[] args) {
JFrame f = new JFrame("BorderLayout Example");
f.setLayout(new BorderLayout());
f.add(new JButton("North"), BorderLayout.NORTH);
f.add(new JButton("South"), BorderLayout.SOUTH);
f.add(new JButton("East"), BorderLayout.EAST);
f.add(new JButton("West"), BorderLayout.WEST);
f.add(new JButton("Center"), BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}
Explanation: This program demonstrates `BorderLayout`, placing buttons in north, south, east, west, and
center regions of the frame.
Q2 (f) Write a program to demonstrate the usage of event handling.
import java.awt.*;
import java.awt.event.*;
public class EventHandlingExample {
public static void main(String[] args) {
Frame f = new Frame("Event Handling");
Button b = new Button("Click Me");
b.setBounds(50, 50, 80, 30);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
f.add(b);
f.setSize(200, 150);
f.setLayout(null);
f.setVisible(true);
}
}
Explanation: This program adds an event listener to a button, and prints a message when the button is
clicked.
Q3 (a) Write a program to implement Invalid Age user defined exception for election voting
case.
class InvalidAgeException extends Exception {
InvalidAgeException(String s) {
super(s);
}
}
public class Voting {
static void checkAge(int age) throws InvalidAgeException {
if (age < 18)
throw new InvalidAgeException("Not eligible to vote");
else
System.out.println("Eligible to vote");
}
public static void main(String[] args) {
try {
checkAge(16);
} catch (InvalidAgeException e) {
System.out.println("Exception: " + e);
}
}
}
Explanation: A custom exception is created for invalid voting age. If age is less than 18, an exception is
thrown.
Q3 (b) Write the inter-threaded version program for producer-consumer problem.
class Q {
int num;
boolean valueSet = false;
synchronized void put(int num) {
while (valueSet) {
try { wait(); } catch (Exception e) {}
}
this.num = num;
valueSet = true;
System.out.println("Put: " + num);
notify();
}
synchronized void get() {
while (!valueSet) {
try { wait(); } catch (Exception e) {}
}
System.out.println("Got: " + num);
valueSet = false;
notify();
}
}
class Producer implements Runnable {
Q q;
Producer(Q q) { this.q = q; new Thread(this, "Producer").start(); }
public void run() {
int i = 0;
while (true) q.put(i++);
}
}
class Consumer implements Runnable {
Q q;
Consumer(Q q) { this.q = q; new Thread(this, "Consumer").start(); }
public void run() {
while (true) q.get();
}
}
public class ProducerConsumer {
public static void main(String[] args) {
Q q = new Q();
new Producer(q);
new Consumer(q);
}
}
Explanation: This program demonstrates inter-thread communication where producer puts data and
consumer gets it, using wait/notify.
Q3 (c) Write a program to print the tables of 5 and 100 using a common printable() method
with synchronization.
class Table {
synchronized void printTable(int n) {
for (int i = 1; i <= 10; i++) {
System.out.println(n + " x " + i + " = " + (n * i));
}
}
}
public class PrintTables extends Thread {
Table t;
int num;
PrintTables(Table t, int num) {
this.t = t;
this.num = num;
}
public void run() {
t.printTable(num);
}
public static void main(String[] args) {
Table t = new Table();
PrintTables t1 = new PrintTables(t, 5);
PrintTables t2 = new PrintTables(t, 100);
t1.start();
t2.start();
}
}
Explanation: The program prints tables of 5 and 100 using synchronized method `printTable()` to avoid
conflicts between threads.