Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please traduce this code in JAVA language to C++ I just have knowled

ID: 673157 • Letter: C

Question

Can someone please traduce this code in JAVA language to C++

I just have knowledge of the c++ language, any help I would appreciate it.

Thanks in advance.

TEXT

-----------------------------------------------------------------------------------------------------

public void put(Object theElement)
{
// increase array length if necessary
if ((rear + 1) % queue.length == front)
{// double array size
// allocate a new array
Object [] newQueue = new Object [2 * queue.length];

// copy elements into new array
int start = (front + 1) % queue.length;
if (start < 2)
// no wrap around
System.arraycopy(queue, start, newQueue, 0,
queue.length - 1);
else
{ // queue wraps around
System.arraycopy(queue, start, newQueue, 0,
queue.length - start);
System.arraycopy(queue, 0, newQueue,
queue.length - start, rear + 1);
}

// switch to newQueue and set front and rear
front = newQueue.length - 1;
rear = queue.length - 2; // queue size is queue.length - 1
queue = newQueue;
}

// put theElement at the rear of the queue
rear = (rear + 1) % queue.length;
queue[rear] = theElement;
}

/** remove an element from the front of the queue
* @return removed element
* @return null if the queue is empty */

// put theElement at the rear of the queue
rear = (rear + 1) % queue.length;
queue[rear] = theElement;
}

/** remove an element from the front of the queue
* @return removed element
* @return null if the queue is empty */

-------------------------------------------------------------------------------------------------------------

Explanation / Answer

#include "queue.h" Queue::Queue(int size){ data = new struct _Node[size+1]; front = data; rear = data + 1; initial = data; length = size; } Queue::~Queue(){ } int Queue::append(int value){ if(rear == front){ return 0; } rear->element = value; moveRearPtr(); return 1; } int Queue::pop(){ int result; if(front + 1 == rear){ return 0; } result = (front+1)->element; moveFrontPtr(); return result; } bool Queue::isFull(){ return front == rear; } bool Queue::isEmpty(){ return front + 1 == rear; } void Queue::moveFrontPtr(){ front = initial + (front + 1 - initial) % length; } void Queue::moveRearPtr(){ rear = initial + (rear + 1 - initial) % length; }