0

I`m trying to understand what my teacher wants from this exercise using the comparable from java.

I`m not sure what I need to do. Someone can give me a head start?

public class BookTag implements Comparable {

    private String left;
    private int mid;
    private String right;

    public BookTag(String left, int mid, String right) {
        check(left, mid, right);
        this.left = left.toUpperCase();
        this.mid = mid;
        this.right = right.toUpperCase();
    }

    @Override
    public int compareTo(Object arg) {

        /*
         * Booktags are sorted as follows: - first go booktags with lowest left
         * attribute. If left attributes cannot discriminate... - ... first go booktags
         * with the lowest mid attribute. If mid cannot discriminate... - ... first go
         * booktags with HIGHEST right attribute.
         */

        /* COMPLETE */
        BookTag tag = (BookTag) arg; 

        return this.left.compareTo(tag.compareTo(left));


    }
}

What king of values I need to compare here?

Jordan
  • 2,273
  • 9
  • 16
  • 1
    You're comparing `BookTag` objects based on their attributes. You'll need to compare based on the `left`, `mid`, and `right` fields of the current object and the `arg`, following the rules indicated by your teacher. – Jordan Apr 17 '19 at 19:14
  • @Sotirios Delimanolis - not a duplicate because this involves three attributes, two strings and an int. Well, it's partially duplicate? –  Apr 17 '19 at 19:39
  • @Benson99 I'll consider reopening if Gabriel edits their question to clarify. Right now, to me, it seems like they don't understand how to compare to strings and the duplicate explains that (for lexicographic order, at least). The number of strings doesn't seem relevant, unless they also want to understand how to include multiple things in their order, in which case I've added another duplicate. – Sotirios Delimanolis Apr 17 '19 at 19:44
  • Sorry Its my first post in here, but I manage to understand how to use the compareTo structure. – Gabriel Vendramini Apr 19 '19 at 10:43

2 Answers2

1

Objects can't be naturally compared, like primitive types, so you must provide some ordering yourself. One way to do this is implement the Comparable<T> interface.

public class BookTag implements Comparable<BookTag> {

     //compares this tag with another: 0 indicates equality, 1 means this is  
     //bigger, -1 means the other is bigger
     public int compareTo(BookTag other) { 

          int c = this.left.compareTo(other.left); //string implements this
          if(c != 0) { 
                return -c; //if the other's left is "smaller", this object is "bigger"
          }

          c = this.mid - other.mid;
          if(c != 0) { 
                return -c; //if the other's mid is "smaller", this object is "bigger"
          }

           c = this.right.compareTo(other.right);
           if(c != 0) { 
                return c; //if the other's right is "bigger", this object is "bigger"
          }

          return 0; //if we get here, then all values are equal, and so are the objects
     }   
}

I implemented Comparable<BookTag> instead of just raw Comparable for a bit simpler code.

Gtomika
  • 845
  • 7
  • 24
  • `mid` is an integer, it would probably be easier to just perform `this.mid - other.mid` rather than calling a `compareTo` method. – Patrick Apr 17 '19 at 19:30
  • @Patrick Good catch, I thought that is a string as well. Will edit that. – Gtomika Apr 17 '19 at 19:31
0
BookTag other = ((BookTag)o); // cast it
int leftcompare = this.left.compareTo(other.getLeft());
int midCompare = this.mid - other.getMid();
// careful with the right compare as the highest goes first
int rightCompare = this.right.compareTo(other.getRight());


// if , then, else statement to fill your teacher's requirements
// negative number means that this book tag is higher on the compare list
// 0 means cannot discriminate (move to the next field left -> mid -> right
// positive number means the other BookTag is higher on the list

And I don't know this one, but maybe you can just weight the values rather than an if-then-else. Have fun :)

  • Gonna try this now. I think I got the idea the return from this String left and right is a number too. – Gabriel Vendramini Apr 17 '19 at 19:40
  • Yep. If you check the Comparable interface, it deals with negative (higher) zero (non discriminate) and positive(lower) values. If you return a -ve number that means that the invoking object is higher on the stack, 0 means equal and +ve means lower. –  Apr 17 '19 at 19:42