File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to build a graphical calculator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to build a graphical calculator" Watch "How to build a graphical calculator" New topic
Author

How to build a graphical calculator

Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

I want to build a calculator, so that I can learn java, and I want to have it graphical, so it looks like one of the calculators that come default with your computer, give or take some functions. However, although I can imagine the coding that happens each time I hit a button, I don't know how to display anything. Can someone suggest to me the best way? I tried using the netbeans swing editor, but I didn't want to have to learn an IDE while programming. I could learn Swing, but I'm not sure that's the best.


"The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!', but 'That's funny...' "
- Isaac Asimov
Naishadh Parmar
Ranch Hand

Joined: Jun 02, 2011
Posts: 95

Ok,
First of all you need to learn Swing. Beacuse only that can be used to make a 'graphical' calculator.
Second you need to know how to substring the input given to you and break it down into the components of the whole equation, solve it, display the result.

So you need to first learn Swing, or better still start low -- develop a calculator which can be used in the command line "cmd". Simple java code that takes input from the cmd.
I did the same thing!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Naishadh Parmar wrote: . . . start low -- develop a calculator which can be used in the command line "cmd". Simple java code that takes input from the cmd.
I did the same thing!!!
Quite right. When you have a calculator working at the command line, you can add a GUI to it. Somebody else has a similar problem, which you would have found by searching the Ranch.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Naishadh Parmar wrote:First of all you need to learn Swing. Beacuse only that can be used to make a 'graphical' calculator.

JavaFX is an option as well.
Naishadh Parmar
Ranch Hand

Joined: Jun 02, 2011
Posts: 95

Yeah thanks Campbell,
In fact even I started by making a calculator in the cmd, and then I made it graphical. It was one of my first sucessful programs.
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

I was thinking of making it so that each time a button is pressed, it adds to a string, which is displayed in the answer part of the calculator. Each time a new button is pressed, I do [answer += ("button_pressed");] and when enter is pressed, it turns the string into an int, and it calculates. I then print the answer. Would that work?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

Alix Ollivier wrote:...and it calculates...

There is a LOT of hand waving here.

What I mean by that is that is can be rather convoluted to calculate this:

((6 + 3) * (18 - 7)^2 / ((14.79 + 84/11) *3 )) ^ 0.7

parsing that just to figure out what is the first step can be rather tricky.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Alix Ollivier wrote:I was thinking of making it so that each time a button is pressed, it adds to a string, which is displayed in the answer part of the calculator.

I agree with Fred. The way we write arithmetic expressions is NOT intuitive for a computer.

Could I suggest an alternative? Start out by writing a reverse Polish notation calculator. Much simpler, and at least you'll get all the arithmetic stuff sorted out before you venture into the realms of arithmetic expression parsing.

Also: DON'T start out with a GUI. Get your calculator working on the console first. If it doesn't work that way, it'll never work as a GUI; and you'll get sidetracked with Swing issues (which are many and varied; and I hate them ).

You'll probably do it with a third of the code too.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41116
    
  45
Ah, that brings back memories. Some of the first Java code I ever worked on implemented that; the result can be found here. I think that must have been in 1996 or 1997.


Ping & DNS - my free Android networking tools app
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Ulf Dittmer wrote:Ah, that brings back memories.

Us old farts have to stick together; though I fear that may be an older fart than you.

Winston
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

There is a LOT of hand waving here.


When I wrote that it would calculate, I meant automatically. I will start off with a basic calculator, with only simple problems like addition to division. If I do problems like that, then there is no hand waving. Say I have a calculator written. I click a button, and the symbol it has is included to the "answer" string. When I finally press the "equals" sign, it converts the "answer" string into an int, which is then calculated. By calculated, I mean that if you make an int with the value of 1+2+3+4, it will make the value of the int 10.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Alix Ollivier wrote:When I wrote that it would calculate, I meant automatically. I will start off with a basic calculator, with only simple problems like addition to division. If I do problems like that, then there is no hand waving. Say I have a calculator written. I click a button, and the symbol it has is included to the "answer" string. When I finally press the "equals" sign, it converts the "answer" string into an int, which is then calculated. By calculated, I mean that if you make an int with the value of 1+2+3+4, it will make the value of the int 10.

OK, but even if you
  • don't allow brackets
  • work strictly from left to right
  • keep strictly to the 4 arithmetic ops
  • you're going to run into things like 2+-3 unless you have a separate key (or symbol) for a leading minus sign.

    The nice thing about RPN is that it's simple. It also allows you perform almost any arithmetic sum without brackets. That allows you to ease yourself into the problem gradually and get the basic stuff working, rather than taking on a lot of extra work at the start.

    Another thing: Even if you don't take my advice, I'd strongly suggest that you require whitespace between your values and your symbols:
    '1 + 2 + 3 + 4' is an awful lot easier to parse than '1+2+3+4'.

    Winston
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38016
        
      22
    What do you mean almost? Surely you can write anything in postfix? I spend quite a lot of time using FORTH, which naturally uses postfix notation.
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7549
        
      18

    Campbell Ritchie wrote:What do you mean almost? Surely you can write anything in postfix? I spend quite a lot of time using FORTH, which naturally uses postfix notation.

    Yup you're right.

    Winston
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38016
        
      22
    Getting things from infix which we are used to into postfix (or even prefix, as used by LISP, (+ 1 2 3) for example) means creating a parse tree, then iterating the parse tree depth‑first, usuallky left‑to‑right.
    Inorder traversal: infix output.
    Preorder traversal: prefix output.
    Postorder traversal: postfix output.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to build a graphical calculator
     
    Similar Threads
    What to do next?
    Linking Buttons and Textbox
    Wanted: Advise on threads and application layout
    Java Calculator
    Guidance in choosing a pattern