0

My professor is having us implement Java comparable interfaces to help my Binary Search Tree compare word objects that hold the word that was scanned and then compare that word to other words, but for whatever reason, the second one I am doing is throwing an error. I do know that the methods he gave us to use does cast (TreeComparable) in it here which I am still not 100% sure why that is the line that the compiler freezes at and gives me the error

java.lang.ClassCastException: class ObjectTreeNode cannot be cast to class 
TreeComparable (ObjectTreeNode and TreeComparable are in unnamed module of loader java.net.URLClassLoader

and this is the method that is causing it

public void insertBSTDup(Object o) {
        ObjectTreeNode p, q;

        ObjectTreeNode r = new ObjectTreeNode(o);
        if (root == null)
            root = r;
        else {
            p = root;
            q = root;
            while (q != null && ((TreeComparable)(r.getInfo())).compareTo(p.getInfo()) != 0) { <---------
                p = q;
                if (((TreeComparable)(r.getInfo())).compareTo(p.getInfo()) < 0)
                    q = p.getLeft();
                else
                    q = p.getRight();
            }
            if (((TreeComparable)(r.getInfo())).compareTo(p.getInfo()) < 0)
                setLeftChild(p, r);
            else if (((TreeComparable)(r.getInfo())).compareTo(p.getInfo()) > 0)
                setRightChild(p, r);
            else ((TreeComparable)(p.getInfo())).operate(r.getInfo());
        }
    }

My Word class is this so far, and the compareTo method is at the bottom, which is implemented in a similar way to the first compareTo I did on my first assignment, and it is comparing word strings so it knows where it will be added.

public class Word implements TreeComparable
{
    private String word;
    private ObjectList list = new ObjectList();
    private ObjectListNode obj;
    private int numberOfTimes = 1, LineNumber, position;


    public Word(String word, int LineNumber, int position)
    {
        this.word = word;
        this.LineNumber = LineNumber;
        this.position = position;
    }

    public int compareTo(Object o)
    {
        Word w = (Word) o;
        return word.compareTo(w.getWord());
    }

    public String getWord()
    {
        return word;
    }

and this is the treeComparable method

public interface TreeComparable
{
    public int compareTo(Object O);
    public void operate(Object O);
    public void visit();
}

what can I change to make it so that error to go away, other than remove the treeComparable casting, which I must keep going for this project

Also, this is the objectTreeNode object that the Word object will go into

public class ObjectTreeNode implements ObjectTreeNodeInterface
{
    private Object info;
    private ObjectTreeNode left;
    private ObjectTreeNode right;

    public ObjectTreeNode() {
        info = null;
        left = null;
        right = null;
    }

    public ObjectTreeNode (Object o) {
        info = o;
        left = null;
        right = null;
    }

    public void setInfo(Object o) {
        info = o;
    }

    public Object getInfo() {
        return info;
    }

    public void setLeft(ObjectTreeNode p) {
        left = p;
    }

    public ObjectTreeNode getLeft() {
        return left;
    }

    public void setRight(ObjectTreeNode p) {
        right = p;
    }

    public ObjectTreeNode getRight() {
        return right;
    }
}

And here is main

public static void main(String args[]) throws IOException
    {
        Hash h = new Hash();
        Word w;
        ObjectBinaryTree bt = new ObjectBinaryTree();
        ObjectTreeNode node;
        Scanner in = new Scanner(new File("getty.txt"));
        PrintWriter pw = new PrintWriter(new FileWriter("csis.txt"));
        int numberOfLines = 1;
        //h.check();


        while(in.hasNext())
        {
            String word = in.nextLine();
            String[] ar = word.split(" ", 0);

            System.out.print("\n" + (numberOfLines++) + ": ");
            int i = 0;
            while(i < ar.length)
            {
                char check = ar[i].charAt(ar[i].length() - 1);
                if(check == ',' || check == '.' || check == '!' || check == '?')
                {
                    ar[i] = ar[i].substring(0, ar[i].length() - 1);
                }
                w = new Word(ar[i], numberOfLines, (i + 1));
                node = new ObjectTreeNode(w);
                //System.out.println(ar[i]);
                bt.insertBSTDup(node);
                System.out.print("(" + (i + 1) + ") " + ar[i] + " ");
                i++;
            }
        }
    }
  • dup of https://stackoverflow.com/questions/907360/explanation-of-classcastexception-in-java – fuat May 17 '20 at 08:41

1 Answers1

0

Your insertBSTDup method expects every return value of getInfo to be of type TreeCompareable. It also accepts as input parameter an object which is presumably the object to insert, which therefore should also be a TreeCompareable. In fact you wrap it in an ObjectTreeNode right away:

public void insertBSTDup(Object o) {
        ObjectTreeNode p, q;

        ObjectTreeNode r = new ObjectTreeNode(o);

However, when you call insertBSTDup you don't pass in a Word object (which would implement TreeComparable but a ObjectTreeNode instead:

            node = new ObjectTreeNode(w);
            bt.insertBSTDup(node);

So either change the parameter of insertBSTDup to a ObjectTreeNode and get rid of wrapping it again or pass w into insertBSTDup instead of Node.

In a more general sense, much of this could have been avoided if you didn't just take a Object as the parameter, when you really expect a TreeComparable. That way the compiler would have told you that an ObjectTreeNode is not a TreeComparable instead of the problem only occurring at runtime.

Basically every place where you use Object should be TreeComparable instead. And in fact you should probably use a generic type instead, if you already learned of them.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614