Question: Consider strings that can be split so that their first half is the same as their second half (ignoring blanks, punctuation, and case). For example,

Consider strings that can be split so that their first half is the same as their second half (ignoring blanks, punctuation, and case). For example, the string "booboo" can be split into "boo" and "boo". Another example is "hello, hello". After ignoring blanks and the comma, the two halves of the string are the same. However, the string "rattan" has unequal halves, as does the string "abcab". Add a method

public static boolean check(String s)

to the class Array Queue (your function must be in the file ArrayQueue.java) that returns true when s has the property above and false otherwise. You must use methods from QueueInterface.java and ArrayQueue.java. Do not use stacks or recursion.

QueueInterface.Java

public interface QueueInterface {

/** Adds a new entry to the back of this queue.

@param newEntry an object to be added */

public void enqueue(T newEntry);

/** Removes and returns the entry at the front of this queue.@return either the object at the front of the queue or, if the queue is empty before the operation, null */

public T dequeue();

/** Retrieves the entry at the front of this queue.

@return either the object at the front of the queue or, if thequeue is empty, null */

public T getFront();

/** Detects whether this queue is empty.

@return true if the queue is empty, or false otherwise */

public boolean isEmpty();

/** Removes all entries from this queue. */

public void clear();

}

// end QueueInterface

ArrayQueue.Java

public class ArrayQueue implements QueueInterface {

private T[] queue; // circular array of queue entries and one unusedlocation

private int frontIndex;

private int backIndex;

private static final int DEFAULT_INITIAL_CAPACITY = 50;

public ArrayQueue() {

this(DEFAULT_INITIAL_CAPACITY);

} // end default constructor

public ArrayQueue(int initialCapacity) {

// the cast is safe because the new array contains null entries@SuppressWarnings("unchecked")

T[] tempQueue = (T[]) new Object[initialCapacity + 1];

queue = tempQueue;frontIndex = 0;

backIndex = initialCapacity;

} // end constructor

public void enqueue(T newEntry) {

ensureCapacity();

backIndex = (backIndex + 1) % queue.length;

queue[backIndex] = newEntry;

} // end enqueue

public T getFront(){

T front = null;

if (!isEmpty())

front = queue[frontIndex];

return front;

} // end getFront

public T dequeue() {

T front = null;

if (!isEmpty()) {

front = queue[frontIndex];

queue[frontIndex] = null;

frontIndex = (frontIndex + 1) % queue.length;}

// end if return front;

} // end dequeue

private void ensureCapacity() {

if (frontIndex == ((backIndex + 2) % queue.length)) { // if array is

full,

T[] oldQueue = queue;

int oldSize = oldQueue.length;

@SuppressWarnings("unchecked")

T[] tempQueue = (T[]) new Object[2 * oldSize];

queue = tempQueue;

for (int index = 0; index < oldSize - 1; index++) {

queue[index] = oldQueue[frontIndex];

frontIndex = (frontIndex + 1) % oldSize;

} // end for

frontIndex = 0;

backIndex = oldSize - 2;}

// end if

} // end ensureCapacity

public boolean isEmpty() {

return frontIndex == ((backIndex + 1) % queue.length);

} // end isEmpty

public void clear() {

if(!isEmpty()) {

for (int index = frontIndex; index != backIndex; index =

(index+1)%queue.length)

queue[index] = null;

queue[backIndex] = null;

}

frontIndex = 0;

backIndex = queue.length - 1;

}

} // end ArrayQueue

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!