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.

Edited 6 Years Ago by plasticfood: n/a

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.

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

Edited 6 Years Ago by coil: n/a

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

Edited 6 Years Ago by plasticfood: n/a

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?

Edited 6 Years Ago by plasticfood: n/a

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.

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));

Edited 6 Years Ago by coil: n/a

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!!!

Edited 6 Years Ago by plasticfood: n/a

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.

This question has already been answered. Start a new discussion instead.