写一个封装硬币概念的类,假设硬币具有以下属性:多个季度,一些硬币,一些镍币和一些便士。包括构造函数,评估者和mutator,以及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 class Coins {
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 :(得分: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
了解更多信息。
对于返回硬币,镍币等数量的其他部分,您只需使用吸气剂。