aspose file tools*
The moose likes Beginning Java and the fly likes Program like ATM where person enters amount in dollars and cents but program uses int for monies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program like ATM where person enters amount in dollars and cents but program uses int for monies" Watch "Program like ATM where person enters amount in dollars and cents but program uses int for monies" New topic
Author

Program like ATM where person enters amount in dollars and cents but program uses int for monies

Christina Bremmerman
Ranch Hand

Joined: Feb 02, 2012
Posts: 33
I've been stuck on this aaall night(it's now 8am) this is my last resort, I don't know what else to do to get this lab to work. First of all here is the assignment I am on the last section(Part C)

http://webpages.sou.edu/~sahrk/cs257/labs/lab03/lab03.pdf

Basically the user is the only one dealing in doubles. the class actually has balance as an int and everything else as doubles still
and here is my Account class



I got frustrated and started over so right now I know that I don't have my multiplication in all the right places, but the big place I'm getting stuck is the initial up in the constructor. How do I use casting so that it won't take away the change?

Any help, hints, tips...I'm pretty desparate right now and it'd be greatly appreciated.
Thank you
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
You are quite right to use ints for money, and denominate everything in ¢. Most people, as you will have seen, make the mistake of allowing floating-point arithmetic (eg doubles) anywhere near their money. You will have to get rid of the double arithmetic with the 0.035; use * 7 / 200 instead, but that will restrict you to a maximum balance of approx $3000000, before you get overflow.

You appear to be mixing doubles and ints indiscriminately. Don’t you get compiler errors?
Where do you get 10000 from? I always thought there were 100¢ to the $1.

You can make life easier for yourself by using BigDecimal; that way you can have a BigDecimal or a String as an argument for the constructorThere. I quite enjoyed writing all those overloaded constructors Notice you do not have a constructor taking a floating-point number as a parameter. Your 3½% would be new BigDecimal("0.035"). Note "" to use Strings not doubles.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
If you are really desperate to maintain the doubles, tryThe + 0.5 is to try and round out tiny errors, otherwise every now and again you will get $123.45 turning into ¢12344. Even that is not reliable. A double value greater than 21474386.47 will produce an overflow error. You will not get the rounding you expect from negative values.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7653
    
  19

Christina Bremmerman wrote:I got frustrated and started over so right now I know that I don't have my multiplication in all the right places, but the big place I'm getting stuck is the initial up in the constructor. How do I use casting so that it won't take away the change?

Any help, hints, tips...I'm pretty desparate right now and it'd be greatly appreciated.

Yes.

Stop. Chill out. If you're old enough, have a beer; and get a good night's sleep. And take your 'F' tomorrow like a man (or as close as you can get to it as a girl).

NEXT TIME: Ask for help sooner.

After you've swallowed your 'F', come back to this thread and read the suggestions when you're in a frame of mind to digest them. Right now, you seem to be all over the place and assuming all sorts of things that the Java Gods never intended (the overriding philosophy of the language - despite its detractors - has always been simplicity).

Programming is basically logic, so its best dealt with when you're calm and rational.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2333
    
  28

Why are you multiplying everything by 10000 instead of 100? Do you want precision beyond cents? Alson, when you convert from a double to a long, you need to downcast it

So for example, if you did something like this



Compiler will cry because you cannot implicity convert double to long. Implicit conversion here requires loss of precision and Java won;t let you do it. What you need is explicit downcasting



So, now in this case


d is a double and 1000 is an int. Java will implicity upcast the 1000 to a double, and then do the multiplication. A double multiplied by a double is another double. So, d*1000 is a double. Since, you cannot implicity downcast a double to a long, you need to explicitly downcast it. So you have to do this



Besides that looking at your code, everything looks fine to me except.. addInterest shouldn't multiply by 10000 and getBalance() should divide by. The constructor as it;s written shouldn;t "take away the change"
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18724
    
  40

Winston Gutkowski wrote:Stop. Chill out. If you're old enough, have a beer; and get a good night's sleep. And take your 'F' tomorrow like a man (or as close as you can get to it as a girl).

NEXT TIME: Ask for help sooner.



That's a pretty brutal response Winston...

