Java/Queues
** Task: Write a JUnit test that shows a failure in some part of the ADT
-----ArrayQueue.java-------
public class ArrayQueue {
private static final int INITIAL_CAPACITY = 2; // to
permit easier testing
private Object[] contents;
private int front, rear;
/**
* Create an empty queue with an initial
capacity.
*/
public ArrayQueue() {
contents = new
Object[INITIAL_CAPACITY];
}
/**
* Add an element to the back of the queue.
* The queue's capacity is increased as
necessary.
* @param element element added
*/
public void enqueue(E element) {
contents[rear] = element;
++rear;
if (rear >= INITIAL_CAPACITY)
{
Object[]
newContents = new Object[contents.length*2];
for (int i=0; i
< contents.length; ++i) {
newContents[i] = contents[i];
}
contents =
newContents;
}
}
/**
* Remove and return the front of the queue.
* @return first element in the queue.
*/
public E dequeue() {
E result = peek();
++front;
if (front >= INITIAL_CAPACITY)
front = 0;
return result;
}
/**
* Return the front of the queue without removing
it.
* @return the first element in the queue.
*/
public E peek() {
@SuppressWarnings("unchecked")
E result =
(E)contents[front];
return result;
}
/**
* Return the number of elements in the queue.
* @return number of elements in the queue.
*/
public int size() {
return front + rear;
}
/**
* Return the capacity of the queue.
* It shouldn't more than twice
* the maximum size (or max size + 1) the queue ever
had.
* @return current capacity of the queue
*/
public int capacity() {
return contents.length;
}
}
-------TestQueue.java-------
import junit.framework.TestCase;
import edu.uwm.apc430.ArrayQueue;
public class TestQueue extends TestCase {
private ArrayQueue queue;
protected void setUp() {
queue = new
ArrayQueue<>();
}
public void test00() {
assertEquals(0,queue.size());
}
public void test01() {
queue.enqueue(42);
assertEquals(1,queue.size());
}
}
package com.HomeworkLib;
import junit.framework.TestCase;
public class TestQueue extends TestCase {
@SuppressWarnings("rawtypes")
private ArrayQueue queue;
protected void setUp() {
queue = new
ArrayQueue<>();
}
public void test00() {
assertEquals(0,
queue.size());
}
public void test01() {
queue.enqueue(42);
assertEquals(1,
queue.size());
}
public void test02() {
queue.enqueue(42);
queue.enqueue(43);
queue.enqueue(44);
queue.enqueue(45);
assertEquals(3,
queue.size());
}
//To test dequeue function
public void test03() {
queue.enqueue(42);
queue.enqueue(43);
queue.enqueue(44);
queue.enqueue(45);
//Dequeue should remove one element
from queue so size should be 3
queue.dequeue();
assertEquals(3,
queue.size());
}
public void test04() {
queue.enqueue(42);
queue.enqueue(43);
queue.enqueue(44);
queue.enqueue(45);
System.out.println(queue.capacity());
//Dequeue should remove one element
from queue so size should be 3
int expectedFirstElement =
42;
assertEquals(expectedFirstElement,
queue.peek());
}
}
----------
package com.HomeworkLib;
public class ArrayQueue<E> {
private static final int INITIAL_CAPACITY = 2; // to
permit easier testing
private Object[] contents;
private int front, rear;
/**
* Create an empty queue with an initial
capacity.
*/
public ArrayQueue() {
contents = new
Object[INITIAL_CAPACITY];
}
/**
* Add an element to the back of the queue. The queue's
capacity is increased as
* necessary.
*
* @param element element added
*/
public void enqueue(E element) {
contents[rear] = element;
++rear;
if (rear >= INITIAL_CAPACITY)
{
Object[]
newContents = new Object[contents.length * 2];
for (int i = 0;
i < contents.length; ++i) {
newContents[i] = contents[i];
}
contents =
newContents;
}
}
/**
* Remove and return the front of the queue.
*
* @return first element in the queue.
*/
public E dequeue() {
E result = peek();
++front;
if (front >=
INITIAL_CAPACITY)
front = 0;
return result;
}
/**
* Return the front of the queue without removing
it.
*
* @return the first element in the queue.
*/
public E peek() {
@SuppressWarnings("unchecked")
E result = (E)
contents[front];
return result;
}
/**
* Return the number of elements in the queue.
*
* @return number of elements in the queue.
*/
public int size() {
return front + rear;
}
/**
* Return the capacity of the queue. It shouldn't more
than twice the maximum
* size (or max size + 1) the queue ever had.
*
* @return current capacity of the queue
*/
public int capacity() {
return contents.length;
}
}
Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the...
My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...
Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue and LinkedQueue: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add N Integers to the structure and how long it takes to remove N Integers from the structure. N should vary from 10 to 100,000,000 increasing N by a factor of 10 for each test. Depending...
Write a method for the Queue class in the queue.java program (Listing 4.4) that displays the contents of the queue. Note that this does not mean simply displaying the contents of the underlying array. You should show the queue contents from the first item inserted to the last, without indicating to the viewer whether the sequence is broken by wrapping around the end of the array. Be careful that one item and no items display properly, no matter where front...
Revision Question Consider the following Java class: { public static void main (String [ ] args) { ArrayQueue<Integer> queue; queue = new ArrayQueue<Integer> () ; Integer x, y ; x = 3; y = 6; queue.offer (x) ; queue.offer (12) ; queue.offer (y) ; y = queue.peek () ; queue.poll () ; queue. offer (x - 2) ; queue.offer (x) ; queue.offer (y + 4) ; System.out.println ("Queue Elements: ") ; while (! queue.empty() ) System.out.print (queue.poll () + "...
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...
Code in C++. Can someone make it so that the code below can be compiled? ▪ Creating an empty queue ▪ Inserting a value ▪ Removing a value ▪ Finding the size of the queue ▪ Printing the contents of the queue ▪ Adding the contents of one queue to the end of another ▪ Merging the contents of two queues into a third, new, queue Class Attributes Your class should be implemented using a linked list and should have...
I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item) Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...
create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s) to return the max element in the queu. min(SinglyLinkedQueue<Integer> s) to return the min element in the queue. sum(SinglyLInkedQueue<Integer> s) to return the sum of elements in the queu. median(SinglyLinkedQueue<Integer> s) to return the median of elements in the queue. split(SinglyLinkedQueue<Integer> s) to separate the SinglyLinkedQueue into two ArrayQueues based on whether the element values are even or odd. package Stack_and_Queue; import java.util.Iterator; import...
Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> { // Data fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] theData; /** The current size */ private int size = 0; /** The current capacity */ private int capacity = 0; @SuppressWarnings("unchecked") public KWArrayList() { capacity...
JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...