OOPS 15 Mark
OOPS 15 Mark
PART B & C
1. a)Write detailed notes on Constructor with suitable example program.
Refer 1.11.1 & 1.11.2 in given notes.
class Main {
private String name;
// constructor
Main() {
System.out.println("Constructor Called:");
name = "Programiz";
}
Output:
Constructor Called:
The name is Programiz
5. What is Package? Explain in detail about how the packages provide access control to
various categories of visibility for class members.
8. Explain in detail about Java’s Built-in Exception. Explain any three exceptions
Refer page no.3.5.1 in given notes.
10. Discuss about the commonly used event listener interfaces with suitable example
programs. Refer page no.5.2.2 in given notes.
11. Write a java program to demonstrate mouse event handlers and appraise the program
with working procedure in detail.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseEventDemo extends JFrame {
private static final int WIDTH = 600;
private static final int HEIGHT = 400;
// A panel to draw on and capture mouse events
private class DrawingPanel extends JPanel {
private int x = -1, y = -1;
public DrawingPanel() {
// Add mouse listeners for handling events
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint(); // Request to redraw the panel
}
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint(); } @Override
public void mouseReleased(MouseEvent e) {
x = -1; // Reset coordinates on release
y = -1;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (x != -1 && y != -1) {
g.setColor(Color.RED);
g.fillOval(x - 10, y - 10, 20, 20); // Draw a circle at the mouse position
}
}
}
public MouseEventDemo() {
setTitle("Mouse Event Demo");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Add the drawing panel to the frame
add(new DrawingPanel());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MouseEventDemo frame = new MouseEventDemo();
frame.setVisible(true);
});
}
}
12. Construct a java program that implements the concept of multithreading programming
@Override
public void run() {
for (int i = start; i <= end; i++) {
System.out.println(Thread.currentThread().getName() + " - Number: " + i);
try {
Thread.sleep(delay); // Pause for the specified delay
} catch (InterruptedException e) {
System.err.println("Thread was interrupted: " + e.getMessage());
}
}
}
}
// Optionally, wait for all threads to finish before exiting the program
try {
thread1.join();
thread2.join();
thread3.join();
} catch (InterruptedException e) {
System.err.println("Main thread was interrupted: " + e.getMessage());
}