Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branch for mini calculator project #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions issue50.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
ISSUE.NO.50 #Android
by tejal hajari

MINI CALCULATOR

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

public class Calculator {
private static JTextField inputField;

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGUI();
});
}

private static void createAndShowGUI() {
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
frame.setLayout(new BorderLayout());

inputField = new JTextField();
inputField.setHorizontalAlignment(JTextField.RIGHT);
frame.add(inputField, BorderLayout.NORTH);

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));

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

for (String label : buttonLabels) {
JButton button = new JButton(label);
button.addActionListener(new ButtonClickListener());
buttonPanel.add(button);
}

frame.add(buttonPanel, BorderLayout.CENTER);

frame.setVisible(true);
}

private static class ButtonClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
String buttonText = source.getText();

switch (buttonText) {
case "C":
inputField.setText("");
break;
case "=":
try {
String expression = inputField.getText();
double result = evaluateExpression(expression);
inputField.setText(String.valueOf(result));
} catch (Exception ex) {
inputField.setText("Error");
}
break;
default:
inputField.setText(inputField.getText() + buttonText);
break;
}
}

private double evaluateExpression(String expression) {
// Implement your expression evaluation logic here
// For simplicity, you can use JavaScript engine or other libraries for this purpose
// Example using JavaScript engine: https://stackoverflow.com/a/34226791
return 0; // Placeholder return value
}
}
}