Java Programming: Question Bank Solutions
Q4. Attempt any THREE of the following:
a. Program to Implement Action Listener
import [Link].*;
import [Link].*;
public class ButtonActionDemo extends JFrame implements ActionListener {
JButton button;
public ButtonActionDemo() {
button = new JButton("Click Me");
[Link](100, 100, 120, 40);
[Link](this);
add(button);
setSize(300, 300);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
[Link](this, "Button Clicked!");
}
public static void main(String[] args) {
new ButtonActionDemo();
}
}
Explanation: Demonstrates event-driven programming using ActionListener.
b. Calculator using GridLayout
import [Link].*;
import [Link].*;
import [Link].*;
public class Calculator extends JFrame implements ActionListener {
JTextField tf;
JButton[] buttons;
String[] labels = {
"7", "8", "9", "/", "4", "5", "6", "*",
"1", "2", "3", "-", "0", "C", "=", "+"
};
String current = "";
public Calculator() {
tf = new JTextField();
Java Programming: Question Bank Solutions
[Link](false);
add(tf, [Link]);
JPanel panel = new JPanel(new GridLayout(4, 4));
buttons = new JButton[16];
for (int i = 0; i < 16; i++) {
buttons[i] = new JButton(labels[i]);
buttons[i].addActionListener(this);
[Link](buttons[i]);
}
add(panel);
setSize(300, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String cmd = [Link]();
if ([Link]("=")) {
try {
[Link]("" + eval(current));
current = "";
} catch (Exception ex) {
[Link]("Error");
current = "";
}
} else if ([Link]("C")) {
current = "";
[Link]("");
} else {
current += cmd;
[Link](current);
}
}
private int eval(String exp) {
return (int) new [Link]().getEngineByName("JavaScript").eval(exp);
}
public static void main(String[] args) {
new Calculator();
}
}
Explanation: Demonstrates GUI layout using GridLayout and Swing components.
c. Retrieving Data Using ResultSet
import [Link].*;
public class ResultSetExample {
Java Programming: Question Bank Solutions
public static void main(String[] args) {
try {
Connection con = [Link]("jdbc:mysql://localhost:3306/mydb", "root", "password");
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM students");
while ([Link]()) {
[Link]([Link](1) + " " + [Link](2));
}
[Link]();
} catch (Exception e) {
[Link]();
}
}
}
Explanation: Uses ResultSet to iterate through rows returned from a SQL query.
Q5. Attempt any TWO of the following:
a. Two Threads for Even and Odd Numbers
class EvenThread extends Thread {
public void run() {
for (int i = 0; i <= 10; i += 2) {
[Link]("Even: " + i);
try { [Link](500); } catch (InterruptedException e) {}
}
}
}
class OddThread extends Thread {
public void run() {
for (int i = 1; i < 10; i += 2) {
[Link]("Odd: " + i);
try { [Link](500); } catch (InterruptedException e) {}
}
}
}
public class EvenOddThreadDemo {
public static void main(String[] args) {
new EvenThread().start();
new OddThread().start();
}
}
Q6. Attempt any TWO of the following:
Java Programming: Question Bank Solutions
a. Interface Implementation
interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() {
[Link]("Drawing Circle");
}
}
public class InterfaceDemo {
public static void main(String[] args) {
Drawable d = new Circle();
[Link]();
}
}
Explanation: Demonstrates abstraction using interface implementation.