0% found this document useful (0 votes)
4 views6 pages

javaprojects

Important

Uploaded by

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

javaprojects

Important

Uploaded by

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

Java Project

Title of the Project: Simple Calculator using Java program

Program :

import java.awt.*;

import java.awt.event.*;

public class Calculator extends Frame implements


ActionListener {

TextField display;

Button[] numberButtons;

Button addButton, subButton, mulButton, divButton,


equalButton, clearButton;

String operation;

double num1, num2, result;

public Calculator() {

setTitle("Basic Calculator");

setSize(400, 600);
setLayout(new BorderLayout());

display = new TextField();

display.setEditable(false);

add(display, BorderLayout.NORTH);

Panel panel = new Panel();

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

numberButtons = new Button[10];

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

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

numberButtons[i].addActionListener(this);

panel.add(numberButtons[i]);

addButton = new Button("+");

subButton = new Button("-");


mulButton = new Button("*");

divButton = new Button("/");

equalButton = new Button("=");

clearButton = new Button("C");

addButton.addActionListener(this);

subButton.addActionListener(this);

mulButton.addActionListener(this);

divButton.addActionListener(this);

equalButton.addActionListener(this);

clearButton.addActionListener(this);

panel.add(addButton);

panel.add(subButton);

panel.add(mulButton);

panel.add(divButton);

panel.add(equalButton);

panel.add(clearButton);
add(panel, BorderLayout.CENTER);

setVisible(true);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0);

});

public void actionPerformed(ActionEvent e) {

String command = e.getActionCommand();

if (Character.isDigit(command.charAt(0))) {

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

} else if (command.equals("+") || command.equals("-")


|| command.equals("*") || command.equals("/")) {

num1 = Double.parseDouble(display.getText());
operation = command;

display.setText("");

} else if (command.equals("=")) {

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

switch (operation) {

case "+":

result = num1 + num2;

break;

case "-":

result = num1 - num2;

break;

case "*":

result = num1 * num2;

break;

case "/":

result = num1 / num2;

break;
}

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

} else if (command.equals("C")) {

display.setText("");

num1 = num2 = result = 0;

public static void main(String[] args) {

new Calculator();

You might also like