在 JAVA 制作存钱罐程序
Making piggy bank program in JAVA
我离开 Java 已经有一段时间了,我仍在努力回忆和学习很多东西。我有一个当前的项目,它是一个存钱罐,您可以向其中添加硬币并获得各种输出。我目前的任务是 5 种方法以及构造函数的辅助方法。我知道我想做什么,但无法通过代码思考来完成它。 我想用helper方法获取另外两个constructor的金额,但是脑子里想不通,只能翻书这么久了。任何输入表示赞赏。
各方法说明如下:
P.S。我的代码可能不正确。
publicChangeJar() 将所有实例变量设置为零的默认构造函数
publicChangeJar(int quarters, int dimes, int nickels, int pennies) 一个构造函数,它使用提供的值转换为 quarters、dimes、nickels 和 pennies 来初始化实例变量。
public ChangeJar(final double amount) 一个构造函数,它使用转换为四分之一、一角硬币、五分硬币和便士的提供值来初始化实例变量。例如,如果金额为 1.34,那么您将有 5 个 25 美分、1 个镍币、4 个便士
public ChangeJar(final String amount)接受字符串作为参数的构造函数,提供的值转换为适当数量的 25 美分、10 美分、5 美分和便士。例如,如果金额为“1.34”,那么您将有 5 个 25 美分、1 个镍币和 4 个便士。
public ChangeJar(final ChangeJar other)构造函数,用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){
}
}
编辑:我的问题是如何编写辅助方法以在两个构造函数中工作。
构造函数ChangeJar(double)
对于带有金额的构造函数,您想使用最大数量的季度,然后是最大数量的便士,依此类推。
假设我有 2.87 美元。
- 首先,我要上11节课。我们还剩下 0.12 美元。
- 那我拿1毛钱。我们还剩 0.02 美元。
- 那我就收0镍吧。我们还剩 0.02 美元。
- 那么,我要2便士。我们完成了。
如何实现?假设金额为 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
s。例如,您可以将单位从 $
更改为 $/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;
}
已经好多了!
但是我的代码里面有很多copy/paste...
我们怎样才能让它变得更好?
我们可以看到,对于每个硬币,我得到硬币的数量,然后从数量中减去价值。
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);
}
我离开 Java 已经有一段时间了,我仍在努力回忆和学习很多东西。我有一个当前的项目,它是一个存钱罐,您可以向其中添加硬币并获得各种输出。我目前的任务是 5 种方法以及构造函数的辅助方法。我知道我想做什么,但无法通过代码思考来完成它。 我想用helper方法获取另外两个constructor的金额,但是脑子里想不通,只能翻书这么久了。任何输入表示赞赏。 各方法说明如下:
P.S。我的代码可能不正确。
publicChangeJar() 将所有实例变量设置为零的默认构造函数
publicChangeJar(int quarters, int dimes, int nickels, int pennies) 一个构造函数,它使用提供的值转换为 quarters、dimes、nickels 和 pennies 来初始化实例变量。
public ChangeJar(final double amount) 一个构造函数,它使用转换为四分之一、一角硬币、五分硬币和便士的提供值来初始化实例变量。例如,如果金额为 1.34,那么您将有 5 个 25 美分、1 个镍币、4 个便士
public ChangeJar(final String amount)接受字符串作为参数的构造函数,提供的值转换为适当数量的 25 美分、10 美分、5 美分和便士。例如,如果金额为“1.34”,那么您将有 5 个 25 美分、1 个镍币和 4 个便士。
public ChangeJar(final ChangeJar other)构造函数,用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){
}
}
编辑:我的问题是如何编写辅助方法以在两个构造函数中工作。
构造函数ChangeJar(double)
对于带有金额的构造函数,您想使用最大数量的季度,然后是最大数量的便士,依此类推。
假设我有 2.87 美元。
- 首先,我要上11节课。我们还剩下 0.12 美元。
- 那我拿1毛钱。我们还剩 0.02 美元。
- 那我就收0镍吧。我们还剩 0.02 美元。
- 那么,我要2便士。我们完成了。
如何实现?假设金额为 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
s。例如,您可以将单位从 $
更改为 $/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;
}
已经好多了!
但是我的代码里面有很多copy/paste...
我们怎样才能让它变得更好?
我们可以看到,对于每个硬币,我得到硬币的数量,然后从数量中减去价值。
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);
}