my display() method is public and looks to be defined , however eclipse says that its not defined.
this is a code for an amortized time resizing circular array. can someone help me out here ? also , please do point out if you see something else wrong with the code.

thanks in advance.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class ResizingCircularArray<E>{

    private int head = 0; // head , tail are modulo capacity , ie modulo arr.length
    private int tail = 0;
    private int size = 0; // a measure of non-null elements in the array
    private E[] arr;
    private void resize(){

        @SuppressWarnings("unchecked")
        E[] tempArr = (E[]) new Object[2*size];
        System.arraycopy(arr, head , tempArr , 0 , size );
        arr = tempArr;
    }

    public void enqueue(E item){
        // when size = arr.length , and tail has made one loop and now pointing to head.
        if( tail == head && size!=0 ){ resize(); }
        arr[tail] = item;
        tail = ( tail + 1 ) % arr.length;
        size++;     
    }
    public E dequeue(){
        E item = arr[head];
        head = ( head + 1 ) % arr.length; // dequeing leads to size reduction , making the head move more towards tail , hence + 1.
        size--;
        if(size == arr.length/4){ resize(); }
        return item;
    }
    public int size(){ return size; }   

    public void display(){
        for(E item : arr)
            System.out.print(" " + item);
        System.out.println("\n\n");
    }

    @SuppressWarnings("unchecked")
    public ResizingCircularArray(){
        arr = (E[]) new Object[5];
    }

    public static void main(String[] args){

        ResizingArrayQueue<String> r = new ResizingArrayQueue<String>();
        String line = null;
        String[] parsed;

        try(BufferedReader is = new BufferedReader(new InputStreamReader(System.in))){
            while((line = is.readLine())!=null){
                if(line.equals("stop")) break;
                parsed = line.trim().split("[ ]+");
                switch (parsed[0]) {
                case "enq":
                    System.out.println(" adding : " + parsed[1]);
                    r.enqueue(parsed[1]);                   
                    break;
                case "deq":
                    System.out.println(" removing : " + r.dequeue());
                    break;
                case "disp":
                    r.display();
                    break;
                default:
                    break;
                }
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

Recommended Answers

All 5 Replies

I don't see anything wrong there, but since we don't have the entire code, we can't actually test it, I guess.

r is an instance of ResizingArrayQueue whereas the display method is part of ResizingCircularArray?

commented: guess I missed the obvious there :) +14
commented: *facepalm* to me. +3

thanks a lot mr Baen Mido !! kudos to your evil eyes... i hope you can use that for more than thrice a day :)

i never noticed that eclipse auto code added something else from my jar package... thanks!

commented: You bet I can ;) +14

i marked it solved , but something's wrong with the code... for the record , im marking it unsolved again
(dont want a faulty code in a solved thread)

i changed the code to read from a file , being :

enq a
enq b
enq c
enq d
enq e
enq f
enq g
enq h
enq i
enq j
enq k
deq
deq
deq
deq
deq
deq
deq

and it gave this as output :
adding : a
adding : b
adding : c
adding : d
adding : e
adding : f
resizing array to size: 10
adding : g
adding : h
adding : i
adding : j
adding : k
removing : f
removing : g
removing : h
removing : i
removing : j
removing : k
removing : null

somethings not right... ill see if i can figure it out. any suggestions would be great as well.
edit : i think my resize condition at enqueue is causing the problem.

edit 2 : overthought the whole modulo thing. not needed . simplified (and working) change :

public void enqueue(E item) {
        // when size = arr.length , and tail has made one loop and now pointing
        // to head.
        arr[size++] = item;
        if (size == arr.length) {
            resize();
        }       

        System.out.println(" head : " + head + " tail : " + tail + " , size : " + size);
    }

    public E dequeue() {
        E item = arr[head++];
        size--;
        System.out.println(" head : " + head + " tail : " + tail + " , size : " + size);
        if (size == arr.length / 4) {
            resize();
            System.out.println(" head : " + head + " tail : " + tail + " , size : " + size);
        }

        return item;
    }

finer debugs needed. but essentially solved i hope. thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.