Objective: Create a scientific calculator. It should behave like any other scien
ID: 3814692 • Letter: O
Question
Objective:
Create a scientific calculator. It should behave like any other scientific calculator where the user presses keys and stuff happens. This must have a graphical user interface (GUI), and include the following features:
-Current total display
-Number pad
-Arithmetic functions
-Addition
-Subtraction
-Multiplication
-Division
(You may disregard order of operations)
Trigonometric functions:
-Sine
-Cosine
-Tangent
-Arcsine
-Arccosine
-Arctangent
Additional mathematic functions:
-Exponent
-Square root
-Logarithm
Additional functions:
-Clear – Zeroes out the current total
-Equals – Gives the running total
Furthermore every time a user enters a number first it should set the running total to said number. If an operation is entered then that operation is applied to the current total. If it is a binary operation like addition, subtraction, multiplication, division, and exponent then the following number will be the second number in that function, and applied as soon as the user presses equals.
YOU MUST COMMENT YOUR CODE!!!
Explanation / Answer
Hi,
Since the language is not mentioned, I am writing the code in Java language.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SCalculator extends JFrame implements ActionListener {
JTextField myTextValue;
// Buttons for all the calculator functions
JButton buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive, buttonSix, buttonSeven,
buttonEight, buttonNine, zero, sqrt, sin, cos, tan, clr, pow2, pow3, exp,
fac, plus, min, div, log, rec, mul, eq, addSub, dot ;
double temp, temp1, result, a;
static double m1, m2;
int k = 1, x = 0, y = 0, z = 0;
char ch;
Container cont;
JPanel textPanel, buttonpanel;
// Default Constructor
SCalculator() {
cont = getContentPane();
cont.setLayout(new BorderLayout());
JPanel textpanel = new JPanel();
// Length of display field
myTextValue = new JTextField(32);
myTextValue.setHorizontalAlignment(SwingConstants.RIGHT);
myTextValue.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent keyevent) {
char c = keyevent.getKeyChar();
if (c >= '0' && c <= '9') {
} else {
keyevent.consume();
}
}
});
textpanel.add(myTextValue);
// Setting the layout for the Calculator
buttonpanel = new JPanel();
buttonpanel.setLayout(new GridLayout(7, 4, 2, 2));
// Adding all buttons for UI
buttonOne = new JButton("1");
buttonpanel.add(buttonOne);
buttonOne.addActionListener(this);
buttonTwo = new JButton("2");
buttonpanel.add(buttonTwo);
buttonTwo.addActionListener(this);
buttonThree = new JButton("3");
buttonpanel.add(buttonThree);
buttonThree.addActionListener(this);
buttonFour = new JButton("4");
buttonpanel.add(buttonFour);
buttonFour.addActionListener(this);
buttonFive = new JButton("5");
buttonpanel.add(buttonFive);
buttonFive.addActionListener(this);
buttonSix = new JButton("6");
buttonpanel.add(buttonSix);
buttonSix.addActionListener(this);
buttonSeven = new JButton("7");
buttonpanel.add(buttonSeven);
buttonSeven.addActionListener(this);
buttonEight = new JButton("8");
buttonpanel.add(buttonEight);
buttonEight.addActionListener(this);
buttonNine = new JButton("9");
buttonpanel.add(buttonNine);
buttonNine.addActionListener(this);
zero = new JButton("0");
buttonpanel.add(zero);
zero.addActionListener(this);
plus = new JButton("+");
buttonpanel.add(plus);
plus.addActionListener(this);
min = new JButton("-");
buttonpanel.add(min);
min.addActionListener(this);
mul = new JButton("*");
buttonpanel.add(mul);
mul.addActionListener(this);
div = new JButton("/");
div.addActionListener(this);
buttonpanel.add(div);
addSub = new JButton("+/-");
buttonpanel.add(addSub);
addSub.addActionListener(this);
dot = new JButton(".");
buttonpanel.add(dot);
dot.addActionListener(this);
eq = new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);
clr = new JButton("Clear");
buttonpanel.add(clr);
clr.addActionListener(this);
rec = new JButton("1/x");
buttonpanel.add(rec);
rec.addActionListener(this);
log = new JButton("log");
buttonpanel.add(log);
log.addActionListener(this);
fac = new JButton("n!");
fac.addActionListener(this);
buttonpanel.add(fac);
sin = new JButton("SIN");
buttonpanel.add(sin);
sin.addActionListener(this);
cos = new JButton("COS");
buttonpanel.add(cos);
cos.addActionListener(this);
tan = new JButton("TAN");
buttonpanel.add(tan);
tan.addActionListener(this);
pow2 = new JButton("x^2");
buttonpanel.add(pow2);
pow2.addActionListener(this);
pow3 = new JButton("x^3");
buttonpanel.add(pow3);
pow3.addActionListener(this);
sqrt = new JButton("Sqrt");
buttonpanel.add(sqrt);
sqrt.addActionListener(this);
exp = new JButton("Exp");
exp.addActionListener(this);
buttonpanel.add(exp);
cont.add("Center", buttonpanel);
cont.add("North", textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// Performing logical calculations on each of the buttons
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if (action.equals("1")) {
if (z == 0) {
myTextValue.setText(myTextValue.getText() + "1");
} else {
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + "1");
z = 0;
}
}
if (action.equals("2")) {
if (z == 0) {
myTextValue.setText(myTextValue.getText() + "2");
} else {
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + "2");
z = 0;
}
}
if (action.equals("3")) {
if (z == 0) {
myTextValue.setText(myTextValue.getText() + "3");
} else {
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + "3");
z = 0;
}
}
if (action.equals("4")) {
if (z == 0) {
myTextValue.setText(myTextValue.getText() + "4");
} else {
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + "4");
z = 0;
}
}
if (action.equals("5")) {
if (z == 0) {
myTextValue.setText(myTextValue.getText() + "5");
} else {
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + "5");
z = 0;
}
}
if (action.equals("6")) {
if (z == 0) {
myTextValue.setText(myTextValue.getText() + "6");
} else {
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + "6");
z = 0;
}
}
if (action.equals("7")) {
if (z == 0) {
myTextValue.setText(myTextValue.getText() + "7");
} else {
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + "7");
z = 0;
}
}
if (action.equals("8")) {
if (z == 0) {
myTextValue.setText(myTextValue.getText() + "8");
} else {
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + "8");
z = 0;
}
}
if (action.equals("9")) {
if (z == 0) {
myTextValue.setText(myTextValue.getText() + "9");
} else {
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + "9");
z = 0;
}
}
if (action.equals("0")) {
if (z == 0) {
myTextValue.setText(myTextValue.getText() + "0");
} else {
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + "0");
z = 0;
}
}
// Logarithm
if (action.equals("log")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
} else {
a = Math.log(Double.parseDouble(myTextValue.getText()));
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + a);
}
}
if (action.equals("1/x")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
} else {
a = 1 / Double.parseDouble(myTextValue.getText());
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + a);
}
}
// Power to 2
if (action.equals("x^2")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
} else {
a = Math.pow(Double.parseDouble(myTextValue.getText()), 2);
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + a);
}
}
// Power to 3
if (action.equals("x^3")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
} else {
a = Math.pow(Double.parseDouble(myTextValue.getText()), 3);
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + a);
}
}
if (action.equals("+/-")) {
if (x == 0) {
myTextValue.setText("-" + myTextValue.getText());
x = 1;
} else {
myTextValue.setText(myTextValue.getText());
}
}
// Exponentiation
if (action.equals("Exp")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
} else {
a = Math.exp(Double.parseDouble(myTextValue.getText()));
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + a);
}
}
// Addition
if (action.equals("+")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
temp = 0;
ch = '+';
} else {
temp = Double.parseDouble(myTextValue.getText());
myTextValue.setText("");
ch = '+';
y = 0;
x = 0;
}
myTextValue.requestFocus();
}
// Subtract
if (action.equals("-")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
temp = 0;
ch = '-';
} else {
x = 0;
y = 0;
temp = Double.parseDouble(myTextValue.getText());
myTextValue.setText("");
ch = '-';
}
myTextValue.requestFocus();
}
// Multiply
if (action.equals("*")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
temp = 1;
ch = '*';
} else {
x = 0;
y = 0;
temp = Double.parseDouble(myTextValue.getText());
ch = '*';
myTextValue.setText("");
}
myTextValue.requestFocus();
}
// Divide
if (action.equals("/")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
temp = 1;
ch = '/';
} else {
x = 0;
y = 0;
temp = Double.parseDouble(myTextValue.getText());
ch = '/';
myTextValue.setText("");
}
myTextValue.requestFocus();
}
// Decimal Point
if (action.equals(".")) {
if (y == 0) {
myTextValue.setText(myTextValue.getText() + ".");
y = 1;
} else {
myTextValue.setText(myTextValue.getText());
}
}
// Tan
if (action.equals("TAN")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
} else {
a = Math.tan(Double.parseDouble(myTextValue.getText()));
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + a);
}
}
// Sine
if (action.equals("SIN")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
} else {
a = Math.sin(Double.parseDouble(myTextValue.getText()));
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + a);
}
}
// Cosine
if (action.equals("COS")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
} else {
a = Math.cos(Double.parseDouble(myTextValue.getText()));
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + a);
}
}
// Square Root
if (action.equals("Sqrt")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
} else {
a = Math.sqrt(Double.parseDouble(myTextValue.getText()));
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + a);
}
}
// factorial
if (action.equals("n!")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
} else {
a = fact(Double.parseDouble(myTextValue.getText()));
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + a);
}
}
// clear the screen
if (action.equals("Clear")) {
myTextValue.setText("");
x = 0;
y = 0;
z = 0;
}
// display on action
if (action.equals("=")) {
if (myTextValue.getText().equals("")) {
myTextValue.setText("");
} else {
temp1 = Double.parseDouble(myTextValue.getText());
switch (ch) {
case '+':
result = temp + temp1;
break;
case '-':
result = temp - temp1;
break;
case '/':
result = temp / temp1;
break;
case '*':
result = temp * temp1;
break;
}
myTextValue.setText("");
myTextValue.setText(myTextValue.getText() + result);
z = 1;
}
}
myTextValue.requestFocus();
}
// Calculating the factorial
double fact(double x) {
int initial = 0;
if (x < 0) {
initial = 20;
return 0;
}
double i, value = 1;
for (i = 2; i <= x; i += 1.0)
value *= i;
return value;
}
public static void main(String args[]) {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception myException) {
}
SCalculator display = new SCalculator();
display.setTitle("Scientific Calculator");
display.pack();
display.setVisible(true);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.