Question

write a program on eclipse where you would build your own scientific calculator that can use...

write a program on eclipse where you would build your own scientific calculator that can use algebraic notations

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package lecture;

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

public class scienticalci extends JFrame implements ActionListener {
   JTextField textfield;
   double temporary, temporary1, r, tempa;
   static double tm1, tm2;
   int k = 1, x1 = 0, y1 = 0, z1 = 0;
   char choice;
   JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, zero, clr, pow2, pow3, exp,
           fac, plus, minus, division, logrithmic, rec, multiplication, eq, addSub, dot, mr, mc, mp,
           mm, sqrt, sin, cos, tan;
   Container container;
   JPanel tpane, bpane;

   scienticalci() {
       container = getContentPane();
       container.setLayout(new BorderLayout());
       JPanel textpanel = new JPanel();
       textfield = new JTextField(25);
       textfield.setHorizontalAlignment(SwingConstants.RIGHT);
       textfield.addKeyListener(new KeyAdapter() {
           public void keyTyped(KeyEvent keyevent) {
               char c = keyevent.getKeyChar();
               if (c >= '0' && c <= '9') {
               } else {
                   keyevent.consume();
               }
           }
       });
       textpanel.add(textfield);
       bpane = new JPanel();
       bpane.setLayout(new GridLayout(8, 4, 2, 2));
       boolean t = true;
       mr = new JButton("MR");
       bpane.add(mr);
       mr.addActionListener(this);
       mc = new JButton("MC");
       bpane.add(mc);
       mc.addActionListener(this);

       mp = new JButton("M+");
       bpane.add(mp);
       mp.addActionListener(this);

       mm = new JButton("M-");
       bpane.add(mm);
       mm.addActionListener(this);

       button1 = new JButton("1");
       bpane.add(button1);
       button1.addActionListener(this);
       button2 = new JButton("2");
       bpane.add(button2);
       button2.addActionListener(this);
       button3 = new JButton("3");
       bpane.add(button3);
       button3.addActionListener(this);

       button4 = new JButton("4");
       bpane.add(button4);
       button4.addActionListener(this);
       button5 = new JButton("5");
       bpane.add(button5);
       button5.addActionListener(this);
       button6 = new JButton("6");
       bpane.add(button6);
       button6.addActionListener(this);

       button7 = new JButton("7");
       bpane.add(button7);
       button7.addActionListener(this);
       button8 = new JButton("8");
       bpane.add(button8);
       button8.addActionListener(this);
       button9 = new JButton("9");
       bpane.add(button9);
       button9.addActionListener(this);

       zero = new JButton("0");
       bpane.add(zero);
       zero.addActionListener(this);

       plus = new JButton("+");
       bpane.add(plus);
       plus.addActionListener(this);

       minus = new JButton("-");
       bpane.add(minus);
       minus.addActionListener(this);

       multiplication = new JButton("*");
       bpane.add(multiplication);
       multiplication.addActionListener(this);

       division = new JButton("/");
       division.addActionListener(this);
       bpane.add(division);

       addSub = new JButton("+/-");
       bpane.add(addSub);
       addSub.addActionListener(this);

       dot = new JButton(".");
       bpane.add(dot);
       dot.addActionListener(this);

       eq = new JButton("=");
       bpane.add(eq);
       eq.addActionListener(this);

       rec = new JButton("1/x1");
       bpane.add(rec);
       rec.addActionListener(this);
       sqrt = new JButton("Sqrt");
       bpane.add(sqrt);
       sqrt.addActionListener(this);
       logrithmic = new JButton("logrithmic");
       bpane.add(logrithmic);
       logrithmic.addActionListener(this);

       sin = new JButton("SIN");
       bpane.add(sin);
       sin.addActionListener(this);
       cos = new JButton("COS");
       bpane.add(cos);
       cos.addActionListener(this);
       tan = new JButton("TAN");
       bpane.add(tan);
       tan.addActionListener(this);
       pow2 = new JButton("x1^2");
       bpane.add(pow2);
       pow2.addActionListener(this);
       pow3 = new JButton("x1^3");
       bpane.add(pow3);
       pow3.addActionListener(this);
       exp = new JButton("Exp");
       exp.addActionListener(this);
       bpane.add(exp);
       fac = new JButton("n!");
       fac.addActionListener(this);
       bpane.add(fac);

       clr = new JButton("AC");
       bpane.add(clr);
       clr.addActionListener(this);
       container.add("Center", bpane);
       container.add("North", textpanel);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

   public void actionPerformed(ActionEvent e) {
       String temps = e.getActionCommand();
       if (temps.equals("1")) {
           if (z1 == 0) {
               textfield.setText(textfield.getText() + "1");
           } else {
               textfield.setText("");
               textfield.setText(textfield.getText() + "1");
               z1 = 0;
           }
       }
       if (temps.equals("2")) {
           if (z1 == 0) {
               textfield.setText(textfield.getText() + "2");
           } else {
               textfield.setText("");
               textfield.setText(textfield.getText() + "2");
               z1 = 0;
           }
       }
       if (temps.equals("3")) {
           if (z1 == 0) {
               textfield.setText(textfield.getText() + "3");
           } else {
               textfield.setText("");
               textfield.setText(textfield.getText() + "3");
               z1 = 0;
           }
       }
       if (temps.equals("4")) {
           if (z1 == 0) {
               textfield.setText(textfield.getText() + "4");
           } else {
               textfield.setText("");
               textfield.setText(textfield.getText() + "4");
               z1 = 0;
           }
       }
       if (temps.equals("5")) {
           if (z1 == 0) {
               textfield.setText(textfield.getText() + "5");
           } else {
               textfield.setText("");
               textfield.setText(textfield.getText() + "5");
               z1 = 0;
           }
       }
       if (temps.equals("6")) {
           if (z1 == 0) {
               textfield.setText(textfield.getText() + "6");
           } else {
               textfield.setText("");
               textfield.setText(textfield.getText() + "6");
               z1 = 0;
           }
       }
       if (temps.equals("7")) {
           if (z1 == 0) {
               textfield.setText(textfield.getText() + "7");
           } else {
               textfield.setText("");
               textfield.setText(textfield.getText() + "7");
               z1 = 0;
           }
       }
       if (temps.equals("8")) {
           if (z1 == 0) {
               textfield.setText(textfield.getText() + "8");
           } else {
               textfield.setText("");
               textfield.setText(textfield.getText() + "8");
               z1 = 0;
           }
       }
       if (temps.equals("9")) {
           if (z1 == 0) {
               textfield.setText(textfield.getText() + "9");
           } else {
               textfield.setText("");
               textfield.setText(textfield.getText() + "9");
               z1 = 0;
           }
       }
       if (temps.equals("0")) {
           if (z1 == 0) {
               textfield.setText(textfield.getText() + "0");
           } else {
               textfield.setText("");
               textfield.setText(textfield.getText() + "0");
               z1 = 0;
           }
       }
       if (temps.equals("AC")) {
           textfield.setText("");
           x1 = 0;
           y1 = 0;
           z1 = 0;
       }
       if (temps.equals("logrithmic")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
           } else {
               tempa = Math.log(Double.parseDouble(textfield.getText()));
               textfield.setText("");
               textfield.setText(textfield.getText() + tempa);
           }
       }
       if (temps.equals("1/x1")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
           } else {
               tempa = 1 / Double.parseDouble(textfield.getText());
               textfield.setText("");
               textfield.setText(textfield.getText() + tempa);
           }
       }
       if (temps.equals("Exp")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
           } else {
               tempa = Math.exp(Double.parseDouble(textfield.getText()));
               textfield.setText("");
               textfield.setText(textfield.getText() + tempa);
           }
       }
       if (temps.equals("x1^2")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
           } else {
               tempa = Math.pow(Double.parseDouble(textfield.getText()), 2);
               textfield.setText("");
               textfield.setText(textfield.getText() + tempa);
           }
       }
       if (temps.equals("x1^3")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
           } else {
               tempa = Math.pow(Double.parseDouble(textfield.getText()), 3);
               textfield.setText("");
               textfield.setText(textfield.getText() + tempa);
           }
       }
       if (temps.equals("+/-")) {
           if (x1 == 0) {
               textfield.setText("-" + textfield.getText());
               x1 = 1;
           } else {
               textfield.setText(textfield.getText());
           }
       }
       if (temps.equals(".")) {
           if (y1 == 0) {
               textfield.setText(textfield.getText() + ".");
               y1 = 1;
           } else {
               textfield.setText(textfield.getText());
           }
       }
       if (temps.equals("+")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
               temporary = 0;
               choice = '+';
           } else {
               temporary = Double.parseDouble(textfield.getText());
               textfield.setText("");
               choice = '+';
               y1 = 0;
               x1 = 0;
           }
           textfield.requestFocus();
       }
       if (temps.equals("-")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
               temporary = 0;
               choice = '-';
           } else {
               x1 = 0;
               y1 = 0;
               temporary = Double.parseDouble(textfield.getText());
               textfield.setText("");
               choice = '-';
           }
           textfield.requestFocus();
       }
       if (temps.equals("/")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
               temporary = 1;
               choice = '/';
           } else {
               x1 = 0;
               y1 = 0;
               temporary = Double.parseDouble(textfield.getText());
               choice = '/';
               textfield.setText("");
           }
           textfield.requestFocus();
       }
       if (temps.equals("*")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
               temporary = 1;
               choice = '*';
           } else {
               x1 = 0;
               y1 = 0;
               temporary = Double.parseDouble(textfield.getText());
               choice = '*';
               textfield.setText("");
           }
           textfield.requestFocus();
       }
       if (temps.equals("MC")) {
           tm1 = 0;
           textfield.setText("");
       }
       if (temps.equals("MR")) {
           textfield.setText("");
           textfield.setText(textfield.getText() + tm1);
       }
       if (temps.equals("M+")) {
           if (k == 1) {
               tm1 = Double.parseDouble(textfield.getText());
               k++;
           } else {
               tm1 += Double.parseDouble(textfield.getText());
               textfield.setText("" + tm1);
           }
       }
       if (temps.equals("M-")) {
           if (k == 1) {
               tm1 = Double.parseDouble(textfield.getText());
               k++;
           } else {
               tm1 -= Double.parseDouble(textfield.getText());
               textfield.setText("" + tm1);
           }
       }
       if (temps.equals("Sqrt")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
           } else {
               tempa = Math.sqrt(Double.parseDouble(textfield.getText()));
               textfield.setText("");
               textfield.setText(textfield.getText() + tempa);
           }
       }
       if (temps.equals("SIN")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
           } else {
               tempa = Math.sin(Double.parseDouble(textfield.getText()));
               textfield.setText("");
               textfield.setText(textfield.getText() + tempa);
           }
       }
       if (temps.equals("COS")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
           } else {
               tempa = Math.cos(Double.parseDouble(textfield.getText()));
               textfield.setText("");
               textfield.setText(textfield.getText() + tempa);
           }
       }
       if (temps.equals("TAN")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
           } else {
               tempa = Math.tan(Double.parseDouble(textfield.getText()));
               textfield.setText("");
               textfield.setText(textfield.getText() + tempa);
           }
       }
       if (temps.equals("=")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
           } else {
               temporary1 = Double.parseDouble(textfield.getText());
               switch (choice) {
               case '+':
                   r = temporary + temporary1;
                   break;
               case '-':
                   r = temporary - temporary1;
                   break;
               case '/':
                   r = temporary / temporary1;
                   break;
               case '*':
                   r = temporary * temporary1;
                   break;
               }
               textfield.setText("");
               textfield.setText(textfield.getText() + r);
               z1 = 1;
           }
       }
       if (temps.equals("n!")) {
           if (textfield.getText().equals("")) {
               textfield.setText("");
           } else {
               tempa = fact(Double.parseDouble(textfield.getText()));
               textfield.setText("");
               textfield.setText(textfield.getText() + tempa);
           }
       }
       textfield.requestFocus();
   }

   double fact(double x1) {
       int er = 0;
       if (x1 < 0) {
           er = 20;
           return 0;
       }
       double p, temps = 1;
       for (p = 2; p <= x1; p += 1.0)
           temps *= p;
       return temps;
   }

   public static void main(String args[]) {
       try {
           UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
       } catch (Exception e) {
       }
       SwingCalculator f = new SwingCalculator();
       f.setTitle("ScientificCalculator");
       f.pack();
       f.setVisible(true);
   }
}

Scientific Calculator - 0 X MR logrithmic x1^2 AC

DON'T FORGET TO LIKE

THANKS BY HEART.

Add a comment
Know the answer?
Add Answer to:
write a program on eclipse where you would build your own scientific calculator that can use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT