开发者

Need help with Java homework

开发者 https://www.devze.com 2023-01-16 15:31 出处:网络
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

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) {
  }
}


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号