Search code examples
javaincompatibletypeerror

Error:incompatible types in a java class


I implemented a java class but come up with two errors which I can't fix. Error1:

incompatible types
Required: RandomizedQueue<Item>
Found:    Item

Error2:

can not find symbol
Symbol: valuable last
location:valuable next of type Item
public class RandomizedQueue<Item> implements Iterable<Item> {
    private int number=0;
    private Node First=null;
    private class Node {
        Item item;
        Item next=null;
        Item last=null;
    }
    private Node Random() {
        double r = Math.random();
        int n = (int) (r*number);
        if(n==0) n=1;
        Node ob=First;
        for(int i=0;i<(n-1);i++) {
            ob = ob.next;   //Error1
        }
        return ob;
    }

    public RandomizedQueue() {
        Node empty=new Node();
    }
    public boolean isEmpty() {
        return number==0;
    }
    public int size() {
        return number;
    }
    public void enqueue(Item item) {
        if(item==null) throw new NullPointerException();
        Node oldfirst = First;
        First = new Node();
        First.item = item;
        First.next = oldfirst; //Error1
        oldfirst.last = First;
        number++;
    }
    public Item dequeue() {
        Node ob=Random();
        Item back = ob.item;
        Node temp = ob.last;
        temp.next = ob.next;
        ob.next.last = temp;   //Error2
        return back;
        }
    public Item sample() {
        return Random();
    }
    public Iterator<Item> iterator() {       //Error:cannot find symbol symbol:class Itertor
        return IndepentRandomIterator();
    }
    private class IndepentRandomIterator implements Iterator<Item> {
        private RandomizedQueue<Item> iq = new RandomizedQueue<Item>();
        Node scan = First;
        public IndepentRandomIterator() {
            while(scan != null) {
                iq.enqueue(scan.item);
                scan=scan.next;
        }
        }
        public boolean hasNext() {
            return iq.number >0;
        }
        public void remove() {
            throw new UnsupportedOperationException();
        }
        public Item next() {
            if(iq.number==0) throw new java.util.NoSuchElementException();
            return iq.dequeue();
        }
    }
    public static void main(String[] args) {
        RandomizedQueue<String> test = new RandomizedQueue<String>();
        test.enqueue("Luo");
        test.enqueue("Jiebin");
        test.enqueue("is");
        test.enqueue("genious");
        test.dequeue();
        test.dequeue();
        StdOut.println("Is it empty?"+test.isEmpty());
        StdOut.println("Size:       "+test.size());
        StdOut.println("A sample:   "+test.siample());
        Iterator<String> it = test.iterator();
        while(test.hasNext()) {
            String s = it.next();
            StdOut.println(s);
        }
    }
}

Solution

  • For the first error, you are trying to assign ob a value of ob.next. ob is a Node, but ob.next is an Item. Obviously, an Item is not a Node, so you can't assign an Item to a Node.

    The same thing applies on the line:

    first.next = oldfirst;
    

    first.next is an Item, but oldFirst is a Node. You can't assign a Node to a variable of type Item.


    For the error cannot find symbol symbol:class Itertor. That is because you need to put this at the very top of your code:

    import java.util.Iterator;
    

    On the line:

    return IndepentRandomIterator();
    

    The code attempts to call the method IndepentRandomIterator(). To make it call the constructor for the IndepentRandomIterator class, you need to add the keyword new right before IndepentRandomIterator() like this:

    return new IndepentRandomIterator();
    

    For your error on this line:

    ob.next.last = temp;
    

    The code attempts to access the field last of the object ob.next. I think you want to access the object ob.last. That will still be a problem, though, because then you will get the first error. Both ob.next and ob.last are of the type Item, but temp is a Node.


    Near the end of your code, you try to use StdOut.println(). StdOut does not exist; you should use System.out.println().