programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Coding Problem Using Only Integers

Ranch Hand
Posts: 30
Hi,

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?

Sheriff
Posts: 23262
46
I don't see why something (you didn't say what) becomes too large. You didn't say what you tried, either, but I would suggest using cents (or pence or whatever you call them) for money and an integer value for the interest rate percentage.

Rancher
Posts: 783
19
In Java there are multiple "integer" types you should be aware:

long - 64 bit
int - 32 bit

Integer class - wrappered int
Long class - wrappered long

Les

S Connor
Ranch Hand
Posts: 30
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.

Paul Clapham
Sheriff
Posts: 23262
46

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?

Bartender
Posts: 1445
30

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.

Paul Clapham
Sheriff
Posts: 23262
46

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 integer-only 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.

S Connor
Ranch Hand
Posts: 30
The wording of the question is:
"Modify the application in Fig 5.6 to use only integers to calculate the compound interest."

Sheriff
Posts: 11629
187
It can be done, although there's a slight difference in the two:

The hint is in the header.

Junilu Lacar
Sheriff
Posts: 11629
187
The solution I used to get those results adheres to the requirement to "use only integers to calculate the compound interest" - you'd still have to use scaling in the calculations and a double for the resulting amount, of course.

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.

Junilu Lacar
Sheriff
Posts: 11629
187
One more hint (I don't think this gives too much away since you still have to figure out the calculation with ints). You don't need to introduce any new variables:

amount / 100.0 forces a conversion to double.

Junilu Lacar
Sheriff
Posts: 11629
187
One last hint: I'm not saying that what he wrote is wrong but my eyes glazed over just skimming Steven's response below the image of the calculator display. It's way too early on a Friday morning for me to process all that. What I did was much simpler.

lowercase baba
Bartender
Posts: 12601
50

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>

Junilu Lacar
Sheriff
Posts: 11629
187
Interestingly, I found that if you increase the scale by one place, you get (nearly) identical results:

Only Year 8 is different by 0.01

 I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS