Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Circular Array

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Circular Array

    Good day im really struggling to see what is wrong with my code any help would be awesome.
    public class RingBuffer {
     
        public double[] rb;          // items in the buffer
        private int first;            // index for the next dequeue or peek
        private int last;             // index for the next enqueue
        private int size;   
        private int Capacity;// number of items in the buffer
     
        // create an empty buffer, with given max capacity
        public RingBuffer(int capacity) {
            rb = new double[capacity];
            for(int k = 0;k < capacity;k++)
            {
            	rb[k] = 0;
            }
            first = 0;
            last = 0;
            size = 0;
            Capacity = capacity;
        }
     
        // return number of items currently in the buffer
        public int size() 
        {  	
        	if((first == last) && (rb[first] ==0))
        	{
        	size = 0;
        	return size;
        	}
     
        		size = (last - first);
     
        			if (size < 0)
        			{
        			size += Capacity;
        			}
        		size++;
        		return size;
        		}
     
     
        // is the buffer empty (size equals zero)?
        public boolean isEmpty()
        {
        	if(size() ==0)
        		return true;
        	else return false;
     
        }
        // is the buffer full (size equals array capacity)?
        public boolean isFull() 
        {
        	if(size() == (Capacity)) return true;
        	else return false;
        }
     
        // add item x to the end
        public void enqueue(double x) {
            if (isFull()) {
                throw new RuntimeException("Ring buffer overflow");
            }
          if(isEmpty())
          {
          rb[0] =x;
          }
          else if(last == Capacity-1)
          {
          last = 0;
          rb[0]=x;
          }
          else
          {
          last++;
          rb[last] =x;
          }
        }
     
        // delete and return item from the front
        public double dequeue() {
            if (isEmpty()) {
                throw new RuntimeException("Ring buffer underflow");
            }
         double temp = rb[first];
         rb[first] = 0;
     
     
         if (first ==( Capacity-1))
         { 
         first =0;
         }
         else
         {
         first++;
         }
         return temp;
        }
     
        // return (but do not delete) item from the front
        public double peek() {
            if (isEmpty()) {
                throw new RuntimeException("Ring buffer underflow");
            }
         return rb[first];
        }
     
        // a simple test of the constructor and methods in RingBuffer
     
     
        public static void main(String[] args) {
            int N = 10;
            RingBuffer buffer = new RingBuffer(N);  
            for (int i = 1; i <= N; i++) {
                buffer.enqueue(i);
            }
            double t = buffer.dequeue();
            buffer.enqueue(t);
            System.out.println("Size after wrap-around is " + buffer.size());
            while (buffer.size() >= 2) {
                double x = buffer.dequeue();
                double y = buffer.dequeue();
                buffer.enqueue(x + y);
            }
            System.out.println(buffer.peek());
        }
    //output should be Size after wrap-around is 10
        //    55.0 
     
     
    }
    Last edited by helloworld922; September 11th, 2012 at 07:12 PM. Reason: please use [code] tags


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Circular Array

    Quote Originally Posted by KobusJordaan View Post
    ...struggling to see what is wrong
    //output should be Size after wrap-around is 10
        //    55.0
    My suggestion: Instrument your code so that the program can tell you what it is seeing and what it is doing:

            System.out.println("Size after wrap-around is " + buffer.size()); // Where things start to go wrong
            while (buffer.size() >= 2) {
                System.out.println("\nIn loop: size is " + buffer.size()); // Debugging code is verbose enough to give some useful information
                double x = buffer.dequeue();
                System.out.println("Size after one dequeue  is " + buffer.size());
                double y = buffer.dequeue();
                System.out.println("Size after two dequeues is " + buffer.size());
                buffer.enqueue(x + y);
                System.out.println("Size after one enqueue  is " + buffer.size());
            }
    Last edited by Zaphod_b; September 11th, 2012 at 01:49 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Circular Array

    Thank you got it working problem was in my enqueue and dequeue:

    public void enqueue(double x) {
    if (isFull()) {
    throw new RuntimeException("Ring buffer overflow");
    }
    if(size() == 0)
    {
    rb[0] =x;
    first =0;
    last =0;

    }
    else if(last == Capacity-1)
    {
    last = 0;
    rb[0]=x;
    }
    else
    {
    last++;
    rb[last] =x;
    }
    }
    and
    public double dequeue() {
    if (isEmpty()) {
    throw new RuntimeException("Ring buffer underflow");
    }
    double temp = rb[first];
    rb[first] = 0;


    if (first ==( Capacity-1))
    {
    first =0;
    }
    else if ((first == last) && (first != 0))
    {
    rb[first] = 0;
    return temp;

    }
    else
    {
    first++;
    }
    return temp;
    }

Similar Threads

  1. help with circular linked list
    By Seans0007 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 12th, 2012, 03:37 PM
  2. Help with a doubly linked circular list
    By TeamRival in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2011, 10:59 PM
  3. Java tip Aug 26, 2010 - Circular Buffers
    By helloworld922 in forum Java Programming Tutorials
    Replies: 0
    Last Post: August 26th, 2010, 10:33 PM
  4. Java tip Aug 26, 2010 - Circular Buffers
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: August 26th, 2010, 10:33 PM
  5. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 11:40 AM

Tags for this Thread