Closed. This question needs details or clarity。它当前不接受答案。












想改善这个问题吗?添加详细信息并通过editing this post阐明问题。

5年前关闭。





我离开Java已有一段时间了,我试图回忆和学习很多东西。我有一个当前项目,是一个存钱罐,您可以向其中添加硬币并获得各种输出。我目前负责5种方法以及构造函数的辅助方法。我知道我想做什么,但是无法思考代码以完成任务。我想使用helper方法来获取其他两个构造函数的数量,但无法解决这个问题,我只能看这么长时间的书。任何输入表示赞赏。
每种方法的说明如下:

附言我所拥有的代码可能不正确。

publicChangeJar()默认构造函数,将所有实例变量设置为零

publicChangeJar(int Quarters,Int Dimes,Int Nicks,Int Pennies)构造函数,使用提供的值初始化实例变量,将其提供的值转换为Quarters,Dimes,Nicks和Pennies。

public ChangeJar(final double amount)一个构造函数,该构造函数使用提供的值初始化实例变量,并将其提供的值转换为四分之一,二分之一,五分之一和几美分。例如,如果金额为1.34,那么您将有5个季度,1个镍,4个便士

public ChangeJar(final String amount)一个构造函数,它接受一个字符串作为参数,并将提供的值转换为适当的四分之一位数,零点数,镍数和便士数。例如,如果金额为“ 1.34”,则您将有5个季度,1个镍,4个便士。

public ChangeJar(最终ChangeJar其他)一个构造函数,用于将“ this” ChangeJar对象的实例变量与另一个对象一起初始化。

public class ChangeJar {
private int pennies;
private int nickels;
private int dimes;
private int quarters;

static boolean globalLock = false;

public ChangeJar(){
    this(0,0,0,0);
}

public ChangeJar(int pennies, int nickels, int dimes, int quarters)throws IllegalArgumentException {
if (pennies < 0 || nickels < 0 || dimes < 0 || quarters < 0)
    throw new IllegalArgumentException("You cannot have negative coins in the jar");
else this.pennies = this.pennies + pennies;
    this.nickels = this.nickels + nickels;
    this.dimes = this.dimes + dimes;
    this.quarters = this.quarters + quarters;

}

public ChangeJar(double amount){

}
public ChangeJar(final String amount){

}
private double amountHelper(double amount){
    amount = pennies*.01 + nickels*.05 + dimes*.10 + quarters*0.25;
    return amount;
}

public ChangeJar(final ChangeJar other){

}


}

编辑:我的问题在这里是如何编写在两个构造函数中均可使用的辅助方法。

最佳答案

构造器ChangeJar(double)

对于具有一定数量的构造函数,您要使用最大季度数,然后使用最大便士数,依此类推。

假设我有$ 2.87。


首先,我要花11个季度。我们还剩下$ 0.12。
然后,我取一角钱。我们还剩下$ 0.02。
然后,我取0镍。我们还剩下$ 0.02。
然后,我取两便士。大功告成


如何实施?假设金额为2.87

public ChangeJar(double amount) {
    // How many quarters?
    int quarters = (int) (amount / .25); // The division gives 9.48 and we cast the result to int so we get 9
    amount = amount - quarters * .25;
    System.out.println(quarters + " quarters. Remains: " + amount);

    // How many dimes?
    int dimes = (int) (amount / .10);
    amount = amount - dimes * .10;
    System.out.println(dimes + " dimes. Remains: " + amount);

    // How many nickels?
    int nickels = (int) (amount / .05);
    amount = amount - nickels * .05;
    System.out.println(nickels + " nickels. Remains: " + amount);

    // How many pennies?
    int pennies = (int) (amount / .01);
    amount = amount - pennies * .01;
    System.out.println(pennies + " pennies. Remains: " + amount);

    // Prints:
    // 11 quarters. Remains: 0.1200000000000001
    // 1 dimes. Remains: 0.0200000000000001
    // 0 nickels. Remains: 0.0200000000000001
    // 2 pennies. Remains: 1.0061396160665481E-16

    // Now we just set this in our properties:
    this.quarters = quartes;
    this.dimes = dimes;
    this.nickels = nickels;
    this.pennies = pennies;
}


如您所见,问题在于余数是奇怪的值。构造函数可以工作,但是并不是很酷。为什么?因为Java approximates the doubles

我建议使用int。例如,您可以将单位从$更改为$/100。与整数值相同的示例(输入不是2.87而是287):

public ChangeJar(int amount) {
    // How many quarters?
    int quarters = amount / 25;
    amount = amount - quarters * 25;
    System.out.println(quarters + " quarters. Remains: " + amount);

    // How many dimes?
    int dimes = amount / 10;
    amount = amount - dimes * 10;
    System.out.println(dimes + " dimes. Remains: " + amount);

    // How many nickels?
    int nickels = amount / 5;
    amount = amount - nickels * 5;
    System.out.println(nickels + " nickels. Remains: " + amount);

    // How many pennies?
    int pennies = amount;
    amount = amount - pennies;
    System.out.println(pennies + " pennies. Remains: " + amount);

    // Prints:
    // 11 quarters. Remains: 12
    // 1 dimes. Remains: 2
    // 0 nickels. Remains: 2
    // 2 pennies. Remains: 0

    // Now we just set this in our properties:
    this.quarters = quartes;
    this.dimes = dimes;
    this.nickels = nickels;
    this.pennies = pennies;
}


那已经更好了!

但是我的代码中有很多复制/粘贴...
我们如何才能使其更好?

我们可以看到,对于每个硬币,我得到硬币的数量,然后从数量中减去该值。

int amount = 287;

int[] values = new int[]{25, 20, 5, 1}; // The values of my coins
int[] results = new int[values.length];

for (int i = 0; i < values.length; i++) {
    int valueOfCoin = values[i];
    int numberOfCoins = amount / valueOfCoin; // Division gives the integer part of the result
    results[i] = numberOfCoins;

    amount = amount % valueOfCoin; // Modulo gives the remainder part of the result
    // Or you could simply write: amount %= valueOfCoin;
}

System.out.println("RESULTS=" + Arrays.toString(results));

// Prints:
// RESULTS=[9, 1, 0, 2]


构造器ChangeJar(String)

我认为String是一个数量,因此我们只将String转换为Double并调用其他构造函数(ChangeJar(double))。

public ChangeJar(String amount) {
    this(Double.valueOf(amount)); // Double.valueOf() will try to convert the String => Double
}


构造器ChangeJar(ChangeJar)

这个想法只是复制另一个ChangeJar的值:

public ChangeJar(ChangeJar other) {
    this(other.quarters, other.dimes, other.nickels, other.pennies);
}

09-25 23:21