Need help with Java homework

I need help with my homework. My task is to create a program that creates an object Till, accepts payment, issues exact changes, tells me which coins I need to use, and then tells me how much it costs to the end. Below is the code I wrote. The class USmoneyis up and running. The teacher offered a cheat sheet. However, it is the main class ( CoinCalc), receiving payment and subtracting the payment from the amount paid to give me a change that I am having problems 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) {
  }
}
+3
source share
4 answers

knapsack,

USMoney, , .

public USMoney subtract(final USMoney value);

public double[] getCoins(final USMoney value);
+2

, .

  • , ?
  • , Java-?

: , , , , , . , , . , Java. .

, X , C. , X >= C. , .

, X >= C, , C , , .

, , X-C. Y.

, , , Y: , V, Y/V . Y '= Y- (Y/V) * V. , , Java (. ). , Y ', .

2. Java, , .

"" "", , / , ?

, , ints.

( ..) , , , , - CoinCalc, .

, , if-else while, . .

, . , , . [dime, penny, penny], , , , , , , . , , Java, LinkedList Java.

!

NB: , , . Java googling java mindprod.com.

+2

changeFor() CoinCalc. -, CoinCalc main() :

CoinCalc cc = new CoinCalc();

cc.changeFor(billTotal, amountPaid);

, changeFor() , till , changeFor().

, , ...

, billTotal - float amountPaid - USMoney, (1 , , , ). - , . USMoney , , ( , ..).

till - USMoney, .

USMoney changeFor(float bill, USMoney paid), USMoney. :

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

changeFor() , , , till, bill amountPaid. , , , , . , .

0

, changeFor, :

billTotal amountPaid. .

Then make some variables hold on to how many changes you are returning. For each quarter, penny, nickel, penny, if changeRemaining is the value of a coin, add a coin and subtract the value. Do this for each coin until this coin value passes the test, and then move on to the next coin.

0
source

Source: https://habr.com/ru/post/1764686/


All Articles