aspose file tools*
The moose likes Beginning Java and the fly likes creating a calculator application - need some math help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating a calculator application - need some math help" Watch "creating a calculator application - need some math help" New topic
Author

creating a calculator application - need some math help

charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
I've created buttons that add, subtract, multiply and divide and these work fine.

I now need to add squared, cubed and sqaure root.

For the sqaured button, I wrote this but it's not working:

int total = totalsofar^2

what am i doing wrong?

also, how would i sqaure root?
charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
i've just discovered the Math class so I have answered my own questions there. However, now I have a new question

I previously had all my values stored as integers and then decided that I needed them stored as doubles.

so at the moment i have:
I'm not really sure how to describe it, but is it because I'm reading in Integers from the string? i'm unsure of how to get it to read in the decimals.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You can use "Double.parseDouble()" instead of "Integer.parseInt()".


[Jess in Action][AskingGoodQuestions]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
To warn, do not use floating points when doing precise arithmetic calculations. Use BigDecimal.

Also see http://docs.sun.com/source/806-3568/ncg_goldberg.html
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39815
    
  28
If you look here, you find the ^ operator does something completely different from what you thought.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Math has a method called pow that does what you think ^ does. Or, for squares and cubes, simply calculate "totalsofar * totalsofar" or "totalsofar * totalsofar * totalsofar".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39815
    
  28
Calculators are never really precise; you can use a double, knowing that the imprecision will be slight. If you do get $1.10 - $0.99 = $0.109999999999999983465092374625 or similar, will that actually cause any problems?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

If so, follow Bauke's advice on BigDecimal.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating a calculator application - need some math help