Assignment No.11

  1. (Adapted from Textbook Chapter 7 Exercise 20) Suppose you have a class StringTwo with just one instance variable private String word. Write public int compareTo( StringTwo o )word that performs the comparison between this and o in terms of the contents of the instance variable word. The value returned by the method is determined as follows:
    1. -1 if either (a) this.word is shorter than than o.word or (b) they have the same length and this.word precedes o.word in the dictionary order;
    2. +1 if either (a) this.word is longer than than o or (b) they have the same length and o.word precedes this.word in the dictionary order;
    3. 0 if the words are identical to each other.

  2. Recall that Coordinate is a class defined with two double variables x and y represenging the (x,y)-codinate of a point on the xy-plane. Suppose two coordinates c=(x,y) and c'=(x',y') are compared to each other as follows:
    1. c is smaller than c' if either x < x' or ( x = x' and y < y')
    2. c is equal to c' if x = x' and y = y'
    3. c is greater than c' if either x > x' or ( x = x' and y > y')
    According to this rule, write an instance method public int compareTo(Coordinate o) that compares this with o that returns -1, 0, and +!, respectively to the above three cases.

Go back to the assignment list page.