Java Programming: Question Bank Solutions
Q4. Attempt any THREE of the following:
a. Program to Implement Action Listener
import javax.swing.*;
import java.awt.event.*;
public class ButtonActionDemo extends JFrame implements ActionListener {
JButton button;
public ButtonActionDemo() {
button = new JButton("Click Me");
button.setBounds(100, 100, 120, 40);
button.addActionListener(this);
add(button);
setSize(300, 300);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(this, "Button Clicked!");
}
public static void main(String[] args) {
new ButtonActionDemo();
}
}
Explanation: Demonstrates event-driven programming using ActionListener.
b. Calculator using GridLayout
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
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
tf.setEditable(false);
add(tf, BorderLayout.NORTH);
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);
panel.add(buttons[i]);
}
add(panel);
setSize(300, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("=")) {
try {
tf.setText("" + eval(current));
current = "";
} catch (Exception ex) {
tf.setText("Error");
current = "";
}
} else if (cmd.equals("C")) {
current = "";
tf.setText("");
} else {
current += cmd;
tf.setText(current);
}
}
private int eval(String exp) {
return (int) new javax.script.ScriptEngineManager().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 java.sql.*;
public class ResultSetExample {
Java Programming: Question Bank Solutions
public static void main(String[] args) {
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
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) {
System.out.println("Even: " + i);
try { Thread.sleep(500); } catch (InterruptedException e) {}
}
}
}
class OddThread extends Thread {
public void run() {
for (int i = 1; i < 10; i += 2) {
System.out.println("Odd: " + i);
try { Thread.sleep(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() {
System.out.println("Drawing Circle");
}
}
public class InterfaceDemo {
public static void main(String[] args) {
Drawable d = new Circle();
d.draw();
}
}
Explanation: Demonstrates abstraction using interface implementation.