Having spent a few all-nighters, I never recommend just giving up on the deadline. After all, it happens in real life. You can't give up if you have a business presentation, or a trade show, that you are trying to meet the deadline for. A school assignment deadline isn't much different.

There are some suggestions in this topic that can be used. Try again.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Christina Bremmerman
Ranch Hand

Joined: Feb 02, 2012
Posts: 33
Thank you all for your replies. I took a break and a nap and I'm about to take another shot at this. I still have about 6 hours to come up with something better than my sleep deprived brain was giving me. It seems in my haze I didn't get enough detail in my initial reply. The 10000s are how far out he wants the number for this lab and I can't add any other variables for the balance I have to strictly use the int balance
Change the data type of the Account class instance variable balance to int. This instance
variable will store the current account balance as some number of 1/100th’s of a cent. For
example, if the current balance is $54.67, then the value of the instance variable balance should
be 546700. Note: you may not create a second instance variable that stores the current balance as
a float or double. The only instance variable that can be used to keep track of the current balance
is the int balance
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7653
    
  19

Henry Wong wrote:That's a pretty brutal response Winston...

Perhaps; and if so, I apologise.
I too have done all-nighters, but if I'm honest about it, the times they actually worked are outnumbered by the ones where I simply wasted the following day by walking around like a zombie.

The number of times I've gone to bed, and woken up a few hours later with a 'Eureka' moment, however....
Sometimes the best way to solve a problem is to give it a rest.

Christina, my final piece of advice: Keep a notebook by your bed.

Winston
Christina Bremmerman
Ranch Hand

Joined: Feb 02, 2012
Posts: 33
Well I had to come back and post this for Winston. It works, and it fits the lab assignment. So I think I avoided the F even though it seems like there should have been an easier way to do this. But my mom came online and gave me similar advice(She said to drink cocoa though, not beer LoL)
Also I do understand the value of sleep, just last week I had a bad loop in my assignment that I spent hours comparing to other loops and trying to alter to no avail, I went to sleep, woke up, added a -1 and it worked.
I just felt like I didn't have enough done to sleep just yet and I ended up just digging myself deeper and deeper


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7653
    
  19

Christina Bremmerman wrote:Well I had to come back and post this for Winston. It works, and it fits the lab assignment. So I think I avoided the F...

Hey, what do I know? Sometimes it's good to be proved wrong.

Well done.

Winston
Ryan Sykes
Ranch Hand

Joined: Jan 18, 2012
Posts: 58
I think you have an error here.

Balance is an int, as is 10000. The division will give you an integer without any decimal digits, so you won't get any information about the amount in cents. Did you get the same output as Part B?
I think that something like this would have been the correct approach:

Dividing by a double would return a result in a value that is still a double. I wrote this small test program:

The resulting output was:

If you look carefully you will understand why the 1st and 3rd conversions gave the correct result. I noticed that you have used something of the form in your program, but it is not clear if you truly understood why it worked. It worked because the (double) type cast operates first on balance, which is an int, and this is then subsequently divided by an int. So a double / int results in a double output. I would personally prefer something like the 6th line of code as the use of the parenthesis makes things more clear.

I had a question myself: Which of the following snippets of code is the better way to approach things?

I personally think it should be the code on line 1 as it would prevent the occasional alignment of numerical errors in amount and fee that add/subtract out to push the resulting number into being rounded up or down the wrong way. Is that correct or are both lines of code equivalent?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
I said earlier, you really want to keep doubles out of money.
That is one way to do it.
Ryan Sykes
Ranch Hand

Joined: Jan 18, 2012
Posts: 58
Thanks Campbell. That is what I was thinking would be the correct way to go about things. I guess in this particular assignment though, they ask that you not change the Account class in terms of the API/public class interface. So you do still have to return a value of type double, but perhaps it would be best to still construct the return value in the manner you wrote above but with type double.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
No, you still shouldn’t use floating-point for money. It is a shame that some people who set assignments don’t seem to know that.
Ryan Sykes
Ranch Hand

Joined: Jan 18, 2012
Posts: 58
I agree. It is a poor example that doesn't really illustrate how floating point arithmetic can give rise to inaccurate results.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program like ATM where person enters amount in dollars and cents but program uses int for monies