Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比较两个对象的实例变量_Java - Fatal编程技术网

Java 比较两个对象的实例变量

Java 比较两个对象的实例变量,java,Java,我需要帮助实现一个函数,在这个函数中,我必须比较调用对象和参数对象的实例变量。我的同学说我可以调用compareTo()函数,但我觉得有一种更容易实现的方法 我之前必须重载构造函数,它是Money对象的一个副本,通过将每个实例变量的值从parameter对象复制到新对象的实例变量,我认为这是正确实现的,但我不确定这是否是问题所在 我的输出是“$10.02不等于$10.02”,这对我来说是没有意义的。非常感谢您的帮助 /** This class represents nonnegative a

我需要帮助实现一个函数,在这个函数中,我必须比较调用对象和参数对象的实例变量。我的同学说我可以调用compareTo()函数,但我觉得有一种更容易实现的方法

我之前必须重载构造函数,它是Money对象的一个副本,通过将每个实例变量的值从parameter对象复制到新对象的实例变量,我认为这是正确实现的,但我不确定这是否是问题所在

我的输出是“$10.02不等于$10.02”,这对我来说是没有意义的。非常感谢您的帮助

/**
 This class represents nonnegative amounts of money.
 */

public class Money
{
    // The number of dollars
    private long dollars;

    // The number of cents
    private long cents;

    /**
     Constructor
     @param amount The amount in decimal format.
     */

    public Money(double amount)
    {
        if (amount < 0)
        {
            System.out.println("Error: Negative amounts " +
                    "of money are not allowed.");
            System.exit(0);
        }
        else
        {
            long allCents = Math.round(amount * 100);
            dollars = allCents / 100;
            cents = allCents % 100;
        }
    }

    // ADD LINES FOR TASK #1 HERE
    // Document and write a copy constructor
    // Creating a copy of the Money constructor
    public Money(Money obj) {
        dollars = obj.dollars;
        cents = obj.cents;
    }

    /**
     The add method
     @param otherAmount The amount of money to add.
     @return The sum of the calling Money object
     and the parameter Money object.
     */

    public Money add(Money otherAmount)
    {
        Money sum = new Money(0);

        sum.cents = this.cents + otherAmount.cents;

        long carryDollars = sum.cents / 100;

        sum.cents = sum.cents % 100;

        sum.dollars = this.dollars +
                otherAmount.dollars +
                carryDollars;

        return sum;
    }

    /**
     The subtract method
     @param amount The amount of money to subtract.
     @return The difference between the calling Money
     object and the parameter Money object.
     */

    public Money subtract (Money amount)
    {
        Money difference = new Money(0);

        if (this.cents < amount.cents)
        {
            this.dollars = this.dollars - 1;
            this.cents = this.cents + 100;
        }

        difference.dollars = this.dollars - amount.dollars;
        difference.cents = this.cents - amount.cents;

        return difference;
    }

    /**
     The compareTo method
     @param amount The amount of money to compare against.
     @return -1 if the dollars and the cents of the
     calling object are less than the dollars and
     the cents of the parameter object.
     0 if the dollars and the cents of the calling
     object are equal to the dollars and cents of
     the parameter object.
     1 if the dollars and the cents of the calling
     object are more than the dollars and the
     cents of the parameter object.
     */

    public int compareTo(Money amount)
    {
        int value;

        if(this.dollars < amount.dollars)
            value = -1;
        else if (this.dollars > amount.dollars)
            value = 1;
        else if (this.cents < amount.cents)
            value = -1;
        else if (this.cents > amount.cents)
            value = 1;
        else
            value = 0;

        return value;
    }

    // ADD LINES FOR TASK #2 HERE
    // Document and write an equals method
    // Document and write a toString method

    // Writing the equals method
    public boolean equals(Money obj) {
        return (this.dollars == obj.dollars && this.cents == obj.cents);
    }

