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.
public interface QueueInterface
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
public class 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
Get step-by-step solutions from verified subject matter experts
