Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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. // Much appreciate if you also run a test case for it.

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial And Temporal Databases 10th International Symposium Sstd 2007 Boston Ma Usa July 2007 Proceedings Lncs 4605

Authors: Dimitris Papadias ,Donghui Zhang ,George Kollios

2007th Edition

3540735399, 978-3540735397

More Books

Students also viewed these Databases questions