我坚持使用add / subtract方法定义一个名为Money的类,其对象代表美元金额。 该类应该有两个类型为int的实例变量,用于金额和分数。 包含一个带有2个int类型参数的构造函数,用于美元和美分,一个带有一个int类型的构造函数,用于美元的零美分和无参数构造函数。 包括添加和减去金额的加减法方法,并返回Money类型的值。 包括一组合理的访问器和mutator方法以及equals和toString方法。
再次,我被困在加/减部分,只是无法钉住它。 加上等于部分有点令人困惑。 是的,这是功课。 我正在努力,但需要一点推动。
请看一下....
public class Money {
private static int dollars;
private static int cents;
public Money()
{
}
public Money(int dollars, int cents)
{
this.dollars = dollars;
this.cents = cents;
}
public Money(int dollars)
{
this.dollars = dollars;
}
public int getDollars() {
return dollars;
}
public void setDollars(int dollars) {
this.dollars = dollars;
}
public int getCents() {
return cents;
}
public void setCents(int cents) {
this.cents = cents;
}
public static Money add(Money m1, Money m2)
{
int cash = m1.dollars + m2.dollars;
int change = m1.cents + m2.cents;
return new Money(dollars, cents);
}
public static int MinusMoney(int m3, int m4)
{
return (m3-m4);
}
public boolean equals(double YourMoney)
{
boolean result;
if (YourMoney > 0)
{
dollars += cents;
result = true;
}
else
{
result = false;
}
return result;
}
public String toString()
{
return ("$" + getDollars() +"." + getCents());
}
public static void main(String[] args)
{
Money MyMoney = new Money(2,30);
System.out.println("You have" + MyMoney.toString());
Money MyMoney1 = new Money(2,30);
Money MyMoney2 = new Money(3,10);
System.out.println("You now have" + Money.add(MyMoney1, MyMoney2));
}
}
不要声明你的字段static !!
在add方法中,如果分数的总和大于100,则现金应为现金+ 1; 分=分 - 100;
你的add()方法没有结转,例如 添加1.75和2.75应该产生4.50,但会产生3.150。 您的MinusMoney完全错误(错误的名称,参数类型,返回类型,......)。 去掉它。 它应该与add()方法非常相似。
这是一个想法,而不是以美元和美分工作,仅以美分工作,它将使加法和减法更简单,并且当您需要时,您可以在dollars.cents中返回"格式化"版本
我也注意到了静电部分并将其取下。
@MadProgrammer似乎特别要求该课程中的美元和分数变量。
要求声明对对象的操作(例如add和subtract)返回一个新实例而不是更改当前实例。 从而:
class Money {
private int dollars;
private int cents;
public Money(int dollars, int cents) {
if (dollars < 0 || cents < 0)
throw new IllegalArgumentException("Negative dollars or cents");
this.dollars = dollars;
this.cents = cents;
}
public Money add(Money other) {
int cents = this.cents + other.cents;
return new Money(this.dollars + other.dollars + cents / 100, cents % 100);
}
}
对于equals,通常会定义为覆盖Object.equals,这意味着它需要接受Object而不是double:
public boolean equals(Object other) {
return other != null && other.getClass() == Money.class
&& this.dollars == (Money)other.dollars
&& this.cents == (Money)other.cents;
}
没有解决结转问题。要求"包含一组合理的访问器和增变器方法"意味着它不应该是不可变的。
@Andreas我添加了结转代码。我会删除不变性以避免混淆。
好。虽然我同意要求add()返回一个新实例对于不可变实现更有意义。如果类是可变的,那么让它更新(对我来说)更有意义。但是,这是一项任务。它们总是没有意义,因为它们习惯于在那个阶段测试简单的编码技巧。
@Andreas我同意它作为一个不可变的类更有意义 - 特别是因为它实际上只是一个具有最小逻辑的复杂数据类型:一个明显的不可变性的候选者但是,正如你所说,它的一个任务......
Ya,我在我的代码中尝试了boolean equals部分,它在this.dollars和this.cents行中出现了错误。
@ Jarvis0007是什么错误
你通过使用2个属性(一个用于美元,一个用于分)来复杂化。 你只需简单地将美元兑换成美分并通过美分或将美元兑换成方法本身。然后使用方法进行计算。
你可以做到
totalcents1= dollar1*100 +cent1;
totalcents2= dollar2*100 + cent2;
//for add
return totalcents1+totalcents2;
//for substract do check which is greater
return totalcents1-totalcents2;
逆转过程一分钱到一美元
谢谢你的建议。
如果你对我的回答有所帮助,请投票。当你获得特权时。
自发布以来我有一些更新。 我想出了加/减,它有效。 谢谢你的平等部分,但在那里出现了一个问题。
public boolean equals(Object other) {
return other != null && other.getClass() == Money.class
&& this.dollars == (Money)other.dollars
&& this.cents == (Money)other.cents
关于other.getClass()的部分......您是指Money类还是我需要添加更多方法?
public class Money {
private int dollars;
private int cents;
public Money()
{
}
public Money(int dollars, int cents)
{
this.dollars = dollars;
this.cents = cents;
}
public Money(int dollars)
{
this.dollars = dollars;
}
public int getDollars() {
return dollars;
}
public void setDollars(int dollars) {
this.dollars = dollars;
}
public int getCents() {
return cents;
}
public void setCents(int cents) {
this.cents = cents;
}
public static Money add(Money m1, Money m2)
{
int dollars1 = m1.getDollars();
int dollars2 = m2.getDollars();
int cents1 = m1.getCents();
int cents2 = m2.getCents();
int sumDollars = dollars1 + dollars2;
int sumCents = cents1 + cents2;
if(sumCents > 100)
{
sumDollars = sumDollars +1;
sumCents = sumCents - 100;
}
Money output = new Money(sumDollars, sumCents);
return output;
}
public static Money minus(Money m1, Money m2)
{
int dollars1 = m1.getDollars();
int dollars2 = m2.getDollars();
int cents1 = m1.getCents();
int cents2 = m2.getCents();
int sumDollars = dollars1 - dollars2;
int sumCents = cents1 - cents2;
if(sumCents > 100)
{
sumDollars = sumDollars +1;
sumCents = sumCents - 100;
}
Money output = new Money(sumDollars, sumCents);
return output;
}
public boolean equals(double YourMoney)
{
boolean result;
if (YourMoney > 0)
{
dollars += cents;
result = true;
}
else
{
result = false;
}
return result;
}
public String toString()
{
return ("$" + getDollars() +"." + getCents());
}
public static void main(String[] args)
{
Money MyMoney = new Money(2,30);
System.out.println("You have" + MyMoney.toString());
Money MyMoney1 = new Money(12,30);
Money MyMoney2 = new Money(7,10);
System.out.println("You now have" + Money.minus(MyMoney1, MyMoney2));
}
}
getClass是Object的方法,它是所有其他类自动扩展的类。它返回对象的类。另请注意,在SO上的不良做法是将答案作为问题发布。最好在原始问题上附加评论。