944,087 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 9322
  • Java RSS
Mar 24th, 2006
0

calculator program

Expand Post »
hai,

i need some help in developing a simple calculator!

i've finished the front-end part using jigloo software

but the problem is when i click the first number,that number will appear in the textfield after that when i click the '+' button the textfield is clearing but i'm not able able to read the entered value.

Also tell me where should i implement the logic to add these 2 numbers
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sheki RDH is offline Offline
9 posts
since Mar 2006
Mar 24th, 2006
0

Re: calculator program

Get the first number
user clicks +... register that
Get the second number
Display addition.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Mar 25th, 2006
0

Re: calculator program

doesn't seem like a logical solution to me, but then I'm used to scientific calculators with built-in equation editors

Create the entire equation as a single string and parse that using (for example) a recursive descent parser.
There's a nice book that does pretty much just that in one chapter called "the art of Java", then in the next chapter turns that calculator into a simplistic BASIC interpreter.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Mar 27th, 2006
0

Re: calculator program

try this:
1.user enter number
2a.user click operator
2b.set code to store number to a variable
2c.set code to store the status of the (last) operator click
3. user enters next numbers
4a. user clicks another operator button
4b. update stored variable (in step 2b, calculating the new value, depending on the operator used in step 2c)...
5...

there is still alot of gap to fill but i think its a good starting point... booleans and counters may be a good helps when writing the real code

--
fd
Reputation Points: 10
Solved Threads: 0
Light Poster
fdrage is offline Offline
28 posts
since Jun 2005
Mar 27th, 2006
0

Re: calculator program

http://www.java2s.com/ExampleCode/Sw...Calculator.htm

Looks a good example, haven't tried it though.

Assuming you know that basics about making a GUI and responding to User Input I don't see many problems.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 27th, 2006
0

Re: calculator program

Quote originally posted by jwenting ...
doesn't seem like a logical solution to me, but then I'm used to scientific calculators with built-in equation editors
True, but I don't think they wanted a full fledged multi operator calculator that follows PEMDAS.

It would be nice to write an equation parse, because there could be some stumbling blocks somewhere in there. Most people would write a parenthesis balancer first, but do you really need it?

1 + (2-5)

should be the same as:

1 + (2-5

without the parenthesis

A simple equation like:

5*2+5*4/45

might not be that bad, but I think the parenthesis would get me.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Mar 28th, 2006
0

Re: calculator program

>might not be that bad, but I think the parenthesis would get me.

I was wondering dat so I decided to do some research into it and build
one on the fly.

I think it can be done using syntax trees. Essentially, you have
to write your own grammer and parsing engine however, the idea would
be as follows:

For example if your expression is:-

3*(7+1)/4+(17-5)

Then you would have your engine parse it into a syntax tree as follows:

Java Syntax (Toggle Plain Text)
  1. (+)
  2. / \
  3. / \
  4. / \
  5. (*) (-)
  6. / \ / \
  7. 3 (/) 17 5
  8. / \
  9. (+) 4
  10. / \
  11. 7 1

Evaluting the expression recursively from the bottom upwards yields the
correct solution.

Most of what I learnt from the binary search tree was obtained from narue'
handy tutorial's www.eternallyconfuzzled.com

My binary tree syntax is a bit rusty in java so I did it in C, however, if I feel like it,
I might write one for java wen I get home from school. Tee he he.

Simple as pie kiddo.
Attached Files
File Type: zip thwee.zip (182.8 KB, 299 views)
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Where can ! get some coding about Java-Security
Next Thread in Java Forum Timeline: how to built an applet in java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC