hi there.!
I have some problem about my calculator.
I already made basic calculator but I want to developed it more instead of computing only 2 input I would like to compute this more than two. my calculator looks like windows calculator.

tnx and more power. =)

Recommended Answers

All 18 Replies

So .... what functionality you want to add, what have you tried to add that functionality, if not I would suggest you jot down somewhere clearly what you want this calculator ... and then work on an implementation. If you get stuck there we are here to help ..

my calculator was only only computed with 2 the two input of the user. like for example:
first try: 1+2+4=3
second try: 2/1+1=2

i want my calculator to compute more than two.
I want it look like JFrame functions;

there is no such thing as a JFrame function the way you describe it, you mean to say a Java desktop application which has a Swing GUI.

first, you'll need to check for yourself, how deep and correctly you're willing to go: for instance:

2 + 6 * 3 = ?
will you use the correct order? meaning
2 + 18
or are you 'content' starting off with first getting everything done, like
8 * 3

also, will you take into account the use of '(' and ')'?

I'm pretty sure a lot of people around here are able and willing to help you, but you will be asked to do the most of the work yourself.
show us the code you have so far, and how you think it should/could be improved, we'll help you filter out the bugs, but don't expect us to write the entire application ourselves.

package javaapplication20;
//***************************************************************************************************************************************
//***************************************************************************************************************************************
//***************************************************************************************************************************************
//***************************************************************************************************************************************
//*******                                      ~~~~~~~~~  INFORMATION ABOUT CODE  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//*******                                      ~~~~Code Creater: Master Saurabh Ankush Mukhekar.                        ~~~~~
//*******                                      ~~~~Email=saurabh1mukhekar@gmail.com                                     ~~~~~
//*******                                      ~~~~Website:[url]http://apanasaurabh.blogspot.com[/url]                              ~~~~~
//*******                                      ~~~~Aim: This code is very simple and understandable                     ~~~~~
//*******                                      ~~~~for the Core Java student to get the idea of the                     ~~~~~
//*******                                      ~~~~LAYOUT MANAGERS ,BUTTONS and SIMPLE EVENT HANDLING etc               ~~~~~
//*******                                      ~~~~                                                                     ~~~~~
//*******                                      ~~~~Tip:All the Buttons working properly except MS,MC & MR               ~~~~~
//*******                                      ~~~~                                                                     ~~~~~
//*******                                      ~~~~Note:Please give me feedback if you find any drowbacks in my code    ~~~~~
//*******                                      ~~~~~~~~~~~ Thank U & ENJOY MY CALCULATOR  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//***************************************************************************************************************************************
//***************************************************************************************************************************************
//***************************************************************************************************************************************
//***************************************************************************************************************************************


import java.awt.*;
import java.awt.event.*;
 class calc extends javax.swing.JFrame implements ActionListener
{
  Button b1,
          b2,
          b3,
          b4,
          b5,
          b6,
          b7,
          b8,
          b9,
          b10,
          b11,
          b12,
          b13,
          b14,
          b15,
          b16,
          b17,b18,b19,b20;
  TextField t1;
  String data;
  int oper=0;

