ok i am writing a program that ask for the amount total and amount tender.
then i'm supposed to calculate a combined amount of coins and dollars to make up the change amount.

i know how to split out the dollar amount, but i'm stuck on how to calculate the combined amount of coins.

i can't use the if/else commands.

can somebody please give me a hint??? you don't have to write any code for me, but just help me on the math part.

Member Avatar for coil

What do you mean by combined amount of coins?

Do you mean the number of coins total to give for change, the value of the coins, or how to calculate how to offer change for a certain amount?

i mean so if the change was $1.52 then it'll display 1 dollar, 2 quarters, and 2 pennies.

Member Avatar for coil

Oh OK. Well you said you can already split the coins up right? Then just use a bunch of if statements.

e.g.

String s="";

if(numDollars!=0)
	s+=numDollars + " dollar(s) ";

and so forth

my teacher said that i can't use if statements...

i'm at a point where i need to convert a char into int. how do i do this?
here is how i extract the dollar amount.

change = (amountPaid - total);
String dollars = Double.toString(change);
char firstNum = dollars.charAt(0);
int dot = dollars.indexOf('.');
char secondNum = dollars.charAt(dot + 1);
char thirdNum = dollars.charAt(dot + 2);				
double coins = secondNum + thirdNum;

also how do i shorten the change value when it is displayed? it gives me something like 1.5265656566

ok nevermind the above post. i need a new way to extract the dollar amount. for example if the change is 5.26, i need a way to read the "5."

i've previously converted it to a string and then read it from there, but then things got complicated. do i need to convert the string back to a double or something to do math?

Okay, so you can't use ifs, that means you have to use integer arithmetic.

Think of it this way: if you need to give $1.52 in quarters, dimes, nickels and pennies, one way to do it would be to take .25 at a time out of that sum until there was less than a quarter. After that, you can take dimes until you're under a dime, then you take as many nickels as you can, then you give one penny at a time.


Looking at your post above - don't try to do this with Strings. Make yourself an integer. $5.26 is 526 cents, do your calculation on that integer value. It'll be much easier to deal with. (you can modify the procedure I outlined to deal with dollars pretty easily as well)

Have fun, come back with your code or on it.

This is an inefficient procedure, but I think you can probably write it easily enough, and without any ifs (or ands or buts, for that matter!)

Once you've written that, we can talk about improving it.

Member Avatar for coil

By the way, if you do need to convert:

Most primitives have a wrapper-class toString() method. int->String: Integer.toString(i); Most primitives also have a conversion from String method as well. String->int: Integer.parseInt(i); Therefore, for something like char to int: Integer.parseInt(Character.toString(c));

Also the Character class has a method for char to int: getNumericValue

i got this!!! yea!!!!!!!

i just divided the change amount by 1, then use the reminder to divide by .25, then by .10, and so on. then i converted the doubles to int and display the output. it works!!!

Cool. The fact that you worked out the division part from what you were given is pretty good, it shows that you're thinking about what you're doing. Good work.

You should probably take a little time to look at floating point arithmetic at some stage. Floating point representation in computers is necessarily inexact, and you might run into trouble with comparison of values. It's possible, for example, that two values which should be equal will differ somewhere out in the godknows-th decimal place - and then they won't be equal. For discrete quantities like money, it's usually best to use ints, to avoid these errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.