1

I'm trying to make this piece of code print the actual numbers, and not the hexadecimal location.

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);
  }
}

This is the main class which is called by moneydriver

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;
  }
}

The objectives is to write equals method (on main class). The method compares the instance variables of the calling object with instance variables of the parameter object for equality and returns true if the dollars and the cents of the calling object are the same as the dollars and the cents of the parameter object. Otherwise, it returns false.

Write toString method (on main class). This method will return a String that looks like money, including the dollar sign. Remember that if you have less than 10 cents, you will need to put a 0 before printing the cents so that it appears correctly with 2 decimal places.

If both of the method is implemented correctly

According to tutorialspoint, you're supposed to do either

String toString()
static String toString(int i)

But the supplied moneydriver already has the tostring method, but doesn't display the numbers, instead it displays a hexadecimal location of the variable.

The equals method is already used in moneydriver, so I'm kinda lost on that too.

The correct output should look like this

The current amount is $500.00 Adding $10.02 gives $510.02 Subtracting $10.88 gives $499.1 $10.02 equals $10.02 $10.88 does not equal $10.02

Completely lost in this, thanks in advance for help.

user207421
  • 305,947
  • 44
  • 307
  • 483
Yellow_13
  • 39
  • 1
  • 1
  • 10
  • You would normally implement `.toString()` on the Object that is being outputted, in this case the `Money` class. This call `System.out.println("The current amount is " + balance.toString());` which converts `balance` (which is an instance of `Money`) to a String needs the method. Either you have misunderstood what is the "main" class, or there is a large issue with the approach. – KevinO Apr 20 '16 at 02:52
  • @KevinO - public class Money is the main class – Yellow_13 Apr 20 '16 at 02:53
  • Therefore, implement (the overridden method) `.toString()` on the `Money` class. In the driver class, when you call `balance.toString()` it is calling the default method on Object. – KevinO Apr 20 '16 at 02:55
  • @KevinO - how exactly do I over-write a toString and convert it to a method? – Yellow_13 Apr 20 '16 at 02:58
  • I have provided an quick example in an answer. – KevinO Apr 20 '16 at 02:59

1 Answers1

0

To get a String output on the Money class, do something akin to:

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
KevinO
  • 4,303
  • 4
  • 27
  • 36
  • Ok thanks. But how about the equals method? How do I return if it's equal or not equal? public boolean equals(Object o) what return line should I write on this method? – Yellow_13 Apr 20 '16 at 03:26
  • @Yellow_13, you implement on the `Money` class `public boolean equals(Object obj) { ... }`. Review the [Javadocs for Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) and [How to override equals method in Java](http://stackoverflow.com/questions/8180430/how-to-override-equals-method-in-java). I'll add a quick example in the answer, but you should be in a position to answer the question yourself. Note that it is standard to override `.hashCode()` when overriding `.equals()`, but that is beyond the scope here. – KevinO Apr 20 '16 at 03:35
  • What does instanceof do? – Yellow_13 Apr 20 '16 at 03:45
  • The [`instanceof`](http://stackoverflow.com/questions/7526817/use-of-instance-of-in-java)` operator ensures that the object is an instance of a specific class, in this case `Money`. When `instanceof` returns true, it is safe to cast the object reference to the type. There are debates about `instanceof` vs. `.getClass()`, but those are not important here. – KevinO Apr 20 '16 at 03:50
  • so instanceof money gets the first money object to be compared with the second money object "chk" ? – Yellow_13 Apr 20 '16 at 04:04
  • The `instanceof` ensures that the `Object obj` is of the class `Money`. In this way, it is possible to cast the `Object` to a specific type (`Money`). This check ensures we are being compared with an appropriate type, and allows access to the instance variables `dollars` and `cents`. I would suggest that additional questions about the `instanceof` in relation to an `equals` method be posed in a new SO question, since comments aren't supposed to be used for extended discussions. – KevinO Apr 20 '16 at 04:07