0% found this document useful (0 votes)
29 views

Java Exp 10

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)
29 views

Java Exp 10

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

Experiment 10

Q1. Develop a Swing GUI based standard calculator program.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CalculatorApp extends JFrame implements ActionListener {


private JTextField display;
private StringBuilder expression = new StringBuilder(); // To store the full
expression

// Constructor to set up the calculator UI


public CalculatorApp() {
// Set up the frame
setTitle("Calculator");
setSize(350, 450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

// Set up display
display = new JTextField();
display.setEditable(false);
display.setFont(new Font("Arial", Font.BOLD, 20));
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);

// Set up buttons panel


JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 5, 5));

// Calculator buttons
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"
};

// Add buttons to panel and register action listener


for (String text : buttons) {
JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.BOLD, 18));
button.addActionListener(this);
panel.add(button);
}
add(panel, BorderLayout.CENTER);
}

// Handle button clicks


@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();

if (command.charAt(0) >= '0' && command.charAt(0) <= '9') {


// If a number button is pressed
expression.append(command);
display.setText(expression.toString());
} else if (command.equals("C")) {
// Clear the display and reset the expression
expression.setLength(0);
display.setText("");
} else if (command.equals("=")) {
// Perform the calculation and display the result
try {
double result = evaluateExpression(expression.toString());
display.setText(expression + " = " + result);
expression.setLength(0); // Clear the expression
expression.append(result); // Start new expression with result
} catch (ArithmeticException ex) {
display.setText("Error: " + ex.getMessage());
expression.setLength(0);
}
} else {
// Operator button pressed (+, -, *, /)
if (expression.length() > 0) {
expression.append(" ").append(command).append(" ");
display.setText(expression.toString());
}
}
}

// Method to evaluate the expression


private double evaluateExpression(String expr) {
String[] tokens = expr.split(" ");
if (tokens.length < 3) throw new ArithmeticException("Incomplete
expression");

double num1 = Double.parseDouble(tokens[0]);


String operator = tokens[1];
double num2 = Double.parseDouble(tokens[2]);

switch (operator) {
case "+": return num1 + num2;
case "-": return num1 - num2;
case "*": return num1 * num2;
case "/":
if (num2 == 0) throw new ArithmeticException("Cannot divide by zero");
return num1 / num2;
default:
throw new ArithmeticException("Invalid operator");
}
}

// Main method to run the calculator


public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
CalculatorApp calculator = new CalculatorApp();
calculator.setVisible(true);
});
}
}

OUTPUT:
PS D:\JAVA_PROGRAMMING> javac CalculatorApp.java
PS D:\JAVA_PROGRAMMING> java CalculatorApp

2024-11-10 16:30:20.184 java[3941:111488] +[IMKClient subclass]: chose


IMKClient_Modern
2024-11-10 16:30:20.184 java[3941:111488] +[IMKInputSession subclass]: chose
IMKInputSession_Modern
1. Addition 2. Multiply

3. Divide 4. Subtract

You might also like