Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7458385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:26:25+00:00 2026-05-29T13:26:25+00:00

I’ve written a class to implement a double ended queue using a left justified

  • 0

I’ve written a class to implement a double ended queue using a left justified array.
I also want to write a class to implement a double ended queue using a “circular” method.
I said that since it’ll share some methods that i’ll make it a subclass.
When I try to override the method insert first i get a compile time error.

CircularArrayBasedDeque.java:6: reference to insertFirst is ambiguous, both method insertFirst(EltType) in ArrayBasedDeque and method insertFirst(EltType) in CircularArrayBasedDeque match
            dq.insertFirst(i);
              ^
CircularArrayBasedDeque.java:21: method does not override or implement a method from a supertype
    @Override
    ^

ArrayBasedDeque.java

public class ArrayBasedDeque <EltType>
               implements Deque <EltType> 
{
    public static void main( String[] args )
    {
        ArrayBasedDeque dq = new ArrayBasedDeque();
        for( int element = 0; element < 64; element ++) 
        {
            dq.insertFirst(element);
            System.out.println(dq);
        } 
        /**for( int element = 0; element < 25; element ++) 
           {
               dq.insertLast(element);
               System.out.println(dq);
           }
          */
        for( int element = dq.size(); element > 0; element --) 
        {
               dq.removeFirst();$
               System.out.println(dq);$
        }
    }
    private final int INITIAL_CAPACITY = 2;
    private int capacity;
    private EltType elements[];
    private int first;
    private int last;
    public ArrayBasedDeque()
    {
        capacity = INITIAL_CAPACITY;
        elements = ( EltType[] ) ( new Object[INITIAL_CAPACITY] );
        first = -1;
        last = 0;
    }
    /**
      *  Returns the size of the Deque by
      *  returning the index of the first element + 1.
      *  @return the deque size.
      */
    public int size() 
    {
        return first + 1;
    }
    /**
      *  Returns true if and only if the deque is empty.
      *  @return true/false indication of emptiness.
      */
    public boolean isEmpty() 
    {
        return ( first + 1 == last );
    }
    /**
      *  Return the first element of the deque;
      *  illegal if the deque is empty.
      *  @return the front element.
      */
    public EltType first() 
    {
        if(isEmpty()) 
        {
            System.err.println("You cannot remove an element from an empty double sided queue");
        }
        return elements[first];
    }
    /**
      *  Return the last element of the deque;
      *  illegal if the deque is empty.
      *  @return the last element.
      */
    public EltType last() 
    {
        if(isEmpty()) 
        {
            System.err.println("You cannot remove an element from an empty double sided queue");
        }
        return elements[last];
    }
    /**
      *  Insert item at the front of the deque.
      *  @param element: the item to be added.
      */
    public void insertFirst( EltType element ) 
    {
        if(isFull()) 
        {
           expand();
        }
        elements[first + 1] = element;
        first++;
    }
    /**
      *  Insert item at the rear the deque.
      *  @param element: the item to be added.
      */
    public void insertLast( EltType element ) 
    {
        if(isFull()) 
        {
            expand();
        }
        for( int index = first + 1; index > 0; index-- ) 
        {
            elements[index] = elements[ index - 1 ];
        }
        elements[0] = element;
        first++;
    }
    /**
      *  Return and remove the front element of the deque;
      *  Illegal if deque is empty.
      */
    public EltType removeFirst() 
    {
        if(isEmpty()) 
        {
            System.err.println("You cannot remove an element from an empty double sided queue");
        }
        if(isQuarterFull())
        {
             contract();
        }
        return elements[first--];
    }
    /**
      *  Return and remove the last element of the deque;
      *  illegal if the deque is empty.
      */
    public EltType removeLast() 
    {
        if(isEmpty()) 
        {
            System.err.println("You cannot remove an element from an empty double sided queue");
        }
        if(isQuarterFull())
        {
             contract();
        }
        EltType last = elements[0];
        for (int index = 0; index < first; index ++) 
        {
             elements[index] = elements[ index + 1 ];
        }

        return last;
    }
    /**
      *  Return true if and only if the queue is full.
      *  @return boolean representsion of weather the queue is full.
      */
    public boolean isFull() 
    {
        return ( size() == capacity );
    }
    /**
      *  Return true if and only if the queue is quarter full
      *  @return boolean representation of weather the queue is quarter full.
      */
    public boolean isQuarterFull() 
    {
        return ( size() == capacity / 4 );
    }
    /**
      *  Doubles the capacity of the array representing the queue
      */
    public void expand() 
    {
        EltType[] tmp;
        tmp = ( EltType[] ) ( new Object[this.size() * 2] );
        for( int element = 0; element < capacity ; element++ ) 
        {
            tmp[element] = elements[element];
        }
        capacity *= 2;
        elements = tmp;
    }
    /**
      *  Halves the capacity of the array representing the queue
      */
    public void contract() 
    {
        EltType[] tmp;
        tmp = ( EltType[] ) ( new Object[ capacity / 2 ] );
        for ( int element = 0; element < size(); element++ ) 
        {
            tmp[element] = elements[element];
        }
        capacity /= 4;
        elements = tmp;
    }
    /**
      *  Returns a string representation of the deque.
      *  @return a string representation of the deque.
      */
    @Override
    public String toString() 
    {
        String string = "{ ";
        for (int index = 0; index <= first; index ++) 
        {
            string = string + elements[index] + " ";
        }
        string = string + "}";
        string = string + "\n";
        string = string + "Size " + size();
        string = string + "\n";
        string = string + "Capacity " + capacity;
        string = string + "\n";
        return string;
    }
}

CircularArrayBasedDeque.java

public class CircularArrayBasedDeque <EltType>
                            extends ArrayBasedDeque
{
    public static void main( String[] args ) 
    {
        CircularArrayBasedDeque dq = new CircularArrayBasedDeque();
        for ( int i = 0; i < 20; i++ )
        {
            dq.insertFirst(i);
            System.out.println(dq);
        }
    }
    private final int INITIAL_CAPACITY = 20;
    private int capacity;
    private int first;
    private int last;
    private EltType[] elements; 
    public CircularArrayBasedDeque() 
    {
        capacity = INITIAL_CAPACITY;
        elements = ( EltType[] ) ( new Object[INITIAL_CAPACITY] );
        first = 0;
        last = 0;
    }
    @Override
    public void insertFirst( EltType element ) 
    {
    }
    @Override
    public boolean isEmpty() 
    {
        return ( first == last );
    }
    @Override
    public int size() 
    {
        return ( capacity - first + last );
    }
}

Thank you for your help in advance.
I’m new to OOP and find class design and inheritance confusing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T13:26:26+00:00Added an answer on May 29, 2026 at 1:26 pm

    the problem is that you dropped the generics from your CircularArrayBasedDeque class, instead use:

    public class CircularArrayBasedDeque<EltType> extends ArrayBasedDeque<EltType>
    

    the way you have it currently defined, the EltType in your CircularArrayBasedDeque class is different from the EltType in your ArrayBasedDeque class which is why the override is not working correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.