在JAVA做存钱罐程序 [英] Making piggy bank program in JAVA

查看:170
本文介绍了在JAVA做存钱罐程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经离开Java了一段时间,我试图回忆和学习很多。我有一个目前的项目,这是一个存钱罐,你添加硬币,可以得到各种输出。我目前任务有5种方法加上一个辅助方法的构造函数。我知道我想做什么,但不能通过代码来完成它。 我想使用辅助方法获取其他两个构造函数的数量,但不能让我的头,我只能看看我的书这么久。任何输入是赞赏。
每个方法的描述如下:

I have been away from Java for awhile and am trying to recall and learn a lot still. I have a current project which is a piggy bank that you add coins to and can get various outputs. I am currently tasked with 5 methods plus a helper method for the constructors. I know what I want to do, but can't think through the code to get it done. I want to use the helper method to get the amounts for the other two constructors, but cannot get my head around it, I can only look through my book so long. Any input is appreciated. The description for each method is as follows:

P.S。我的代码我可能不正确。

P.S. The code I do have may not be correct.

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

publicChangeJar() Default constructor that sets all the instance variables to zero

publicChangeJar(int quarters,int dimes ,int nickels,int pennies)一个构造函数,用于将所提供的值转换为季度,二进制数,镍价和便士的初始化实例变量。

publicChangeJar(int quarters, int dimes, int nickels, int pennies) A constructor that initializes the instance variables with the provided values converted to quarters, dimes, nickels, and pennies.

public ChangeJar )一个构造函数,用于将实例变量初始化为提供的值,转换为季度,dimes,nick和pennies。例如,如果金额是1.34,那么你将有5个季度,1镍,4便士

public ChangeJar(final double amount) A constructor that initializes the instance variables with the provided value converted to quarters, dimes, nickels, and pennies. For example, if amount was 1.34 then you would have 5 quarters, 1 nickel, 4 pennies

public ChangeJar(final String amount)接受字符串作为参数的构造函数其中所提供的值被转换为适当的四分之一,角,镍和便士的数量。例如,如果金额是1.34,那么你将有5个季度,1镍,4便士。

public ChangeJar(final String amount)A constructor that accepts a string as a parameter with the provided value converted to appropriate number of quarters, dimes, nickels, and pennies. For example, if amount was "1.34" then you would have 5 quarters, 1 nickel, 4 pennies.

public ChangeJar(final ChangeJar other) thisChangeJar对象与另一个对象。

public ChangeJar(final ChangeJar other)A constructor that initializesthe instance variables of "this" ChangeJar object with the other object.

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

}

}

编辑:我的问题是如何编写帮助器方法在两个构造函数中工作。

My problem here is how to write the helper method to work in both constructors.

推荐答案

构造函数 ChangeJar(double)



对于具有金额的构造函数,您要使用最大数量的季度,然后是最大数量的便士,等等。

Constructor ChangeJar(double)

For your constructor with an amount, you want to use the maximum number of quarters, then the maximum number of pennies, and so on.

假设我有$ 2.87。

Let's say I have $2.87.


  • 首先,我将需要11个季度。我们还剩下$ 0.12。

  • 然后,我会拿一分钱。我们还剩下$ 0.02。

  • 然后,我会拿0个镍。

  • 然后,我将花2美分。已完成。

  • First, I will take 11 quarters. We still have $0.12 left.
  • Then, I will take 1 dime. We still have $0.02 left.
  • Then, I will take 0 nickel. We still have $0.02 left.
  • Then, I will take 2 pennies. We're done.

如何实现?让我们说金额 2.87

How to implement that? Let's say the amount is 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接近两倍

As you can see, the problem is that the remainders are strange values. The constructor works but it's not really cool. Why? Because Java approximates the doubles.

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

I would suggest to work with ints. For example, you could change your unit from $ to $/100. Our same example with integer values (the input is not 2.87 but 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;
}

这已经更好了!

但是我的代码中有很多复制/粘贴...

我们如何才能使它更好?

But there is a lot of copy/paste in my code...
How could we make it better?

我们可以看到

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]

$ b b

构造函数 ChangeJar(String)



我假设String是一个数量我们只需将 String 转换为 Double 并调用其他构造函数( ChangeJar )

)。

Constructor ChangeJar(String)

I suppose that the String is an amount so we will just convert the String to a Double and call the other constructor (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做存钱罐程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