I am attempting to find a solution to an exercise in Deitel and Deitel How to Program Java. The exercise says you should be able to modify the code below to use only integers.
I am stuck since using an int for the interest calculations soons becomes too large to be of use. Do you have any suggestions?
long  64 bit
int  32 bit
Integer class  wrappered int
Long class  wrappered long
Les
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
S Connor wrote:hi, I used an int to represent the rate of interest, which is 5%
to find 105% of a given value I used an int 105.
To find the following years interest I thought would be 105*105 and the following year 105*105*105 etc. This soon became too long for an int to hold.
But 105 * 105 is 11025, which would represent 11025% and not 110.25%, so that isn't correct. Besides, that calculation doesn't exist in the original code so it shouldn't be in your revised code either, should it?
S Connor wrote:hi, I used an int to represent the rate of interest, which is 5%
to find 105% of a given value I used an int 105.
To find the following years interest I thought would be 105*105 and the following year 105*105*105 etc. This soon became too long for an int to hold.
It sure would!
Think about the display on a calculator:
It is made up of so many digits, right? One place to start might be to observe that each individual digit is an integer. From there, you could have an array, or a class, that used individual integer digits to do what a calculator display does.
But, that's probably more complicated than it has to be... In that picture, the display is showing a number that has a "whole" part (87), and a "decimal" part (93). The whole part could be represented by a single integer, couldn't it? Now, what about the decimal part? Well, it could also be represented by an integer, but it's a little more complicated than when you use an integer to represent the whole part. Why? Because the whole part is just 87, but the decimal part is 93 somethings. 93 what? 93 hundredths. Now, two decimal places of accuracy may be enough for your problem, but it may not. Suppose you wanted three decimal places. The number in the display would be "87.930," which we know is numerically equal to 87.93. But, if you represented the decimal part with 930, and still treated that as 930 hundredths, your result would, numerically, be 96.300, because you'd be combining 87 and 9.30 into the final value. So, to make it work, you have to remember that, if you want three decimal places of accuracy, 930 means 930 thousandths.
In this scheme, you start by deciding how many decimal places of accuracy you want, and treat your decimal part integer accordingly.
To do math, you will need routines that can add, subtract, multiply, and divide numbers represented this way. Adding and subtracting are pretty straightforward. To multiply, remember that, for any two numbers, X and Y, where X = A + B and Y = C + D, X * Y = A * C + A * D + B * C + B * D.
Division is messier, but leave that until after you have addition, subtraction, and multiplication working.
Hope that helps.
"Il y a peu de choses qui me soient impossibles..."
S Connor wrote:The exercise says you should be able to modify the code below to use only integers.
It's not clear to me what exactly that means (or maybe that wasn't exactly the way the requirement was phrased). In one interpretation you would just replace the double variables by int variables and scale the calculations accordingly. In another interpretation you wouldn't be allowed to use Math.pow() because it returns a double value, and you would have to rewrite that code using your own integeronly logic.
In the second interpretation you have the problem that you can't represent the second year's interest rate (10.25%) without having to rescale the data in the way you described, and as you say it doesn't take long to overflow. Now perhaps you could use long instead of int, as Les implied  notice that the requirement was to use integers and not int variables, assuming that you quoted the requirement accurately. But all of this seems a bit much for what is meant to be a fairly simple beginner exercise.
So if it was me I would just go with my first interpretation, and leave the Math.pow() part as is, except for scaling the inputs. Looks like Stevens has provided a detailed post about how scaling should work.
The hint is in the header.
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
EDIT: Actually, highlighting "compound interest" might be misleading since I did convert all the variables to int and scaled the values as others suggested, and I didn't introduce any new variables either. So scaling and accumulation is the key to making the necessary modifications.
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
amount / 100.0 forces a conversion to double.
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
S Connor wrote:The exercise says you should be able to modify the code below to use only integers.
<literalist mode>
I don't think this is actually possible, taken at face value. There is no way I know of to modify this line to use only integers:
by definition, it has to have a String Array, which is not an Integer.
</literalist mode>
There are only two hard things in computer science: cache invalidation, naming things, and offbyone errors
Only Year 8 is different by 0.01
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
I would challenge you to a battle of wits, but I see you are unarmed  shakespear. Unarmed tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