   calc()
   {
     super("My Calcultor");
     setVisible(true);
     setSize(200,230);
     setLayout(new BorderLayout());


     t1=new TextField(" ");

     add(t1,"North");

     Panel p1=new Panel();
     add(p1,"Center");

     p1.setLayout(new GridLayout(5,4));

     b1=new Button("7");
     p1.add(b1);

     b2=new Button("8");
     p1.add(b2);

     b3=new Button("9");
     p1.add(b3);

     b4=new Button("+");
     p1.add(b4);

     b5=new Button("4");
     p1.add(b5);

     b6=new Button("5");
     p1.add(b6);

     b7=new Button("6");
     p1.add(b7);

     b8=new Button("-");
     p1.add(b8);

     b9=new Button("1");
     p1.add(b9);

     b10=new Button("2");
     p1.add(b10);

     b11=new Button("3");
     p1.add(b11);

     b12=new Button("*");
     p1.add(b12);

     b13=new Button("0");
     p1.add(b13);

     b14=new Button(".");
     p1.add(b14);

     b15=new Button("=");
     p1.add(b15);

     b16=new Button("/");
     p1.add(b16);

     b17=new Button("CLS");
     p1.add(b17);

     b18=new Button("MS");
          p1.add(b18);

     b19=new Button("MR");
     p1.add(b19);

     b20=new Button("MC");
     p1.add(b20);


     b1.setBackground(Color.ORANGE);
     b2.setBackground(Color.BLUE);
     b3.setBackground(Color.GREEN);
     b4.setBackground(Color.ORANGE);
     b5.setBackground(Color.BLUE);
     b6.setBackground(Color.GREEN);
     b7.setBackground(Color.ORANGE);
     b8.setBackground(Color.BLUE);
     b9.setBackground(Color.GREEN);
     b10.setBackground(Color.ORANGE);
     b11.setBackground(Color.BLUE);
     b12.setBackground(Color.GREEN);
     b13.setBackground(Color.ORANGE);
     b14.setBackground(Color.BLUE);
     b15.setBackground(Color.GREEN);
     b16.setBackground(Color.ORANGE);
     b17.setBackground(Color.BLUE);
     b18.setBackground(Color.GREEN);
     b19.setBackground(Color.ORANGE);
     b20.setBackground(Color.BLUE);



     b1.setForeground(Color.BLACK);
     b2.setForeground(Color.WHITE);
     b3.setForeground(Color.BLACK);
     b4.setForeground(Color.BLACK);
     b5.setForeground(Color.WHITE);
     b6.setForeground(Color.BLACK);
     b7.setForeground(Color.BLACK);
     b8.setForeground(Color.WHITE);
     b9.setForeground(Color.BLACK);
     b10.setForeground(Color.BLACK);
     b11.setForeground(Color.WHITE);
     b12.setForeground(Color.BLACK);
     b13.setForeground(Color.BLACK);
     b14.setForeground(Color.WHITE);
     b15.setForeground(Color.BLACK);
     b16.setForeground(Color.BLACK);
     b17.setForeground(Color.WHITE);
     b20.setForeground(Color.WHITE);



     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);
     b10.addActionListener(this);
     b11.addActionListener(this);
     b12.addActionListener(this);
     b13.addActionListener(this);
     b14.addActionListener(this);
     b15.addActionListener(this);
     b16.addActionListener(this);
     b17.addActionListener(this);





  }



public void actionPerformed(ActionEvent me)
{

    int a,b,c,d,e,f,g,h,i,j,k;String s="";float cal;

    if(me.getSource()==b1)
       {

        s=t1.getText();
        t1.setText(s+"7");

       }

    else if(me.getSource()==b2)
       {

        s=t1.getText();
         t1.setText(s+"8");

       }
    else if(me.getSource()==b3)
       {

        s= t1.getText();
         t1.setText(s+"9");

        }
    else if(me.getSource()==b5)
       {

        s=t1.getText();
         t1.setText(s+"4");

       }

    else if(me.getSource()==b6)
       {

        s=t1.getText();
         t1.setText(s+"5");

       }

    else if(me.getSource()==b7)
        {

          s=t1.getText();
          t1.setText(s+"6");

         }

    else if(me.getSource()==b9)
        {

         s=t1.getText();
          t1.setText(s+"1");

        }

    else if(me.getSource()==b10)
        {

         s=t1.getText();
          t1.setText(s+"2");

        }
    else if(me.getSource()==b11)
       {

        s=t1.getText();
         t1.setText(s+"3");

       }
     else if(me.getSource()==b13)
       {

         s=t1.getText();
          t1.setText(s+"0");

        }
      else if(me.getSource()==b14)
       {

         s=t1.getText();
          t1.setText(s+".");

        }

    else if(me.getSource()==b4)
       {
             oper=1;
             data=t1.getText();
              t1.setText("+");

        }

    else if(me.getSource()==b8)
       {
             oper=2;
             data=t1.getText();
              t1.setText(" ");

           }
    else if(me.getSource()==b12)
       {
             oper=3;
             data=t1.getText();
              t1.setText(" ");

           }

       else if(me.getSource()==b16)
       {
             oper=4;
             data=t1.getText();
              t1.setText(" ");

        }


     else if(me.getSource()==b15)
           {

                  if(oper==1)
                 {
                      cal=Float.parseFloat(data)+Float.parseFloat(t1.getText());
                    t1.setText(""+cal);

                 }
                 if(oper==2)
                 {
                     cal=Float.parseFloat(data)-Float.parseFloat(t1.getText());
                    t1.setText(""+cal);

                 }
                 if(oper==3)
                 {
                      cal=Float.parseFloat(data)*Float.parseFloat(t1.getText());
                    t1.setText(""+cal);

                 }
                 if(oper==4)
                 {
                      cal=Float.parseFloat(data)/Float.parseFloat(t1.getText());
                    t1.setText(""+cal);

                 }


           }
       else if(me.getSource()==b17)
         {

         data=" ";
         t1.setText("");

         }

  }

}



  class CalulatorDemo
  {

    public static void main(String aas[])
    {

    calc f=new calc();


    }


  }
 //5608 characters, 510 words, 336 lines

