• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to build a graphical calculator

 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naishadh Parmar
Ranch Hand
Posts: 95
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12203
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Pie
    Posts: 50278
    80
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 10527
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 50278
    80
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic