Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit a162ecc5 authored by First Naja's avatar First Naja
Browse files

"Calculator"

parents
Branches Exam
No related tags found
No related merge requests found
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Test implements WindowListener, ActionListener {
double num1, num2, ans;
String operand1, operand2, operator;
String numStr, ansStr;
TextField t;
Frame f;
Panel p;
Panel p2;
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bPlus, bSub, bMul, bDiv, bEqual, bDot;
public Test() {
numStr = "";
f = new Frame("Easy Calculator");
f.setSize(400, 500);
f.setBackground(Color.gray);
f.addWindowListener(this);
p = new Panel();
p.setSize(200, 100);
p.setBackground(Color.pink);
t = new TextField();
t.setSize(400, 100);
t.setColumns(50);
p.add(t);
p2 = new Panel();
p2.setSize(100, 100);
p2.setBackground(Color.orange);
p2.setLayout(new GridLayout(4, 4));
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b0 = new Button("0");
bPlus = new Button("+");
bSub = new Button("-");
bMul = new Button("*");
bDiv = new Button("/");
bDot = new Button(".");
bEqual = new Button("=");
// Add listeners to buttons
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
bDot.addActionListener(this);
bPlus.addActionListener(this);
bSub.addActionListener(this);
bMul.addActionListener(this);
bDiv.addActionListener(this);
bEqual.addActionListener(this);
// Add buttons to panel
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bPlus);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bSub);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMul);
p2.add(b0);
p2.add(bDot);
p2.add(bEqual);
p2.add(bDiv);
f.add(p, BorderLayout.NORTH);
f.add(p2, BorderLayout.CENTER);
f.setVisible(true);
}
public static void main(String[] args) {
Test test2 = new Test();
}
@Override
public void windowActivated(WindowEvent arg0) { }
@Override
public void windowClosed(WindowEvent e) { }
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
@Override
public void windowDeactivated(WindowEvent e) { }
@Override
public void windowDeiconified(WindowEvent e) { }
@Override
public void windowIconified(WindowEvent e) { }
@Override
public void windowOpened(WindowEvent e) { }
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
// If a number or decimal is pressed
if (cmd.equals("0") || cmd.equals("1") || cmd.equals("2") || cmd.equals("3") ||
cmd.equals("4") || cmd.equals("5") || cmd.equals("6") || cmd.equals("7") ||
cmd.equals("8") || cmd.equals("9") || cmd.equals(".")) {
numStr = numStr + cmd;
t.setText(numStr);
}
// If an operator is pressed
else if (cmd.equals("+") || cmd.equals("-") || cmd.equals("*") || cmd.equals("/")) {
operand1 = numStr;
operator = cmd;
numStr = "";
}
// If equal sign is pressed
else if (cmd.equals("=")) {
operand2 = numStr;
num1 = Double.parseDouble(operand1);
num2 = Double.parseDouble(operand2);
switch (operator) {
case "+":
ans = num1 + num2;
break;
case "-":
ans = num1 - num2;
break;
case "*":
ans = num1 * num2;
break;
case "/":
if (num2 != 0) {
ans = num1 / num2;
} else {
ansStr = "Error";
t.setText(ansStr);
return;
}
break;
}
ansStr = Double.toString(ans);
t.setText(ansStr);
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment