Program like ATM where person enters amount in dollars and cents but program uses int for monies
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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"
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
That is one way to do it.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
| straws are for suckers. tiny ads are for attractive people. The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |







