• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 80137
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80137
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80137
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I said earlier, you really want to keep doubles out of money.
That is one way to do it.
 
Ryan Sykes
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80137
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. It is a poor example that doesn't really illustrate how floating point arithmetic can give rise to inaccurate results.
 
Something must be done about this. Let's start by reading this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic