-1

What precisely do the last four lines of the following code do?

class Money {

    private int fAmount;
    private String fCurrency;

    public Money(int amount, String currency) {
        fAmount = amount;
        fCurrency = currency;
    }

    public int amount() { return fAmount; }
    public String currency() { return fCurrency; }

    public Money add(Money m) {
        return new Money(amount() + m.amount(), currency());
    }

    public boolean equals(Object anObject) {
        if (anObject instanceof Money) {
            Money aMoney= (Money)anObject;
            return aMoney.currency().equals(fCurrency) && aMoney.amount() == fAmount;
        }
        return false;
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3198880
  • 57
  • 1
  • 3

4 Answers4

1

They check for equality of two money objects. It checks if the parameter passed to equals is indeed a money object and then checks if the value and currency are equal

1
  • if (anObject instanceof Money) {

Is the object a Money Object? true or false

  • Money aMoney= (Money)anObject;

The object is a Money object. Cast the object to a Money object so we can access all of its Money-like fields and methods

  • return aMoney.currency().equals(fCurrency) && aMoney.amount() == fAmount;

Compare some Money things and return true or false

  • return false;

The object is not a Money object. Return false

Rainbolt
  • 3,542
  • 1
  • 20
  • 44
1

I'll take it from here:

 public boolean equals(Object anObject) {
   if (anObject instanceof Money) {
     Money aMoney= (Money)anObject;
     return aMoney.currency().equals(fCurrency) && aMoney.amount() == fAmount;
   }
   return false;
 }

What you have here is a method.

public boolean equals(Object anObject) {

Declares that this method is publilc, should return a boolean, and takes an Object as an arguement.

if (anObject instanceof Money) {

This line compares the object which is passed in as an argument, to the Class you've created called Money. the call "instanceof" returns true or false, and determines if the Objecct is an Instance of the Money Class.

Money aMoney= (Money)anObject;

This line allocates a portion of memory, and identifies the Object in that space as an object of Class Money. It then takes the object passed into the Method and Casts it to the Money Class. This only occurs if the preceding line (instanceof) returns true. If you did this without the prior check you might get an exception, say if you passed in a String.

return aMoney.currency().equals(fCurrency) && aMoney.amount() == fAmount;

Now that you have an object which is the type Money, you can use methods which are declared in the Money class. These include the method currency() and amount(). What you are doing here is retreiving the values for currency in the passed in object to some constant value (fCurrencY) and amount() to some other value (fAmount). You check to determine their equivlance, and return the truth result of ANDing the results of the two comparisons together.

You could rewrite this line as:

boolean currencyEqual = aMoney.currency().equals(fCurrency); // string and or object comparison for equivlance
boolean amount = aMoney.amount() == fAmount; // numeric comparison
boolean bothEqual = currencyEqual && amount;  //boolean AND operation (1 && 1 = 1, 1&&0 = 0, 0 && 0 = 0)
return bothEqual;

The final line:

return false;

Only runs if the passed in argument is not an instanceof Money, and thus you return false.

For a very good description of instanceof:

What is the 'instanceof' operator used for?

For a discussion of method declarations:

http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

for inheritance trees and casting:

http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Community
  • 1
  • 1
Nathaniel D. Waggoner
  • 2,856
  • 2
  • 19
  • 41
0

When comparing not primitive data type (that is not int, double, boolean, etc.) with == you are not comparing their values, but their references. That means you are basically comparing if they point to the same block of memory. To compare the values of these objects you need to override the method equals in their class.

You can see in your example code that String also uses equals, not ==, because String is also not primitive type.

Skylark
  • 66
  • 3