0% found this document useful (0 votes)
44 views9 pages

Ajp Mca

Uploaded by

SANGEETHA San
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)
44 views9 pages

Ajp Mca

Uploaded by

SANGEETHA San
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/ 9

C:\Users\student>

C:\Users\student>d:

D:\>set path=c:\Program Files\Java\jdk1.8.0_202\bin

D:\>javac Calculator.java

D:\>java Calculator

1. Develop a program to implement Calculator using Swing technology

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Calculator {

private JFrame frame;

private JTextField textField;

private JButton[] numberButtons;

private JButton[] functionButtons;

private JButton addButton, subButton, mulButton, divButton;

private JButton decButton, equButton, delButton, clrButton;

private JPanel panel;

private Font font = new Font("Arial", Font.PLAIN, 20);

private double num1 = 0, num2 = 0, result = 0;

private char operator;

public Calculator() {

frame = new JFrame("Calculator");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 550);

frame.setLayout(null);

textField = new JTextField();

textField.setBounds(50, 25, 300, 50);

textField.setFont(font);

textField.setEditable(false);

numberButtons = new JButton[10];

for (int i = 0; i < 10; i++) {

numberButtons[i] = new JButton(String.valueOf(i));

numberButtons[i].setFont(font);

numberButtons[i].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String buttonText = e.getActionCommand();

textField.setText(textField.getText() + buttonText);

});

functionButtons = new JButton[4];

addButton = new JButton("+");

subButton = new JButton("-");

mulButton = new JButton("*");

divButton = new JButton("/");

functionButtons[0] = addButton;

functionButtons[1] = subButton;

functionButtons[2] = mulButton;

functionButtons[3] = divButton;

for (int i = 0; i < 4; i++) {


functionButtons[i].setFont(font);

functionButtons[i].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String buttonText = e.getActionCommand();

num1 = Double.parseDouble(textField.getText());

operator = buttonText.charAt(0);

textField.setText("");

});

decButton = new JButton(".");

equButton = new JButton("=");

delButton = new JButton("Delete");

clrButton = new JButton("Clear");

decButton.setFont(font);

equButton.setFont(font);

delButton.setFont(font);

clrButton.setFont(font);

decButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (!textField.getText().contains(".")) {

textField.setText(textField.getText() + ".");

});

equButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {


num2 = Double.parseDouble(textField.getText());

switch (operator) {

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

result = num1 / num2;

break;

textField.setText(String.valueOf(result));

num1 = result;

});

delButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String str = textField.getText();

textField.setText("");

for (int i = 0; i < str.length() - 1; i++) {

textField.setText(textField.getText() + str.charAt(i));

});

clrButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

textField.setText("");

});

panel = new JPanel();

panel.setBounds(50, 100, 300, 300);

panel.setLayout(new GridLayout(4, 4, 10, 10));

panel.add(numberButtons[1]);

panel.add(numberButtons[2]);

panel.add(numberButtons[3]);

panel.add(addButton);

panel.add(numberButtons[4]);

panel.add(numberButtons[5]);

panel.add(numberButtons[6]);

panel.add(subButton);

panel.add(numberButtons[7]);

panel.add(numberButtons[8]);

panel.add(numberButtons[9]);

panel.add(mulButton);

panel.add(decButton);

panel.add(numberButtons[0]);

panel.add(equButton);

panel.add(divButton);

panel.add(clrButton);

panel.add(delButton);
frame.add(panel);

frame.add(textField);

frame.setVisible(true);

public static void main(String[] args) {

new Calculator();

OUPUT

2.Develop a program that displays two textboxes for entering a students’ Rollno and Name with
appropriate labels and buttons

import java.awt.*;

import java.awt.event.*;

public class StudentInfoGUI extends JFrame implements ActionListener {


private JLabel rollNoLabel, nameLabel;

private JTextField rollNoField, nameField;

private JButton submitButton, clearButton;

public StudentInfoGUI() {

setTitle("Student Information");

setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

rollNoLabel = new JLabel("Roll No:");

nameLabel = new JLabel("Name:");

rollNoField = new JTextField(20);

nameField = new JTextField(20);

submitButton = new JButton("Submit");

clearButton = new JButton("Clear");

// Add action listeners to the buttons

submitButton.addActionListener(this);

clearButton.addActionListener(this);

// Layout setup

setLayout(new GridLayout(3, 2, 10, 10));

// Add components to the frame

add(rollNoLabel);

add(rollNoField);

add(nameLabel);

add(nameField);

add(submitButton);
add(clearButton);

setVisible(true);

// ActionPerformed method to handle button clicks

public void actionPerformed(ActionEvent e) {

if (e.getSource() == submitButton) {

String rollNo = rollNoField.getText();

String name = nameField.getText();

JOptionPane.showMessageDialog(this, "Roll No: " + rollNo + "\nName: " + name);

} else if (e.getSource() == clearButton) {

rollNoField.setText("");

nameField.setText("");

public static void main(String[] args) {

// Create an instance of StudentInfoGUI

new StudentInfoGUI();

Output
import java.io.*;
import javax.servlet.*;

public class DateSrv extends GenericServlet


{
//implement service()
public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException
{
//set response content type
res.setContentType("text/html");
//get stream obj
PrintWriter pw = res.getWriter();
//write req processing logic
java.util.Date date = new java.util.Date();
pw.println("<h2>"+"Current Date & Time: " +date.toString()+"</h2>");
//close stream object
pw.close();
}
}

You might also like