Title: WordInfo.java
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

WordInfo.java



public class WordInfo implements Comparable
{
    String word;
    int line;
    int count=1;
    WordInfo next=null;

    public String toString()
    {
        return word + ":"+line + ":"+ count;
    }


    public int compareTo(Object ob)
    {
        WordInfo w = (WordInfo)ob;
        if (word.equals(w.word)) return (line-w.line);
        else return word.compareTo(w.word);
    }
    WordInfo(String word, int line)
    {
        this.word = word;
        this.line = line;
    }
    public boolean sameWord(WordInfo w)
    {
        if (w == null) return false;
        return word.equals(w.word);
    }
    public static void main(String[] args)
    {
        WordInfo w= new WordInfo("a",2);
        WordInfo w2 = new WordInfo("b",1);
        WordInfo w3 = new WordInfo("b",2);
        WordInfo w4 = new WordInfo("b",2);

        // Should see return codes of -1,-1, 0, 1
        System.out.println("w.compareTo(w2)="+ w.compareTo(w2));
        System.out.println("w2.compareTo(w3)="+ w2.compareTo(w3));
        System.out.println("w4.compareTo(w3)="+ w4.compareTo(w3));
        System.out.println("w3.compareTo(w2)="+ w3.compareTo(w2));
    }

}