this code is not mine.

this code is not mine.

yes, I've noticed.
it also look somewhat like rubbish to me. a bit like someone learned a very bit about Swing, and wrote a blog to help starters "really learn development the right way".

if he was only a bit of a decent developer, he would've used JTextField instead of TextField, not to mention the way he organizes his code.

just to quote the author:

Aim: This code is very simple and understandable ~~~~~
//******* ~~~~for the Core Java student to get the idea of the ~~~~~
//******* ~~~~LAYOUT MANAGERS ,BUTTONS and SIMPLE EVENT HANDLING etc ~~~~~
//******* ~~~~ ~~~~~
//******* ~~~~Tip:All the Buttons working properly except MS,MC & MR

1. as I said, this code is crap, there are a lot better ways to learn how to program
2. working properly: a car being pushed down a hill will 'drive' too, but that doesn't mean at that time it's "working properly"
3. he states himself that several buttons don't even work. throw away this code and start your own application. you said you already have a basic calculator, re-use whatever you want/can from it, don't just copy code you don't know/understand. if it's for a school assignment, and your teacher sees you hand in something like this, either he's really not qualified for the job, or he 'll see immediately notice you cheated.

Absolutely ur ryt. It's my homework. Sorry to tell but I can't post my own code here bcos my teacher says that if one of my classmates has the same codes including the output we were fail.

I only post that code in order for me to gather some idea to improve my program by my own.

well .. since we have no idea what your code looks like, we can't really comment on it, now can we? :-/

we cant really help u with ur problem if u dont show what u can do, dont be shy everyone of us begin like this :)

My program was just look like those program I've post

in which case, improvement is possible, but "looks like" and "is the same" are no synonyms, so don't ask where.

I just only need to improve my calculator.
I need to calculate my calculator operations(5+3-1=7)
please help through the code that I've posted a while ago.
tnx in advance.

Can you answer these first

first, you'll need to check for yourself, how deep and correctly you're willing to go: for instance:

2 + 6 * 3 = ?
will you use the correct order? meaning
2 + 18
or are you 'content' starting off with first getting everything done, like
8 * 3

also, will you take into account the use of '(' and ')'?

next if your still using that code, I believe that just adding lines of codes won't be enough for this. There's gonna be a lot of changes... how about checking examples for calculators made in java from the web

i want my calculator to compute more than two.
I want it look like JFrame functions;

you want your calculator to be able to do more then a single operation on multiple numbers, you would have to add an accumulator, which will be much like what you have now except the answers will be accumulated (added to a running total)until = is pressed this total will be displayed and set to 0

then post some code snippets exactly where u are stuck, and where where u wish we can help u imporove it, i have developed a very interactive java calc, so do dat we are at ur service.

guys. thanks for your time. I highly appreciate your efforts.
My problem was now solve. tnx anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.