Skip to content

Instantly share code, notes, and snippets.

@kishida
Created April 15, 2024 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kishida/21234e4f3a4d5f2947807aad6c46af57 to your computer and use it in GitHub Desktop.
Save kishida/21234e4f3a4d5f2947807aad6c46af57 to your computer and use it in GitHub Desktop.
calculator generated by CodeGemma 7B
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton[] numberButtons;
private JButton[] operatorButtons;
private JButton clearButton;
private JButton equalsButton;
private double firstNumber;
private double secondNumber;
private String operator;
public Calculator() {
// Set up the frame
setTitle("Calculator");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the text field
textField = new JTextField();
textField.setFont(new Font("Arial", Font.BOLD, 20));
textField.setHorizontalAlignment(JTextField.RIGHT);
add(textField, BorderLayout.NORTH);
// Create the number buttons
numberButtons = new JButton[10];
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
}
// Create the operator buttons
operatorButtons = new JButton[] {
new JButton("+"),
new JButton("-"),
new JButton("*"),
new JButton("/")
};
for (JButton button : operatorButtons) {
button.addActionListener(this);
}
// Create the clear button
clearButton = new JButton("C");
clearButton.addActionListener(this);
// Create the equals button
equalsButton = new JButton("=");
equalsButton.addActionListener(this);
// Add the buttons to the frame
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
for (int i = 0; i < 10; i++) {
buttonPanel.add(numberButtons[i]);
}
for (JButton button : operatorButtons) {
buttonPanel.add(button);
}
buttonPanel.add(clearButton);
buttonPanel.add(equalsButton);
add(buttonPanel, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
// Handle number button clicks
if (command.matches("\\d")) {
textField.setText(textField.getText() + command);
}
// Handle operator button clicks
if (command.matches("[+\\-*/]")) {
firstNumber = Double.parseDouble(textField.getText());
operator = command;
textField.setText("");
}
// Handle clear button click
if (command.equals("C")) {
textField.setText("");
firstNumber = 0;
secondNumber = 0;
operator = null;
}
// Handle equals button click
if (command.equals("=")) {
secondNumber = Double.parseDouble(textField.getText());
double result = 0;
switch (operator) {
case "+":
result = firstNumber + secondNumber;
break;
case "-":
result = firstNumber - secondNumber;
break;
case "*":
result = firstNumber * secondNumber;
break;
case "/":
result = firstNumber / secondNumber;
break;
}
textField.setText(String.valueOf(result));
}
}
public static void main(String[] args) {
new Calculator().setVisible(true);
}
}
@kishida
Copy link
Author

kishida commented Apr 15, 2024

prompt is "generate a calculator in Java swing"
https://gist.github.com/assets/1178746/42461b77-3e03-421b-9b24-aff24cfbf937

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment