0

I need to sort my array of Money objects in ascending order, but I get 3 compiler errors.

TestMoney.java:44: error: bad operand types for binary operator '<'
            if (list[j] < list[min]) {
                        ^
  first type:  Money
  second type: Money
TestMoney.java:50: error: incompatible types: Money cannot be converted to int
            final int temp = list[i];
                                 ^
TestMoney.java:52: error: incompatible types: int cannot be converted to Money
            list[min] = temp;
                        ^

class TestMoney
{
   public static void main(String [] args)
   {
      Money[] list = new Money[15];
      for(int i =0; i<15; i++)
      {
         int dollar =(int) (Math.random() * 30 + 1);
         int cent = (int) (Math.random() * 100);
         list[i] = new Money(dollar, cent);

      }
      sortArray(list);
      printArray(list);

   }
   public static void printArray(Money[] list)
   {
      for(int i =0; i <list.length; i++)
      {

         if(i%10 ==0)
         {
          System.out.println();
         }

         System.out.print(" " + list[i]);
     }

   }
   public static void sortArray(Money[] list)
   {
   int min;
    for (int i = 0; i < list.length; i++) {
        // Assume first element is min
        min = i;
        for (int j = i + 1; j < list.length; j++) {
            if (list[j] < list[min]) {
                min = j;

            }
        }
        if (min != i) {
            final int temp = list[i];
            list[i] = list[min];
            list[min] = temp;
        }
        System.out.println(list[i]);// I print the in ascending order
    }

  }
}

class Money
{
   private int dol;
   private int cen;

   Money()
   {
      dol = 0;
      cen = 00;
   }
   Money(int dol,int cen)
   {
      int remainder = cen % 100;
      int divisor = cen / 100;
      this.dol = dol+ divisor;
      this.cen = remainder;
   }
   public int getDollors(int dol)
   {
      return dol;
   }
   public int getCents(int cen)
   {
      return cen;
   }
   public void setDollors(int d)
   {
      dol = d;
   }
   public void setCents(int c)
   {
      cen = c;
   }
   public Money addMoney(Money m)
   {
      int d = this.dol + m.dol;
      int c = this.cen + m.cen;
      return new Money(d, c);
   }
   public int compareTo(Money m)
   {

     if(this.dol<m.dol && this.cen<m.cen)
     return -1;
     else if(m.dol<this.dol && m.cen<this.cen )
     return 1;
     return 0;


   }
   public Money subtract(Money m)
   {
      int cents1 = this.dol*100 + this.cen;
      int cents2 = m.dol *100 + m.cen;
      int cents = cents1 -cents2;
      return new Money(cen/100,cen%100);
   }


   public String toString()
   {

      return String.format("$%d.%02d", this.dol,this.cen);

   }
}
4
  • you need to implement the comparator class in order for the < to evaluate the two Money objects, check out this example - tutorialspoint.com/java/java_using_comparator.htm Commented Apr 4, 2017 at 22:46
  • 3
    Read the code you posted. int temp = list[i], when list[i] is Money, clearly isn't going to work. int is not the same as Money, so they're not assignment compatible. Did you even read the error message? Commented Apr 4, 2017 at 22:48
  • 1
    You can't convert between an object (Money) and a value (int). What would you expect that to do? Commented Apr 4, 2017 at 22:49
  • 2
    Your getters aren't doing what you think they're doing. They just return the value you pass them, they don't return anything from the object. Commented Apr 4, 2017 at 22:50

2 Answers 2

1

That's because you are trying to compare two Money objects with < operator which applies to Numbers only, you need to replce the following:

if (list[j] < list[min]) {

with

if (list[j].getDollors() < list[min].getDollors() 
    || (list[j].getDollors() == list[min].getDollors() && list[j].getCents() < list[min].getCents())) {

Also, you don't need your getters to accept an argument, they can be zero argument methods as they just return a value.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much !
0

Another way, maybe, is using Comparator interface, like this:

public class Money implements Comparator<Money>{
int n; //as an example, compare two object by them "n" variable

@Override
public int compare(Money o1, Money o2) {
    int result;
    if (o1.n > o2.n)
        result = 1; //greater than case
    else
        result = o1.n < o2.n ? -1 // less than case
                : 0; // equal case
    return result;
}

}

In the compare method you can use the criteria of gt-ls-eq based on amount of money. Finally do:

for (int j = i + 1; j < list.length; j++) {
        if (list[j].compare(list[min]) < 0) { //you ask if is greater, less,
                                              //or equal than 0
            min = j;
        }
}

3 Comments

Thanks alot ! I appreciate this
I put you a thumbs up..but it won't show for you because I'm new here
It's ok! I'm glad I could help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.