tags:

views:

276

answers:

4

I need some help with my homework assignement. My assignment is to create a program that creates a Till object, takes a payment, issues exact change, tells me which coins I need to use and then tells me how much is in the till after. Below is the code I have written. The USmoney class is done and is working. The teacher offered a cheat sheet. However it's the main class (CoinCalc), getting the till to take the payment and subtract the payment from the amount paid to give me the change that I'm having issues with. Any help would be most appreciated.

public class USmoney {
  int dollars = 0;
  int cents = 0;

  public USmoney(int newcents) {
    dollars = newcents /100;
    cents = newcents %100;
  }
  public USmoney(int dollars, int cents) {
    this.dollars = dollars;
    this.cents = cents;
  }

  public int getDollars() {
    return dollars;
  }

  public int getCents(){
    return cents;
  }
}

public class CoinCalc {
  public static void main(String[] args) {
    USmoney Till1 = new USmoney(100,0);
    USmoney billTotal = new USmoney(49);
    USmoney amountPaid = new USmoney(100);

    double penny = 1;
    double nickel = 5;
    double dime = 10;
    double quarter = 25;

    double[] Coin = new double []{penny,nickel,dime,quarter};

  }

  private void changeFor(USmoney billTotal, USmoney amountPaid) {
  }
}
+2  A: 

This is a varient of the knapsack problem, you have a couple of steps to implement

  1. Calculate total change
  2. Satisfy change using smallest number of coins

I'd start by implementing a method with the first signature in your USMoney class, and then read up on the knapsack problem in order to implement the second method.

public USMoney subtract(final USMoney value);

public double[] getCoins(final USMoney value);
Jon Freedman
Knapsack problem? The problems might be related, but this problem is much easier. For this homework question dynamic programming would be overkill.
Ishtar
I'm just trying to give an answer that doesn't provide the solution, its a 1 dimensional packing problem, depends on whether the point of the homework is to learn Java or think about algorithms.
Jon Freedman
A: 

In order to call the changeFor() method you need a reference to a CoinCalc class. You can do this different ways, one way is to declare a variable of the CoinCalc class in the main() method like so:

CoinCalc cc = new CoinCalc();

cc.changeFor(billTotal, amountPaid);

But there are still problems, the method changeFor() currently does nothing and the way you have declared the till object and coin array they are not accessible in the changeFor() method.

So work on that problem for a bit then get back to us...

EDIT

Here is how I would structure it, billTotal is a float amountPaid is a USMoney object that internaly keeps track of each denomination of money ($1 bill, quarter, dime, nickel and penny). When someone pays they give you differing amounts of those kinds of money. USMoney needs and method that will return the value of the kinds of money it has (sum of dollars, quarters, etc).

till is another instance of USMoney that has an initial value of the different denominations of money.

USMoney has a method called changeFor(float bill, USMoney paid) that returns another instance of USMoney. You would then call it like so:

USMoney change = till.changeFor(bill, amountPaid);

The changeFor() method then has to determine how to make change based on what is in the till and the difference between bill and the value of amountPaid. NOTE if you don't have enough quarters your calculations should be smart enough to use dimes and nickels if it has enough. If you want to really get clever throw an exception if you can't make change.

BigMac66
Thats part of the the problem. I can see in my head what each method needs to do for the most part. but i cant seem to get it down in code. Basically the changefor method needs to take the difference from the billTotal and the amountPaid objects. If this logic is correct, how do you subtract the attributes of these objects. or am i completely off base?
Jon
A: 

It looks like you want help with your changeFor algorithm, so here's an outline:

First subtract billTotal from amountPaid. That will get you how much change to make.

Then make some variables to hold how much change you're giving back. For each of quarter, dime, nickel, penny, if changeRemaining is > the value of the coin, add a coin and subtract the value. Do this for each coin in order until that coin's value fails the test, then move on to the next coin.

Nathon
Thats exactly what i need. However how do i subtract the two objects?
Jon
Think how you would do it on paper, for example $2.20 - $1.30 = $0.90 - you have the dollars and cents as separate values, so you need to first deal with the cents, and then the dollars
Jon Freedman
Sorry what i was meaning is how do i either set the object billTotal and amountPaid to int values so i can do math on them
Jon
You'll want to write a method in USMoney that subtracts 1 of them from the other.
Nathon
+2  A: 

There are two issues here, which you can consider separately.

  1. Are you able to describe algorithmically how to determine the answer to your question?
  2. Given this description, can you turn it into Java code?

I'll start by addressing the first item: It is essential that you understand exactly what the input is and what the required output is and that you are able to give a precise pseudo code description of how to solve the problem. If you don't know what pseudocode is, then look it up on Wikipedia. Then you can start thinking about how to code it in Java. From an algorithmic perspective, your problem is very simple.

Say the customer pays amount X and for an item that costs amount C. First you must check that X >= C. otherwise the customer won't have paid enough, and you can stop right there.

Assuming that X >= C, and that you are able to give exact change, the amount of money in the till will have increased by C after the transaction completes, as this exactly what the customer ends up paying.

Now, the amount of change you have to give should equal X-C. Call this Y.

Test how many times the biggest coin you have available divides Y: Say the biggest coin has value V, then you should give back the customer Y/V coins of this value. Afterwards, you need to pay back the customer the remaining money Y'=Y-(Y/V)*V. Make sure you know how division works in Java (see link at the end of this post). Repeat the procedure to pay Y' back using the second biggest coin and so on.

2. I won't write out the whole thing in Java, but there are some things you should consider.

Does the Till contain "money" without it being specified exactly what bills/coins it contains, or should you be representing the money as a number of bills and coins?

You will be doing integer division, so your coin values should not be doubles but ints.

You need to access the coin values (how much a dime is worth etc.) from inside the function that calculates change, so the values of the different coins should probably be declared as static member variables of the class CoinCalc, not inside a function.

You need to make sure you know how basic if-else statements and while loops work and how to assign to variables. Then you should be able to code the solution.

You also need to decide what exactly the solution is. It could be a list of coin names with a name repeated for each time it is needed, e.g. [dime,penny,penny], or maybe an array of four numbers with that say how many quartes, dimes, nickels and pennies are needed. If you want a list, you should learn how list datastructures work in Java, by reading the entry LinkedList in the Java documentation.

Good luck!

NB: Because I'm a new user, I can't post as many links as I would like. You can find a good description of division in Java by googling java division and selecting the link to mindprod.com.

worksintheory
Excellent! this is helping a lot. My question on how to turn my billTotal and amountPaid objects into integers so i can do the basic math function on them
Jon
int billCents = 100*billTotal.getDollars() + billTotal.getCents()
worksintheory