    // Writing the toString method
    public String toString() {
        if (this.cents < 10) {
            return "$" + this.dollars + ".0" + this.cents;
        }
        else {
            return "$" + this.dollars + "." + this.cents;
        }
    }

}
/**
此类表示非负金额的货币。
*/
公款
{
//美元的数目
私人多头美元;
//分的数目
私人长分;
/**
建造师
@param amount十进制格式的金额。
*/
公共资金(双倍金额)
{
如果(金额<0)
{
System.out.println(“错误:负金额”+
“不允许任何形式的金钱。”);
系统出口(0);
}
其他的
{
long allCents=数学整数(金额*100);
美元=100美分;
美分=所有美分%100;
}
}
//在此处为任务#1添加行
//记录并编写一个副本构造函数
//创建Money构造函数的副本
公共资金(货币obj){
美元=对象美元;
美分=对象美分;
}
/**
加法
@param otherAmount要添加的金额。
@返回调用货币对象的总和
以及参数Money对象。
*/
公共资金增加(其他金额)
{
货币总额=新货币(0);
sum.cents=此.cents+其他金额.cents;
长结转美元=sum.cents/100;
sum.cents=sum.cents%100;
sum.dollars=this.dollars+
其他金额+
carryDollars;
回报金额;
}
/**
减法
@param amount要减去的金额。
@返回调用货币之间的差额
对象和参数Money对象。
*/
公款减去(金额)
{
货币差额=新货币(0);
如果(本美分<金额美分)
{
this.dollars=this.dollars-1;
this.cents=this.cents+100;
}
difference.dollars=this.dollars-amount.dollars;
差额.cents=此.cents-金额.cents;
收益差;
}
/**
比较法
@param amount要比较的金额。
@返回-1,如果
调用对象少于美元和
参数对象的名称。
0如果通话的美元和美分
对象等于的美元和美分
参数对象。
1如果通话的美元和美分
对象不仅仅是美元和美元
参数对象的名称。
*/
公共国际比较(金额)
{
int值;
如果(本美元<金额美元)
值=-1;
else if(this.dollars>amount.dollars)
数值=1;
else if(本.分<金额.分)
值=-1;
else if(this.cents>amount.cents)
数值=1;
其他的
数值=0;
返回值;
}
//在此处为任务2添加行
//记录并编写一个equals方法
//记录并编写一个toString方法
//写equals方法
公共布尔等于(货币对象){
返回(this.dollars==obj.dollars&&this.cents==obj.cents);
}
//编写toString方法
公共字符串toString(){
如果(这0.5美分<10美分){
返回“$”+this.dollars+”.0“+this.cents;
}
否则{
返回“$”+this.dollars+”+this.cents;
}
}
}

方法的正确签名是
布尔等于(对象o)
;这已经存在于java.lang.Object中,因此,所有对象都有它

您创建了一个完全不同的方法,它根本不重写此方法。它恰好也被命名为
equals
,但它不是相同的方法,因为java就是这样工作的。类型必须匹配

所以,解决这个问题。使用例如
other.getClass()==Money.class
检查类型,如果不是这样,您可以返回
false
,如果是,您可以强制转换
other
,然后从那里继续

关于代码质量的其他一些评论:

  • 双打并不精确;不要用它们来代表金钱。使用
    int
    long
    ,并用它们表示美分
  • 在出现错误时调用
    System.exit()
    ,这相当残忍。正确的做法是抛出新的IllegalArgumentException(“不允许负货币”)(请注意,异常消息不应以标点符号结尾)
  • 你的代码在很大程度上是说,一个
    货币
    对象一旦制造出来就永远不会改变。一直走下去,把那些字段设置为final,这样你的复制构造函数就没用了。没有理由复制——所有“改变”事物的方法实际上并没有改变事物——它们已经创造了一个新的金钱对象

  • 提示:对于
    等于
    ,请始终使用
    @Override
    ,当您重写
    等于
    时,必须重写
    hashCode
    。他是对的。如果您不使用
    @override
    ,它将使用Java自己的方法。@chrylis谨慎的选择当我尝试实现hashCode的override时,我该如何实现它?我尝试使用public long hashCode()并返回此.dollars,但在'M'中声明'hashCode()'时出错