0

We are building a train station Simulator and when we started creating our methods we got errors about our dequeue method.

Type mismatch: cannot convert from Object to Passenger package SimulateWaitingLine;

 public class Station {
        private QueueInterface waiting;
        private int timeToNextStation;
        public Station(int timeToNext){
            waiting = new CircularLinkedQueue();
            timeToNextStation = timeToNext;
        }
        public void addPassengers(Passenger rider){
            waiting.enqueue(rider);
        }
        public boolean isWaiting(){
            return !waiting.isEmpty();
        }
        public Passenger getPassenger(){
            return waiting.dequeue(); //this is where the error is
        }
        public int getTimeToNextStation(){
            return timeToNextStation;
        }
    }

here is our Definition of Linked Queue package SimulateWaitingLine;

public class CircularLinkedQueue < T > implements QueueInterface<T>
{
    private Node queueNode;
    private Node freeNode;
    public CircularLinkedQueue()
    {
        freeNode = new Node();
        freeNode.setNextNode(freeNode);
        queueNode = freeNode;
    }
    private class Node
    {
        private T data;
        private Node next;
        public T getData() { return data; }
        public void setData(T inData) { data = inData; }
        public Node getNextNode() { return next; }
        public void setNextNode(Node inNext) { next = inNext; }

    }
    public void enqueue(T newEntry)
    {
    freeNode.setData(newEntry);
    if(isChainFull())
    {
        Node newNode = new Node();
        freeNode.setNextNode(newNode);
    }
    freeNode = freeNode.getNextNode();
    }
    @Override
    public T dequeue() {
        // TODO Auto-generated method stub
        T front = getFront();
        assert !isEmpty();
        queueNode.setData(null);
        queueNode = queueNode.getNextNode();
        return front;

    }
    @Override
    public T getFront() {
        // TODO Auto-generated method stub
        T frontNode = null;
        if(!isEmpty())
            queueNode = queueNode.getNextNode();
        return (T) queueNode;


    }
    public boolean isChainFull() {
        return queueNode == freeNode.getNextNode();
    }
    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
    return queueNode == freeNode;

    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub

    }

    }

1 Answer 1

3

Set the generic type of the queue toPassenger:

private QueueInterface<Passenger> waiting;

More on generics.

Sign up to request clarification or add additional context in comments.

2 Comments

we made that fixed the only problem is when it runs its not running correctly see here:
If that's solved you can mark this answer as accepted. As for your new problem - that may be a good follow-up question once you narrow the suspected code and share you stack trace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.