寫一個封裝硬幣的概念的類,假設硬幣具有以下屬性:一些宿舍,一些硬幣,一些鎳,和一個便士數量。包含構造函數,評估者和增變器,以及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;
}
}
你得到的錯誤是什麼?或者如果你對一個特定的概念模糊不清,也許把它放在一起會幫助我們指導你正確的方向:) – WillBD