我已经离开Java一段时间了,我正在努力回忆和学习很多东西。我有一个当前的项目,这是一个存钱罐,你可以添加硬币,并可以获得各种输出。我目前的任务是5个方法加上构造函数的辅助方法。我知道我想做什么,但无法通过代码来思考它。 我想使用辅助方法获取其他两个构造函数的数量,但无法理解它,我只能查看我的书这么久。任何输入都表示赞赏。 每种方法的描述如下:
P.S。我所拥有的代码可能不正确。
publicChangeJar()将所有实例变量设置为零的默认构造函数
publicChangeJar(int quarters,int dimes,int nickels,int pennies)一个构造函数,用于初始化实例变量,并将提供的值转换为quarters,dimes,nickels和pennies。
public ChangeJar(final double amount)一个构造函数,用于初始化实例变量,并将提供的值转换为quarters,dimes,nickels和pennies。例如,如果金额是1.34那么你将有5个季度,1个镍,4个便士
public ChangeJar(final String amount)一个构造函数,它接受一个字符串作为参数,并将提供的值转换为适当的四分之一,一角硬币,一分钱和一分钱。例如,如果金额是“1.34”那么你将有5个季度,1个镍,4个便士。
public ChangeJar(final ChangeJar other)一个构造函数,用另一个对象初始化“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){
}
}
编辑:我的问题是如何编写辅助方法以在两个构造函数中工作。
答案 0 :(得分:1)
ChangeJar(double)
对于具有金额的构造函数,您希望使用最大季度数,然后是最大便士数,依此类推。
假设我有2.87美元。
如何实现?假设金额为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
转换为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);
}