0% found this document useful (0 votes)
10 views12 pages

Java Output

java output s of practical

Uploaded by

kiranyerolkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

Java Output

java output s of practical

Uploaded by

kiranyerolkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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;

System.out.println("Car started. Speed: " + speed + " km/h");

public void stop() {

speed = 0;

System.out.println("Car stopped.");

}
public int getSpeed() {

return speed;

}public class Main {

public static void main(String[] args) {

Vehicle myCar = new Car(); // Interface reference, Car object

myCar.start();

System.out.println("Current Speed: " + myCar.getSpeed() + " km/h");

myCar.stop();

P-10
P-11
P-13
P-14
import java.awt.*;

import java.awt.event.*;

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");

submitButton.addActionListener(this);

add(submitButton);

setSize(300, 200);

setVisible(true);

public void actionPerformed(ActionEvent e) {

System.out.println("Name: " + nameField.getText());

System.out.println("Age: " + ageField.getText());

public static void main(String[] args) {

new AWTForm();

}
P-17

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

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();

display.setEditable(false);

add(display, BorderLayout.NORTH);

JPanel panel = new JPanel();

panel.setLayout(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);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public void actionPerformed(ActionEvent e) {

String cmd = e.getActionCommand();

display.setText(display.getText() + cmd);

public static void main(String[] args) {

new Calculator();

}
P-20
import java.awt.event.*;
import javax.swing.*;

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) {


System.out.println("Key Typed: " + e.getKeyChar());
}

public void mouseClicked(MouseEvent e) {


System.out.println("Mouse Clicked at: (" + e.getX() + ", " +
e.getY() + ")");
}

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

You might also like