2014-02-25 70 views
-1

写一个封装硬币的概念的类,假设硬币具有以下属性:一些宿舍,一些硬币,一些镍,和一个便士数量。包含构造函数,评估者和增变器,以及toString和equals方法。简单的货币值,不知道如何采取输入和输出货币

不知道如何做到这一点的一部分:

  • 还编写了以下方法:一个小数点后二显著数字,和其他人返回的钱回来的钱在美元符号总量宿舍,硬币,镍和便士。编写一个接受用户输入的客户端类来测试你的类中的所有方法。

    public class CoinsApp { 
    
    public static void main(String[] args) { 
    
    
    
    Coins c = new Coins(); 
    Scanner scan = new Scanner(System.in); 
    System.out.print("Enter the number of Quarters: "); 
    int q = scan.nextInt(); 
    
    System.out.print("Enter the number of Dimes: "); 
    int d = scan.nextInt(); 
    
    System.out.print("Enter the number of nickels: "); 
    int n = scan.nextInt(); 
    
    System.out.print("Enter the number of pennies: "); 
    int p = scan.nextInt(); 
    
    Coins c1 = new Coins(q,d,n,p); 
    
    
    System.out.println(c1); 
    } 
    } 
    

我会有什么变化,使我目前的班?

ublic类钱币{

private int quarters; 
private int dimes; 
private int nickles; 
private int pennies; 

public Coins() { 
    quarters = 0; 
    dimes = 0; 
    nickles = 0; 
    pennies = 0; 
} 

public Coins(int quarters, int dimes, int nickles, int pennies) { 
    this.quarters = quarters; 
    this.dimes = dimes; 
    this.nickles = nickles; 
    this.pennies = pennies; 
} 

/** 
* 
* @return the value of nickles 
*/ 
public int getNickles() { 
    return nickles; 
} 

/** 
* 
* @param nickles 
*/ 
public void setNickles(int nickles) { 
    this.nickles = nickles; 
} 

public int getPennies() { 
    return pennies; 
} 

public void setPennies(int pennies) { 
    this.pennies = pennies; 
} 

/** 
* Get the value of dimes 
* 
* @return the value of dimes 
*/ 
public int getDimes() { 
    return dimes; 
} 

/** 
* Set the value of dimes 
* 
* @param dimes new value of dimes 
*/ 
public void setDimes(int dimes) { 
    this.dimes = dimes; 
} 

/** 
* Get the value of quarters 
* 
* @return the value of quarters 
*/ 
public int getQuarters() { 
    return quarters; 
} 

/** 
* Set the value of quarters 
* 
* @param quarters new value of quarters 
*/ 
public void setQuarters(int quarters) { 
    this.quarters = quarters; 
} 

@Override 
public String toString() { 
    return "Coins{" + "quarters=" + quarters + ", dimes=" + dimes + ", nickles=" + nickles + ", pennies=" + pennies + '}'; 
} 

@Override 
public boolean equals(Object obj) { 
    if (obj == null) { 
     return false; 
    } 
    if (getClass() != obj.getClass()) { 
     return false; 
    } 
    final Coins other = (Coins) obj; 
    if (this.quarters != other.quarters) { 
     return false; 
    } 
    if (this.dimes != other.dimes) { 
     return false; 
    } 
    if (this.nickles != other.nickles) { 
     return false; 
    } 
    if (this.pennies != other.pennies) { 
     return false; 
    } 
    return true; 
} 

}

+0

你得到的错误是什么?或者如果你对一个特定的概念模糊不清,也许把它放在一起会帮助我们指导你正确的方向:) – WillBD

回答

0

有很多方法来解决这个问题。如果你想看起来很聪明,但是一个简单的解决方案要好得多,你可以用多态来做到。

基本上你有一个类(除了你主),我会打电话给wallet

public class Wallet{ 
private int dimes=0; 
//...etc for all denominations 

//We just print the total value, essentially counting coins here 
//Not familiar with american coinage; in this example a dime=5c and a 
//nickel=10c 
public void printDollars(){ 
    float total=0.0; 
    total+=this.dime*5; 
    total+=this.nickel*10; 
    //...etc 
    //Divide by 100 to get value in dollars instead of cents 
    total/=100; 
    //Now to format the output. Java's System.out.format works a lot 
    //like C's printf. 
    System.out.format("%.2f$", total); 
} 

} 

System.out.format看看更多。

对于返回硬币,镍币等数量的其他部分,你只需要使用getters。

+0

我很关心在这里使用'float'来存储一些美元。它在这个例子中起作用,但建议某人去做是一件危险的事情 - 例如,下一个练习可能涉及将多个“Wallet”对象的钱一起加入;如果金额是“浮动”,这将不会很好地工作。我的感觉是堆栈溢出的答案应该试图让这些细节正确。 –

+0

感谢您的帮助 – user3317953

+0

我该如何编辑我的班级 – user3317953