如何编写toString()和equals()方法?

时间:2016-04-20 02:48:58

标签: java

我正在尝试使这段代码打印实际数字,而不是十六进制位置。

import re
details = '(2,5  cr / K / M)'
m = re.match(r'\((.*?)\w+cr\w/\w(.)\w/\w(.)\)', details)

credit = m.group(0)
state = m.group(1)
grade = m.group(2)


course = {'credit': credit, 'state': state, 'grade': grade}

print course

这是由moneydriver调用的主要类

? cr / ? / ?

目标是编写equals方法(在public class MoneyDriver { //This is a driver for testing the class public static void main(String[] args) { final int BEGINNING = 500; final Money FIRST_AMOUNT = new Money(10.02); final Money SECOND_AMOUNT = new Money(10.02); final Money THIRD_AMOUNT = new Money(10.88); Money balance = new Money(BEGINNING); System.out.println("The current amount is " + balance.toString()); balance = balance.add(SECOND_AMOUNT); System.out.println("Adding " + SECOND_AMOUNT + " gives " + balance.toString()); balance = balance.subtract(THIRD_AMOUNT); System.out.println("Subtracting " + THIRD_AMOUNT + " gives " + balance.toString()); boolean equal = SECOND_AMOUNT.equals(FIRST_AMOUNT); if(equal) System.out.println(SECOND_AMOUNT + " equals " + FIRST_AMOUNT); else System.out.println(SECOND_AMOUNT.toString() + " does not equal " + FIRST_AMOUNT); equal = THIRD_AMOUNT.equals(FIRST_AMOUNT); if(equal) System.out.println(THIRD_AMOUNT + " equals " + FIRST_AMOUNT); else System.out.println(THIRD_AMOUNT + " does not equal " + FIRST_AMOUNT); } } 类上)。该方法将调用对象的实例变量与参数对象的实例变量进行相等性比较,如果调用对象的美元和美分与参数对象的美元和美分相同,则返回true。否则,它返回false。

编写public class Money { private long dollars; private long cents; 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; } } 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; } 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; } 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; } } 方法(在main类上)。此方法将返回一个String 看起来像钱,包括美元符号。请记住,如果你的价格低于10美分,你需要在打印美分之前加上0才能正确显示2位小数。

如果两种方法都正确实施

根据tutorialspoint,你应该做任何一个

toString

但是提供的moneydriver已经有main方法,但不显示数字,而是显示变量的十六进制位置。

equals方法已经在moneydriver中使用了,所以我也很丢失。

正确的输出应该如下所示

目前的金额为$ 500.00 增加10.02美元给出510.02美元减去10.88美元给出499.1美元 10.02美元等于10.02美元 $ 10.88不等于$ 10.02

完全失去了这一点,先谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

要在Money类上获取字符串输出,请执行以下操作:

public class Money
{
   private long dollars;
   private long cents;

   // suggested approach for constructor
   public Money(long amount) throws IllegalArgumentException
   {
      if (amount < 0) {
        throw new IllegalArgumentException("Amount may not be less than 0");
      }

      // do other stuff
   }

   ...

   public String toString()
   {
     return String.format("%d.%02d", dollars, cents);
   }

   public boolean equals(Object obj)
   {
      boolean equal = false;

      if (obj instanceof Money) {
         Money chk = (Money)obj;
         equal = (chk.dollars == this.dollars &&
                  chk.cents == this.cents);
      }
      return equal;
   }
} // end class Money