Saturday, August 28, 2010

compareTo() method is necessary to implement the java.util.Comparable interaface

Courtesy of Sun Web Learning Center



Given:

 2. import java.util.*;
 3. class MyInfo implements Comparable {
 4.   MyInfo(String s) { desc = s; }
 5.   String desc;
 6.   // insert code here
 7.   public String toString() { return desc; }
 8. }
Which method, inserted at line 6, correctly implements java.util.Comparable?


A

int compare(MyInfo m) {
return desc.compareTo(m.desc); }

B

int compareTo(MyInfo m) {
return desc.compareTo(m.desc); }

C

int compareable(MyInfo m) {
return desc.compareTo(m.desc); }

D

int compare(Object o) {
return desc.compareTo(o.toString()); }

E

int compareTo(Object o) {
return desc.compareTo(o.toString()); }

F

int compareable(Object o) {
return desc.compareTo(o.toString()); }


B correctly implements the compareTo() method necessary to implement the java.util.Comparable interface.

No comments:

Post a Comment