P-1
P-4
P-5
P-9
interface Vehicle {
Abstract methods (no implementation)
void start();
void stop();
int getSpeed();
class Car implements Vehicle {
private int speed;
public void start() {
speed = 10;
[Link]("Car started. Speed: " + speed + " km/h");
public void stop() {
speed = 0;
[Link]("Car stopped.");
}
public int getSpeed() {
return speed;
}public class Main {
public static void main(String[] args) {
Vehicle myCar = new Car(); // Interface reference, Car object
[Link]();
[Link]("Current Speed: " + [Link]() + " km/h");
[Link]();
P-10
P-11
P-13
P-14
import [Link].*;
import [Link].*;
public class AWTForm extends Frame implements ActionListener {
/**
*/
private static final long serialVersionUID = 1L;
TextField nameField, ageField;
Button submitButton;
public AWTForm() {
setLayout(new FlowLayout());
add(new Label("Name:"));
nameField = new TextField(20);
add(nameField);
add(new Label("Age:"));
ageField = new TextField(20);
add(ageField);
submitButton = new Button("Submit");
[Link](this);
add(submitButton);
setSize(300, 200);
setVisible(true);
public void actionPerformed(ActionEvent e) {
[Link]("Name: " + [Link]());
[Link]("Age: " + [Link]());
public static void main(String[] args) {
new AWTForm();
}
P-17
import [Link].*;
import [Link].*;
import [Link].*;
public class Calculator extends JFrame implements ActionListener {
JTextField display;
JButton[] buttons;
String[] labels = {"7", "8", "9", "/", "4", "5", "6", "*",
"1", "2", "3", "-", "0", ".", "=", "+"};
public Calculator() {
display = new JTextField();
[Link](false);
add(display, [Link]);
JPanel panel = new JPanel();
[Link](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);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
public void actionPerformed(ActionEvent e) {
String cmd = [Link]();
[Link]([Link]() + cmd);
public static void main(String[] args) {
new Calculator();
}
P-20
import [Link].*;
import [Link].*;
public class SimpleEventDemo extends JFrame implements KeyListener,
MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public SimpleEventDemo() {
addKeyListener(this);
addMouseListener(this);
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setFocusable(true);
}
public void keyTyped(KeyEvent e) {
[Link]("Key Typed: " + [Link]());
}
public void mouseClicked(MouseEvent e) {
[Link]("Mouse Clicked at: (" + [Link]() + ", " +
[Link]() + ")");
}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public static void main(String[] args) {
new SimpleEventDemo();
}
}
P-21
P-24