Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mochawoof authored Jan 31, 2023
0 parents commit 138e265
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
Binary file added main$1.class
Binary file not shown.
Binary file added main$2.class
Binary file not shown.
Binary file added main$3.class
Binary file not shown.
Binary file added main.class
Binary file not shown.
19 changes: 19 additions & 0 deletions main.ctxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#BlueJ class context
comment0.target=main
comment1.params=a
comment1.target=void\ answer(java.lang.String)
comment2.params=d\ t\ f
comment2.target=void\ msg(java.lang.String,\ java.lang.String,\ int)
comment3.params=sender
comment3.target=void\ sbl(javax.swing.JButton)
comment4.params=e
comment4.target=void\ actionPerformed(java.awt.event.ActionEvent)
comment5.params=
comment5.target=void\ randomize()
comment6.params=args
comment6.target=void\ main(java.lang.String[])
comment7.params=e
comment7.target=void\ actionPerformed(java.awt.event.ActionEvent)
comment8.params=e
comment8.target=void\ actionPerformed(java.awt.event.ActionEvent)
numComments=9
129 changes: 129 additions & 0 deletions main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@

/**
* Write a description of main here.
*
* @author (your name)
* @version (a version number or a date)
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import org.mariuszgromada.math.mxparser.*;
public class main {
private static JTextArea ans;
private static String pstr = "";
private static ArrayList<Component> carr = new ArrayList<Component>();
private static JFrame window;
private static Font f = new Font("Comic Sans MS", Font.PLAIN, 20);
private static long cps = 0;
private static long lt = 0;
private static boolean first = false;
private static void answer(String a) {
ans.setText(a);
pstr = "";
}
private static void msg(String d, String t, int f) {
JOptionPane.showMessageDialog(window, d, t, f);
}
private static void sbl(JButton sender) {
sender.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (System.currentTimeMillis() - lt > 5000) {
cps = 0;
lt = System.currentTimeMillis();
} else {
cps++;
if (cps > 5) {
msg("You clicked too fast!", "Oops", JOptionPane.INFORMATION_MESSAGE);
cps = 0;
lt = System.currentTimeMillis();
}
}
pstr += sender.getText();
ans.setText(pstr);
}
});
}
private static void randomize() {
window.getContentPane().removeAll();
if (first) {
Collections.shuffle(carr);
} else {
first = true;
}
for (int i=0; i < carr.size(); i++) {
window.add(carr.get(i));
}
window.validate();
}

public static void main(String[] args) {
window = new JFrame("Calculator");
License.iConfirmNonCommercialUse("qwert");
window.setSize(300, 400);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(0,4));
ans = new JTextArea("0");
ans.setEditable(false);
ans.setLineWrap(true);
carr.add(ans);

JButton add = new JButton("+");
add.setFont(f);
sbl(add);
carr.add(add);
JButton subtract = new JButton("-");
subtract.setFont(f);
sbl(subtract);
carr.add(subtract);
JButton multiply = new JButton("*");
multiply.setFont(f);
sbl(multiply);
carr.add(multiply);
JButton divide = new JButton("/");
divide.setFont(f);
sbl(divide);
carr.add(divide);

for (int i=0; i < 10; i++) {
JButton b = new JButton(String.valueOf(i));
b.setFont(f);
sbl(b);
b.setBackground(Color.LIGHT_GRAY);
carr.add(b);
}

JButton clear = new JButton("C");
clear.setFont(f);
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
randomize();
pstr = "";
ans.setText("0");
}
});
carr.add(clear);

JButton equal = new JButton("=");
equal.setFont(f);
equal.setBackground(Color.RED);
equal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
randomize();
String vo = String.valueOf(new Expression(pstr).calculate());
if (vo == "NaN") {
msg("Calculation error, quitting.", "Oops", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
ans.setText(vo);
pstr = "";
}
});
carr.add(equal);

randomize();
window.setVisible(true);
}
}

0 comments on commit 138e265

Please sign in to comment.