2014-02-25 97 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